Obteniendo recuento por día

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.

preguntado el 01 de febrero de 12 a las 22:02

I don't really understand how you can have a monthly total when you are grouping by DATEPART(dd, [Date Entered]) so can't help you there, but can offer some general advise - the DATENAME Function can be used instead of your case statements to get the month/day as a string: LEFT(DATENAME(MONTH, GETDATE()), 3) = 'jan' : Also if you are still in development phase consider an overhaul of your database structure. Just because you can put spaces in column names, does not mean you should! Lastly I don't understand why you are using a CTE, then performing a single select from it? -

@GarethD thank you so much for your valuable advice. i will update this with your advice -

@GarethD im sorry i want the DAILY not monthly -

any other help would me most appreciated, im very stuck on the GROU PBY i think that is my main problem -

I don't understand how the query can be returning a monthly count, do you mean the count on each row is for one month, or each time you execute the query it is returning a full month's worth of data? -

1 Respuestas

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...

Respondido 02 Feb 12, 03:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.