【问题标题】:How to use group by based on 2 columns in SQL Server如何使用基于 SQL Server 中的 2 列的分组依据
【发布时间】:2018-11-05 19:21:41
【问题描述】:

我在 SQL Server 中有一个表,如下所示

我想按月和年计算每个国家和组的不同 order_number,如下所示

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

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

我得到如下结果

您能否告知组中的问题,以便月份-年份将在标题中水平显示?

这是表的创建和值,以防您需要尝试。感谢您的支持。

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

【问题讨论】:

  • group by 本身永远不会给你列......只有行。如果您想将行转换为列,请查看pivot 函数。
  • 将那些 CASE WHEN DATENAME 更改为对 MONTH(purchasedate) 的调用。在这个查询中每次使用 DATENAME 都是多余的

标签: sql sql-server database tsql


【解决方案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

【讨论】:

  • 将那些 CASE WHEN DATENAME 更改为对 MONTH(purchasedate) 的调用
【解决方案2】:

这是一个带有旋转的示例。它不会像 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

【讨论】:

  • 哎呀,将日期转换为 yyyy-mm 有点讨厌。为什么不使用 FORMAT() 或 MONTH() / YEAR() / DATEPART() 的组合?
【解决方案3】:

使用这个 sn-p。我使用了 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

【讨论】:

    【解决方案4】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 2014-11-22
      相关资源
      最近更新 更多