|
-
January 24th, 2007, 04:21 PM
#1
[RESOLVED] VB6 (Access) SQL Question
I have 2 SQL Queries that I'd like to combine.
First, I get BatchDate and Closed Totals.
Code:
SELECT DISTINCTROW Transactions.BatchDate, Count(Transactions.BatchDate) AS [OpenTotal]
FROM Transactions
WHERE (((Transactions.DeletedFlag)=0))
GROUP BY Transactions.BatchDate;
and this gets BatchDate and Open Totals
Code:
SELECT DISTINCTROW Transactions.BatchDate, Count(Transactions.BatchDate) AS [ClosedTotal]
FROM Transactions
WHERE (((Transactions.DeletedFlag)=1))
GROUP BY Transactions.BatchDate;
How could I combine this to get:
BatchDate, OpenTotal, ClosedTotal
instead of two different RecordSets?
Thanks.
-
January 24th, 2007, 06:04 PM
#2
Re: VB6 (Access) SQL Question
Group by 'DeletedFlag' and use HAVING DeletedFlag = 0 or deletedflag = 1.
Code:
SELECT DISTINCTROW Transactions.BatchDate, '0' as [OpenTotal] Count(Transactions.BatchDate) AS [ClosedTotal]
FROM Transactions
GROUP BY Transactions.BatchDate, Transactions.DeletedFlag
HAVING Transactions.DeletedFlag= 1
OR Transactions.DeletedFlag = 0;
Or you can do a UNION Query if you only want one record
Code:
SELECT DISTINCTROW Transactions.BatchDate, '0' as [OpenTotal] Count(Transactions.BatchDate) AS [ClosedTotal]
FROM Transactions
WHERE (((Transactions.DeletedFlag)=1))
GROUP BY Transactions.BatchDate
UNION ALL
SELECT DISTINCTROW Transactions.BatchDate, Count(Transactions.BatchDate) as [OpenTotal], '0' AS [ClosedTotal]
FROM Transactions
WHERE (((Transactions.DeletedFlag)=1))
GROUP BY Transactions.BatchDate
Last edited by Sabin_33; January 24th, 2007 at 06:08 PM.
Even if everybody spoke the same language, nobody would be speaking the same language.
--Daniel
-
January 24th, 2007, 08:25 PM
#3
Re: VB6 (Access) SQL Question
Thanks for the help. Things are getting better, but there seems to be a problem.
The second OPEN column is all '0's, and the closed counts Open or Closed, apparently.
I'd like the second column to count the '0's (Open) and the third column to count the same column, except tallying the '1's.
Batch__:Open_:Closed
_123___:__34_:___4_
_124___:_536_:__24_
I haven't tried the UNION yet, as I want one record for each Batch number. There is also a TransNo that pertain to each Batch, and they are combined into a text key.
-
January 24th, 2007, 08:27 PM
#4
Re: VB6 (Access) SQL Question
try this, I hope it's useful
Code:
SELECT Transactions.BatchDate, sum(iif(Transactions.DeletedFlag=0,1,0)) AS ClosedTotal,
sum(iif(Transactions.DeletedFlag=1,1,0)) AS OpenTotal
FROM Transactions
WHERE Transactions.DeletedFlag in (0,1)GROUP BY Transactions.BatchDate;
-
January 24th, 2007, 08:50 PM
#5
Re: VB6 (Access) SQL Question
Thanks. It's working the way I want, this way.
Code:
SELECT DISTINCTROW Transactions.BatchDate, Sum(IIf([Transactions].[DeletedFlag]=1,0,1)) AS OpenTotal, Sum(IIf([Transactions].[DeletedFlag]=0,1,0)) AS ClosedTotal
FROM Transactions
WHERE (((Transactions.DeletedFlag) In (0,1)))
GROUP BY Transactions.BatchDate;
EDIT: Arrgh! Keep in mind that the IIF() statement seems to
work only if DeletedFlag is a String!!!
Last edited by dglienna; January 25th, 2007 at 09:14 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|