sql-server - 我如何在 SQL SERVER 中的数据透视表的结果中显示“0”而不是 NULL
问题描述
在我的某些列的@disttable 中,结果显示为空。相反,我想显示 0。我想在数据透视表中显示 'O' 来代替 NULL。我试图为 Total 提供 isnull ..但它不能正常工作。
declare @DistTable Table
(
Party nvarchar(200),
DistName nvarchar(200),
Total int,
TotalSeats int,
DeclaredSeats int
)
insert into @DistTable
SELECT C.English AS Party,f.English as DistNAME,count(C.English) as Total,
TOTALSEATS=(SELECT COUNT(*) FROM TBL_CONSTITUENCYMASTER c WHERE c.Phase= 3 and c.StateCode=29 and c.reg_code = 61),
DECLAREDSEATs=(SELECT COUNT(*) FROM TBL_CONSTITUENCYMASTER c WHERE c.Phase= 3 and c.StateCode=29 and c.reg_code = 61 and Lead_WonCode=100)
FROM TBL_CONSTITUENCYMASTER A
LEFT OUTER JOIN tbl_CandidateMaster B ON A.Lead_CandiCode = B.Cand_Code
LEFT OUTER JOIN tbl_AllianceMaster C ON B.AllianceCode = C.AllianceCode
join tbl_regionmaster f on a.reg_code=f.reg_code
join tbl_olddistrictMaster e on a.Old_dist_code=e.old_dist_code
join tbl_DistrictMaster D on A.Dist_Code=D.Dist_Code WHERE A.STATECODE = 29 and A.Phase = 3 and A.Lead_WonCode = 100 and f.reg_code = 61 group by c.English,f.English order by F.English
select * from @DistTable pivot (min(Total) for Party in([TRS],[INC],[TDP],[BJP],[CPI],[CPM],[OTH])) as t
结果是
ADILABAD 163 163 67 27 NULL NULL NULL NULL 69
我如何0
在 SQL SERVER 中显示数据透视表的结果而不是 NULL?
解决方案
我会改为进行条件聚合:
SELECT dt.DistName,
min(case when dt.Party = 'TRS' then dt.Total else 0 end) as TRS,
. . .
FROM @DistTable dt
GROUP BY dt.DistName;
推荐阅读
- c# - Binding a setter value inside a trigger to an element
- ssl - Squeak-5.2 can't connect to https://github.com/
- apache-spark - How to debug the apache spark when it stuck at the certain line?
- html - Empty post title Wordpress
- python - 如何在 9.414 版本中使用 executemany 忽略 postgres 中的重复项?
- javascript - $ character in ReactJs
- redundancy - 在链接失败的情况下将请求重定向到其他 IP 地址
- python - TensorFlow 在集群上运行(40k 核心,500TFLOP/s 和 40k RAM)与 NVIDIA GTX 1080
- swift - 如何动态更改 PageViewController 中的背景?
- reactjs - 会话存储无法保存在 reactjs 应用程序中