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.
| Author |
Topic |
|
brettgab
Starting Member
4 Posts |
Posted - 2004-05-24 : 21:52:37
|
| Hi all,Frustrated at my lack of Transact-SQL knowledge, wish my employer would send me on a course!!! For now I will beg someone who is cluey to help me...I also have a table called PRODUCTS, key field PRODUCTIDI have a table called STOCK. The important fields here are the ID and the quantity of that item in the warehouse.STOCKID QUANTITYONHAND1 1040 3100 5 Each Product is built using a quantity of 1 or more Stock Items. The definitions for a product's stock items are found in the PRODUCTPARENTCHILDREN table which has the structurePRODUCTID STOCKID QUANTITYTOBUILD10 1 510 40 110 100 1011 40 112 1 112 40 112 100 513 1 1514 40 2What I need is a VIEW which shows the QUANTITY available for each PRODUCT based on the quantity available of the STOCK ITEMS that make it up.Based on the above values, the VIEW would have results like so...PRODUCTID QUANTITYAVAILABLE10 211 312 113 0What I effectively need to achieve is to do DIV the QUANTITYONHAND by the QUANTITYTOBUILD (ie get the whole number only) for each stock item of the Product. The QUANTITYAVAILABLE is then the highest value of each of the product's (QUANTITYONHAND DIV QUANTITYTOBUILD) results.Would be very appreciative, because if I have to write a function to calculate this, it's going to slow everything down.Regards,Brett |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2004-05-24 : 22:19:51
|
| Brett,This is how it could be done in one statement:SELECT T.ProductID, Min(T.Units) FROM (SELECT PPC.ProductID, PPC.StockID, PPC.QuantityToBuild, S.QuantityOnHand, S.QuantityOnHand / PPC.QuantityToBuild as UnitsFROM dbo.PRODUCTPARENTCHILDREN PPC INNER JOIN dbo.STOCK S ON PPC.StockID = S.StockID) TGROUP BY ProductIDAlthough I'd prefer to create a view of the bracketed subquery as it looks a bit neater.BTW - You can't build any of product 10 as you have only 5 of item 100, when you need 10 per unit.Timbtw - Are you in Oz? |
 |
|
|
brettgab
Starting Member
4 Posts |
Posted - 2004-05-25 : 01:06:21
|
It worked, mate you are a legend. Great knowledge of SQL.Yes I am in Australia, Melbourne. If you are as well, we will have to meet up and I'll buy you a beer or few.Cheers,Brett. |
 |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2004-05-25 : 01:39:16
|
| You're not the Brett, once of Crazy John's are you??? |
 |
|
|
brettgab
Starting Member
4 Posts |
Posted - 2004-05-25 : 19:00:56
|
| OMG. Yep that would be me. How did you guess that ?????Are you the Tim the Pronto expert who was put on just after I was made redundant ? If so, are you still at Crazy Fuckheads ? |
 |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2004-05-25 : 19:07:08
|
| Nope - I was the one you replaced. |
 |
|
|
brettgab
Starting Member
4 Posts |
Posted - 2004-05-25 : 19:12:44
|
| Freaky Tim, freaky. There was a Tim who also replaced me, hence the confusion.So what's happening? Last time I saw you at South Melbourne. Are you working in the city ? If so I will buy you a beer like I promised. |
 |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2004-05-25 : 19:16:15
|
| I'm working at the ASX building. Send me a mail - check my profile for the address.Tim |
 |
|
|
|
|
|
|
|