Question : How to add grouped counts from different table to a single result

I'm trying to produce the following result

Date     PO Header   PO Line    GR Header  GR Line
--------- --------------- ------------ -------------- -------------

Where date is a group by clause against four separate tables each returning a count of records grouped by date.  The following produces dates and the PO Header count.

DECLARE @ScottTable TABLE (BatchDate varchar(20), POHeader int, POLine int, GRHeader int, GRLine int)
INSERT INTO @ScottTable (BatchDate, POHeader)
SELECT
      SUBSTRING(CONVERT(varchar(20), master.dbo.J2DMY(POH.SYUPMJ), 103),7,4) + '-' + -- Year
            SUBSTRING(CONVERT(varchar(20), master.dbo.J2DMY(POH.SYUPMJ), 103),4,2) + '-' + -- Month
            SUBSTRING(CONVERT(varchar(20), master.dbo.J2DMY(POH.SYUPMJ), 103),1,2), -- Date
      COUNT(*)
FROM PRODDTA.F47016 AS POH
WHERE POH.SYEDSP = 'Y'
GROUP BY
      SUBSTRING(CONVERT(varchar(20), master.dbo.J2DMY(POH.SYUPMJ), 103),7,4) + '-' + -- Year
            SUBSTRING(CONVERT(varchar(20), master.dbo.J2DMY(POH.SYUPMJ), 103),4,2) + '-' + -- Month
            SUBSTRING(CONVERT(varchar(20), master.dbo.J2DMY(POH.SYUPMJ), 103),1,2) -- Date

But how do I then add the grouped count for the PO Line and others.  And how do I ensure records are inserted if the date is not already in the result.


Cheers,
Scott.....


Answer : How to add grouped counts from different table to a single result

you can do this, if you are running the queries from a form..

using the key down event of the form

set the form's KeyPreview property to True

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyEscape Then
        KeyCode = 0
    End If
End Sub
Random Solutions  
 
programming4us programming4us