Currently my query is resulting in the [CountAccns] field it is returning the monthly count but I need the daily count!
;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]) ,
CASE WHEN DATEPART(mm, [DATE entered]) = 01 THEN 'Jan'
WHEN DATEPART(mm, [DATE entered]) = 02 THEN 'Feb'
WHEN DATEPART(mm, [DATE entered]) = 03 THEN 'Mar'
WHEN DATEPART(mm, [DATE entered]) = 04 THEN 'Apr'
WHEN DATEPART(mm, [DATE entered]) = 05 THEN 'May'
WHEN DATEPART(mm, [DATE entered]) = 06 THEN 'Jun'
WHEN DATEPART(mm, [DATE entered]) = 07 THEN 'Jul'
WHEN DATEPART(mm, [DATE entered]) = 08 THEN 'Aug'
WHEN DATEPART(mm, [DATE entered]) = 09 THEN 'Sep'
WHEN DATEPART(mm, [DATE entered]) = 10 THEN 'Oct'
WHEN DATEPART(mm, [DATE entered]) = 11 THEN 'Nov'
WHEN DATEPART(mm, [DATE entered]) = 12 THEN 'Dec'
END
,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
** how do I return the daily CountAccns count instead of the monthly?**
I believe that the main issue is with my GROUP BY, but I dont know how to solve it.
I am not certain of your data structure but It doesn’t seem possible to get what you are after, for instance if records exist for more than one sales rep at the same practice code the only way to show both the sales reps is to have more than one row of data for that practise code…