I have a scenario. Below are my 3 tables:
create table #cdes(client_id int,identifier int)
insert #cdes values(9908,789654123)
create table #temp1
(
client_id int,
identifier int,
pp_id int,
ch_id int,
code varchar(20),
program varchar(20),
startdate date,
enddate date,
ssc varchar(50)
)
insert into #temp1
values (9908,789654123,1567,1566,'OP','xASMT','1/1/2019','1/4/2019','A201901044F010134NNN01D 151 143 093 ')
-------------------------------------------
create table #temp2
(
client_id int,
identifier int,
pp_id int,
ch_id int,
code varchar(20),
program varchar(20),
startdate date,
enddate date,
ssc varchar(20)
)
insert into #temp2
values(9908,789654123,1574,1573,'OP','SU1','1/1/2019','1/4/2019',NULL)
I have a case condition in my final select query as my query revolves around multiple conditions.Most case conditions throw only one row as output however, I want to report multiple lines for a specific scenario.
My desired output:
My query:
select
d.client_id,
case when d.client_id = 9908
then
(
select CONCAT(t1.code,t1.startdate,t1.enddate,t1.ssc)
from #temp1 t1
left join #temp2 t2 on t1.client_id = t2.client_id and t1.identifier = t2.identifier
union all
select CONCAT(t2.code,t2.startdate,t2.enddate,t2.ssc)
from #temp1 t1
left join #temp2 t2 on t1.client_id = t2.client_id and t1.identifier = t2.identifier)
end
from #cdes d
left join #temp1 t1 on d.client_id = t1.client_id
left join #temp2 t2 on d.client_id = t2.client_id
The problem is Unionall is producing myltiple rows that I am finding difficult to accommodate in my select statement. Any help?!
This case condition is fake. I have included for just a sample.
Don't put a UNION ALL query in your subquery. make your entire query a UNION ALL query.
Instead of:
SELECT ColA, ColB, (
SELECT CalculationX FROM MyTable
UNION ALL
SELECT CalculationY FROM MyTable
) AS ColC
FROM MyTable
You need something like this:
SELECT ColA, ColB, CalculationX AS ColC FROM MyTable
UNION ALL
SELECT ColA, ColB, CalculationY AS ColC FROM MyTable
You already have the subquery so just add the condition after where
:
select t.* from (
select t1.client_id client_id, t1.identifier identifier, CONCAT(t1.code,t1.startdate,t1.enddate,t1.ssc) ssc_concatenated
from #temp1 t1
union all
select t2.client_id client_id, t2.identifier identifier, CONCAT(t2.code,t2.startdate,t2.enddate,t2.ssc) ssc_concatenated
from #temp2 t2) t
where t.client_id in (select client_id from #cdes)
or
where t.client_id = 9908
Or you can add the condition directly in each part of the subquery:
select t.* from (
select t1.client_id client_id, t1.identifier identifier, CONCAT(t1.code,t1.startdate,t1.enddate,t1.ssc) ssc_concatenated
from #temp1 t1
where t1.client_id = 9908
union all
select t2.client_id client_id, t2.identifier identifier, CONCAT(t2.code,t2.startdate,t2.enddate,t2.ssc) ssc_concatenated
from #temp2 t2
where t2.client_id = 9908
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.