CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    [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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  2. #2
    Join Date
    Dec 2004
    Posts
    423

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Sep 2006
    Posts
    635

    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;

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured