Subquery Returned More Than 1 Value with the Group By

后端 未结 2 535
面向向阳花
面向向阳花 2021-01-23 01:50

I have some problem creating query with this tables and codes, I know \"GROUP BY Branch.BranchName\" cause to get multiple records,but,How can avoid this and to do that in sing

2条回答
  •  醉梦人生
    2021-01-23 02:23

    The error is fairly self explanatory, within your subquery you are returning multiple results, which is not allowed. If you think about it logically if there is more than one row which row should be displayed?

    The reason you are having this problem is that even though you have grouped by branch.BracnName you have not linked back to your outer query, so your subqueries are returning results for all branches. You would need something like:

    SELECT
        (
            SELECT SUM (dd.DeptValue)
            FROM dbo.SudentPayments sp
                INNER JOIN dbo.Student s ON dbo.sp.StudentId = s.StudentId
                INNER JOIN dbo.DeptDesciption dd ON sp.DeptDesciptionId = dd.DeptDesciptionId
            WHERE s.IsDeptPayed = 0
            AND s.BranchID = Branch.BranchID    -- LINK TO OUTER BRANCH TABLE
        ) AS Payed
    FROM dbo.Branch
    

    However your entire query cold be rewritten as follows with no need for correlated subqueries, and if you can avoid correlated subqueries then it is usually a good idea. In some DBMS the optimiser can optimise some subqueries away and turn them into joins, but in it simplest terms with a correlated subquery you are asking the subquery to execute once for each row, this leads to much more overhead than using joins

    SELECT  Branch.BranchName, 
            Payed = SUM(CASE WHEN SudentPayments.IsDeptPayed = 1 THEN DeptDesciption.DeptValue ELSE 0 END),
            Notpayed = SUM(CASE WHEN SudentPayments.IsDeptPayed = 0 THEN DeptDesciption.DeptValue ELSE 0 END)
    FROM    dbo.SudentPayments
            INNER JOIN dbo.Student 
                ON dbo.SudentPayments.StudentId = dbo.Student.StudentId
            INNER JOIN dbo.DeptDesciption 
                ON SudentPayments.DeptDesciptionId = DeptDesciption.DeptDesciptionId
            INNER JOIN dbo.Branch 
                ON dbo.Branch.BranchId = Student.StudentId
    GROUP BY Branch.BranchName;
    

提交回复
热议问题