这个问题是从这个问题演变而来的。
我有两张表需要查询并从中收集一些计算得出的总和;我需要一个基于单位的结果集——每个单位一行,为它们计算的数据折叠到该行中。
这两个表包含以下相关成员:
客户类别日志:
Unit varchar(25)
MemberNo varchar(10)
Category varchar(50)
Subcategory varchar(50)
BeginDate Datetime
EndDate Datetime
ReportingMonthlySales :
Unit (VarChar)
MemberNo (VarChar)
MonthlySales (Money)
CYear (Int)
Cmonth (Int)
对于每个 Unit(在两个表中都有很多行,每个 MemberNo 一个,但在结果集中包含一行),我需要填充四列:New、Assumed、Existing 和 Organic。这些值是基于属于相应“子类别”字段的单元的所有成员的总和。CustomerCategoryLog 表的 BeginDate/EndDate 值用于确定单位/成员在评估的月/年期间属于哪个子类别。
因此,结果集的简化形式如下所示:
Unit New Assumed Existing Organic Total
---- --- ------- -------- ------- -----
Abuelos $22 $44 $33 $11 $110
Gramps $12 $23 $1 $34 $70
. . .
用英语来表达这个问题,它是这样的:
给定用户作为参数提供的月份和年份(例如“1”代表月份(一月)和“2016”代表年份),找出每个 Unit 在该月内每个 Subcategory 的 MonthlySales 中有多少美元(其中BeginDate 小于或等于用户提供的月/年日期并且 EndDate 大于或等于提供的月/年日期)。
所以似乎我需要从年/月参数创建一个日期,以便与 CustomerCategoryLog 表中的 BeginDate 和 EndDate 值进行比较(另一个选项是更改 CustomerCategoryLog 表,使其具有 BeginDateMonth、BeginDateYear、EndDateMonth , 和 EndDateYear 整数;但我认为必须有一种直接的方法来从提供的年/月参数创建日期)。
我的问题是如何在 TSQL 中实际构建它。我不是 SQLhead,我最好的(伪sql)刺是:
DECLARE @Unit varchar(30);
DECLARE @Year Int;
DECLARE @Month Int;
. . .
DECLARE @PARAMDATE DATETIME = (Year + Month + 01).ToDateTime();
SELECT DISTINCT UNIT INTO #UNITS U FROM ReportingMonthlySales
WHILE NOT U.EOF DO
Unit = U.Unit
SELECT Unit, MonthlySales as 'NewSales' FROM (SELECT MonthlySales FROM ReportingMonthlySales RMS left join
CustomerCategoryLog CCL on RMS.Unit = CCL.Unit WHERE RMS.Unit = Unit AND RMS.CYear = @Year and RMS.CMonth = @Month
AND CCL.BeginDate >= PARAMDATE AND CCL.EndDate <= PARAMDATE AND CCL.Subcategory = 'New'),
MonthlySales as 'AssumedSales' FROM (SELECT MonthlySales FROM ReportingMonthlySales RMS left join
CustomerCategoryLog CCL on RMS.Unit = CCL.Unit WHERE RMS.Unit = Unit AND RMS.CYear = @Year and RMS.CMonth = @Month
AND CCL.BeginDate >= PARAMDATE AND CCL.EndDate <= PARAMDATE AND CCL.Subcategory = 'Assumed'),
MonthlySales as 'ExistingSales' FROM (SELECT MonthlySales FROM ReportingMonthlySales RMS left join
CustomerCategoryLog CCL on RMS.Unit = CCL.Unit WHERE RMS.Unit = Unit AND RMS.CYear = @Year and RMS.CMonth = @Month
AND CCL.BeginDate >= PARAMDATE AND CCL.EndDate <= PARAMDATE AND CCL.Subcategory = 'Existing'),
MonthlySales as 'OrganicSales' FROM (SELECT MonthlySales FROM ReportingMonthlySales RMS left join
CustomerCategoryLog CCL on RMS.Unit = CCL.Unit WHERE RMS.Unit = Unit AND RMS.CYear = @Year and RMS.CMonth = @Month
AND CCL.BeginDate >= PARAMDATE AND CCL.EndDate <= PARAMDATE AND CCL.Subcategory = 'Organic')
FROM ReportingMonthlySales RMS2
ORDER BY RMS2.Unit
END WHILENOTEOF
我知道这不太正确,甚至可能不太正确,但希望对于 SQL 专家/jr 来说已经足够清楚了。读心者以了解我需要做什么/正在尝试做什么。
更新
以下是查询的两个表中的一些示例数据:
CustomerCategoryLog 表:
MemberNo = 007
Unit = AMC THEATERS
Subcategory = New
BeginDate = 1/1/2016
EndDate = 12/31/2016
MemberNo = 029
Unit = FOODBUY HMS
Subcategory = Existing
BeginDate = 1/1/2015
EndDate = 12/31/2015
ReportingMonthlySales 表:
Unit = AMC THEATERS
MemberNo = 007
MonthlySales = $988.82
CYear = 2016
Cmonth = 1
Unit = FOODBUY HMS
MemberNo = 029
MonthlySales = $61,479.28
CYear = 2017
Cmonth = 3