使用SQL Server 2012 - 这是我的查询。除了我想添加状态之外它完美无缺。添加状态的问题是“无区域”区域在技术上可以是多个状态的一部分,因此对于No Zones
,我只想将该部分标记为'NULL'
是否有某种陈述要说 -
if z.Zonename = 'NO zone' then st.Stateshortcode = Null ?
尝试使用CASE
,但这似乎给我带来了一些麻烦。
DECLARE @BulkInfo TABLE (
ZoneID INT,
ZoneName VARCHAR(100),
StateID INT,
StateShortCode VARCHAR(5),
SiteID BIGINT,
SiteEveGID INT,
Papers BIGINT
);
INSERT INTO @BulkInfo (ZoneID, ZoneName, StateID, StateShortCode, SiteID, SiteEveGID, Papers)
SELECT
z.ZoneID,
ISNULL(z.ZoneName,'NO ZONE'),
CASE WHEN st.StateID is null THEN 0 END /** Just screwing around here **/,
st.StateShortCode,
s.SiteID,
wfset.SiteEveGID,
COUNT(c.PaperID) Papers
FROM
dbo.Sites s
LEFT JOIN dbo.SiteLocation sl ON s.SiteID = sl.SiteID
LEFT JOIN dbo.States st ON sl.StateID=st.StateID
LEFT JOIN dbo.Zones z ON sl.ZoneID = z.ZoneID
LEFT JOIN dbo.WFSiteEves wfse ON s.SiteID = wfse.SiteID AND EveStatusID IN (1,2)
LEFT JOIN dbo.WFSiteEveTs wfset ON wfse.EveTID = wfset.EveTID
LEFT JOIN dbo.Papers c ON s.SiteID = c.SiteID
WHERE
s.ProjID = 1
GROUP BY
z.ZoneID,
z.ZoneName,
st.StateID,
st.StateShortCode,
s.SiteID,
wfset.SiteEveGID,
wfse.EveTID
SELECT
t.ZoneName,
t.StateShortCode,
TotalSites,
TotalPapers,
CONVERT(DECIMAL(10,2),TotPer) 'TotPer%',
ISNULL(TotalSitesPart1, 0) TotalSitesPart1,
ISNULL(TotalPapersPart1, 0) TotalPapersPart1,
ISNULL(CONVERT(DECIMAL(10,2),Part1Per),0) 'Part1Per%',
ISNULL(TotalSitesPart2,0) TotalSitesPart2,
ISNULL(TotalPapersPart2,0) TotalPapersPart2,
ISNULL(CONVERT(DECIMAL(10,2),Part2Per),0) 'Part2Per%',
ISNULL(TotalSitesPart3, 0) TotalSitesPart3,
ISNULL(TotalPapersPart3, 0) TotalPapersPart3,
ISNULL(CONVERT(DECIMAL(10,2),Part3Per),0) 'Part3Per%',
ISNULL(TotalSitesPart4,0) TotalSitesPart4,
ISNULL(TotalPapersPart4,0) TotalPapersPart4,
ISNULL(CONVERT(DECIMAL(10,2),Part4Per),0) 'Part4Per%',
ISNULL(TotalSitesPart5,0) TotalSitesPart5,
ISNULL(TotalPapersPart5,0) TotalPapersPart5,
ISNULL(CONVERT(DECIMAL(10,2),Part5Per),0) 'Part5Per%',
ISNULL(TotalSitesPart6,0) TotalSitesPart6,
ISNULL(TotalPapersPart6,0) TotalPapersPart6,
ISNULL(CONVERT(DECIMAL(10,2),Part6Per),0) 'Part6Per%',
ISNULL(TotalSitesPart7,0) TotalSitesPart7,
ISNULL(TotalPapersPart7,0) TotalPapersPart7,
ISNULL(CONVERT(DECIMAL(10,2),Part7Per),0) 'Part7Per%'
FROM
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSites,
SUM(Papers) TotalPapers,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) TotPer
FROM
@BulkInfo
GROUP BY ZoneName,StateShortCode) t
LEFT JOIN
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSitesPart1,
SUM(Papers) TotalPapersPart1,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) Part1Per
FROM
@BulkInfo
WHERE
SiteEveGID = 2
GROUP BY ZoneName,StateShortCode) f ON t.ZoneName = f.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSitesPart2,
SUM(Papers) TotalPapersPart2,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) Part2Per
FROM
@BulkInfo
WHERE
SiteEveGID = 2
GROUP BY ZoneName,StateShortCode) o ON t.ZoneName = o.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSitesPart3,
SUM(Papers) TotalPapersPart3,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) Part3Per
FROM
@BulkInfo
WHERE
SiteEveGID = 4
GROUP BY ZoneName,StateShortCode) fm ON t.ZoneName = fm.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSitesPart4,
SUM(Papers) TotalPapersPart4,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) Part4Per
FROM
@BulkInfo
WHERE
SiteEveGID = 6
GROUP BY ZoneName,StateShortCode) r ON t.ZoneName = r.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSitesPart5,
SUM(Papers) TotalPapersPart5,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) Part5Per
FROM
@BulkInfo
WHERE
SiteEveGID = 7
GROUP BY ZoneName,StateShortCode) cs ON t.ZoneName = cs.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSitesPart6,
SUM(Papers) TotalPapersPart6,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) Part6Per
FROM
@BulkInfo
WHERE
SiteEveGID = 9
GROUP BY ZoneName,StateShortCode) d ON t.ZoneName = d.ZoneName
LEFT JOIN
(SELECT
ZoneName,
StateShortCode,
COUNT(SiteID) TotalSitesPart7,
SUM(Papers) TotalChartsPart7,
SUM(Papers)*100.0/(SELECT SUM(Papers) FROM @BulkInfo) Part7Per
FROM
@BulkInfo
WHERE
SiteEveGID = 8
GROUP BY ZoneName,StateShortCode) m ON t.ZoneName = m.ZoneName
答案 0 :(得分:0)
确定有:是否有某种陈述要说 - 如果z.Zonename = NO区域则st.Stateshortcode = Null ??
update tablename st
join othertable z on z.join_col=st.join_col
set st.Stateshortcode=null
where z.Zonename='NO ZONE'
但是,如果您已经进行了大量更新,则可能需要一个案例陈述:
...
INSERT INTO @BulkInfo (ZoneID, ZoneName, StateID, StateShortCode, SiteID, SiteEveGID, Papers)
SELECT
z.ZoneID,
ISNULL(z.ZoneName,'NO ZONE'),
CASE WHEN st.StateID is null THEN 0 END /** Just screwing around here **/,
case when z.ZoneName is null then null else st.StateShortCode end,
s.SiteID,
wfset.SiteEveGID,
COUNT(c.PaperID) Papers
FROM
dbo.Sites s
LEFT JOIN dbo.SiteLocation sl ON s.SiteID = sl.SiteID
...
答案 1 :(得分:0)
我想你想要这个逻辑:
(case when z.ZoneName is not null then st.StateShortCode end) as StateShortCode
如果它可以取值" No Zone",那么:
(case when z.ZoneName is not null and z.ZoneName <> 'No Zone'
then st.StateShortCode
end) as StateShortCode