我有一个非常混乱的查询,我想知道是否有任何方法来分离/简化子查询或它需要什么。看起来我好像是这个!! http://weblogs.sqlteam.com/jeffs/archive/2005/12/14/8546.aspx
;with
cte_biggie ( [Full Date], [Year Entered], [Month Entered], [Day Entered],
[DOW], [Week Ending] ,[CountAccns],[Sales Rep], [MLNPI], [IMSNPI], [Physician],
[Practice Code], [MLIS Code], [Practice Name],
[Date Established], [Address], [Address2], [City], [State], [Status]
) as (
select CONVERT(VARCHAR(8), [DATE entered], 1),DATEPART(yy, [DATE entered]) ,
LEFT(DATENAME(MONTH, GETDATE()), 3)
,DATEPART(dd, [DATE entered]),
case when DATEPART(WEEKDAY, [DATE entered])=1 THEN 'Sun'
when DATEPART(WEEKDAY, [DATE entered])=2 THEN 'Mon'
when DATEPART(WEEKDAY, [DATE entered])=3 THEN 'Tus'
when DATEPART(WEEKDAY, [DATE entered])=4 THEN 'Wed'
when DATEPART(WEEKDAY, [DATE entered])=5 THEN 'Thu'
when DATEPART(WEEKDAY, [DATE entered])=6 THEN 'Fri'
when DATEPART(WEEKDAY, [DATE entered])=7 THEN 'Sat'
end,
CONVERT(VARCHAR(8), DATEADD (D, -1 * DatePart (dw,[date entered]) + 6, [date entered]), 1),
count(a.[specimen id]) ,c.salesrep,c.npi,e.npib,[Requesting Physician] ,
a.[practice code],b.[mlis practice id],[practice name],
c.dateestablished , c.practiceaddress1, c.practiceaddress2,c.practicecity,c.practicestate,
b.[Active Inactive]
from quicklabdump a
left outer join qlmlismapping b
on (b.[practice code] = a.[practice code])
left outer join PracticeandPhysician c
on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME
and a.[practice code]=c.practicecode)
left outer join TestResults d
on a.QuickLabDumpID = d.QuickLabDumpID
left outer join IMSData e
on c.NPI=e.npib
where [Date Entered] <= '20111231'
and [Date Entered] >= '20111201'
group by [DATE entered],DATEPART(yy, [DATE entered]), DATEPART(mm, [DATE entered]),DATEPART(dd, [DATE entered]), a.[practice name],b.[mlis practice id],a.[practice code],
a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate,c.npi,e.npib,c.practiceaddress1 ,c.practiceaddress2,
b.[Active Inactive]
)
select * from cte_biggie
答案 0 :(得分:2)
我通常不同意link that you provided关于使用子选择来制作分组更清晰的问题。自从它从Sybase烤箱出来以来,我一直在SQLServer中编写TSQL。对我来说,很明显,在结果集中返回的不是聚合的列都在GROUP BY中迭代。事实上,一旦他们在GROUP BY部分编码,我甚至不关注它们。对我来说,看到一个不必要的子选择实际上令人分心。我倾向于只使用子选项,因为它们是绝对必要的,这种情况确实会不时发生。但是,当我看到一个子选择时,我特别注意逻辑,因为我知道我只是在发生特殊情况时才使用它们。我在GROUP BY的最后看到一些额外的列没有问题,我认为SQL引擎足够聪明,拥有完整的列列表并不会造成性能损失。
以下是我如何格式化SQL。我不喜欢使用有空格的列名,所以我可能会删除列名中的空格并删除所有括号,但是因为你有它们我只是跟着你的领导。
select
[Full Date]=CONVERT(VARCHAR(8), [DATE entered], 1),
[Year Entered]=DATEPART(yy, [DATE entered]) ,
[Month Entered]=LEFT(DATENAME(MONTH, GETDATE()), 3),
[Day Entered]=DATEPART(dd, [DATE entered]),
[DOW]=
case when DATEPART(WEEKDAY, [DATE entered])=1 THEN 'Sun'
when DATEPART(WEEKDAY, [DATE entered])=2 THEN 'Mon'
when DATEPART(WEEKDAY, [DATE entered])=3 THEN 'Tus'
when DATEPART(WEEKDAY, [DATE entered])=4 THEN 'Wed'
when DATEPART(WEEKDAY, [DATE entered])=5 THEN 'Thu'
when DATEPART(WEEKDAY, [DATE entered])=6 THEN 'Fri'
when DATEPART(WEEKDAY, [DATE entered])=7 THEN 'Sat'
end,
[Week Ending]=CONVERT(VARCHAR(8),
DATEADD (D, -1 * DatePart (dw,[date entered]) + 6, [date entered]), 1),
[CountAccns]=count(a.[specimen id]),
[Sales Rep]=c.salesrep,
[MLNPI]=c.npi,
[IMSNPI]=e.npib,
[Physician]=[Requesting Physician],
[Practice Code]=a.[practice code],
[MLIS Code]=b.[mlis practice id],
[practice name],
[Date Established]=c.dateestablished ,
[Address]=c.practiceaddress1,
[Address2]=c.practiceaddress2,
[City]=c.practicecity,
[State]=c.practicestate,
[Status]=b.[Active Inactive]
from
quicklabdump a
left outer join qlmlismapping b on (b.[practice code] = a.[practice code])
left outer join PracticeandPhysician c on
a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME
and a.[practice code]=c.practicecode
left outer join TestResults d on a.QuickLabDumpID = d.QuickLabDumpID
left outer join IMSData e on c.NPI=e.npib
where
[Date Entered] <= '20111231'
and [Date Entered] >= '20111201'
group by
c.salesrep,
c.npi,
e.npib,
[Requesting Physician],
a.[practice code],
b.[mlis practice id],
[practice name],
c.dateestablished ,
c.practiceaddress1,
c.practiceaddress2,
c.practicecity,
c.practicestate,
b.[Active Inactive]