SQL Server SQL数据透视表未返回预期结果

jogvjijk  于 2022-11-21  发布在  其他
关注(0)|答案(2)|浏览(109)

这是我第一次不得不使用SQL数据透视表,所以我不是很熟练地使用该方法获得结果,我的数据透视表没有返回我预期的结果,我不知道为什么...
下面是我的指示:
“创建一个SQL,以显示2020 - 2021年每个月每个分支的患者总数create [ddateCreated]。显示必填字段分支名称、年份、一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月。”
所以我提出了下面的查询:

select * from (

  select datename(month, b.dDateCreated) as [Month] ,
  sName as BranchName ,
  datename(Year, b.dDateCreated) as [Year],
  p.ipkPatientID from Branch b
  inner join Patients p
  on p.ifkBranchID=b.ipkBranchID
  where (b.dDateCreated BETWEEN '2020-01-01 00:00:00.000'AND '2021-12-31 23:59:59.999')

  

  ) as Src
  pivot(
  
  count(ipkPatientID)
   for [Month] in ([Jan],
[Feb],
[Mar],
[Apr],
[May],
[Jun],
[Jul],
[Aug],
[Sept],
[Oct],
[Nov],
[Dec])
  
  ) as Pivot_Table

结果如下:

BranchName                   Year   Jan Feb Mar Apr May Jun Jul Aug Sept    Oct Nov Dec
Cdldttd dd Fhdftwt wndfplpgy 2020   0   0   0   0   0   0   0   0      0    0   0   0
Ddjdlppmdnt                  2020   0   0   0   0   14  0   0   0      0    0   0   0
Ddpn Cdhpnfp                 2020   0   0   0   0   0   0   0   0      0    0   0   0
dlmfdn Lpnw Lfchtdnwnhg      2020   0   0   0   0   0   0   0   0      0    0   0   0
dlmfdn Lpnw Mwffkdng         2020   0   0   0   0   0   0   0   0      0    0   0   0
Dnhwwnjflld                  2020   0   0   0   0   0   0   0   0      0    0   0   0
dthpwd                       2020   0   0   0   0   0   0   0   0      0    0   0   0
fnc and wttpcfwtdt           2020   0   0   0   0   5   0   0   0      0    0   0   0
Fwdhfd Gldn                  2020   0   0   0   0   0   0   0   0      0    0   0   0
Hdwhdwt pfffcd               2020   0   0   0   0   0   0   0   0      0    0   0   0
Hpmd jftft                   2020   0   0   0   0   91  0   0   0      0    0   0   0

我不明白为什么它到处都显示0?我已经多次尝试重构这个查询,但我不能正确...还请注意,由于一些奇怪的原因,五月份的数据很少。
请看下面的查询,它是数据透视表的源:

select datename(month, b.dDateCreated) as [Month] ,
  sName as BranchName ,
  datename(Year, b.dDateCreated) as [Year],
  p.ipkPatientID from Branch b
  inner join Patients p
  on p.ifkBranchID=b.ipkBranchID
  where (b.dDateCreated BETWEEN '2020-01-01 00:00:00.000'AND '2021-12-31 23:59:59.999')

这些结果看起来正确吗?

并且每个月都有数据......那么我如何计算每个月的患者ID并显示它呢?

wh6knrhe

wh6knrhe1#

非常肯定这将纠正您的问题

Select * 
 From (
      select left(datename(month, b.dDateCreated),3) as [Month] ,   -- Notice Jan,Feb,Mar
             sName as BranchName ,
             datename(Year, b.dDateCreated) as [Year],
             p.ipkPatientID 
       from  Branch b
       inner join Patients p on p.ifkBranchID=b.ipkBranchID
       where b.dDateCreated BETWEEN '2020-01-01 00:00:00.000'AND '2021-12-31 23:59:59.997'
  ) as Src
  Pivot( count(ipkPatientID) for [Month] in ([Jan],
                                             [Feb],
                                             [Mar],
                                             [Apr],
                                             [May],
                                             [Jun],
                                             [Jul],
                                             [Aug],
                                             [Sep],  -- Notice Sep not Sept
                                             [Oct],
                                             [Nov],
                                             [Dec] ) ) as Pivot_Table
w8ntj3qf

w8ntj3qf2#

我建议使用条件聚合进行透视,而不是使用特定于供应商的pivot查询。它也不太灵活,当然也不容易跨数据库移植。
使用条件聚集,并从现有的工作查询开始,您可以像这样进行透视:

select b.sName as BranchName, x.[Year],
    sum(case when x.[Month] = 'Jan' then 1 else 0 end) as Jan
    sum(case when x.[Month] = 'Feb' then 1 else 0 end) as Feb
    -- ... repeat for the following months...
    sum(case when x.[Month] = 'Dec' then 1 else 0 end) as Dec
from Branch b
inner join Patients p on p.ifkBranchID = b.ipkBranchID
cross apply ( values (datename(year, b.dDateCreated), datename(month, b.dDateCreated) ) d([Month], [Year])
where b.dDateCreated >= '2020-01-01' and b.dDateCreated < '2022-01-01'
group by b.ifkBranchID, b.sName, x.[Year]

附注:

  • 筛选日期通常比使用半开间隔比使用between更简单
  • 您可能希望group bybranch表的主键(我假定它是ifkBranchID);不同的分支可以具有相同的名称。
  • 我只使用outer apply计算datename一次,而不是在整个查询中重复这些表达式
  • 最好在查询中为 all 列加上它们所属的表的前缀

相关问题