Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query help

Status
Not open for further replies.

jcisco2

Programmer
Apr 13, 2004
102
US
I have a table that has the values of:
PalletID, and TransactionType (plus some other data)
but what i want to do is have a query that pulls PalletID that only have a Transaction Type of C and 4. (all tags have this data) here's an example of what i'm looking for.

PalletID transType
1 C
1 3
1 4
1 3
1 4
2 C
2 4
3 C
3 3
3 4

so in my query I want to look at all tags that have only the C then the 4 following it. exclude any pallets that have a 3. so the only tag that should return in the result set would be the palletID of 2. suggestions?
 
Code:
SELECT a.PalletID,
       a.transType AS "Type C",
       b.transType AS "Type 3",
       c.transType AS "Type 4"
FROM MyTable a
LEFT JOIN MyTable b ON b.PalletID = a.PalletID
                   AND b.transType = '3'
LEFT JOIN MyTable c ON c.PalletID = a.PalletID
                   AND c.transType = '4'
WHERE a.transType = 'C'
This should yield a result with all of the needed facts in one row. Then add a couple more conditions to obtain just the C4s.
Code:
SELECT a.PalletID,
       a.transType AS "Type C",
       b.transType AS "Type 3",
       c.transType AS "Type 4"
FROM MyTable a
LEFT JOIN MyTable b ON b.PalletID = a.PalletID
                   AND b.transType = '3'
LEFT JOIN MyTable c ON c.PalletID = a.PalletID
                   AND c.transType = '4'
WHERE a.transType = 'C'

  AND b.transType IS NULL
  AND c.transType = 4
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top