我最近做了一个SQL PIVOT,我发现它不太正常。这是我正在使用的
SQL Server 2005 使用供应商视图的供应商数据库 - 我无法创建额外的表或视图或直接查询数据表(长篇故事)
我每月查询一组包含多个条目的数据,并将其与第二组结合起来制作我称之为数据集的数据集。我正在使用CASE WHEN分组对象(即dbid 1,7和10的结果是'a',2,3和9是'b'等。
我正在转动数据集以按月显示总量。
我的问题在于其中一个列,它没有计算数据集中的所有结果,这些结果似乎是一年中的一个月。仔细研究后,我发现缺少的数据似乎是两行数据在下面的查询中对N_Entered具有相同值的情况。
我设法使用通用查询重建问题(如下所示)。我在sql server express 2008 r2和sql server 2005上运行这个,我得到了相同的结果 - 这是
Begin_Time Totals A Apples B香蕉香蕉C胡萝卜胡萝卜D狗狗 2014-02-01 00:00:00.000 174732 44000 4591 581 208
2014年2月的B总和为4594,但此查询返回4591.您会注意到表2中“2”的两个条目,其中“3”为N_Entered。
如果我删除了用table2对数据集进行联合的部分 - 我得到了正确的答案--4594。
我的100万美元的问题是
此代码与我的生产查询之间存在差异
我从生产中的视图中选择而不是使用表变量 生产跨度超过一年的结果,我只是保留了'datediff条款,尽量保持尽可能接近。
非常感谢!
declare @vartable as table (Begin_Time DateTime, Letter varchar(30), N_Entered int)
declare @vartable2 as table (Begin_Time DateTime, Letter varchar(30), N_Entered int)
--populate first table
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'D', 125)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 2519)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 23)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 271)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 9)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 3)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 37)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 357)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 24230)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 9)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'D', 83)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 1998)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 13)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 217)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 3)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 1)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'B', 24)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 212)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 18656)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'A', 12)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'C', 272)
insert into @vartable (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'C', 309)
--populate second table
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 1009)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 90810)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 198)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 253)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 447)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 829)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 80427)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 152)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 149)
insert into @vartable2 (Begin_Time, Letter, N_Entered) Values ('2014-02-01 00:00:00', 'T', 458)
SELECT * FROM
(SELECT Begin_Time,
CASE Letter
WHEN 'A' THEN 'A Apples'
WHEN 'B' THEN 'B Banana Bananas'
WHEN 'C' THEN 'C Carrot Carrots'
WHEN 'D' THEN 'D Dog Dogs'
ELSE Letter
END 'Option Selected'
, N_Entered
From @vartable
WHERE (Datediff(month, begin_time, getdate()) Between 1 and 12)
AND Letter IN ( 'A', 'B', 'C', 'D' ) -- end first select
--removing this union section does give the right answer
UNION
(
SELECT Begin_Time, 'Totals' as 'Total Numbers', Sum(N_Entered) As N_Entered
FROM @vartable2
WHERE
Letter IN ('T')
AND (Datediff(month, begin_time, getdate()) Between 1 and 12)
GROUP BY begin_time
) -- end second select
-- end of union section
) As Data
PIVOT
(
SUM(N_ENTERED)
FOR [Option Selected] IN ([Totals], [A Apples], [B Banana Bananas], [C Carrot Carrots], [D Dog Dogs])
)
As PivotTable;
答案 0 :(得分:0)
将Union替换为Union ALL
Union正在消除重复值,即B中的值3. Union ALL认为它。