三表连接sql server获取最新版本和计数

时间:2016-02-25 23:10:03

标签: sql sql-server stored-procedures

我试图计算具有特定状态的记录数。

第一个表名为Feedback,结构为:

+------------+---------+--------+
| FeedbackID | Version | Status |
+------------+---------+--------+
|      1     |    1    |  Open  |
+------------+---------+--------+
|      2     |    1    |  Open  |
+------------+---------+--------+
|      2     |   1.1   |  Open  |
+------------+---------+--------+
|      1     |   1.3   |  Open  |
+------------+---------+--------+
|      1     |   1.3   | Closed |
+------------+---------+--------+

第二个ProjectID

+-----------+------------+-----------------+
| ProjectID | FeedbackID | FeedbackVersion |
+-----------+------------+-----------------+
|     53    |      1     |       1.1       |
+-----------+------------+-----------------+
|     23    |      2     |       1.1       |
+-----------+------------+-----------------+
|     24    |      2     |        1        |
+-----------+------------+-----------------+
|     76    |      2     |       1.1       |
+-----------+------------+-----------------+
|     76    |      1     |       1.3       |
+-----------+------------+-----------------+

第三个项目

+-----------+--------------+
| ProjectID | Project_Name |
+-----------+--------------+
|     53    |     Test     |
+-----------+--------------+
|     23    | Test Project |
+-----------+--------------+
|     24    |   Feedback   |
+-----------+--------------+
|     76    |     Class    |
+-----------+--------------+

我使用这三个查询来获取前两个表中的最新版本,我想将它们全部编译到第三个但不确定如何加入它们。

-- GET Latest Feedback FROM CSLL.Feedback
SELECT fb.ID, fb.Version, fb.Status
FROM CSLL.Feedback fb
INNER JOIN (
    SELECT ID, MAX(Version) Version
    FROM CSLL.Feedback
    GROUP BY ID
) MaxVer ON fb.ID = MaxVer.ID AND fb.Version = MaxVer.Version;

-- GET Latest Project FROM CSLL.ProjectID
SELECT Proj.ProjectID, Proj.FeedbackID, Proj.FeedbackVersion
FROM CSLL.ProjectID Proj
INNER JOIN (
    SELECT FeedbackID, MAX(FeedbackVersion) Version
    FROM CSLL.ProjectID
    GROUP BY FeedbackID
) MaxVer ON Proj.FeedbackID = MaxVer.FeedbackID AND Proj.FeedbackVersion = MaxVer.Version;

SELECT 
    Project_ID
    ,Project_Name
    ,(SELECT COUNT(FeedbackID) FROM CSLL.ProjectID WHERE CSLL.ProjectID.ProjectID = CSLL.Projects.Project_ID) AS cnt
FROM CSLL.Projects;

我试图通过将打开/关闭记录的数量连接在一起来到达小数值的位置。这些也可以在单独的列中完成

+-----------+--------------+---------------+-----------------+
| ProjectID | Project_Name | Count of Open | Count of Closed |
+-----------+--------------+---------------+-----------------+
|     53    |     Test     |       0       |        0        |
+-----------+--------------+---------------+-----------------+
|     23    | Test Project |       1       |        0        |
+-----------+--------------+---------------+-----------------+
|     24    |   Feedback   |       0       |        0        |
+-----------+--------------+---------------+-----------------+
|     76    |     Class    |       1       |        1        |
+-----------+--------------+---------------+-----------------+

更新代码

我使用了三张桌子。现在我只需要分组并计算每个项目中的状态数

-- GET Latest Project FROM CSLL.ProjectID
SELECT Proj.ProjectID
    ,(SELECT Project_Name FROM CSLL.Projects WHERE CSLL.Projects.Project_ID = proj.ProjectID) AS ProjectName
    ,(SELECT Status FROM CSLL.Feedback WHERE CSLL.Feedback.ID = Proj.FeedbackID AND CSLL.Feedback.Version = Proj.FeedbackVersion) AS Status
    ,(SELECT COUNT(Status) FROM CSLL.Feedback WHERE CSLL.Feedback.ID = Proj.FeedbackID AND CSLL.Feedback.Version = Proj.FeedbackVersion) AS cnt
FROM CSLL.ProjectID Proj
INNER JOIN (
    SELECT FeedbackID, MAX(FeedbackVersion) Version
    FROM CSLL.ProjectID
    GROUP BY FeedbackID
) MaxVer ON Proj.FeedbackID = MaxVer.FeedbackID AND Proj.FeedbackVersion = MaxVer.Version;

我只是努力将它们组合在一起以获得总计数

更新 这更有意义吗?我已经改变了表格的结构,因为它更合乎逻辑

+-----------+--------------+--------+-------+
| ProjectID | Project_Name | Status | Count |
+-----------+--------------+--------+-------+
|     23    | Test Project |  Open  |   1   |
+-----------+--------------+--------+-------+
|     76    |     Class    |  Open  |   1   |
+-----------+--------------+--------+-------+
|     76    |     Class    | Closed |   1   |
+-----------+--------------+--------+-------+

SELECT Proj.ProjectID
    ,(SELECT Project_Name FROM CSLL.Projects WHERE CSLL.Projects.Project_ID = proj.ProjectID) AS ProjectName
    ,(SELECT Status FROM CSLL.Feedback WHERE CSLL.Feedback.ID = Proj.FeedbackID AND CSLL.Feedback.Version = Proj.FeedbackVersion) AS Status
    ,COUNT(Proj.ProjectID) AS cnt
FROM CSLL.ProjectID Proj
INNER JOIN (
    SELECT FeedbackID, MAX(FeedbackVersion) Version
    FROM CSLL.ProjectID
    GROUP BY FeedbackID
) MaxVer ON Proj.FeedbackID = MaxVer.FeedbackID AND Proj.FeedbackVersion = MaxVer.Version
GROUP BY Proj.ProjectID

表格结构的粗略绘图。我试图总结一下图片

前两名都是项目。

反馈项目被输入项目

我想获得在每种状态下分配给每个项目的反馈项数量的计数。我也只对具有最高版本的反馈感兴趣。

Crude Table Structure

1 个答案:

答案 0 :(得分:0)

目前还有很多含糊不清的内容,但基于你的描述...

首先让我们转换反馈表

此数据集仅提供最新版本以及与打开和关闭相对应的两列:

    SELECT 
    FeedbackID, FeedbackVersion,
    CASE WHEN Status='Open' THEN 1 ELSE 0 END As OpenCount
    CASE WHEN Status='Closed' THEN 1 ELSE 0 END As ClosedCount
    FROM Feedback F
    WHERE FeedbackVersion = (
             SELECT MAX(FeedbackVersion)
             FROM Feedback FM
             WHERE FM.FeedbackID = F.FeedbackID 
             )

现在我们将其加入项目:

SELECT P.ProjectID, SUM(OpenCount), SUM(ClosedCount)
FROM ProjectID P
INNER JOIN
(
    SELECT 
    FeedbackID, FeedbackVersion,
    CASE WHEN Status='Open' THEN 1 ELSE 0 END As OpenCount
    CASE WHEN Status='Closed' THEN 1 ELSE 0 END As ClosedCount
    FROM Feedback F
    WHERE FeedbackVersion = (
             SELECT MAX(FeedbackVersion)
             FROM Feedback FM
             WHERE FM.FeedbackID = F.FeedbackID 
             )
) MaxVersion
ON  P.FeedbackID=MaxVersion.FeedbackID
AND P.FeedbackVersion=MaxVersion.FeedbackVersion
GROUP BY P.ProjectID

目前尚不清楚您希望使用旧版本的ProjectID记录做什么。即如果项目ID 7 存在于ProjectID旧版本中,则它不会出现在此查询中。