i have a pretty messy query, i would like to know if there is any way to separate/simplify it subqueries or what ever it takes. it looks like i am a suspect of this!! 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
I would generally disagree with the link that you provided regarding the use of sub-selects to make group by’s clearer. I’ve been writing TSQL in SQLServer ever since it came out of the Sybase oven. For me, it is quite clear that whatever columns being returned in the result set that are not aggregates are iterated in the GROUP BY. In fact, once they’re coded in the GROUP BY section I don’t even pay attention to them. For me it’s actually distracting to see a sub-select that is not necessary. I tend to only use sub-selects for when they’re an absolute necessity, which does happen from time to time. But, then when I see a sub-select that’s my cue to pay special attention to the logic because I know I’m only using them when something special is happening. I have no problem seeing a few extra columns at the end in my GROUP BY and I think the SQL engine is smart enough that having the full list of columns is not a performance loss.
Here’s how I would format your SQL. I’m not a fan of using column names that have spaces so I would probably remove spaces from column names and get rid of all the brackets but since you have them I just followed your lead.