Home > database >  Grouping SQL Server query
Grouping SQL Server query

Time:12-09


For grouping table query, one month is variable, can be specified, your I want to take a few months a few months

CodePudding user response:

 
The CREATE TABLE # TA (category VARCHAR (20), date VARCHAR (20), number of INT)

INSERT INTO # TA values (' apple ', '2019-01, 20)
INSERT INTO # TA values (' banana ', '2019-01, 30)
INSERT INTO # TA values (' orange ', '2019-01, 10)
INSERT INTO # TA values (' apple ', '2019-02, 20)
INSERT INTO # TA values (' banana ', '2019-02, 30)
INSERT INTO # TA values (' banana ', '2019-03, 25)
INSERT INTO # TA values (' orange ', '2019-03, 15)

Select * from # TA
The pivot
(
Max (quantity)
For [date] (in 2019-01], [[2019-02], [2019-03])
) a
  • Related