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 |
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-06-17 : 06:20:32
|
Well probably not that complex for some of you out there!I need to work out the amount of stock which was sold between @datefrom and @dateto and how much we currently have on hand (to work out if we are over ordering etc). That's the pretty easy part but I also need to include a column which works out how many items have been sold 3 months prior to @datefrom (from invoiceline). The proc I have so far works out the items sold between 2 dates so basically what I need is another column which is the amount sold (QtySold) in the 3 months prior to datefromThis is the basic part I have so far:ALTER PROCEDURE [dbo].[rptstockholdinglevel] -- Add the parameters for the stored procedure here@datefrom datetime,@dateto datetime,@periodname varchar(50),@percentage intASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure hereSELECT products.productname, @periodname AS periodname, isnull(SUM(invoiceline.qty),0) AS QtySold, products.qtyonhand AS OnHand, nominals.nominalname, productcategories.categoryname, productmanufacturers.manufacturernameFROM productmanufacturers RIGHT OUTER JOINinvoices INNER JOINinvoiceline ON invoices.invoiceid = invoiceline.invoiceid RIGHT OUTER JOINproducts ON invoiceline.productid = products.productid ON productmanufacturers.manufacturerid = products.manufacturerid LEFT OUTER JOINnominals INNER JOINproductcategories ON nominals.nominalid = productcategories.salesnominal ON products.categoryid = productcategories.productcategoryidWHERE (invoices.invoicedate BETWEEN @datefrom AND @dateto) OR (invoices.invoicedate is null)GROUP BY products.productname, products.qtyonhand, productcategories.categoryname, nominals.nominalname, productmanufacturers.manufacturernameHAVING (SUM(isnull(invoiceline.qty,0)) < products.qtyonhand)ORDER BY nominals.nominalname, productcategories.categoryname, productmanufacturers.manufacturernameEND I'd be really grateful for any pointers as I'm just going round in circles on this one.Thanks in advance as usual all :)Stephen. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-17 : 06:35:53
|
quote: Originally posted by steve_c Well probably not that complex for some of you out there!I need to work out the amount of stock which was sold between @datefrom and @dateto and how much we currently have on hand (to work out if we are over ordering etc). That's the pretty easy part but I also need to include a column which works out how many items have been sold 3 months prior to @datefrom (from invoiceline). The proc I have so far works out the items sold between 2 dates so basically what I need is another column which is the amount sold (QtySold) in the 3 months prior to datefromThis is the basic part I have so far:ALTER PROCEDURE [dbo].[rptstockholdinglevel] -- Add the parameters for the stored procedure here@datefrom datetime,@dateto datetime,@periodname varchar(50),@percentage intASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure hereSELECT products.productname, @periodname AS periodname, isnull(SUM(invoiceline.qty),0) AS QtySold, products.qtyonhand AS OnHand, nominals.nominalname, productcategories.categoryname, productmanufacturers.manufacturernameFROM productmanufacturers RIGHT OUTER JOINinvoices INNER JOINinvoiceline ON invoices.invoiceid = invoiceline.invoiceid RIGHT OUTER JOINproducts ON invoiceline.productid = products.productid ON productmanufacturers.manufacturerid = products.manufacturerid LEFT OUTER JOINnominals INNER JOINproductcategories ON nominals.nominalid = productcategories.salesnominal ON products.categoryid = productcategories.productcategoryidWHERE (invoices.invoicedate BETWEEN @datefrom AND @dateto) OR (invoices.invoicedate is null)GROUP BY products.productname, products.qtyonhand, productcategories.categoryname, nominals.nominalname, productmanufacturers.manufacturernameHAVING (SUM(isnull(invoiceline.qty,0)) < products.qtyonhand)ORDER BY nominals.nominalname, productcategories.categoryname, productmanufacturers.manufacturernameEND I'd be really grateful for any pointers as I'm just going round in circles on this one.Thanks in advance as usual all :)Stephen.
Not sure how it worked for you. there's a syntax error in code posted. you have two ON statements following a join |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-06-17 : 06:38:39
|
| Hi it's working no probs for me :/ |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-06-17 : 09:18:12
|
| Would the way to do this be something to do with inserting into temp tables? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-17 : 09:51:57
|
| [code]ALTER PROCEDURE [dbo].[rptstockholdinglevel] -- Add the parameters for the stored procedure here@datefrom datetime,@dateto datetime,@periodname varchar(50),@percentage intASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure hereSELECT products.productname, @periodname AS periodname, isnull(SUM(invoiceline.qty),0) AS QtySold, products.qtyonhand AS OnHand, nominals.nominalname, productcategories.categoryname, productmanufacturers.manufacturername,q.PrevQtyFROM productmanufacturers RIGHT OUTER JOINinvoices INNER JOINinvoiceline ON invoices.invoiceid = invoiceline.invoiceid RIGHT OUTER JOINproducts ON invoiceline.productid = products.productid ON productmanufacturers.manufacturerid = products.manufacturerid LEFT OUTER JOINnominals INNER JOINproductcategories ON nominals.nominalid = productcategories.salesnominal ON products.categoryid = productcategories.productcategoryidOUTER APPLY (SELECT SUM(qty) AS PrevQty FROM invoiceline WHERE productid=products.productid AND DATEDIFF(mm,invoicedate,@datefrom)>=0 AND DATEDIFF(mm,invoicedate,@datefrom)<=3) qWHERE (invoices.invoicedate BETWEEN @datefrom AND @dateto) OR (invoices.invoicedate is null)GROUP BY products.productname, products.qtyonhand, productcategories.categoryname, nominals.nominalname, productmanufacturers.manufacturernameHAVING (SUM(isnull(invoiceline.qty,0)) < products.qtyonhand)ORDER BY nominals.nominalname, productcategories.categoryname, productmanufacturers.manufacturernameEND[/code] |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-06-17 : 10:04:37
|
| Hi thanks for the quick reply: I get:Msg 156, Level 15, State 1, Procedure rptstockholdinglevel, Line 35Incorrect syntax near the keyword 'WHERE'.I removed the "q" before WHERE and still a syntax error ::( |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-06-17 : 10:05:43
|
| Ignore that message - I think you forgot to put "AS" before q - thanks!"! |
 |
|
|
|
|
|
|
|