简体   繁体   中英

SQL Query CASE WHEN Statement

I am doing a SQL Query to try to figure out production by shift... When I run this query.. it returns things that are in a location of 5-FG as having a shift 3... How is this even possible?

SELECT * FROM (select pt_desc1, pt_desc2, tr_part, tr_loc, tr_time, case when custpart = '' then         tr_part else custpart end as custpart, sum(tr_qty_loc) as 'Total', 
CASE WHEN tr_loc = '6-LDT2' then 'Dakkota'
    WHEN tr_loc = '5-FG' then 'Dexsys' end as 'location',
CASE WHEN tr_time > '14400' AND tr_time < '50400' AND tr_loc = '6-LDT2'Then '1'
    WHEN tr_time > '50400' AND tr_time < '75600' AND tr_loc = '6-LDT2' Then '2' 
    WHEN tr_time > '75600' OR tr_time < '14400' AND tr_loc = '6-LDT2' Then '3'
    WHEN tr_time > '21600' AND tr_time < '59400' AND tr_loc = '5-FG' Then '1'
    WHEN tr_time > '59400' or tr_time < '21600' AND tr_loc = '5-FG' Then '2' end as 'Shift'
from tr_hist_sql
join pt_mstr_sql on tr_part = pt_part
join cp_mstr_sql on pt_part = intpart
where tr_loc IN ('5-FG') 
and tr_type = 'rct-wo' 
group by tr_part, pt_desc1, pt_desc2, tr_loc, tr_time, case when custpart = '' then tr_part else      custpart end, CASE WHEN tr_time > '14400' AND tr_time < '50400' AND tr_loc = '6-LDT2' Then '1'
WHEN tr_time > '50400' AND tr_time < '75600' AND tr_loc = '6-LDT2' Then '2' 
WHEN tr_time > '75600' OR tr_time < '14400' AND tr_loc = '6-LDT2' Then '3'
WHEN tr_time > '21600' AND tr_time < '59400' AND tr_loc = '5-FG' Then '1'
    WHEN tr_time > '59400' or tr_time < '21600' AND tr_loc = '5-FG' Then '2'  end,
CASE WHEN tr_loc = '6-LDT2' then 'Dakkota'
    WHEN tr_loc = '5-FG' then 'Dexsys' end
having sum(tr_qty_loc) > 0) as dt
where Shift = 1 or shift = 2 or shift =3

order by tr_part

在此处输入图片说明

My guess is that the line:

WHEN tr_time > '75600' or tr_time < '14400' and tr_loc = '6-LDT2' then '3'

is being interpreted as:

WHEN tr_time > '75600' or (tr_time < '14400' and tr_loc = '6-LDT2') then '3'

Then would come to the first clause:

WHEN tr_time > '75600'

Which satisfies the OR statement, giving a value of '3' .

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