我正在使用 SQL Server 2005。我正在尝试收集 3 个不同表的信息。
表 1 是[User_Table],如下所示:
Lead ID
1 5
1 8
1 12
2 13
2 14
2 15
2 18
2 24
表 2 是[交互],如下所示:
Int_Date ID Interaction
2012-03-01 5 Hang Up
2012-03-01 13 Reset
2012-03-01 8 Basic
2012-03-01 13 Basic
2012-03-02 14 Reset
2012-03-02 12 Advanced
2012-03-02 12 Hang Up
2012-03-02 24 Reset
2012-03-02 5 Basic
表 3 是[重复],如下所示:
Repeat_d ID R_Interaction
2012-03-01 13 Reset
2012-03-01 8 Basic
2012-03-02 14 Reset
2012-03-02 12 Advanced
2012-03-02 24 Reset
我需要做的是将来自 [Interactions] 的总调用数作为每个交互类型和日期的总和,并按 Lead 和 Int_Date = Repeat_d 对它们进行分组
我希望结果如何:
DATE Lead Total_Interactions Total_Repeats Interaction
2012-03-01 1 1 0 Hang Up
2012-03-01 1 2 1 Basic
2012-03-01 2 1 1 Reset
2012-03-01 2 1 0 Basic
2012-03-02 1 1 1 Advanced
2012-03-02 1 1 0 Hang Up
2012-03-02 1 1 0 Basic
2012-03-02 2 2 1 Reset
到目前为止,我的查询看起来像:
DECLARE @StartDate smalldatetime, @EndDate smalldatetime, @TL_ID as smallint
SET @StartDate = '20120301'
SET @EndDate = '20120331'
SET @TL_ID = '2'
SELECT
DATEADD(dd, DATEDIFF(dd, 0, [Int_Date]),0) as [Int_Date]
,[Interaction]
,COUNT([Interaction]) as [Total_Repeats]
FROM [Interactions] (NOLOCK)
LEFT JOIN [user_table] (NOLOCK) ON [id] = [id]
WHERE [Int_Date] BETWEEN @StartDate AND @EndDate
AND [Lead] = @TL_ID
GROUP BY DATEADD(dd, DATEDIFF(dd, 0, [Int_Date]),0), [Interaction]
ORDER BY [Int_Date], [Total_Repeats] DESC
任何帮助将非常感激。谢谢!