目前我在下面有这个查询产生了很棒的结果,但是,我试图计算总的SiteID数(不管查询中的其他因素如何)。
到目前为止,这是我的查询:
SELECT
TotalCharts ,
SiteID ,
Type,
CASE
WHEN TotalCharts BETWEEN 1 AND 4 THEN '1-4 Charts'
WHEN TotalCharts BETWEEN 5 AND 9 THEN '5-9 Charts'
WHEN TotalCharts BETWEEN 10 AND 24 THEN '10-24 Charts'
WHEN TotalCharts BETWEEN 25 AND 49 THEN '25-49 Charts'
WHEN TotalCharts BETWEEN 50 AND 99 THEN '50-99 Charts'
WHEN TotalCharts BETWEEN 100 AND 249 THEN '100-249 Charts'
WHEN TotalCharts BETWEEN 250 AND 499 THEN '250-499 Charts'
WHEN TotalCharts BETWEEN 500 AND 1000 THEN '500-1000 Charts'
WHEN TotalCharts > 1000 THEN 'Greater 1000'
END AssignmentType,
CASE
WHEN TotalCharts BETWEEN 1 AND 4 THEN '1'
WHEN TotalCharts BETWEEN 5 AND 9 THEN '2'
WHEN TotalCharts BETWEEN 10 AND 24 THEN '3'
WHEN TotalCharts BETWEEN 25 AND 49 THEN '4'
WHEN TotalCharts BETWEEN 50 AND 99 THEN '5'
WHEN TotalCharts BETWEEN 100 AND 249 THEN '6'
WHEN TotalCharts BETWEEN 250 AND 499 THEN '7'
WHEN TotalCharts BETWEEN 500 AND 1000 THEN '8'
WHEN TotalCharts > 1000 THEN '9'
END AssignmentTypeIndex
FROM (
SELECT
COUNT(c.ChartID) TotalCharts,
c.SiteID,
CASE WHEN ChartRetrievalMethodID IS NULL THEN wfseg.SiteEventGroupID
ELSE ChartRetrievalMethodID END as Type
FROM
dbo.Sites s
INNER JOIN dbo.Charts c ON s.SiteID = c.SiteID
LEFT JOIN dbo.ChartRetrievalInformation cri ON c.ChartID=cri.ChartID
LEFT JOIN dbo.WFSiteEvents wfse ON s.SiteID = wfse.SiteID AND EventStatusID IN (1,2)
LEFT JOIN dbo.WFSiteEventTypes wfset ON wfse.EventTypeID = wfset.EventTypeID
LEFT JOIN dbo.WFSiteEventGroups wfseg ON wfset.SiteEventGroupID = wfseg.SiteEventGroupID
WHERE
c.SiteID <> 110
and s.ProjectID=160
GROUP BY
c.SiteID,
CASE WHEN ChartRetrievalMethodID IS NULL THEN wfseg.SiteEventGroupID
ELSE ChartRetrievalMethodID END
) d
ORDER BY SiteID
所以现在它显示了单独的siteID,我想显示完整的SiteIds数
然而,我认为真正使用count(distinct siteID)
的棘手部分是因为存在具有相同SiteID的各种信息的行。示例:
TotalCharts | SiteID | SiteEventGroupID | AssignmentType | AssignmentTypeIndex
-------------------------------------------------------------------------------
9 | 12345 | 4 | 5-9 Charts | 2
-------------------------------------------------------------------------------
1 | 12345 | 3 | 1-4 Charts | 1
所以在这种情况下,我有两个不同的行用于相同的SiteID,我只想计算这个特定的SiteID而不管其他信息。因此,我不想两次计算此SiteID,而是只计算一次。
有没有办法可以算上那个特定的专栏?
答案 0 :(得分:1)
在这种情况下,您需要的是窗口功能。 以下是TSQL窗口函数的参考:http://msdn.microsoft.com/en-ca/library/ms189461.aspx
修改现有代码块:
SELECT
TotalCharts,
SiteID,
Type,
AssignmentType,
AssignmentTypeIndex,
COUNT(SiteID) OVER (PARTITION BY SiteID) NumRowsbySite
FROM(
SELECT
TotalCharts ,
SiteID,
Type,
CASE
WHEN TotalCharts BETWEEN 1 AND 4 THEN '1-4 Charts'
WHEN TotalCharts BETWEEN 5 AND 9 THEN '5-9 Charts'
WHEN TotalCharts BETWEEN 10 AND 24 THEN '10-24 Charts'
WHEN TotalCharts BETWEEN 25 AND 49 THEN '25-49 Charts'
WHEN TotalCharts BETWEEN 50 AND 99 THEN '50-99 Charts'
WHEN TotalCharts BETWEEN 100 AND 249 THEN '100-249 Charts'
WHEN TotalCharts BETWEEN 250 AND 499 THEN '250-499 Charts'
WHEN TotalCharts BETWEEN 500 AND 1000 THEN '500-1000 Charts'
WHEN TotalCharts > 1000 THEN 'Greater 1000'
END AssignmentType,
CASE
WHEN TotalCharts BETWEEN 1 AND 4 THEN '1'
WHEN TotalCharts BETWEEN 5 AND 9 THEN '2'
WHEN TotalCharts BETWEEN 10 AND 24 THEN '3'
WHEN TotalCharts BETWEEN 25 AND 49 THEN '4'
WHEN TotalCharts BETWEEN 50 AND 99 THEN '5'
WHEN TotalCharts BETWEEN 100 AND 249 THEN '6'
WHEN TotalCharts BETWEEN 250 AND 499 THEN '7'
WHEN TotalCharts BETWEEN 500 AND 1000 THEN '8'
WHEN TotalCharts > 1000 THEN '9'
END AssignmentTypeIndex
FROM (
SELECT
COUNT(c.ChartID) TotalCharts,
c.SiteID,
CASE WHEN ChartRetrievalMethodID IS NULL THEN wfseg.SiteEventGroupID
ELSE ChartRetrievalMethodID END as Type
FROM
dbo.Sites s
INNER JOIN dbo.Charts c ON s.SiteID = c.SiteID
LEFT JOIN dbo.ChartRetrievalInformation cri ON c.ChartID=cri.ChartID
LEFT JOIN dbo.WFSiteEvents wfse ON s.SiteID = wfse.SiteID AND EventStatusID IN (1,2)
LEFT JOIN dbo.WFSiteEventTypes wfset ON wfse.EventTypeID = wfset.EventTypeID
LEFT JOIN dbo.WFSiteEventGroups wfseg ON wfset.SiteEventGroupID = wfseg.SiteEventGroupID
WHERE
c.SiteID <> 110
and s.ProjectID=160
GROUP BY
c.SiteID,
CASE WHEN ChartRetrievalMethodID IS NULL THEN wfseg.SiteEventGroupID
ELSE ChartRetrievalMethodID END
) d
ORDER BY SiteID) a