我可以使用 SQL CTE 实现以下目标吗?或者如何实现?
Can I achieve the below with SQL CTE? Or how can this be achieved?
这是一个虚拟客户table。
我附上了输入 table
[![在此处输入图片描述][1]][1]
SELECT COUNT(*) as c1 FROM [Customer]
WHERE country='Germany'
and Purchasedate is null
SELECT COUNT(*) as c2 FROM [Customer]
WHERE country='Germany'
and convert(date,Purchasedate) ='2020-04-07'
SELECT COUNT(*) as c3 FROM [Customer]
WHERE country='Germany'
and convert(date,Purchasedate) ='2020-04-05'
The above code gives
c1
5
---
c2
3
--
c3
3
But I need a solution where I need all the 3 outputs into a single row
Eg:
c1|c2|c3
5 |3 |3
我需要将此结构作为单个数据集,因为我将在我的 ssrs 报告中使用它。
我可以使用什么 sql 技术来实现这一目标?有什么办法可以使用 CTE。请帮助我。
[1]: https://i.stack.imgur.com/ZLb29.png
您可以使用 case 表达式来执行此操作。
SELECT
SUM(CASE WHEN code=1 AND month=1 THEN 1 END) as c1,
SUM(CASE WHEN code=2 AND month=2 THEN 1 END) as c2,
SUM(CASE WHEN code=3 AND month=3 THEN 1 END) as c3
FROM yourtable;
也许这是一个基本的解决方案:
创建样本table并用数据填充它
CREATE TABLE TableA (no INT , code INT , MONTH INT)
GO
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
解决方案:
SELECT (Select count(no) from TableA
where code=1 and month =1
) AS c1 , (Select count(no) from TableA
where code=2 and month =2) as c2 ,(Select count(no) from TableA
where code=3 and month =3) as c3
+----+----+----+
| c1 | c2 | c3 |
+----+----+----+
| 4 | 6 | 5 |
+----+----+----+
这是一个虚拟客户table。 我附上了输入 table [![在此处输入图片描述][1]][1]
SELECT COUNT(*) as c1 FROM [Customer]
WHERE country='Germany'
and Purchasedate is null
SELECT COUNT(*) as c2 FROM [Customer]
WHERE country='Germany'
and convert(date,Purchasedate) ='2020-04-07'
SELECT COUNT(*) as c3 FROM [Customer]
WHERE country='Germany'
and convert(date,Purchasedate) ='2020-04-05'
The above code gives
c1
5
---
c2
3
--
c3
3
But I need a solution where I need all the 3 outputs into a single row
Eg:
c1|c2|c3
5 |3 |3
我需要将此结构作为单个数据集,因为我将在我的 ssrs 报告中使用它。 我可以使用什么 sql 技术来实现这一目标?有什么办法可以使用 CTE。请帮助我。
[1]: https://i.stack.imgur.com/ZLb29.png
您可以使用 case 表达式来执行此操作。
SELECT
SUM(CASE WHEN code=1 AND month=1 THEN 1 END) as c1,
SUM(CASE WHEN code=2 AND month=2 THEN 1 END) as c2,
SUM(CASE WHEN code=3 AND month=3 THEN 1 END) as c3
FROM yourtable;
也许这是一个基本的解决方案:
创建样本table并用数据填充它
CREATE TABLE TableA (no INT , code INT , MONTH INT)
GO
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (1,1,1)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (2,2,2)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
INSERT TABLEA Values (3,3,3)
解决方案:
SELECT (Select count(no) from TableA
where code=1 and month =1
) AS c1 , (Select count(no) from TableA
where code=2 and month =2) as c2 ,(Select count(no) from TableA
where code=3 and month =3) as c3
+----+----+----+
| c1 | c2 | c3 |
+----+----+----+
| 4 | 6 | 5 |
+----+----+----+