目前正在编写一个显示大量信息的SQL Server查询,目前它正在吐出正确的信息,但是大块的信息是重复的,有些区域应该组合在一起。请注意,StateAcronym
部分实际上是新的 - 在添加StateAcronym
之前它都是hunky dory,但是它会全部重复/非组合 - 就像。
我的代码:
DECLARE @BulkInfo TABLE (
ZoneID INT,
ZoneName VARCHAR(100),
StateAcronym VARCHAR(5),
SiteID BIGINT,
SiteGroupID INT,
Papers BIGINT
);
INSERT INTO @BulkInfo (ZoneID, ZoneName, StateAcronym, SiteID, SiteGroupID, Papers)
SELECT DISTINCT
z.ZoneID,
ISNULL(z.ZoneName,'NO ZONE'),
(case when z.ZoneName is not null and z.ZoneName <> 'No Zone'
then st.StateAcronym
else null
end) as StateAcronym,
s.SiteID,
wfset.SiteEventID,
COUNT(distinct c.PaperID) Papers
FROM
Sites s
LEFT JOIN
SiteLoc sl ON s.SiteID = sl.SiteID
LEFT JOIN
States st ON sl.StateID = st.StateID
LEFT JOIN
Zones z ON sl.ZoneID = z.ZoneID
LEFT JOIN
WFSite wfse ON s.SiteID = wfse.SiteID AND StatusID IN (1,2)
LEFT JOIN
WFSiteTypes wfset ON wfse.TypeID = wfset.TypeID
LEFT JOIN
Papers c ON s.SiteID = c.SiteID
WHERE
s.ProjectID = 110
GROUP BY
z.ZoneID,
z.ZoneName,
st.StateAcronym,
s.SiteID,
wfset.SiteGroupID,
wfse.TypeID
SELECT DISTINCT
t.ZoneName,
t.StateAcronym,
TotalSites,
TotalPapers,
CONVERT(DECIMAL(10,2),TotPer) 'TotPer%',
ISNULL(TotalSitesType1, 0) TotalSitesType1,
ISNULL(TotalPapersType1, 0) TotalPapersType1,
ISNULL(CONVERT(DECIMAL(10,2),Type1Per),0) 'Type1Per%',
ISNULL(TotalSitesType2,0) TotalSitesType2,
ISNULL(TotalPapersType2,0) TotalPapersType2,
ISNULL(CONVERT(DECIMAL(10,2),Type2Per),0) 'Type2Per%',
ISNULL(TotalSitesType3, 0) TotalSitesType3,
ISNULL(TotalPapersType3, 0) TotalPapersType3,
ISNULL(CONVERT(DECIMAL(10,2),Type3Per),0) 'Type3Per%',
ISNULL(TotalSitesType4,0) TotalSitesType4,
ISNULL(TotalPapersType4,0) TotalPapersType4,
ISNULL(CONVERT(DECIMAL(10,2),Type4Per),0) 'Type4Per%',
ISNULL(TotalSitesType5,0) TotalSitesType5,
ISNULL(TotalPapersType5,0) TotalPapersType5,
ISNULL(CONVERT(DECIMAL(10,2),Type5Per),0) 'Type5Per%',
ISNULL(TotalSitesType6,0) TotalSitesType6,
ISNULL(TotalPapersType6,0) TotalPapersType6,
ISNULL(CONVERT(DECIMAL(10,2),Type6Per),0) 'Type6Per%',
ISNULL(TotalSitesType7,0) TotalSitesType7,
ISNULL(TotalPapersType7,0) TotalPapersType7,
ISNULL(CONVERT(DECIMAL(10,2),Type7Per),0) 'Type7Per%'
FROM
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSites,
SUM(distinct Papers) TotalPapers,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) TotPer
FROM
@BulkInfo
GROUP BY ZoneName,StateAcronym) t
LEFT JOIN
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSitesType1,
SUM(distinct Papers) TotalPapersType1,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) Type1Per
FROM
@BulkInfo
WHERE
SiteGroupID = 2
GROUP BY ZoneName,StateAcronym) f ON t.ZoneName = f.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSitesType2,
SUM(distinct Papers) TotalPapersType2,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) Type2Per
FROM
@BulkInfo
WHERE
SiteGroupID = 3
GROUP BY ZoneName,StateAcronym) o ON t.ZoneName = o.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSitesType3,
SUM(distinct Papers) TotalPapersType3,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) Type3Per
FROM
@BulkInfo
WHERE
SiteGroupID = 4
GROUP BY ZoneName,StateAcronym) fm ON t.ZoneName = fm.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSitesType4,
SUM(distinct Papers) TotalPapersType4,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) Type4Per
FROM
@BulkInfo
WHERE
SiteGroupID = 6
GROUP BY ZoneName,StateAcronym) r ON t.ZoneName = r.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSitesType5,
SUM(distinct Papers) TotalPapersType5,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) Type5Per
FROM
@BulkInfo
WHERE
SiteGroupID = 7
GROUP BY ZoneName,StateAcronym) cs ON t.ZoneName = cs.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSitesType6,
SUM(distinct Papers) TotalPapersType6,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) Type6Per
FROM
@BulkInfo
WHERE
SiteGroupID = 9
GROUP BY ZoneName,StateAcronym) d ON t.ZoneName = d.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSitesType7,
SUM(distinct Papers) TotalPapersType7,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) Type7Per
FROM
@BulkInfo
WHERE
SiteGroupID = 8
GROUP BY ZoneName,StateAcronym) m ON t.ZoneName = m.ZoneName
ORDER BY t.ZoneName, t.StateShortCode
以下是查询吐出的一些示例
ZoneName | StateAcronym | TotalSites | TotalPapers | TotPer% | TotalSitesType1 | TotalPapersType1 | Type1Per% | TotalSitesType2 | TotalPapersType2 | Type2Per% | TotalSitesType3 | TotalPapersType3 | Type3Per% | TotalSitesType4 | TotalPapersType4 | Type4Per% | TotalSitesType5 | TotalPapersType5 | Type5Per% | TotalSitesType6 | TotalPapersType6 | Type6Per% | TotalSitesType7 | TotalPapersType7 | Type7Per%
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| NJ | 64 | 198 | 0.19 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 8 | 46 | 0.04 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 55 | 174 | 0.17 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 1 | 6 | 0.01 | 39 | 133 | 0.13
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZoneName1| PA | 2123 | 7764 | 7.38 | 648 | 2332 | 2.22 | 316 | 4167 | 3.96 | 900 | 2881 | 2.74 | 0 | 0 | 0.00 | 161 | 1017 | 0.97 | 47 | 475 | 0.45 | 39 | 133 | 0.13
我理解为什么它有点偏离,因为相同的区域可以分配给不同的StateAcronyms,但我不确定为什么它仍然显示多个并且没有组合信息
答案 0 :(得分:0)
如果查看输出,您会发现实际上没有任何重复项。
我猜您的子查询返回了多个StateAcronym
值,因此需要将JOIN
条件更改为:
ON t.ZoneName = f.ZoneName
AND t.StateAcronym = f.StateAcronym
在这种情况下进行的一个简单测试就是运行一个子查询,限制为单个ZoneName
,我打赌如果你运行:
SELECT
ZoneName,
StateAcronym,
COUNT(distinct SiteID) TotalSites,
SUM(distinct Papers) TotalPapers,
SUM(distinct Papers)*100.0/(SELECT SUM(distinct Papers) FROM @BulkInfo) TotPer
FROM @BulkInfo
WHERE ZoneName = 'ZoneName1'
GROUP BY ZoneName,StateAcronym
由于StateAcronym
值不同,您将获得超过1行。