根据子查询

时间:2016-03-15 09:30:56

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2

我有一个SQL查询,如下所示:

SELECT SUM(Z.MaxMarks) AS TotMaxMarks,
    SUM(Z.MinMarks) AS TotMinMarks,
    SUM(Z.SecuredMarks) AS TotSecuredMarks,
    Z.IsPass
FROM (
    SELECT Y.*
    FROM (
        SELECT X.MaxMarks,
            X.SecuredMarks,
            (X.MaxMarks * X.PassPercentage / 100) AS MinMarks,
            CASE WHEN ((X.MaxMarks * X.PassPercentage / 100) < X.SecuredMarks) THEN 'True' ELSE 'False' END AS IsPass,
            row_number() OVER (
                PARTITION BY ChapterID ORDER BY UpdatedDate DESC
                ) AS SequencNo
        FROM (
            SELECT C.ChapterID,
                T.MinimumMarks AS PassPercentage,
                (
                    SELECT Sum(Q.MaximumMarks)
                    FROM MstQuestion Q,
                        TransStudentAnswer SA
                    WHERE Q.QuestionID = SA.QuestionID AND SA.TestAllotmentID = TA.TestAllotmentID
                    ) AS MaxMarks,
                (
                    SELECT Sum(Score)
                    FROM TransStudentAnswer
                    WHERE TestAllotmentID = TA.TestAllotmentID
                    ) AS SecuredMarks,
                TA.UpdatedDate
            FROM TransTestAllotment TA,
                MstStudent S,
                MstBatchDetails B,
                MstTest T,
                MstChapter C
            WHERE TA.StudentID = 47 AND TA.BatchID = 10 AND T.TestID = TA.TestID AND S.StudentID = TA.StudentID AND B.BatchID = TA.BatchID AND T.ChapterID = C.ChapterID AND TA.IsAttempted = 'True' AND TA.IsEvaluated = 'True'
            ) X
        ) Y
    WHERE SequencNo = 1
    ) Z
GROUP BY Z.IsPass

返回结果如下:

+-------------+-------------+-----------------+--------+
| TotMaxMarks | TotMinMarks | TotSecuredMarks | IsPass |
+-------------+-------------+-----------------+--------+
|          20 |          10 |               3 | False  |
|          30 |          15 |              28 | True   |
+-------------+-------------+-----------------+--------+

是否可以修改此查询,使其返回一条SUM(Z.MaxMarks),SUM(Z.MinMarks),SUM(Z.SecuredMarks) and TempIsPass column的记录,如果Z的所有记录中的列true的值为Z.Ispass is true,则该值为var permissions = new List<Permission>(); ,否则应该是假的。

2 个答案:

答案 0 :(得分:0)

希望这有效。

使用SELECT INTO查询选择进入临时表'z'的表。然后这样做

title1=textGrob("Test title", gp=gpar(fontface="bold"))
grid.arrange(g1, g3, g4+theme(legend.position="none"),mylegend, top=title1,
             layout_matrix=matrix(c(1,1,2,3,4,4), ncol=2, byrow=TRUE),heights=c(1,1.5,0.3))

答案 1 :(得分:0)

在您的select子句中,将Z.IsPass替换为:

case when min(case when Z.IsPass = 'true' then 1 else 0 end) = 1 then 'true' else 'false' end