Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 16th, 2009, 11:32 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default Select statement question?????

Hi to all...I have 6 select statements that start the sameSELECT Store,SUM(ABS(Qty)*Flag) Qty,SUM((abs(Price)*Flag)) Returns$ FROM Transactionswhere Modifier4 and Modifier7 andDate between '03/01/09' and '03/16/09' andup to this point just that in each other where cases i have different modifier or flags to select...is there a way i can make one select statement instead of 6...thanking you in advance...Rino
 
Old March 16th, 2009, 11:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It depends on the modifier. If you are changing both the left hand side and the right hand side of the expression, or the operator then unless you use dynamic SQL it is probably not possible.
Can you show a couple of real queries illustrating the differences?
__________________
Joe
http://joe.fawcett.name/
 
Old March 16th, 2009, 11:54 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default

-- Returns by Store**SELECT StoreID, SUM(ABS(Quantity)*PosNegFlag) qty,SUM((abs(ExtendedSellingPrice)*PosNegFlag)) Returns$ FROM TransactionDetailswhere TransactionModifier4 and TransactionModifier7 andTransactionDetailDate between '03/01/09' and '03/16/09' and(TransactionModifier=1 OR TransactionModifier=3 )GROUP BY StoreIDorder by 1-- Post Voids by Store**SELECT StoreID, SUM(ABS(Quantity)*PosNegFlag) qty,SUM((abs(ExtendedSellingPrice)*PosNegFlag)) PostVoid$ FROM TransactionDetailswhere TransactionModifier4 and TransactionModifier7 andTransactionDetailDate between '03/01/09' and '03/16/09' andTransactionVoidFlag=1GROUP BY StoreIDorder by 1-- Exchanges by Store**SELECT StoreID, SUM(ABS(Quantity)*PosNegFlag) qty,SUM((abs(ExtendedSellingPrice)*PosNegFlag)) Exchanges$ FROM TransactionDetailswhere TransactionModifier4 and TransactionModifier7 andTransactionDetailDate between '03/01/09' and '03/16/09' andTransactionModifier=5GROUP BY StoreIDorder by 1--the other 2 would be one with TransactionModifier=2 and the other with TransactionModifier=6
 
Old March 16th, 2009, 03:38 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default Try to make your posts READABLE...

...like this:
Code:
-- Returns by Store

SELECT StoreID, SUM(ABS(Quantity)*PosNegFlag) AS qty, SUM((abs(ExtendedSellingPrice)*PosNegFlag)) AS Returns$
FROM TransactionDetails 
where TransactionModifier4 
and TransactionModifier7 
and TransactionDetailDate between '03/01/09' and '03/16/09' 
and (TransactionModifier=1 OR TransactionModifier=3 )
GROUP BY StoreID
order by 1

-- Post Voids by Store

SELECT StoreID, SUM(ABS(Quantity)*PosNegFlag) qty,SUM((abs(ExtendedSellingPrice)*PosNegFlag)) PostVoid$ 
FROM TransactionDetails
where TransactionModifier4 
and TransactionModifier7 
and TransactionDetailDate between '03/01/09' and '03/16/09' 
and TransactionVoidFlag=1
GROUP BY StoreID
order by 1

-- Exchanges by Store

SELECT StoreID, SUM(ABS(Quantity)*PosNegFlag) qty,SUM((abs(ExtendedSellingPrice)*PosNegFlag)) Exchanges$ 
FROM TransactionDetails
where TransactionModifier4 
and TransactionModifier7 
and TransactionDetailDate between '03/01/09' and '03/16/09' 
and TransactionModifier=5
GROUP BY StoreID
order by 1

--the other 2 would be one with TransactionModifier=2 and the other with TransactionModifier=6
So are you wanting to use a Stored Procedure to do all this?

I don't think it would be hard.

But what database is this????

You APPEAR to be using BOOLEAN fields:
Code:
where TransactionModifier4 
and TransactionModifier7
And SQL Server doesn't support boolean fields. Are those actually BIT fields???
 
Old March 16th, 2009, 03:45 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I thnk this does it:
Code:
CREATE PROCEDURE storeReports
    @startDate DATETIME,
    @endDate DATETIME,
    @firstModifier INT DEFAULT -1,
    @secondModifier INT DEFAULT -1,
    @voidFlag BIT DEFAULT = 0
AS
SELECT StoreID, 
       SUM(ABS(Quantity)*PosNegFlag) AS qty,
       SUM((abs(ExtendedSellingPrice)*PosNegFlag)) AS dollarAmount
FROM TransactionDetails
WHERE TransactionModifier4 = 1
AND TransactionModifier7 = 1
AND TransactionDetailDate between @startDate and @endDate
AND (    TransactionModifier IN ( @firstModifier, @secondModifier ) 
      OR ( @voidFlag = 1 AND TransactionVoidFlag = 1 ) 
    )
GROUP BY StoreID
ORDER BY StoreID
Again, that assumes the TransactionModifier4 and TransactionModifier7 are BIT fields in the DB.

Couldn't somebody come up with more meaningful names than "TransactionModifier7" et al.??
 
Old March 16th, 2009, 03:46 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default

it's my own DB... it's not a boolean field... I'm using the less then & greater then signs... the not equal too...I don't know why when i copy pasted it in it didn't work...let me try againwhere TransactionModifier4 and TransactionModifier7
 
Old March 16th, 2009, 03:53 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default

don't know why when i put in "the not equal signs" it doesn't work...
 
Old March 16th, 2009, 05:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

So edit your posts after the copy/paste.

What do you mean "it's my own DB"??? You mean it's not SQL Server?

If it's a non-standard DB, I don't know that anybody but the creator of the product will know all the answers.
 
Old March 16th, 2009, 05:35 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default

you asked me before "But what database is this????"it's my own DB "database" i created... with tables and etc. to my needs...of course it's SQL server...or else i wouldn't be writing in this section...
 
Old March 16th, 2009, 05:37 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

But if it is SQL Server, are you saying that what you really want is:
Code:
CREATE PROCEDURE storeReports
    @startDate DATETIME,
    @endDate DATETIME,
    @firstModifier INT DEFAULT -1,
    @secondModifier INT DEFAULT -1,
    @voidFlag BIT DEFAULT = 0
AS
SELECT StoreID, 
       SUM(ABS(Quantity)*PosNegFlag) AS qty,
       SUM((abs(ExtendedSellingPrice)*PosNegFlag)) AS dollarAmount
FROM TransactionDetails
WHERE TransactionModifier <> 4 
AND TransactionModifier <> 7
AND TransactionDetailDate between @startDate and @endDate
AND (    TransactionModifier IN ( @firstModifier, @secondModifier ) 
      OR ( @voidFlag = 1 AND TransactionVoidFlag = 1 ) 
    )
GROUP BY StoreID
ORDER BY StoreID
The <> 4 and <> 7 is a bit redundant when you DO specify the first and second modifier values, but they won't hurt and you'd need them for the void case.

Hmmm... Maybe could simplify a little bit:
Code:
CREATE PROCEDURE storeReports
    @startDate DATETIME,
    @endDate DATETIME,
    @firstModifier INT DEFAULT -1,
    @secondModifier INT DEFAULT -1,
    @voidFlag BIT DEFAULT = 0
AS
SELECT StoreID, 
       SUM(ABS(Quantity)*PosNegFlag) AS qty,
       SUM((abs(ExtendedSellingPrice)*PosNegFlag)) AS dollarAmount
FROM TransactionDetails
WHERE TransactionDetailDate between @startDate and @endDate
AND (    TransactionModifier IN ( @firstModifier, @secondModifier ) 
      OR ( @voidFlag = 1 AND TransactionVoidFlag = 1 AND TransactionModifier NOT IN (4,7) ) 
    )
GROUP BY StoreID
ORDER BY StoreID
If it's not obvious, you would then call the SP passing the two dates and then optionally passing the various other values.

For example:
Code:
-- returns by store
EXEC storeReports '3/1/2009','3/16/2009',1,3
-- voids by store
EXEC storeReports '3/1/2009','3/16/2009', -1, -1, 1
-- exchanges by store
EXEC storeReports '3/1/2009','3/16/2009',5





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select In Statement mr_pc1963 SQL Server 2005 2 April 19th, 2008 09:59 AM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Question on select statement RinoDM SQL Server 2000 3 January 19th, 2007 10:21 PM
Select Into Statement I think, Basic question gmoney060 SQL Server 2000 4 December 31st, 2004 02:52 AM
Select statement Sarju Mehta SQL Server 2000 1 March 30th, 2004 03:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.