sql server - SQL Select - Calculated Column if Value Exists in another Table -


trying work through sql query limited knowledge , experience. tried quite few things i've found through searches, haven't come desired result.

i have 4 tables:

orders [id][date]  order_details [id][item_no][qty]  items [item_no][desc]  kits [kit_no][item_no] 

re: kits - [kit_no] , [item_no] both fk items table. concatenation of them pk.

i want select orders, orders.date, order_details.item_no, items.desc

no problem. few simple inner joins , i'm on way.

the difficulty lies in adding column select statement, is_kit, true if:

exists(select null kits kits.item_no = order_details.item_no).

(if kits table contains item, flag row)

is there way calculate column?

there different ways this.

the simplest left join case calculated column:

select   o.date,   od.item_no,   i.desc,   case when k.item_no null 0 else 1 end is_kit      orders        o join      order_details od on od.id=o.id join      items          on i.item_no = od.item_no left join kits          k  on k.item_no = od.item_no 

but use subselect:

select   o.date,   od.item_no,   i.desc,   (select count(*) kits k k.item_no = od.item_no) is_kit orders        o join order_details od on od.id=o.id join items          on i.item_no = od.item_no 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -