4

Trying to work through a SQL query with some very limited knowledge and experience. Tried quite a few things I've found through searches, but haven't come up with my desired result.

I have four tables:

ORDERS
[ID][DATE]

ORDER_DETAILS
[ID][ITEM_NO][QTY]

ITEMS
[ITEM_NO][DESC]

KITS
[KIT_NO][ITEM_NO]

Re: KITS - [KIT_NO] and [ITEM_NO] are both FK to the ITEMS table. The concatenation of them is the PK.

I want to select ORDERS, ORDERS.DATE, ORDER_DETAILS.ITEM_NO, ITEMS.DESC

No problem. A few simple inner joins and I'm on my way.

The difficulty lies in adding a column to the select statement, IS_KIT, that is true if:

EXISTS(SELECT null FROM KITS WHERE KITS.ITEM_NO = ORDER_DETAILS.ITEM_NO).

(if the kits table contains the item, flag this row)

Is there any way to calculate that column?

1 Answer 1

9

There are different ways to do this.

The simplest is probably a LEFT JOIN with a CASE calculated column:

SELECT
  o.date,
  od.item_no,
  i.desc,
  CASE WHEN k.item_no IS NULL THEN 0 ELSE 1 END AS is_kit
FROM      orders        o
JOIN      order_details od ON od.id=o.id
JOIN      items         i  ON i.item_no = od.item_no
LEFT JOIN kits          k  ON k.item_no = od.item_no

But you could also use a SUBSELECT:

SELECT
  o.date,
  od.item_no,
  i.desc,
  (SELECT COUNT(*) FROM kits k WHERE k.item_no = od.item_no) AS is_kit
FROM orders        o
JOIN order_details od ON od.id=o.id
JOIN items         i  ON i.item_no = od.item_no
7
  • I still don't have a great understanding of Left Joins. If the Kits table has more than one row with the same ItemNo, will I get bad results (duplicated records)?
    – Michael
    Jun 3, 2014 at 22:08
  • With the LEFT JOIN, yes... but not with the SUBSELECT. But if that is the case, then maybe you should document the table structure better in your question. As it stands is seems your kits table only has one column, which would be a reasonable unique or primary key.
    – Frazz
    Jun 3, 2014 at 22:38
  • Updated table schema to more accurately reflect the case. I also successfully implemented your SUBSELECT solution.
    – Michael
    Jun 4, 2014 at 12:25
  • Ok, but I still have some doubts. You say that both kits.kit_no and kits.item_no reference the item table. I suppose that a kit is composed of many items. Can an item be part of many kits? And most importantly... your is_kit variable... should it mean that the item is a kit (which I suppose translates to k.kit_no=od.item_no) or should it mean that the item is part of a kit (which I suppose translates to k.item_no=od.item_no)?
    – Frazz
    Jun 4, 2014 at 13:15
  • A kit is an item. An item may only be a part of a kit if it is not itself a kit. An item may be part of many kits. These constraints are not imposed directly on the database (I don't own it). IS_KIT means the item is a kit (it has components). If IS_KIT > 0 (using the subselect), that should indicate the item is a kit. I think it is reliable. Concerns?
    – Michael
    Jun 4, 2014 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.