一张表示会员登记历史,要查看每个季度新增的会员信息,会员有可能在一个季度升级,如果升级则会员等级历史中新增一条。
而我们只要升级的会员信息。
这个表有几个字段 START_DATE__C(等级日期)Membership__c (会员名称)Member_Tier_Name__c(会员等级)
我的语句如下:
SELECT DISTINCT
CAST(DATEPART(YYYY,A.START_DATE__C) AS VARCHAR(4)) + '年' +
CASE DATEPART(QUARTER,A.START_DATE__C)
WHEN 1 THEN '一季度'
WHEN 2 THEN '二季度'
WHEN 3 THEN '三季度'
WHEN 4 THEN '四季度'
END AS 季度,
(SELECT COUNT(0) FROM
(SELECT MAX(START_DATE__C),Membership__c FROM 会员登记历史 WHERE DATEPART(YYYY,START_DATE__C) = DATEPART(YYYY,A.START_DATE__C)
AND DATEPART(QUARTER,START_DATE__C) = DATEPART(QUARTER,A.START_DATE__C) AND Member_Tier_Name__c='普通会员'
GROUP BY Membership__c) AS D) AS 新增普通会员,
(SELECT COUNT(0) FROM
(SELECT MAX(START_DATE__C),Membership__c FROM 会员登记历史 WHERE DATEPART(YYYY,START_DATE__C) = DATEPART(YYYY,A.START_DATE__C)
AND DATEPART(QUARTER,START_DATE__C) = DATEPART(QUARTER,A.START_DATE__C) AND Member_Tier_Name__c='黄金会员'
GROUP BY MEMBERSHIP__C) AS K) AS 新增黄金会员
FROM
会员登记历史 AS A ORDER BY 季度
报错。奇怪了。
------解决方案--------------------
要习惯用别名啊。。。看的头晕