This is question similar to: TSQL select rows by one from 2 conditions, but it is different in results that I would like to have
I have a table like so:
ORDER_ID CODE1 CODE2 CODE3 STATUS TYPE SUM GROUP
1 '001' 'BIGP' NULL 4 'company' 120 48
2 '002' 'BIGP' NULL 1 'priv' 100 20
3 '001' NULL NULL 6 'priv' 50 49
4 '002' NULL 'L' 1 'company' 1253 22
and second table like so:
ADDRESS_ID ORDER_ID ZIP TYPE ADD_DATE CATEGORY VERIFIED
1 1 '15-125' 'K1' '2010-01-01' 'CLIENT' 1
2 1 '22-022' 'D1' '2010-01-02' 'SYSTEM' 1
3 2 '16-159' 'D2' '2010-01-02' 'SYSTEM' 1
4 2 '15-125' 'D2' '2010-02-01' 'CLIENT' 0
Third and fourth table contains zip codes and city names like so:
ZIP CITY
'15-125' 'Warszawa'
'22-022' 'Koszalin'
'16-159' 'Krakow'
'15-125' 'Lublin'
For every order that has
- status not in (4,6)
- code1 between '002' and '005'
- (code2=null and code3=null) or (code2 in ('BIGA', 'BIGP') and code3=null) or (code2=NULL and code3 = 'L')
If code1 ='002' AND group IN (48,59,60,87) I must choose a single address
(big thanks to Nikola Markovinović):
SELECT TOP 1000 o.order_Id
, a.Address_Id
, a.Zip
--, *
FROM orders o
CROSS APPLY
(
select TOP 1
a.Address_Id,
a.Zip
from address a
WHERE a.order_Id = o.order_Id
ORDER BY case a.Type
when 'D2' then 1
when 'K1' then 2
else 3
end,
a.ADD_DATE
) a
WHERE
o.Status NOT IN (4, 6)
AND code1='002'
AND group IN (48,59,60,87)
AND ((code2 IS NULL AND code3 IS NULL) OR (code2 IN ('BIGA', 'BIGP') AND code3 IS NULL) OR (code2 IS NULL AND code3 = 'L'))
For all other orders that meet top criteria and got code1 ='002' AND group NOT IN (48,59,60,87) I must select all addresses for those orders that have verified=1
After collecting those addresses I will be able to check if a specific post company can deliver my mail to those addresses (I will check in another table containing zip codes)
I was thinking about making union all, taking first select and doing union with second that will return all addresses for code1 ='002' AND group NOT IN (48,59,60,87).
But maybe it is possible to do it without union all?
This it the final result I would like to get:
CODE1 TYPE COUNT_OF_ORDERS COUNT_OF_ADDRESSES COMPANY1 OTHER
'001' 'NORMAL' 125 150 110 40
'002' 'NORMAL' 100 122 100 22
'003' 'NORMAL' 150 110 100 10
'004' 'NORMAL' 200 220 220 0
'005' 'NORMAL' 220 240 210 30
'005' 'PRIORITY' 100 110 110 0
'SX1' 'PRIORITY' 100 100 20 80
So if my type is 'normal' I must check if that address for order exists in table having normal zip codes, if it has type 'priority' I must check in table with priority codes.
If code exists in specific table I add +1 to COMPANY1 column, if not to OTHER, so that sum of those columns must be sum of my addresses.
This is query that I've managed to do (with help of @Nikola Markovinović)
SELECT TOP 1000 o.order_Id
, a.Address_Id
, a.Zip
--, *
FROM orders o
CROSS APPLY
(
select TOP 1
a.Address_Id,
a.Zip
from address a
WHERE a.order_Id = o.order_Id
AND code1='002'
AND o.[group] IN (48,59,60,87)
ORDER BY case a.Type
when 'D2' then 1
when 'K1' then 2
else 3
end,
a.ADD_DATE
UNION ALL
select
a.Address_Id,
a.Zip
from address a
WHERE a.order_Id = o.order_Id
AND ((code1='002' AND o.[group] NOT IN (48,59,60,87)) OR code1 IN ('001', '003', '004', '005'))
--I'm not shure of that top line, it work's but mayby it con de written better
AND Verified = 1
) a
WHERE
o.Status NOT IN (4, 6)
AND ((code2 IS NULL AND code3 IS NULL)
OR (code2 IN ('BIGA', 'BIGP') AND code3 IS NULL)
OR (code2 IS NULL AND code3 = 'L'))