MySQL为每个类别获得前10行

时间:2016-09-28 09:14:36

标签: mysql sql

我遇到SQL查询问题。

我希望获得上个月每个地点最受欢迎的十大零件编号。因此,对于6个地点,我最终应按人气和地点排序60行。

我有一个查询,它会提取上个月的所有零件编号,但它会拉出近500行。

感谢任何帮助或建议。

这些是我的表

CREATE TABLE `tableA` (
    `part_number` varchar(20) NOT NULL,
    `description` varchar(200) DEFAULT NULL
    PRIMARY KEY (`part_number`)
)

CREATE TABLE `tableB` (
    `tableA_part_number` varchar(20) NOT NULL,
    `tableC_id` int(8) NOT NULL,
    `qty` int(4) NOT NULL,
    PRIMARY KEY (`tableA_part_number`,`tableC_id`)
)

CREATE TABLE `tableC` (
    `id` int(8) NOT NULL AUTO_INCREMENT,
    `location` varchar(12) NOT NULL,
    `date_created` int(11) NOT NULL,
    PRIMARY KEY (`id`)
)

这是我当前的查询:

SELECT tableA_part_number,
        location,
        description, 
        SUM(qty) as total
FROM tableB
    INNER JOIN tableA ON 
        tableB.parts_part = tableA.part_number
    INNER JOIN tableC ON
        tableB.tableC_id = tableC.id
WHERE YEAR(FROM_UNIXTIME(tableC.date_created, '%Y-%m-%d')) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
    AND MONTH(FROM_UNIXTIME(tableC.date_created, '%Y-%m-%d')) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
GROUP BY tableA_part_number

修改

以下是一些插页

INSERT INTO tableA (part_number, description) VALUES ('123456', 'something');
INSERT INTO tableA (part_number, description) VALUES ('123457', 'something else');
INSERT INTO tableA (part_number, description) VALUES ('123458', 'something drh dh');
INSERT INTO tableA (part_number, description) VALUES ('123459', 'something dg sdg');
INSERT INTO tableA (part_number, description) VALUES ('123410', 'somethingdf fds ');

INSERT INTO tableB (tableA_part_number, tableC_id, qty) VALUES ('123456', 'somewhere', 'something', 2);
INSERT INTO tableB (tableA_part_number, tableC_id, qty) VALUES ('123457', 'somewhere', 'something', 1);
INSERT INTO tableB (tableA_part_number, tableC_id, qty) VALUES ('123456', 'elsewhere', 'something', 1);
INSERT INTO tableB (tableA_part_number, tableC_id, qty) VALUES ('123458', 'elsewhere', 'something', 3);
INSERT INTO tableB (tableA_part_number, tableC_id, qty) VALUES ('123456', 'somewhere', 'something', 1);

INSERT INTO tableC (tableA_part_number, tableC_id, qty) VALUES (1, 'somewhere', '2016-09-28');
INSERT INTO tableC (tableA_part_number, tableC_id, qty) VALUES (2, 'somewhere', '2016-09-28');
INSERT INTO tableC (tableA_part_number, tableC_id, qty) VALUES (3', 'elsewhere', '2016-09-28');
INSERT INTO tableC (tableA_part_number, tableC_id, qty) VALUES (4, 'elsewhere', '2016-09-28');
INSERT INTO tableC (tableA_part_number, tableC_id, qty) VALUES (5, 'somewhere', '2016-09-28');

我已尝试在发布的链接中使用我的查询并给出答案但仍返回相同数量的行。我看到使用变量作为行号应该做什么,但它在我的查询中没有递增。

set @num := 0, @loc := '';

SELECT tableA_part_number,
        location,
        description,
        total,
        @num,
        @loc
FROM 
(
    SELECT  tableA_part_number,
            location,
            description, 
            SUM(qty) as total,
            @num := if(@loc = dummy, @num + 1, 1) as row_number,
            @loc := location as dummy
    FROM tableB
        INNER JOIN tableA ON 
            tableB.parts_part = tableA.part_number
        INNER JOIN tableC ON
            tableB.tableC_id = tableC.id
    WHERE FROM_UNIXTIME(tableC.date_created, '%Y-%m') = DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y-%m')

    GROUP BY tableA_part_number
    ORDER BY location, total DESC
) as x 
WHERE x.row_number <= 2;

0 个答案:

没有答案