简体   繁体   中英

Find min value in column by grouping more than one column in sql

+----+-------+-----------------------------+-------------------------+-------------------+-------------+
| Id | grade | shot_name                   | submitted_by_supervisor | version_submitted | submit_type |
+----+-------+-----------------------------+-------------------------+-------------------+-------------+
| 27 | A     | elx_reel01_scn1020_shot1720 | Salil Devji             | 33                | Fresh       |
| 27 | A     | elx_reel01_scn1020_shot1720 | Deepali                 | 34                | Fresh       |
| 37 | A     | elx_reel01_scn1030_shot3480 | Salil Devji             | 15                | Fresh       |
| 37 | A     | elx_reel01_scn1030_shot3480 | Salil Devji             | 20                | Fresh       |
| 7  | B     | elx_reel01_scn1010_shot1030 | Darshan                 | 4                 | Fresh       |
| 7  | B     | elx_reel01_scn1010_shot1030 | Varion                  | 6                 | Fresh       |
| 17 | B     | elx_reel01_scn1010_shot1140 | Varion                  | 17                | Fresh       |
| 17 | B     | elx_reel01_scn1010_shot1140 | Varion                  | 14                | Fresh       |
+----+-------+-----------------------------+-------------------------+-------------------+-------------+

I have column (submit_type) inserting new column in that value are set by CASE condition here is my sql query:

SELECT   s.shot_id,
         s.shot_name ,
         isn.reviewer AS 'submitted_by_supervisor',
         isn.version  AS 'version_submitted',
         isn.grade    AS 'grade',
         CASE
                  WHEN isn.version = Min(isn.version) THEN 'FRESH'
                  ELSE 'Once Submitted'
         end AS 'submit_type'
FROM     viewd_elx.india_supe_note isn
JOIN     viewd_elx.shot s
ON       s.shot_id = isn.shot_id
JOIN     viewd_elx.team t
ON       isn.shot_id = t.shot_id
JOIN     viewd_elx.viewd_team vt
ON       isn.shot_id = vt.shot_id
WHERE    isn.promoted='Yes'
AND      isn.grade IN ('A',
                       'B')
GROUP BY isn.grade,
         s.shot_id,
         isn.version; 

mentioned query gives 'Fresh' value in all field in (submit_type) column. which is not correct.

What I need exactly is, I am grouping column and finding MIN(value) of column,

Work Flow: - grade A => group (shot_name) => min(version_submited) => set 'Fresh' else 'Once submitted' in (submit_type)

In Grade 'A' have same shot_name, grouping this (shot_name) and then finding min(version_submited) if min value found then set 'fresh' else set 'once submitted' value in (submit_type) column. Also for Grade B like Grade A.

I need result like this =>

+----+-------+-----------------------------+-------------------------+-------------------+----------------+
| Id | grade | shot_name                   | submitted_by_supervisor | version_submitted | submit_type    |
+----+-------+-----------------------------+-------------------------+-------------------+----------------+
| 27 | A     | elx_reel01_scn1020_shot1720 | Salil Devji             | 33                | Fresh          |
| 27 | A     | elx_reel01_scn1020_shot1720 | Deepali                 | 34                | Once Submitted |
| 37 | A     | elx_reel01_scn1030_shot3480 | Salil Devji             | 15                | Fresh          |
| 37 | A     | elx_reel01_scn1030_shot3480 | Salil Devji             | 20                | Once Submitted |
| 7  | B     | elx_reel01_scn1010_shot1030 | Darshan                 | 4                 | Fresh          |
| 7  | B     | elx_reel01_scn1010_shot1030 | Varion                  | 6                 | Once Submitted |
| 17 | B     | elx_reel01_scn1010_shot1140 | Varion                  | 17                | Once Submitted |
| 17 | B     | elx_reel01_scn1010_shot1140 | Varion                  | 14                | Fresh          |
+----+-------+-----------------------------+-------------------------+-------------------+----------------+

The issue here is that you are grouping by on isn.version field as well, and trying to compute the minimum of the same field in that group. It will simply return the same value, and that is why all are coming as "Fresh". You will need to determine the minimum isn.version value for a group of (grade, shot_id) separately in a subquery ( Derived Table ), and then use that to check whether minimum or not.

SELECT   s.shot_id,
         s.shot_name ,
         isn.reviewer AS submitted_by_supervisor,
         isn.version  AS version_submitted,
         isn.grade    AS grade,
         CASE
             WHEN isn.version = dt.min_version THEN 'FRESH'
             ELSE 'Once Submitted'
         END AS 'submit_type'
FROM     viewd_elx.india_supe_note isn
JOIN     viewd_elx.shot s
ON       s.shot_id = isn.shot_id
JOIN     viewd_elx.team t
ON       isn.shot_id = t.shot_id
JOIN     viewd_elx.viewd_team vt
ON       isn.shot_id = vt.shot_id
JOIN     (
           SELECT grade, 
                  shot_id, 
                  MIN(version) AS min_version 
           FROM viewd_elx.india_supe_note 
           WHERE promoted = 'YES' 
             AND grade IN ('A', 'B') 
           GROUP BY grade, 
                    shot_id
        ) AS dt 
ON       dt.grade = isn.grade 
         AND dt.shot_id = isn.shot_id 
WHERE    isn.promoted='Yes'
         AND isn.grade IN ('A','B')
GROUP BY s.shot_id, 
         s.shot_name, 
         isn.reviewer, 
         isn.version, 
         isn.grade, 
         dt.min_version 

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