Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Select with Case union / Conditional selection

$
0
0

Hello Could you please help me with this issue,

in this selection when there is partciluar segment found in the selection then need to add 2 records per segment otherwise just selected results

--Drop table #list
CREATE TABLE #LIST ( email varchar(20),Segment varchar(20), Addr1 varchar(20), City varchar(20),ST varchar(20), Code int)
--Drop table #subject
CREATE TABLE #Subject (Segment varchar(20), Fname varchar(20), LName varchar(20))


INSERT INTO #LIST VALUES ('mmm@m.com','VEC_BAL','20 MORC ST','KIMS','TX',1234)
INSERT INTO #LIST VALUES ('vin@tta.com','VEC_BAL','345 KIM RD','ROBOR','TX',2345)
INSERT INTO #LIST VALUES ('imjh@ui.com','VEC_BAL','44 SILVER PL','SINGOOR','TX',323)
INSERT INTO #LIST VALUES ('lrk@m.com','PPC_BAL','DRIVE ST','BOSTON','TX',123)
INSERT INTO #LIST VALUES ('tom@tre.com','PPC_BAL','STREET RD','NYC','NY',1234)
INSERT INTO #LIST VALUES ('jim@rty.com','LEDU_BAL','PARK PLACE','NORWALK','CT',324)
INSERT INTO #LIST VALUES ('kim@a.com','VUNDADU','WATH CT','OSCAR','OH',455)
INSERT INTO #LIST VALUES ('tina@k.com','LIGHT','SNADAR RD','NYC','NY',655)
INSERT INTO #LIST VALUES ('sona@al.com','VIRTUAL_BAL','MILL PK','HARTFORD','CT',456)


INSERT INTO #Subject VALUES ('VEC_BAL','Mark','Toolsi')
INSERT INTO #Subject VALUES ('PPC_BAL','Sony','Nimura')
INSERT INTO #Subject VALUES ('LEDU_BAL','Jonathan','Peox')
INSERT INTO #Subject VALUES ('VUNDADU','Dan','Arnold')
INSERT INTO #Subject VALUES ('LIGHT','Mike','Steven')
INSERT INTO #Subject VALUES ('VIRTUAL_BAL','Phiolip','Dora')
INSERT INTO #Subject VALUES ('Monarch_BAL','RAS','KILEN')

/*
select Segment , Addr1 , City ,ST  from #LIST 
select Segment , Fname , LName  from #Subject 
*/

---Selection results
SELECT L.Segment , l.email, l.Addr1 , l.City ,l.ST, l.code, s.Fname , s.LName into #expectedresult FROM #LIST L JOIN #Subject S ON l.Segment =s.Segment

--Now here what i am trying to get,  union the below two record to the above "selection Results" for only segment ='VEC_BAL' or segment = 'PPC_BAL'

--2 records 
SELECT l.segment , l.email , Addr1,  City,  ST, 999 as Code,'' fname, ''lname from #LIST l where email ='mmm@m.com'
SELECT l.segment , l.email , Addr1,  City,  ST, 888 as Code, '' fname, ''lname from #LIST l where email ='lrk@m.com'

--- expected results please execute below block ( per each segment 2 records will insert if there is segment ='VEC_BAL' or 'PPC_BAL'  ). If there is no Vec_Bal or PPC BAL then no additional inserts, if there is only VEC_BAL no PPC_BAL then only 2 records insert
SELECT L.Segment , l.email, l.Addr1 , l.City ,l.ST, l.code, s.Fname , s.LName into #expctresults1 FROM #LIST L JOIN #Subject S ON l.Segment =s.Segment
union
SELECT l.segment , l.email , Addr1,  City,  ST, 999 as Code,'VEC_BAL' fname, ''lname from #LIST l where email ='mmm@m.com'
union
SELECT l.segment , l.email , Addr1,  City,  ST, 888 as Code, 'VEC_BAL' fname, ''lname from #LIST l where email ='lrk@m.com'
union
SELECT l.segment , l.email , Addr1,  City,  ST, 999 as Code,'PPC_BAL' fname, ''lname from #LIST l where email ='mmm@m.com'
union
SELECT l.segment , l.email , Addr1,  City,  ST, 888 as Code, 'PPC_BAL' fname, ''lname from #LIST l where email ='lrk@m.com'


select * from #expctresults1
-----

 

Please help me

Thank you in advance
Asita


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>