筛选周末和周日的结果

时间:2018-07-24 14:17:12

标签: sql-server

我在下面的查询中给出了如下所示的结果。因此,表 [Ops]。[dbo] .Inventory 实际上是每日数据的库存快照。但我希望最终结果显示“每周”的总数。例如,当我执行查询时,它应该显示从星期六开始的每周的结果。有什么想法吗 ?谢谢

                        drop table #tempRD
                        drop table #temp
                        drop table #TempActive
                        drop table #dt


                        select f.ClaimNum,
                        datepart(wk, convert(date,CONVERT(varchar(10),SnapshotDateKey,101))) as WeekNo
                        ,DATEADD(dd, 7-(DATEPART(dw, convert(date,CONVERT(varchar(10),SnapshotDateKey,101)))), convert(date,CONVERT(varchar(10),SnapshotDateKey,101))) as Weekend
                        , DATEADD(DAY, 1 - DATEPART(WEEKDAY, convert(date,CONVERT(varchar(10),SnapshotDateKey,101))), CAST(convert(date,CONVERT(varchar(10),SnapshotDateKey,101)) AS DATE)) [WeekStart]
                        , year(convert(date,CONVERT(varchar(10),SnapshotDateKey,101))) as YearNo

                        into #Dt
                               from [Ops].[dbo].Inventory f  

                        select f.ClaimNum
                         --into #tempRD
                               from [Ops].[dbo].Inventory f                  
                               WHERE f.RDIndicator =  1
                               and Claim_featureInd = 1
                               and Specialty = 'RD'
                               and convert(date,CONVERT(varchar(10),SnapshotDateKey,101)) = '07/23/2018'



                        select a.ClaimNum , case when Subro = 1 and PD + BI +SIU + LITIGATION = 0 THEN 1 else 0 end as subro_only, PD,BI,Subro,SIU,Litigation, 
                        case when rd.ClaimNum is not null then 1 else 0 end as RD ,
                        a.Claim_FeatureStatus,c.LossRsvAmt, c.ExpRsvAmt 
                        into #temp
                        from (               SELECT distinct  f.ClaimNum, Claim_FeatureStatus,
                                                                                sum(case when PD_featureInd = 1  and isnull(PD_featurestatus,'') not in ('Closed','Cancelled','Abandoned','')  then 1 else 0 end ) as PD
                                                                                ,sum(case when BI_featureInd= 1 and isnull(BI_featurestatus,'') not in ('Closed','Cancelled','Abandoned','') then 1 else 0 end ) as BI
                                                                                ,sum(case when Subro_featureInd= 1 and isnull(Subro_featurestatus,'') not in ('Closed','Cancelled','Abandoned','')  then 1 else 0 end ) as Subro
                                                                                ,sum(case when SIU_featureInd= 1 and isnull(SIU_featurestatus,'') not in ('Closed','Cancelled','Abandoned','')  then 1 else 0 end ) as SIU
                                                                                ,sum(case when Lit_featureInd= 1 and isnull(Lit_featurestatus,'') not in ('Closed','Cancelled','Abandoned','')  then 1 else 0 end ) as Litigation
                                                                                ,sum(case when Claim_featureInd= 1 then 1 else 0 end ) as Claim

                                                                    FROM [Ops].[dbo].Inventory  F    
                                                                    where convert(date,CONVERT(varchar(10),SnapshotDateKey,101)) = '07/23/2018'
                                                                    -- and ClaimNum = '2279919' 
                                                                    group by ClaimNum, Claim_FeatureStatus
                                                                    ) a  
                        left outer join  #tempRD  Rd on rd.ClaimNum = a.ClaimNum
                        left outer join [Ops].[dbo].Inventory C on  c.ClaimNum = a.ClaimNum and isnull(c.Claim_FeatureStatus,'') not in ('Closed','Cancelled','Abandoned','')

                        where

                        ---claim = 1 and  a.ClaimNum  > 2000009 and 
                        ( isnull(LossRsvAmt,0) <>  0 or isnull(ExpRsvAmt,0) <> 0  or  Subro <> 0 or  siu <> 0 ) 
                        and convert(date,CONVERT(varchar(10),SnapshotDateKey,101)) = '07/23/2018'
                        ------------------------------------------------------------------------------

                        select  case when RD = 1 then 'RD' 
                                     else 
                                     case when Claim_FeatureStatus <> 'Re-opened' and subro_only = 0 and SIU=0 then 'Open' 
                                                          when Claim_FeatureStatus = 'Re-opened' and subro_only = 0  and SIU = 0 then 'Re-Opened'
                                                          when SIU = 1 then 'SIU'
                                                          else 'Subrogation'
                                             end

                                   end  as ClaimStatus,ClaimNum , LossRsvAmt 
                                  into #tempActive
                                    from #temp

                        Select claimstatus , count(claimNum) as claimCount from #tempActive
                        where LossRsvAmt <>0 or claimstatus = 'Subrogation'
                        group by claimstatus
                        union 
                        select case when subro = 1 then 'Subrogation Inventory' end , count(ClaimNum) from #temp where subro = 1 group by case when subro = 1 then 'Subrogation Inventory' end

当前结果

            claimstatus         claimCount
            Open                    7309
            RD                      19659
            Re-Opened               616
            SIU                     271
            Subrogation             819
            Subrogation Inventory   1827

预期结果

            WeekNo  WeekStart        WeekEnd        YearNo      RD      Open    Subrogation     Re-Opened       Subrogation Inventory
            29      2018-07-15      2018-07-21      2018        19559   7102        786             568             1750
            30      2018-07-22      2018-07-28      2018        19659   7308        819             616             1827    

0 个答案:

没有答案