Query Statement | Need Help | URGENT ||

Scenario1:-

SELECT DISTINCT MATERIAL_CAT11_CD AS A
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT11_CD <> '-' and MATERIAL_CAT11_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD (Coming From Table XX)

Scenario2:-

SELECT DISTINCT MATERIAL_CAT13_CD AS B
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT13_CD <> '-' and MATERIAL_CAT13_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)

Scenario3:-

SELECT DISTINCT MATERIAL_CAT12_CD AS C
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT12_CD <> '-' and MATERIAL_CAT12_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)


--------------------------------------------------------------------------------------------------------------------------------------------------------------

Please help experts I need to combine all three of the above scenarios in one combination.

<<Help URGENT>>
Jasmine SandlasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
for which database engine? your topics are showing both MySql and MS SQL. They are 2 different beast!

Can't you just "union all" your 3 queries?

BTW, if it is urgent, you should have set the priority to high.
0
Jasmine SandlasAuthor Commented:
This is for Microsoft SQL server
0
Éric MoreauSenior .Net ConsultantCommented:
Can't you just "union all" your 3 queries?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jasmine SandlasAuthor Commented:
coming from the same table (TABLE1) - can't it be combined and have 1 combination. UNION ALL will not give perfect results.
0
Éric MoreauSenior .Net ConsultantCommented:
you are not querying for the same fields in your 3 queries.

have you tried (you don't even need the DISTINCT clause as the UNION takes care of it!):
SELECT MATERIAL_CAT11_CD AS A
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT11_CD <> '-' and MATERIAL_CAT11_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD (Coming From Table XX)

union 

SELECT MATERIAL_CAT13_CD AS B
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT13_CD <> '-' and MATERIAL_CAT13_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)

union 

SELECT MATERIAL_CAT12_CD AS C
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT12_CD <> '-' and MATERIAL_CAT12_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)

Open in new window

0
Jasmine SandlasAuthor Commented:
This doesn't work, Need to populate all the three fields (A,B,C) with the conditions that are given in Another Table TEST
0
Éric MoreauSenior .Net ConsultantCommented:
do you need to return the values in 3 rows or 3 columns?

for rows, add a dummy field to each sub-query:
SELECT 11 as Category, MATERIAL_CAT11_CD AS A
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT11_CD <> '-' and MATERIAL_CAT11_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD (Coming From Table XX)
union 
SELECT 13,MATERIAL_CAT13_CD AS B
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT13_CD <> '-' and MATERIAL_CAT13_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)
union 
SELECT 12, MATERIAL_CAT12_CD AS C
FROM TABLE1 M
where curr_rec_ind = 'Y' and (MATERIAL_CAT12_CD <> '-' and MATERIAL_CAT12_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)

Open in new window


if you need columns:
select 
(SELECT MATERIAL_CAT11_CD AS A FROM TABLE1 M where curr_rec_ind = 'Y' and (MATERIAL_CAT11_CD <> '-' and MATERIAL_CAT11_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD (Coming From Table XX)) AS Cat11
, (SELECT MATERIAL_CAT13_CD AS B FROM TABLE1 M where curr_rec_ind = 'Y' and (MATERIAL_CAT13_CD <> '-' and MATERIAL_CAT13_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)) AS Cat13
, (SELECT MATERIAL_CAT12_CD AS C FROM TABLE1 M where curr_rec_ind = 'Y' and (MATERIAL_CAT12_CD <> '-' and MATERIAL_CAT12_CD <> '') and M.SERVICE_CD = STAGING.SERVICE_CD(Coming From Table XX)) AS Cat12

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jasmine SandlasAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.