SQL Statement Join With CASE

前端 未结 3 1097
失恋的感觉
失恋的感觉 2021-01-23 12:42

I have problem with this 3 table below from MS ACCESS to do \"SQL CASE IF ELSE\" where I don\'t how to start.

Table A (Registrations)

Name    | Desc              


        
3条回答
  •  失恋的感觉
    2021-01-23 13:15

    It's not too clear from the question how TableA and TableB are related togheter, but it looks like you want a UNION ALL query and you want to exclude some categories:

    SELECT
      c.Name,
      c.Year,
      c.Class,
      ab.Desc,
      ab.Amount
    FROM
      TableC AS C INNER JOIN (
        SELECT * FROM TableA WHERE Desc NOT IN ('BOOKS', 'UNIFORM', 'OTHERS')
        UNION ALL
        SELECT * FROM TableB WHERE Desc NOT IN ('BOOKS', 'UNIFORM', 'OTHERS')
      ) AS ab
      ON c.Name = ab.Name AND c.Year = ab.Year
    

    then you can use a Pivot using the previous query as a subquery:

    TRANSFORM Sum(Amount) AS SumOfAmount
    SELECT Name, Year, Class, Sum(Amount) AS [Sum_Amount]
    FROM (
    
      ...the query above...
    
    ) AS s
    GROUP BY Name, Year, Class
    PIVOT Desc;
    

提交回复
热议问题