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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with SQL statement

Author  Topic 

texassynergy
Starting Member

26 Posts

Posted - 2011-05-06 : 13:49:08
I have the following SQL statement:
SELECT DISTINCT OD.PO_ID, OD.LINE_NUMBER, OD.PART_ID, OD.PO_DESC, OD.REV_DEL_DATE, OD.REV_ORDER_QTY, 
RT.QUANTITY, OD.PO_LINE_STATUS, VM.VENDOR_NAME
FROM ((ESIDB.dbo.POFOD OD
RIGHT OUTER JOIN ESIDB.dbo.POFRT RT ON (OD.PO_ID=RT.PO_ID) AND (OD.LINE_NUMBER=RT.LINE_NUMBER))
INNER JOIN ESIDB.dbo.POFOM OM ON RT.PO_ID=OM.PO_ID)
INNER JOIN ESIDB.dbo.POFVM VM ON OM.VENDOR_ID=VM.VENDOR_ID
WHERE OD.REV_DEL_DATE < GETDATE() AND OD.PO_LINE_STATUS = 'O'


The problem is that I also need to get All open POs whose REV_DEL_DATE < GETDATE() when there is no POFRT record. Right now, I only get those records when there is a POFRT record. I would like to get it when there is not one. The only thing I can think of is to do a union with a similar statement. However, I am hoping to not do that as it duplicates records. I also need to put this into a Crystal Report and an Alert. I don't know if both products will use the UNION without a lot of complication.

In case you are wondering, the POFOM is only used to link to a record in the POFVM table, that is why I don't select any fields from that table. There is no direct link between POFVM and the other two tables.

I am not a great SQL programmer and know that some of you will have a slick way of doing this.

Any help would be greatly appreciated.


I am getting closer. Here is what I have now.
SELECT DISTINCT OD.PO_ID, OD.LINE_NUMBER, OD.PART_ID, OD.PO_DESC, OD.REV_DEL_DATE, OD.REV_ORDER_QTY, 
RT.QUANTITY, OD.PO_LINE_STATUS, VM.VENDOR_NAME
FROM (ESIDB.dbo.POFOD OD
RIGHT OUTER JOIN ESIDB.dbo.POFRT RT ON (OD.PO_ID=RT.PO_ID) AND (OD.LINE_NUMBER=RT.LINE_NUMBER)
or (OD.PO_ID <> RT.PO_ID))
INNER JOIN ESIDB.dbo.POFOM OM ON OD.PO_ID=OM.PO_ID
INNER JOIN ESIDB.dbo.POFVM VM ON OM.VENDOR_ID=VM.VENDOR_ID
WHERE OD.REV_DEL_DATE < GETDATE() AND OD.PO_LINE_STATUS = 'O'

But I get too many duplicate - erroneous records. I get 3050 records when I should only have about 12.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-05-06 : 14:08:56
I think you just may need to change the right outer join to a left outer join. The left join says give me records in tableA even if there is no match in tableB. Right join does the opposite.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

texassynergy
Starting Member

26 Posts

Posted - 2011-05-09 : 10:40:56
Jimf, thanks for the response. Left or Right, I would get the same results. I finally gave in and created two reports and two Alerts based on two slightly different queries. I tried many things but could not get it to work. Thanks anyways.
Go to Top of Page
   

- Advertisement -