| Author |
Topic |
|
tocroi72
Yak Posting Veteran
89 Posts |
Posted - 2007-08-03 : 10:39:10
|
| Hello all,I have 3 tables below: create table Book(bookid int,title varchar(50))insert into BOOK select 1,'mssql' unition all select 2 ,'c++' union all select 3,'Visual Basic' union all select 4,'Java'----------------create table BookDiscount(discountid int,bookid int)insert into BookDiscount select 1,1 union all select 2,3------------------create table BookDiscountSequence(discountid int,bookid int,Sequenceid int,Sequencedesc varchar(50))insert into BookDiscountSequence select 1,1,1,'Christmas' union all select 1,1,2,'newyear'-------------I ran a query below:select a.*,b.*,Occasion1 = case when c.sequenceid = 1 then Sequencedesc end,Occasion2 = case when c.sequenceid = 2 then SequenceDesc endfrom book a, BookDiscount b ,BookDiscountSequence cwhere a.bookid = b.bookidand a.bookid = c.bookidand b.bookid = c.bookid-----------------the result is below:1 mssql 1 1 Christmas NULL1 mssql 1 1 NULL newyearhow do i get the result just as below:1 mssql 1 1 Christmas newyear--------------any helps will be greatly appreciated.Thanks |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-03 : 11:01:22
|
| Here's one way:create table #Book(bookid int,title varchar(50))insert into #BOOK select 1,'mssql' union all select 2 ,'c++' union all select 3,'Visual Basic' union all select 4,'Java'----------------create table #BookDiscount(discountid int,bookid int)insert into #BookDiscount select 1,1 union all select 2,3------------------create table #BookDiscountSequence(discountid int,bookid int,Sequenceid int,Sequencedesc varchar(50))insert into #BookDiscountSequence select 1,1,1,'Christmas' union all select 1,1,2,'newyear'------------- select a.bookid,a.title,b.discountid,b.bookid,Occasion1 = MAX(case when c.sequenceid = 1 then c.Sequencedesc else ''end), Occasion2 = MAX(casewhen c.sequenceid = 2 then c.SequenceDesc else ''end)from #book a, #BookDiscount b ,#BookDiscountSequence cwhere a.bookid = b.bookidand a.bookid = c.bookidand b.bookid = c.bookidGROUP BY a.bookid,a.title,b.discountid,b.bookidJim |
 |
|
|
|
|
|