我正在尝试使用CTE将多个结果集合并为一个包含6列的结果集。
这是我开始的查询,它返回3个结果集,每行2行:
DECLARE @User AS VARCHAR(50)
SET @User = 'testuser'
DECLARE @LastYearEndDate AS DATETIME
DECLARE @LastMonthEndDate AS DATETIME
DECLARE @LastWeekEndDate AS DATETIME
SET @LastYearEndDate = (SELECT DATEADD(dd,-1, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)))
SET @LastMonthEndDate = (SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
SET @LastWeekEndDate = (SELECT DATEADD(day, -1 - (DATEPART(dw, GETDATE()) + @@DATEFIRST - 2) % 7, GETDATE()))
SELECT SUM(os.db_Exp)
AS YearToDateRevenue,
COUNT(*) AS YearToDateCount
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date39 > @LastYearEndDate
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%'
SELECT SUM(os.db_Exp)
AS MonthToDateRevenue,
COUNT(*) AS MonthToDateCount
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date39 > @LastMonthEndDate
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%'
SELECT SUM(os.db_Exp)
AS WeekToDateRevenue,
COUNT(*) AS WeekToDateCount
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date39 > @LastWeekEndDate
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%'
输出:
Result set 1:
YearToDateRevenue | YearToDateCount
120 3
Result set 2:
MonthToDateRevenue | MonthToDateCount
75 2
Result set 3:
WeekToDateRevenue | WeekToDateCount
18 1
可能有一种更简单的方法可以做到这一点,因为我每次只切换一个参数,但这是我的尝试:
/* Same declarations as above */
WITH cte AS (
SELECT
os.db_Exp
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date39 > @LastYearEndDate
AND os.db_Date39 > @LastMonthEndDate
AND os.db_Date39 > @LastWeekEndDate
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%')
SELECT
(SELECT SUM(os.db_Exp) AS YearToDateRevenue,
COUNT(*) AS YearToDateCount
FROM cte
WHERE os.db_Date39 > @LastYearEndDate)
(SELECT SUM(os.db_Exp) AS YearToDateRevenue,
COUNT(*) AS YearToDateCount
FROM cte
WHERE os.db_Date39 > @LastMonthEndDate)
(SELECT SUM(os.db_Exp) AS YearToDateRevenue,
COUNT(*) AS YearToDateCount
FROM cte
WHERE os.db_Date39 > @LastWeekEndDate)
现在我收到了错误:
Msg 4104, Level 16, State 1, Line 28
The multi-part identifier "os.db_Date39" could not be bound.
Msg 4104, Level 16, State 1, Line 25
The multi-part identifier "os.db_Exp" could not be bound.
Msg 116, Level 16, State 1, Line 30
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
输出我想要:
YearToDateRevenue | YearToDateCount | MonthToDateRevenue | MonthToDateCount | WeekToDateRevenue | WeekToDateCount
120 3 75 2 18 1
我不确定将参数合并到cte中的正确方法并让它运行。任何我看不到的更简单的解决方案也会受到赞赏。我只是希望每个总和都计入不同的列。每个只返回一个数字,因此应该返回一行和6列。感谢
答案 0 :(得分:1)
您可以使用CASE
来实现此目标:
SELECT SUM(CASE WHEN os.db_Date39 > @LastYearEndDate THEN os.db_Exp ELSE 0 END) AS YearToDateRevenue,
SUM(CASE WHEN os.db_Date39 > @LastYearEndDate THEN 1 ELSE 0 END) AS YearToDateCount,
SUM(CASE WHEN os.db_Date39 > @LastMonthEndDate THEN os.db_Exp ELSE 0 END) AS MonthToDateRevenue,
SUM(CASE WHEN os.db_Date39 > @LastMonthEndDate THEN 1 ELSE 0 END) AS MonthToDateCount,
SUM(CASE WHEN os.db_Date39 > @LastWeekEndDate THEN os.db_Exp ELSE 0 END) AS WeekToDateRevenue,
SUM(CASE WHEN os.db_Date39 > @LastWeekEndDate THEN 1 ELSE 0 END) AS WeekToDateCount
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%'
答案 1 :(得分:0)
有几种不同的方法可以解决这个问题,但我相信最快捷的方法就是使用类似的东西......这是对你的第一个查询的修改,应该让你快速上手。有更好的方法可以做到这一点,但根据您的问题和提供的信息,这将启动和运行。
DECLARE @User AS VARCHAR(50)
SET @User = 'testuser'
DECLARE @LastYearEndDate AS DATETIME
DECLARE @LastMonthEndDate AS DATETIME
DECLARE @LastWeekEndDate AS DATETIME
SET @LastYearEndDate = (SELECT DATEADD(dd,-1, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)))
SET @LastMonthEndDate = (SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
SET @LastWeekEndDate = (SELECT DATEADD(day, -1 - (DATEPART(dw, GETDATE()) + @@DATEFIRST - 2) % 7, GETDATE()))
with YTD_total as (
SELECT 1 as joinpoint
SUM(os.db_Exp)
AS YearToDateRevenue,
COUNT(*) AS YearToDateCount
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date39 > @LastYearEndDate
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%'
),
MTD_total as (
SELECT 1 as joinpoint
SUM(os.db_Exp)
AS MonthToDateRevenue,
COUNT(*) AS MonthToDateCount
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date39 > @LastMonthEndDate
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%'
),
WTD_total as (
SELECT 1 as joinpoint
SUM(os.db_Exp)
AS WeekToDateRevenue,
COUNT(*) AS WeekToDateCount
FROM tblOrderSum os
JOIN tblUserProfile up
ON os.db_RTS = up.db_UserId
WHERE os.db_Deleted = 0
AND os.db_Date39 > @LastWeekEndDate
AND os.db_Date45 = '1900/01/01'
AND db_Email LIKE @User + '%'
)
select YearToDateRevenue,
YearToDateCount,
MonthToDateRevenue,
MonthToDateCount
WeekToDateRevenue,
WeekToDateCount
from ytd_totals ytd
join mtd_total mtd
on mtd.joinpoint = ytd.joinpoint
join wtd_total wtd
on wtd.joinpoint = mtd.joinpoint