我正在尝试学习一些先进的技术。我有以下内容
从'(日期)'开始执行每月退出信息的查询
与union all
s。
有没有更短的方法呢?执行此操作需要很长时间 时间。
谢谢!
select left((datename(mm, me.StateDisenrollDate)),3) as ReportedMonth,
me.StateDisenrollDate,
me.ReinstatementDate,
me.MemberID,
dis.DRI_Name,
case
when me.ReinstatementDate is null then 'Disenrolled'
else 'Reinstated' end as Status
from tblMemEnrollment me
left join tlkpDisenrolReasInvestigated dis on dis.DisenrolReasInvestigID = me.DisenrolRsnInvstgtdID
where me.StateDisenrollDate between '1-1-2015' and '1/31/2015'
and me.ReinstatementDate is null or me.ReinstatementDate between '1/1/2015' and '1/31/2015'
and SubscriberID like '%H%'
union all
select left((datename(mm, me.StateDisenrollDate)),3) as ReportedMonth,
me.StateDisenrollDate,
me.ReinstatementDate,
me.MemberID,
dis.DRI_Name,
case
when me.ReinstatementDate is null then 'Disenrolled'
else 'Reinstated' end as Status
from tblMemEnrollment me
left join tlkpDisenrolReasInvestigated dis on dis.DisenrolReasInvestigID = me.DisenrolRsnInvstgtdID
where me.StateDisenrollDate between '2-1-2015' and '2/28/2015'
and me.ReinstatementDate is null or me.ReinstatementDate between '2/1/2015' and '2/28/2015'
and SubscriberID like '%H%'
union all
select left((datename(mm, me.StateDisenrollDate)),3) as ReportedMonth,
me.StateDisenrollDate,
me.ReinstatementDate,
me.MemberID,
dis.DRI_Name,
case
when me.ReinstatementDate is null then 'Disenrolled'
else 'Reinstated' end as Status
from tblMemEnrollment me
left join tlkpDisenrolReasInvestigated dis on dis.DisenrolReasInvestigID = me.DisenrolRsnInvstgtdID
where me.StateDisenrollDate between '3-1-2015' and '3/31/2015'
and me.ReinstatementDate is null or me.ReinstatementDate between '3/1/2015' and '3/31/2015'
and SubscriberID like '%H%'
union all
select left((datename(mm, me.StateDisenrollDate)),3) as ReportedMonth,
me.StateDisenrollDate,
me.ReinstatementDate,
me.MemberID,
dis.DRI_Name,
case
when me.ReinstatementDate is null then 'Disenrolled'
else 'Reinstated' end as Status
from tblMemEnrollment me
left join tlkpDisenrolReasInvestigated dis on dis.DisenrolReasInvestigID = me.DisenrolRsnInvstgtdID
where me.StateDisenrollDate between '4/1/2015' and '4/30/2015'
and me.ReinstatementDate is null or me.ReinstatementDate between '4/1/2015' and '4/30/2015'
and SubscriberID like '%H%'
union all
select left((datename(mm, me.StateDisenrollDate)),3) as ReportedMonth,
me.StateDisenrollDate,
me.ReinstatementDate,
me.MemberID,
dis.DRI_Name,
case
when me.ReinstatementDate is null then 'Disenrolled'
else 'Reinstated' end as Status
from tblMemEnrollment me
left join tlkpDisenrolReasInvestigated dis on dis.DisenrolReasInvestigID = me.DisenrolRsnInvstgtdID
where me.StateDisenrollDate between '5-1-2015' and getdate()
and me.ReinstatementDate is null or me.ReinstatementDate between '5/1/2015' and getdate()
and SubscriberID like '%H%'
答案 0 :(得分:1)
你有相同的列列表和相同的连接,只是不同的WHERE条件,所以只需使用OR
组合它们:
SELECT LEFT((datename(mm, me.StateDisenrollDate)),3) AS ReportedMonth,
me.StateDisenrollDate,
me.ReinstatementDate,
me.MemberID,
dis.DRI_Name,
CASE
WHEN me.ReinstatementDate IS NULL THEN 'Disenrolled'
ELSE 'Reinstated' end AS Status
FROM tblMemEnrollment me
LEFT JOIN tlkpDisenrolReasInvestigated dis ON dis.DisenrolReasInvestigID = me.DisenrolRsnInvstgtdID
WHERE (me.StateDisenrollDate BETWEEN '1-1-2015' AND '1/31/2015'
AND me.ReinstatementDate IS NULL OR me.ReinstatementDate BETWEEN '1/1/2015' AND '1/31/2015'
AND SubscriberID LIKE '%H%')
OR ( me.StateDisenrollDate BETWEEN '2-1-2015' AND '2/28/2015'
AND me.ReinstatementDate IS NULL OR me.ReinstatementDate BETWEEN '2/1/2015' AND '2/28/2015'
AND SubscriberID LIKE '%H%')
OR ( me.StateDisenrollDate BETWEEN '3-1-2015' AND '3/31/2015'
AND me.ReinstatementDate IS NULL OR me.ReinstatementDate BETWEEN '3/1/2015' AND '3/31/2015'
AND SubscriberID LIKE '%H%')
OR ( me.StateDisenrollDate BETWEEN '4/1/2015' AND '4/30/2015'
AND me.ReinstatementDate IS NULL OR me.ReinstatementDate BETWEEN '4/1/2015' AND '4/30/2015'
AND SubscriberID LIKE '%H%')
OR ( me.StateDisenrollDate BETWEEN '5-1-2015' AND getdate()
AND me.ReinstatementDate IS NULL OR me.ReinstatementDate BETWEEN '5/1/2015' AND getdate()
AND SubscriberID LIKE '%H%')
但我敢打赌,这不是正确的结果,你在没有括号的情况下混合AND
和OR
。提示:优先顺序先是AND
,然后是OR
。
答案 1 :(得分:0)
你说你想分成一年中的每个月,但你根本没有进行任何聚合或计算,只列出一组结果。
您可以将where子句合并为一个(并更改检查两个日期的月份和年份相同的位置)?
select left((datename(mm, me.StateDisenrollDate)),3) as ReportedMonth,
me.StateDisenrollDate,
me.ReinstatementDate,
me.MemberID,
dis.DRI_Name,
case
when me.ReinstatementDate is null then 'Disenrolled'
else 'Reinstated' end as Status
from tblMemEnrollment me
left join tlkpDisenrolReasInvestigated dis on dis.DisenrolReasInvestigID = me.DisenrolRsnInvstgtdID
where SubscriberID like '%H%'
AND ((DATEPART(month, me.StateDisenrollDate) = DATEPART(month, me.ReinstatementDate)
AND DATEPART(year, me.StateDisenrollDate) = DATEPART(year, me.ReinstatementDate))
OR me.ReinstatementDate is null)