Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 better solution

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-03-28 : 03:55:14
hi,

i want to get following output:
id_order | type | number
-------------------------
1234 | A | 1
1235 | A | 0
1235 | B | 0
1236 | B | 1
1237 | C | 0
1237 | D | 0


create table tbl_order
(
id_order int
,type nvarchar(40)
)

insert into tbl_order (id_order, type) values (1234, 'A' )
insert into tbl_order (id_order, type) values (1235, 'A' )
insert into tbl_order (id_order, type) values (1235, 'B' )
insert into tbl_order (id_order, type) values (1236, 'B' )
insert into tbl_order (id_order, type) values (1237, 'C' )
insert into tbl_order (id_order, type) values (1237, 'D' )
insert into tbl_order (id_order, type) values (1238, 'A' )
insert into tbl_order (id_order, type) values (1239, 'D' )
insert into tbl_order (id_order, type) values (1239, 'B' )
insert into tbl_order (id_order, type) values (1239, 'A' )


select
id_order
,type
--,isnull(orderX,'') as number
,case when orderX > 1 then 1 else 0 end as number2
from tbl_order
left join (select t2.id_order as orderX
from tbl_order as t2
where
(select count(t1.id_order)
from tbl_order as t1
where
t1.id_order = t2.id_order) = 1
)as x
on tbl_order.id_order = x.orderX


Is there any better/faster select sentance to do this? i'm using sql2000.

thank you

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-28 : 04:06:03
[code]SELECT y.id_order,
y.type,
CASE WHEN t.id_order IS NULL THEN 0
ELSE 1
END AS number
FROM YourTable y
LEFT OUTER JOIN (SELECT id_order
FROM YourTable
GROUP BY id_order
HAVING COUNT(type)=1)t
ON y.id_order = t.id_order[/code]
Go to Top of Page
   

- Advertisement -