0

I am having trouble with a SQL query.

I am looking to get the top 10 most popular part numbers for each location for the last month. So for 6 locations I should end up with 60 rows ordered by popularity and location.

I have a query at the minute which pulls out all the part numbers for the previous month but it pulls out nearly 500 rows.

Any help or advice appreciated.

These are my tables

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`)
)

Here is my current query:

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

EDIT

Here are some inserts

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');

And I've tried using my query with an answer in the link posted but it still returns the same amount of rows. I see what using a variable as the row number is supposed to do but it's not incrementing in my query.

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;
AdRock
  • 2,959
  • 10
  • 66
  • 106

0 Answers0