使用sql server 2008如何獲得前12周的2個不同聚合體
我正在尋找單一的sql解決方案。
現在我有兩個查詢,無法弄清楚如何將它變成一個單一的sql結果集。
如果可能的話,我不想使用臨時表。
我想在結束日期的最後12周內計算每週所有打開和關閉的電話。 任何通話狀態> 3表示已結束通話。
-- Create table with data
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--drop table calltable
--go
CREATE TABLE [dbo].[calltable](
[PKID] [int] IDENTITY(1,1) NOT NULL,
[incident_number] [int] NOT NULL,
[call_date] [datetime] NOT NULL,
[status] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (145, CAST(0x0000A36500F55347 AS DateTime), 9)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (192, CAST(0x0000A3A200F5534C AS DateTime), 1)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (105, CAST(0x0000A36800F5534C AS DateTime), 3)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (732, CAST(0x0000A39C00F5534C AS DateTime), 9)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (62, CAST(0x0000A35B00F5534C AS DateTime), 9)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (188, CAST(0x0000A394010A48D0 AS DateTime), 7)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (844, CAST(0x0000A380010A48F1 AS DateTime), 1)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (77, CAST(0x0000A387010A48F1 AS DateTime), 8)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (263, CAST(0x0000A352010A48F1 AS DateTime), 8)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (556, CAST(0x0000A394010A48F1 AS DateTime), 8)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (546, CAST(0x0000A37E010A5D8F AS DateTime), 3)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (17, CAST(0x0000A378010A5D8F AS DateTime), 5)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (652, CAST(0x0000A377010A5D94 AS DateTime), 7)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (107, CAST(0x0000A356010A5D94 AS DateTime), 9)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (96, CAST(0x0000A3A5010A5D94 AS DateTime), 7)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (668, CAST(0x0000A36D010A5E33 AS DateTime), 6)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (756, CAST(0x0000A37A010A5E33 AS DateTime), 3)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (286, CAST(0x0000A361010A5E33 AS DateTime), 1)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (591, CAST(0x0000A39D010A5E33 AS DateTime), 7)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (975, CAST(0x0000A37F010A5E33 AS DateTime), 3)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (788, CAST(0x0000A37C010A5F0B AS DateTime), 9)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (892, CAST(0x0000A390010A5F10 AS DateTime), 9)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (51, CAST(0x0000A38C010A5F14 AS DateTime), 10)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (302, CAST(0x0000A356010A5F14 AS DateTime), 0)
INSERT [dbo].[calltable] ([incident_number], [call_date], [status]) VALUES (717, CAST(0x0000A374010A5F14 AS DateTime), 6)
-- Queries
DECLARE @WeeksBack SMALLINT
DECLARE @ReportEndDate DATETIME
SET @ReportEndDate = '2014-09-27'
SET @WeeksBack = 12
SELECT ct.call_date
, DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + (CAST(DATEPART(YY, ct.call_date) AS CHAR(4)))) + ((DATEPART(WK, ct.call_date)) - 1), 6) AS reportstartdate
, DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + (CAST(DATEPART(YY, ct.call_date) AS CHAR(4)))) + ((DATEPART(WK, ct.call_date)) - 1), 5) AS reportenddate
, count(*) OVER (PARTITION BY datepart(week, ct.call_date)) AS OpenedCallCt
, NULL
FROM calltable ct
WHERE ct.call_date <= @ReportEndDate
AND ct.call_date >= dateadd(day, (- 7 * @WeeksBack), @ReportEndDate)
SELECT ct.call_date
, DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + (CAST(DATEPART(YY, ct.call_date) AS CHAR(4)))) + ((DATEPART(WK, ct.call_date)) - 1), 6) AS reportstartdate
, DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + (CAST(DATEPART(YY, ct.call_date) AS CHAR(4)))) + ((DATEPART(WK, ct.call_date)) - 1), 5) AS reportenddate
, count(*) OVER (PARTITION BY datepart(week, ct.call_date)) AS ClosedCallCt
, NULL
FROM calltable ct
WHERE ct.call_date <= @ReportEndDate
AND ct.call_date >= dateadd(day, (- 7 * @WeeksBack), @ReportEndDate)
and ct.STATUS > 3
查詢1結果集(部分):
reportstartdate reportenddate OpenedCallCt
------------------------- ------------------------- --------------
2014-07-06 00:00:00.000 2014-07-12 00:00:00.000 2
2014-07-06 00:00:00.000 2014-07-12 00:00:00.000 2
2014-07-13 00:00:00.000 2014-07-19 00:00:00.000 2
2014-07-13 00:00:00.000 2014-07-19 00:00:00.000 2
2014-07-20 00:00:00.000 2014-07-26 00:00:00.000 1
QUERY2結果集(部分):
reportstartdate reportenddate ClosedCallCt
2014-07-06 00:00:00.000 2014-07-12 00:00:00.000 1
2014-07-13 00:00:00.000 2014-07-19 00:00:00.000 1
2014-07-20 00:00:00.000 2014-07-26 00:00:00.000 1
我如何能得到以下結果集只是一個查詢,沒有中間表的使用? ?
期望中的結果集(部分):
reportstartdate reportenddate OpenedCallCt ClosedCallCt
2014-07-06 00:00:00.000 2014-07-12 00:00:00.000 2 1
2014-07-13 00:00:00.000 2014-07-19 00:00:00.000 2 1
2014-07-20 00:00:00.000 2014-07-26 00:00:00.000 1 1
感謝您的幫助,讓我知道,如果你需要更多的信息。
是的,這就是我正在尋找...謝謝! – exitstageleft 2014-09-29 18:00:23