Unix Technical Forum

Possibly simple query but I'm not good enough to fathom it!

This is a discussion on Possibly simple query but I'm not good enough to fathom it! within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi All I know that I should supply the DDL for the tables I'm going to talk about, but ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 06:38 PM
Laphan
 
Posts: n/a
Default Possibly simple query but I'm not good enough to fathom it!

Hi All

I know that I should supply the DDL for the tables I'm going to talk about,
but I'm not 100% on how to generate them just yet. Hopefully my question is
more a query methodology question than how the tables are constructed.

My first attempt at the query is as follows:

SELECT st.STOCKID, sd.FULLDESCRIPTION, sc.NAME, Sum(sq.QUANTITYINSTOCK) AS
'Qty In Stock Total', Sum(st.QUANTITY) AS 'Qty Sold'
FROM STOCK s, STOCKCATEGORIES sc, STOCKDESCRIPTIONS sd, STOCKQUANTITIES sq,
STOCKTRANSACTIONS st
WHERE sc.STOCKCATEGORYID = s.STOCKCATEGORYID AND st.STOCKID = s.STOCKID AND
sd.STOCKID = s.STOCKID
AND sq.STOCKID = s.STOCKID AND (sd.LANGUAGEID='UK') AND
(st.TRANSACTIONTYPE=8) AND
(sq.QUANTITYINSTOCK > 0)
GROUP BY st.STOCKID, sd.FULLDESCRIPTION, sc.NAME

This works in a fashion, but I need to sort of query the stocktransactions
table again to get the sum of the st.QUANTITY table for st.TRANSACTIONTYPE=1
so that I can have an extra select field of Sum(st.QUANTITY) AS 'Qty
Ordered', ie transactions with transactiontype of 1 are sales orders and
type 8 are invoices.

I basically need to get a report result of:

PRODUCT, DESCRIPTION, CATEGORY, CURRENT_STOCK_QTY, SALES_IN_PERIOD,
ORDERS_IN_PERIOD

Is there any pointers whatsoever you can give me to try and get this
double-double query to work?

Many thanks.

Rgds Laphan



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 06:39 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Possibly simple query but I'm not good enough to fathom it!

Laphan (info@SpamMeNot.co.uk) writes:
> This works in a fashion, but I need to sort of query the stocktransactions
> table again to get the sum of the st.QUANTITY table for
> st.TRANSACTIONTYPE=1
> so that I can have an extra select field of Sum(st.QUANTITY) AS 'Qty
> Ordered', ie transactions with transactiontype of 1 are sales orders and
> type 8 are invoices.


It sounds like you could just add one column to your SELECT list:

SUM(CASE st.TRANSACTIONTYPE WHEN 1 THEN st.QUANTITY ELSE 0 END)
AS "Qty Ordered"

By the way, use of single quotes to delimit column aliases is deprected
in SQL 2005.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 06:39 PM
Astra
 
Posts: n/a
Default Re: Possibly simple query but I'm not good enough to fathom it!

Hi Erland

The following script is sooooooooo close to being right, but it is only
bringing back the right Sums when the stock items have a sold value and an
ordered value not all, just sold or just ordered:

SELECT st.STOCKID,
sd.FULLDESCRIPTION,
sc.NAME,
sq.QUANTITYINSTOCK AS 'Qty In Stock Total',
Sum(CASE WHEN st.TRANSACTIONTYPE=8 THEN st.QUANTITY ELSE 0 END)
AS 'Qty Sold',
Sum(CASE WHEN st.TRANSACTIONTYPE=1 THEN st.QUANTITY ELSE 0 END)
AS 'Qty Ordered'
FROM STOCK s
INNER JOIN STOCKCATEGORIES sc ON sc.STOCKCATEGORYID = s.STOCKCATEGORYID
INNER JOIN STOCKDESCRIPTIONS sd ON sd.STOCKID = s.STOCKID AND
sd.LANGUAGEID='UK'
INNER JOIN STOCKQUANTITIES sq ON sq.STOCKID = s.STOCKID AND
sq.WAREHOUSEID='BC' AND
sq.QUANTITYINSTOCK > 0
INNER JOIN STOCKTRANSACTIONS st ON st.STOCKID = s.STOCKID AND
st.TRANSACTIONTYPE IN(1,8)
GROUP BY st.STOCKID, sd.FULLDESCRIPTION, sc.NAME, sq.QUANTITYINSTOCK

It's got to be down to the way the joins work so is there anyway round this?

Many thanks

Regards

Robbie


"Erland Sommarskog" <esquel@sommarskog.se> wrote in message
news:Xns978112C11FF4Yazorman@127.0.0.1...
Laphan (info@SpamMeNot.co.uk) writes:
> This works in a fashion, but I need to sort of query the stocktransactions
> table again to get the sum of the st.QUANTITY table for
> st.TRANSACTIONTYPE=1
> so that I can have an extra select field of Sum(st.QUANTITY) AS 'Qty
> Ordered', ie transactions with transactiontype of 1 are sales orders and
> type 8 are invoices.


It sounds like you could just add one column to your SELECT list:

SUM(CASE st.TRANSACTIONTYPE WHEN 1 THEN st.QUANTITY ELSE 0 END)
AS "Qty Ordered"

By the way, use of single quotes to delimit column aliases is deprected
in SQL 2005.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 06:39 PM
Astra
 
Posts: n/a
Default Re: Possibly simple query but I'm not good enough to fathom it!

Apologies it works perfectly

It was me.

Many, many thanks.

Rgds Robbie

"Erland Sommarskog" <esquel@sommarskog.se> wrote in message
news:Xns978112C11FF4Yazorman@127.0.0.1...
Laphan (info@SpamMeNot.co.uk) writes:
> This works in a fashion, but I need to sort of query the stocktransactions
> table again to get the sum of the st.QUANTITY table for
> st.TRANSACTIONTYPE=1
> so that I can have an extra select field of Sum(st.QUANTITY) AS 'Qty
> Ordered', ie transactions with transactiontype of 1 are sales orders and
> type 8 are invoices.


It sounds like you could just add one column to your SELECT list:

SUM(CASE st.TRANSACTIONTYPE WHEN 1 THEN st.QUANTITY ELSE 0 END)
AS "Qty Ordered"

By the way, use of single quotes to delimit column aliases is deprected
in SQL 2005.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:20 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com