如何基于SQL Server中的2列使用分组依据

时间:2018-11-05 19:21:42

标签: sql sql-server database tsql

我在SQL Server中有一个表,如下所示 enter image description here

我想按月份和年份分别计算每个国家和组的不同订单号,如下所示 enter image description here

我正在使用此查询,但没有得到想要的结果

select 
concat(DATENAME(month, purchase_date), '  ' , DATENAME(year, purchase_date)) as [Month-Year],
country, count(distinct [order_number]) as [Distinct Order Number]
from SC.mytable
group by DATENAME(year, purchase_date), DATENAME(month, purchase_date),country
Order by DATENAME(year, purchase_date) ASC,
case DATENAME(month, purchase_date) 
when 'January' then 1
when 'February' then 2
when 'March' then 3
when 'April' then 4
when 'May' then 5
when 'June' then 6
when 'July' then 7
when 'August' then 8
when 'September' then 9
when 'October' then 10
when 'November' then 11
when 'December' then 12
end asc
,country asc

我得到的结果如下

enter image description here

请问您在分组中出了什么问题,以便在标题中水平显示“月份”?

这里是表的创建和值,以备需要时使用。谢谢您的支持。

CREATE TABLE SC.mytable (
  country varchar(50) NOT NULL,
  order_number varchar(10) NOT NULL,
  order_item varchar(2) NOT NULL,
  purchase_date date NOT NULL

);

INSERT INTO SC.mytable (country, order_number, order_item, purchase_date) VALUES
  ('Germany', '4311787830', '10','2018-11-01'),
  ('France', '4221669938', '90','2018-10-29'),
  ('Austria', '4216370706', '50','2018-08-17'),
  ('Austria', '4216370706', '60','2018-08-17'),
  ('Germany', '4320162822', '10','2018-07-16'),
  ('Germany', '4320162822', '20','2018-07-16'),
  ('UK', '4216775391', '80','2018-04-30'),
  ('UK', '4214370307', '50','2018-08-23'),
  ('Germany', '4311780287', '40','2018-03-11'),
  ('Germany', '4216334860', '70','2018-08-27'),
  ('Spain', '4911235852', '30','2018-12-10'),
  ('Spain', '4719832003', '90','2018-12-18'),
  ('France', '4216325304', '30','2018-05-04'),
  ('France', '4216325304', '40','2018-05-04');

4 个答案:

答案 0 :(得分:1)

请尝试以下查询,它将为您提供帮助。

SELECT country,
 CASE WHEN DATENAME(month, purchase_date) = 'January' THEN count(distinct [order_number]) ELSE 0 END AS "JAN-18",
 CASE WHEN DATENAME(month, purchase_date) = 'February' THEN count(distinct [order_number]) ELSE 0 END AS "FEB-18",
 CASE WHEN DATENAME(month, purchase_date) = 'March' THEN count(distinct [order_number]) ELSE 0 END AS "MAR-18",
 CASE WHEN DATENAME(month, purchase_date) = 'April' THEN count(distinct [order_number]) ELSE 0 END AS "APR-18",
 CASE WHEN DATENAME(month, purchase_date) = 'May' THEN count(distinct [order_number]) ELSE 0 END AS "MAY-18",
 CASE WHEN DATENAME(month, purchase_date) = 'June' THEN count(distinct [order_number]) ELSE 0 END AS "JUN-18",
 CASE WHEN DATENAME(month, purchase_date) = 'July' THEN count(distinct [order_number]) ELSE 0 END AS "JUL-18",
 CASE WHEN DATENAME(month, purchase_date) = 'August' THEN count(distinct [order_number]) ELSE 0 END AS "AUG-18",
 CASE WHEN DATENAME(month, purchase_date) = 'September' THEN count(distinct [order_number]) ELSE 0 END AS "SEP-18",
 CASE WHEN DATENAME(month, purchase_date) = 'October' THEN count(distinct [order_number]) ELSE 0 END AS "OCT-18",
 CASE WHEN DATENAME(month, purchase_date) = 'November' THEN count(distinct [order_number]) ELSE 0 END AS "NOV-18",
 CASE WHEN DATENAME(month, purchase_date) = 'December' THEN count(distinct [order_number]) ELSE 0 END AS "DEV-18"
 FROM 
mytable
 GROUP BY country,purchase_date

答案 1 :(得分:1)

这里是带有枢轴的示例。但是,它不会像BI工具那样动态生成列。使其动态化的唯一方法是使用过程生成sql代码,然后将其作为字符串执行。

select P.* from (
select country, left(purchase_date,7) as mon, order_number  from SC.mytable) M
pivot (
  count(order_number) 
  for mon in ([2018-08], [2018-09], [2018-10], [2018-11])
 ) as P

答案 2 :(得分:1)

使用此代码段。我使用了Pivot并获得了与所需表相同的结果:

 SELECT country,[1] as Jan, [2] as Feb, [3] as Mar, [4] as Apr,[5] as May, [6] as 
 Jun, [7] as jul , [8] as Aug, [9] as Sep,[10] as Oct, [11] as  Nov, [12] as Dec  
 FROM   (SELECT country,  datepart( mm,purchase_date )  as mnt FROM mytable) p  
 PIVOT  (count( mnt) FOR Mnt IN ( [1], [2], [3], [4],[5], [6], [7], [8], [9],[10], 
 [11], [12]))AS unpvt;  

此外,您可以使用此链接获取更多说明:https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-2017

  [1]: https://i.stack.imgur.com/rAplA.jpg

答案 3 :(得分:1)

您也可以使用此脚本,虽然效率不高,但可以使用:

select country , 
  (select count(*) from mytable t2 where t2.country = t.country and 
  month(purchase_date) = 1) as Jan,
  (select count(*) from mytable t2 where t2.country = t.country and 
  month(purchase_date) = 2) as Feb,
  (select count(*) from mytable t2 where t2.country = t.country and 
  month(purchase_date) = 3) as Mar,
  (select count(*) from mytable t2 where t2.country = t.country and 
  month(purchase_date) = 4) as Apr,
  (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 5) as May,
  (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 6) as Jun,
  (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 7) as Jul,
  (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 8) as Aug,
  (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 9) as Sep,
  (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 10) as Oct,
 (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 11) as Nov,
 (select count(*) from mytable t2 where t2.country = t.country and 
 month(purchase_date) = 12) as Dec
   FROM mytable t
   GROUP BY country


  [1]: https://i.stack.imgur.com/E2F8u.jpg