sql - 如何在 SQL Server 中使用基于 2 列的 group by

标签 sql sql-server database tsql

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

我想按月和年计算每个国家和组的不同 order_number,如下所示 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

能否请您告知 group by 中有什么问题,以便 Month-Year 将在标题中水平显示?

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

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

最佳答案

请尝试以下查询,它会对您有所帮助。

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

关于sql - 如何在 SQL Server 中使用基于 2 列的 group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160827/

相关文章:

mysql - 我想将两个 Sequelize 查询合并为单个 Sequelize 查询

SQL Server - 在 WHERE 子句中使用谓词的替代方法

database - 使用迁移的好处

PostgreSQL存储过程,如何返回结果集

PHP fatal error : Uncaught exception 'PDOException' with message 'SQLSTATE[42000]

sql - 如何在不使用 CDC 功能的情况下在单个表中维护多个表的历史记录

sql-server - ANSI 相关设置的推荐配置是什么?

SQL Server 2005 按别名分组

database - Django 中唯一的 BooleanField 值?

Java、mysql错误:Column count doesn't match value count at row 1