嗨,我试图用两个以上的表创建一个数据表,但是我的返回数据集有一个空值。我想使它返回0并同时使用ISNULL和COALESCE,但是自从我使用Pivot以来,这两个表都没有对我起作用操作。 以下是我的SQL查询。
SELECT MetricHeader,[16],[17],[18],[22],[23],[24],[26],[27],[28],[29],[30]
FROM (
SELECT 'P1' as MetricHeader,co.OptionId as CrossbreakHeader,
ISNULL(CONVERT(decimal,k.Response),0) as KpiResponse
FROM
(
SELECT PanelistId,ProjectId FROM zcachetable_Edm_Sampling_1_5
INTERSECT
SELECT PanelistId,ProjectId FROM zcachetable_Edm_Sampling_1_5
INTERSECT
SELECT PanelistId,ProjectId FROM zcachetable_Edm_Responses_1_17_51 as d
WHERE d.ResponseCode IN (42,43,44,45,46,47,48,49,50,51)
INTERSECT
SELECT PanelistId,ProjectId FROM zcachetable_Edm_Responses_1_17_54 as d
WHERE d.ResponseCode IN (16,17,18,22,23,24,26,27,28,29,30)
INTERSECT
SELECT PanelistId,ProjectId FROM zcachetable_Edm_Responses_1_17_55 as d
WHERE d.ResponseCode IN (19,20,21,31,32,33,34,35)
) f
INNER JOIN zcachetable_Edm_Responses_1_17_54 cc ON cc.PanelistId = f.PanelistId AND f.ProjectId=cc.ProjectId
INNER JOIN n1 co ON ((cc.ResponseCode IS NOT NULL AND cc.ResponseCode = co.OptionId))
INNER JOIN zcachetable_Edm_Responses_1_17_55_KPIScore k ON cc.PanelistId=k.PanelistId AND cc.ProjectId=k.ProjectId
) sq
PIVOT
(
AVG(KpiResponse)
FOR sq.CrossbreakHeader IN ([16],[17],[18],[22],[23],[24],[26],[27],[28],[29],[30])
) AS pt
我已经附加了结果集的图片,并且它的列值30为空值。我想将其设置为0,并且尝试了Internet上的每个选项都找不到解决方案。我将非常感谢你们可以建议我这样做。
答案 0 :(得分:1)
您可以使用ISNULL()
或COALESCE()
处理NULL值
示例,您的查询
ISNULL ( [30], 0)
答案 1 :(得分:1)
进行透视时,全神贯注于此非常容易,但实际上,这是您需要使用COALESE或ISNULL的最终select子句,如下所示:
SELECT
MetricHeader
, COALESCE([16], 0) AS [16]
, COALESCE([17], 0) AS [17]
, COALESCE([18], 0) AS [18]
, COALESCE([22], 0) AS [22]
, COALESCE([23], 0) AS [23]
, COALESCE([24], 0) AS [24]
, COALESCE([26], 0) AS [26]
, COALESCE([27], 0) AS [27]
, COALESCE([28], 0) AS [28]
, COALESCE([29], 0) AS [29]
, COALESCE([30], 0) AS [30]
FROM (
SELECT
'P1' AS metricheader
, co.OptionId AS crossbreakheader
, ISNULL(CONVERT(decimal, k.Response), 0) AS kpiresponse
FROM (
SELECT
PanelistId
, ProjectId
FROM zcachetable_Edm_Sampling_1_5
INTERSECT
SELECT
PanelistId
, ProjectId
FROM zcachetable_Edm_Sampling_1_5
INTERSECT
SELECT
PanelistId
, ProjectId
FROM zcachetable_Edm_Responses_1_17_51 AS d
WHERE d.ResponseCode IN (42, 43, 44, 45, 46, 47, 48, 49, 50, 51)
INTERSECT
SELECT
PanelistId
, ProjectId
FROM zcachetable_Edm_Responses_1_17_54 AS d
WHERE d.ResponseCode IN (16, 17, 18, 22, 23, 24, 26, 27, 28, 29, 30)
INTERSECT
SELECT
PanelistId
, ProjectId
FROM zcachetable_Edm_Responses_1_17_55 AS d
WHERE d.ResponseCode IN (19, 20, 21, 31, 32, 33, 34, 35)
) f
INNER JOIN zcachetable_Edm_Responses_1_17_54 cc ON cc.PanelistId = f.PanelistId
AND f.ProjectId = cc.ProjectId
INNER JOIN n1 co ON ((cc.ResponseCode IS NOT NULL
AND cc.ResponseCode = co.OptionId))
INNER JOIN zcachetable_Edm_Responses_1_17_55_KPIScore k ON cc.PanelistId = k.PanelistId
AND cc.ProjectId = k.ProjectId
) sq
PIVOT
(
AVG(KpiResponse)
FOR sq.CrossbreakHeader IN ([16], [17], [18], [22], [23], [24], [26], [27], [28], [29], [30])
) AS pt
答案 2 :(得分:0)
@Squirrel
为您提供了解决此问题的技巧。ISNULL
已发生,然后再进行透视。对于CrossbreakHeader = '30'
,没有行,仅此而已。
–伊万·斯塔诺斯汀
create table #null ( i int, name varchar(10))
insert #null values
(1,'One'),(2,'Two'),(3,'Three'),(4,'Four'),(null,'Five')
select one, five, six from ( -- Here, NULL for 'six'
select name, ISNULL(i, 0) i from #null
) n
pivot
(
sum(i) for name in (one, five, six) -- I cant insert 'six'
)p