简体   繁体   中英

SQL join query set values in multiple columns

select "mag" as project, n.created_date, s.shot_name, 
   (CASE WHEN (n.grade = 'C'  AND n.promoted = 'No') THEN  1 ELSE 0  END) AS 'Depth KB ',  
   (CASE WHEN (n.grade = 'B'  AND n.promoted = 'No') THEN  1 ELSE 0  END) AS 'Finaling KB',   
   b.team, a.team, n.reviewer, n.version, n.grade as supe_grade, n.promoted as supe_promoted,
   ,i.created_date, i.reviewer,i.version,  
   (CASE WHEN (i.grade = 'C'  AND i.promoted = 'No') THEN  1 ELSE 0  END) AS 'PFW B KB', 
   (CASE WHEN (i.grade = 'B'  AND i.promoted = 'No') THEN  1 ELSE 0  END) AS 'PFW A KB', 
   i.grade as internal_grade, i.promoted as internal_promoted             
FROM viewd_mag.india_supe_note n
JOIN viewd_mag.artist_report_viewd_b b on n.shot_id = b.shot_id
JOIN viewd_mag.artist_report_viewd_a a on b.shot_id = a.shot_id
JOIN viewd_mag.internal_note i on a.shot_id = i.shot_id 
JOIN viewd_mag.shot s on i.shot_id = s.shot_id 
group by n.shot_id, n.version, n.grade,n.promoted;

Need help to solve below joins query. i have mentioned details below setting values in multiple column.
Details:- Here m applying conditions:-

(CASE WHEN (n.grade = C  AND n.promoted = No) THEN  1 ELSE 0  END) AS "Depth KB",  
(CASE WHEN (n.grade = B  AND n.promoted = No) THEN  1 ELSE 0  END) AS "Finaling KB" ,   
(CASE WHEN (i.grade = C  AND i.promoted = No) THEN  1 ELSE 0  END) AS "PFW B KB", 
(CASE WHEN (i.grade = B  AND i.promoted = No) THEN  1 ELSE 0  END) AS "PFW A KB"

setting values in 4 column: - Depth KB , Finaling KB , PFW B KB , PFW A KB
Mentioned query not working, kindly help to get below result.

Result like this :- 


'+---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | project | created_date     | shot_name | Depth KB | Finaling KB | team  | team | reviewer     | version | supe_grade | supe_promoted | created_date     | reviewer | version | PFW B KB | PFW A KB | internal_grade | internal_promoted |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | demo    | 10/04/2019 14:28 | Demo1     | 0        | 0           | Vimal | Amit | Vimal        | 3       | WIP        |               | 09/18/2019 18:10 | Prod     | 5       | 1        | 0        | C              | No                |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | demo    | 10/04/2019 22:39 | Demo1     | 1        | 0           | Vimal | Amit | Vimal        | 4       | C          | No            | 09/18/2019 18:10 | Prod     | 1       | 0        | 0        | WIP            | Yes               |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | demo    | 10/04/2019 23:07 | Demo1     | 0        | 0           | Vimal | Amit | Vimal        | 6       | B          | Yes           | 09/18/2019 18:10 | Prod     | 6       | 0        | 0        | A              | Yes               |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | demo    | 10/10/2019 22:52 | Demo1     | 0        | 0           | Vimal | Amit | Vimal        | 9       | B          | Yes           | 09/18/2019 18:10 | Prod     | 0       | 1        | 0        | C              | No                |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | demo    | 10/18/2019 02:32 | Demo1     | 0        | 0           | Vimal | Amit | Chandrakanth | 107     | A          | Yes           | 09/18/2019 18:10 | Prod     | 8       | 0        | 1        | B              | No                |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | demo    | 10/17/2019 02:50 | Demo1     | 0        | 1           | Vimal | Amit | Chandrakanth | 105     | B          | No            | 09/18/2019 18:10 | Prod     | 2       | 0        | 0        | B              | Yes               |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+
        | demo    | 10/18/2019 02:32 | Demo1     | 0        | 1           | Vimal | Amit | Chandrakanth | 106     | B          | No            | 09/18/2019 18:10 | Prod     | 1       | 0        | 1        | B              | No                |
        +---------+------------------+-----------+----------+-------------+-------+------+--------------+---------+------------+---------------+------------------+----------+---------+----------+----------+----------------+-------------------+'

Based on your code, I suspect the error is being produced by the double ","

See the line: i.grade as internal_grade,,

Next time, show the error that you get which will speed up with the debugging process.

Better formatting can assist with quickly identifying the error.

I hope that helps your error.

One possibility beyond the double comma -- you mixed up your double and single quotes.

First line should use single quotes:

select 'mag' as project

And then each of your case columns should be AS "your column name" . Though more commonly you'll see AS [your column name] . But really most people avoid the spaces entirely:

(CASE WHEN (n.grade = 'C' AND n.promoted = 'No') THEN 1 ELSE 0 END) AS Depth_KB,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM