SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Quick Query Question
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jcawood
Starting Member

USA
2 Posts

Posted - 04/11/2013 :  11:51:01  Show Profile  Reply with Quote
I am trying to modify my query so that it checks if an inventory item is dead at all locations (1-7 and 50). I want the query to check each location for a given item and return the result only if the item meets that criteria at all locations and has a QTY > 0.

My current query...

SELECT inventory.SUP,
inventory.PART,
location.LOC,
inventory.DESCR1 AS Description,
location.LSALDT AS [Last Sale],
location.LRECDT AS [Last Rec],
location.LTRNDT AS [Last Trans],
location.LADJDT AS [Last Adj],
location.QTYONH AS QTY,
price.COST,
location.QTYONH * price.COST AS Total

FROM DATABASE.dbo.INVMAS AS inventory LEFT OUTER JOIN
DATABASE.dbo.PRICE AS price ON inventory.SUP = price.SUP AND inventory.PART = price.PART LEFT OUTER JOIN
DATABASE.dbo.INVIDX AS location ON inventory.SUP = location.SUP AND inventory.PART = location.PART

WHERE (location.LSALDT < 20120401) AND
(location.LRECDT < 20120401) AND
(location.LTRNDT < 20120401) AND
(location.QTYONH > 0)

The image below is a table I pulled by removing my "QTY>0" from the Where division.
http://imgur.com/FmKMhlg

Thanks in advance!

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 04/11/2013 :  12:00:39  Show Profile  Reply with Quote
What is the logic behind "an inventory item is dead"? how do you decide deadness ..

Cheers
MIK
Go to Top of Page

jcawood
Starting Member

USA
2 Posts

Posted - 04/11/2013 :  12:40:55  Show Profile  Reply with Quote
If it hasn't been received (LRECDT), transfered (LTRNDT), and sold (LSALDT) in over a year.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/12/2013 :  05:24:20  Show Profile  Reply with Quote

SELECT *
FROM
(
SELECT inventory.SUP, 
inventory.PART, 
location.LOC, 
inventory.DESCR1 AS Description,
location.LSALDT AS [Last Sale], 
location.LRECDT AS [Last Rec],
location.LTRNDT AS [Last Trans],
location.LADJDT AS [Last Adj],
location.QTYONH AS QTY, 
price.COST,
location.QTYONH * price.COST AS Total,
SUM(CASE WHEN QTY > 0 AND (location.LSALDT > DATEADD(yy,DATEDIFF(yy,0,GETDATE())-1,0) OR location.LRECDT > DATEADD(yy,DATEDIFF(yy,0,GETDATE())-1,0) OR location.LTRNDT > DATEADD(yy,DATEDIFF(yy,0,GETDATE())-1,0)) THEN 1 ELSE 0 END) OVER (PARTITION BY inventory.PART) AS Occ
FROM DATABASE.dbo.INVMAS AS inventory LEFT OUTER JOIN
DATABASE.dbo.PRICE AS price ON inventory.SUP = price.SUP AND inventory.PART = price.PART LEFT OUTER JOIN
DATABASE.dbo.INVIDX AS location ON inventory.SUP = location.SUP AND inventory.PART = location.PART
)t
WHERE Occ=0
AND QTYONH > 0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000