每天计算

时间:2012-02-01 22:21:11

标签: sql sql-server sql-server-2008

目前我的查询导致[CountAccns]字段返回每月计数但我需要每日计数!

;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

**如何返回每日CountAccns次而非每月?**

我认为主要问题在于我的GROUP BY,但我不知道如何解决它。

1 个答案:

答案 0 :(得分:1)

我不确定您的数据结构,但似乎无法获得您所追求的内容,例如,如果在同一练习代码中存在多个销售代表的记录,则只能显示销售代表是为该练习代码提供多行数据......