Pergunta

hoping someone here can be of some help.

I'm running a query that returns something like this.

https://i.stack.imgur.com/tyQxg.png

This is my current query:

SELECT i.prtnum, i.lodnum, i.lotnum, i.untqty, i.ftpcod, i.invsts
FROM inventory_view i, locmst m
WHERE i.stoloc = m.stoloc
AND m.arecod = 'PART-HSY'
ORDER BY i.prtnum

If you're looking at the picture, I need the query to exclude rows like the 3rd one. (00005-86666-000)

Foi útil?

Solução

You didn't give much reasoning on why to exclude that row but you can exclude by prtnum like you've requested:

SELECT i.prtnum, i.lodnum, i.lotnum, i.untqty, i.ftpcod, i.invsts
FROM inventory_view i, locmst m
WHERE i.stoloc = m.stoloc
  AND m.arecod = 'PART-HSY'
  AND i.prtnum NOT IN(SELECT i2.prtnum  FROM inventory_view i2, locmst m2 
                                        WHERE i2.stoloc = m2.stoloc AND m2.arecod = 'PART-HSY'
                                        GROUP BY i2.prtnum HAVING COUNT(*) = 1)
ORDER BY i.prtnum

Outras dicas

SELECT i.prtnum, i.lodnum, i.lotnum, i.untqty, i.ftpcod, i.invsts
FROM inventory_view i, locmst m
WHERE i.stoloc = m.stoloc
AND EXISTS (SELECT * FROM Inventory_view k 
                     WHERE k.prtnum == i.prtnum AND k.id <> i.id)
AND m.arecod = 'PART-HSY'
ORDER BY i.prtnum

where i.id/k.id is your table id. It's a bit inefficient, but it should work.

There're a couple of ways to do what you're asking. If I understand correctly, it's just excluding rows which have a value in a given column that shows up only once in your table. If so, you can do something like this:

SELECT i.prtnum, i.lodnum, i.lotnum, i.untqty, i.ftpcod, i.invsts
FROM inventory_view i
JOIN locmst m ON i.stoloc = m.stoloc AND m.arecod = 'PART-HSY'
JOIN inventory_view i1 ON i1.prtnum = i.prtnum AND i1.lodnum != i.lodnum
ORDER BY i.prtnum

This method will ensure that the rows returned have a prtnum which matches prtnum of other records in the same table. I'm assuming lodnum is License No from your image.

How about something like:

SELECT      i.prtnum,
            i.lodnum,
            i.lotnum,
            i.untqty,
            i.ftpcod,
            i.invsts
FROM        inventory_view i,
            locmst.m
WHERE       i.stoloc = m.stoloc
AND         m.arecod = 'PART-HSY'
AND         i.prtnum IN
(
    SELECT      prtnum
    FROM        inventory_view j
    HAVING      count(prtnum)>1
    GROUP BY    prtnum
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top