简体   繁体   中英

SQL How to COUNT each item with each month quantities?

I was learning sql server by myself, and I have question to ask:

I have A_table like this

row | Itemid | Itemname | Class | Price |
----+--------+----------+-------+-------+
1   |   1    |     A    |   a1  |   20  |
2   |   2    |     B    |   a2  |   25  |
3   |   3    |     C    |   a2  |   30  |
4   |   4    |     D    |   a1  |   35  |

B_table like this

row |    Date    | Itemid |
----+------------+--------+
1   | 2019-01-11 |   1    |
2   | 2019-02-11 |   2    | 
3   | 2019-03-11 |   1    |
4   | 2019-04-11 |   3    | 

and I want result be like

row |  Itemname  | Jan | Feb | Mar | Apr | TotalPrice |
----+------------+-----+-----+-----+-----+------------+
1   |      A     |  1  |  0  |  1  |  0  |     40     |
2   |      B     |  0  |  1  |  0  |  0  |     25     |
3   |      C     |  0  |  0  |  0  |  1  |     30     |
4   |      D     |  0  |  0  |  0  |  0  |     0      |

Here is the best I can do...

SELECT
    itemid, a.amount, (amount * ItemUnitPrice) 'total' 
FROM   
    ((SELECT      
          itemdb.ItemId, itemdb.ItemName, 
          ISNULL(COUNT(SalesStatisticsDb.ItemId), 0) AS 'amount', 
          ItemDb.ItemUnitPrice
      FROM              
          dbo.SalesStatisticsDb 
      RIGHT JOIN 
          ItemDb ON SalesStatisticsDb.ItemId = ItemDb.ItemId
      WHERE          
          (SUBSTRING(SalesStatisticsDb.UpdateTime, 6, 2) = '01')
      GROUP BY 
          ItemDb.ItemId, ItemDb.ItemName, ItemDb.ItemUnitPrice
      UNION ALL
      SELECT      
          itemdb.ItemId, itemdb.ItemName, 
          COUNT(SalesStatisticsDb.ItemId) AS 'amount', ItemDb.ItemUnitPrice
      FROM              
          dbo.SalesStatisticsDb 
      RIGHT JOIN
          ItemDb ON SalesStatisticsDb.ItemId = ItemDb.ItemId
      WHERE          
          (SUBSTRING(SalesStatisticsDb.UpdateTime, 6, 2) = '02')
      GROUP BY 
          ItemDb.ItemId, ItemDb.ItemName, ItemDb.ItemUnitPrice)) AS a 
GROUP BY 
    itemid, amount, ItemUnitPrice

Could someone help me out please...

You need to use a conditional aggregate. This appears to get you the answer you're after:

CREATE TABLE A_table ([row] int,
                      Itemid int,
                      Itemname char(1),
                      Class char(2),
                      Price int);
CREATE TABLE B_table ([row] int,
                      [Date] date,
                      ItemID int);

INSERT INTO A_table
VALUES (1,1,'A','a1',20),
       (2,2,'B','a2',25),
       (3,3,'C','a2',30),
       (4,4,'D','a1',35);

INSERT INTO B_table
VALUES(1,'20190111',1),
      (2,'20190211',2), 
      (3,'20190311',1),
      (4,'20190411',3);

SELECT A.[row],
       A.Itemname,
       COUNT(CASE DATEPART(MONTH, B.[Date]) WHEN 1 THEN 1 END) AS Jan,
       COUNT(CASE DATEPART(MONTH, B.[Date]) WHEN 2 THEN 1 END) AS Feb,
       COUNT(CASE DATEPART(MONTH, B.[Date]) WHEN 3 THEN 1 END) AS Mar,
       COUNT(CASE DATEPART(MONTH, B.[Date]) WHEN 4 THEN 1 END) AS Apr,
       COUNT(B.[row]) * A.Price AS TotalPrice
FROM A_table A
     LEFT JOIN B_table B ON A.Itemid = B.ItemID
GROUP BY A.[row],
         A.Itemname,
         A.Price;

GO

DROP TABLE A_table;
DROP TABLE B_table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM