Help with Report Query

  • Hi

     

    I’m trying to suss out how to write a query to produce a grouped report giving columns with counts and or sums for separate periods.  The relevant bits of the two main tables are:

     

    tblData:

     

    Site            Category            Date(SmDateTime)

     

    tblSource:

     

    Site 

     

    The report I’m aiming for should look like this:

     

    Site            Category          Pd1                Pd2

    Site1          A                      Count            Count

    Site1          B                      Count            Count

    Site1          C                      Count            Count

    Site2          A                      Count            Count

    Site2          B                      Count            Count

    Site2          C                      Count            Count

     

    I’d like to list all sites and all categories showing a count of 0 where there are nulls.  Should I create a temporary table or a view that combines the sites and categories and left join the data table to that.  Any guidance would be greatly appreciated.

     

    Cheers

     

    Geoff

  • Could you give some testdata for tables tblData and tblSource along with the expected output of the query?

     

  • Hi

    After some fiddling about in Excel I've produced the following:

    tblData:

    labIDSourceSpecCodeTestDateTestCodecolPK
    Tsite1ME5       11/01/200417HP30
    Tsite1ME5       12/01/200417HP36
    Msite1GP        02/01/20042
    Bsite1ME2       03/01/200417HP11
    Bsite1ME2       04/01/200417HP12
    Bsite1ME2       05/01/200417HP13
    Bsite1ME5       06/01/200417HP10
    Bsite1ME5       07/01/200417HP14
    Bsite1ME5       08/01/200417HP16
    Bsite1ME5       09/01/200417HP17
    Bsite1ME5       10/01/200417HP22
    Tsite2GP        01/01/200417HP31
    Tsite2ME5       21/01/200417HP48
    Tsite2ME5       22/01/200417HP28
    Tsite2ME5       23/01/200417HP29
    Tsite2ME5       24/01/200417HP32
    Tsite2ME5       25/01/200417HP37
    Tsite2ME5       26/01/200417HP38
    Tsite2ME5       27/01/200417HP39
    Bsite2ME5       13/01/200417HP18
    Bsite2ME5       14/01/200417HP19
    Bsite2ME5       15/01/200417HP20
    Bsite2ME5       16/01/200417HP21
    Bsite2ME5       17/01/200417HP23
    Bsite2ME5       18/01/200417HP24
    Bsite2ME5       19/01/200417HP25
    Bsite2ME5       20/01/200417HP26
    Msite3ME5       28/01/200417HP44
    Msite3ME5       29/01/200417HP45
    Msite3ME5       30/01/200417HP46
    Msite3ME5       31/01/200417HP47
    Bsite1ME5       07/02/200411DE5
    Bsite1ME5       08/02/200411DE6
    Bsite1ME5       09/02/200411DE7
    Bsite1ME5       10/02/200411DE8
    Bsite1ME5       11/02/200411DE9
    Bsite1OH        15/02/200417HP15
    Msite1ME7       12/02/20041
    Msite1ME7       13/02/20044
    Msite1SU10      19/02/20043
    Bsite2PA1       16/02/200417HP27
    Tsite2NK        14/02/200417HP35
    Tsite2PA1       17/02/200417HP40
    Msite3ME5       01/02/200417HP50
    Msite3PA1       18/02/200417HP49
    Tsite3ME5       02/02/200417HP33
    Tsite3ME5       03/02/200417HP34
    Tsite3ME5       04/02/200417HP41
    Tsite3ME5       05/02/200417HP42
    Tsite3ME5       06/02/200417HP43

    tblSource:

    pathcodeEntityOrganisation NameRO CodeHA CodeAddress Line 1Address Line 2Address Line 4Address Line 5Postcode
    site1xDr AliasY43Q02The ClinicHigh StreetAnytownHertfordshireSG1 4AB
    site2zDr SmithY43Q02The SurgeryMain RoadAcityHertfordshireSG1 4AB
    site3zDr JonesY43Q02The Health CentreBack LaneThevillageHertfordshireSG1 4AB

    Ideal report with additional grouping:

    Entity xJanFeb
    Site1Dr Alias
    Lab B26
    Lab M13
    Lab T80
    Entity yJanFeb
    Site2Dr Smith
    Lab B81
    Lab M80
    Lab T02
    Site1Dr Jones
    Lab B00
    Lab M42
    Lab T05

    I think I copied the subtotals OK.  There a good few addtional columns in the data table and some in the sources one but they are not relevant to the query although I've left a few in for the feel of it.  The data table contains about 3,500,000 lines at present.  I hope this sheds some more light on the matter.

    Cheers

    Geoff

     

     

  • I still don't see how these numbers come up. Consider Entity x, Site1, Lab B. This should be 2 in Jan and 6 in Feb. Why? Also, in your desired result, where does entity y come from? (Or should this be entity z?)

  • Hi

    Sorry a few errors in the report, I've just gone back into Excel and make x, site1, labB 8 in Jan and 6 in Feb.  You're right abou the entity - should be z.  Also the line site1 under z should be site3

    Should look like this:

    <TD style="BORDER-RIGHT: #d8d5ec; BORDER-TOP: #d8d5ec; BORDER-LEFT: #d8d5ec; BORDER-BOTTOM: #d8d5ec; BACKGROUND-COLOR: transparent" align=right x:num x:fmla="=SUM(D315)">19

    Entity xJanFeb
    Site1Dr Alias
    Lab B86
    Lab M13
    Lab T20
    Entity zJanFeb
    Site2Dr Smith
    Lab B81
    Lab M00
    Lab T82
    Site3Dr Jones
    Lab B00
    Lab M42
    Lab T05
    31
  • A first attempt, I haven't joined with tblSource yet. Am I on the right track?

     

    select

    source,

    labid,

    sum(case when month(testdate) = 1 then 1 else 0 end),

    sum(case when month(testdate) = 2 then 1 else 0 end)

    from tblData group by source, labid

  • Hi

    I see what you're saying, but what will happen in instances where a source has does not have an entry in the period/s?

     

  • Sorry

    Just looked again - can see now.

  • Ok - does this give you what you want?

    select

    s.entity,

    s.organisation,

    d.source,

    d.labid,

    sum(case when month(d.testdate) = 1 then 1 else 0 end),

    sum(case when month(d.testdate) = 2 then 1 else 0 end)

    from tblSource s left join tblData d

    on s.pathcode = d.source

    group by s.entity, s.organisation, d.source, d.labid

    order by s.entity, s.organisation, d.source, d.labid

  • Hi

    This looks like it might do it I was just having a look to see if I can add the year to the case statement.

     

  • Hi Jesper

    Sorry I didn't get back to you more positively on Friday.  It was certainly very helpful.  My script now looks like this:

    USE PathData

    SELECT d.TestDate, d.Source, d.labid, d.colPK

    INTO tblDataNoXXXXX

    FROM tblData d JOIN tblTestLib t

    ON d.TestCode = t.[Test Code]

    WHERE [Price Band] <> 'XXXXX'

    GO

    SELECT

    s.entity, s.[organisation name], s.Site, d.source, d.labid,

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 4) THEN 1 ELSE 0 end) AS 'APR 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 5) THEN 1 ELSE 0 end) AS 'MAY 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 6) THEN 1 ELSE 0 end) AS 'JUN 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 7) THEN 1 ELSE 0 end) AS 'JUL 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 8) THEN 1 ELSE 0 end) AS 'AUG 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 9) THEN 1 ELSE 0 end) AS 'SEP 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 10) THEN 1 ELSE 0 end) AS 'OCT 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 11) THEN 1 ELSE 0 end) AS 'NOV 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2004 AND MONTH(d.TestDate) = 12) THEN 1 ELSE 0 end) AS 'DEC 04',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 1) THEN 1 ELSE 0 end) AS 'JAN 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 2) THEN 1 ELSE 0 end) AS 'FEB 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 3) THEN 1 ELSE 0 end) AS 'MAR 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 4) THEN 1 ELSE 0 end) AS 'APR 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 5) THEN 1 ELSE 0 end) AS 'MAY 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 6) THEN 1 ELSE 0 end) AS 'JUN 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 7) THEN 1 ELSE 0 end) AS 'JUL 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 8) THEN 1 ELSE 0 end) AS 'AUG 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 9) THEN 1 ELSE 0 end) AS 'SEP 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 10) THEN 1 ELSE 0 end) AS 'OCT 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 11) THEN 1 ELSE 0 end) AS 'NOV 05',

    SUM(CASE WHEN (YEAR(d.TestDate) = 2005 AND MONTH(d.TestDate) = 12) THEN 1 ELSE 0 end) AS 'DEC 05'

    FROM tblSources s left join tblDataNoXXXXX d

    ON s.pathcode = d.source

    WHERE s.entity IN ('5GG', '5GH', '5GJ', '5GK')

    GROUP BY s.entity, s.[organisation name], s.Site, d.source, d.labid

    ORDER BY s.entity, s.[organisation name], s.Site, d.source, d.labid

    GO

    /*DROP TABLE tblDataNoXXXXX*/

    GO

    Is there a more elegant way of putting the select/join/tblTestLib and keep the indexing from tblData?

    Cheers

    Geoff

     

     

  • I didn't know you needed that many columns

    Maybe the following query is better:

    select

    s.entity,

    s.organisation,

    d.source,

    d.labid,

    month(d.testdate),

    year(d.testdate),

    count(*)

    from tblSource s left join tblData d

    on s.pathcode = d.source

    group by s.entity, s.organisation, d.source, d.labid, month(d.testdate), year(d.testdate)

    order by s.entity, s.organisation, d.source, d.labid, month(d.testdate), year(d.testdate)

    It changes the structure of the output a bit, but the data should still be there...

    To produce the original output (if you need it), you might also insert the output of the select above into a new table and then query this table... I don't know if that's faster... You might also consider using the query above and then modify the result on the client, but I don't know it that's a possibility...

  • Hi

    No worries - I just went wild and produced a set for all the data I have at present.  This could be useful as I envisage having an Access front end on the db where users would select periods from/to on a form.  I believe I can use VB to pass a variable from a form to a stored procedure?

    In the mean time I can show them this (they have some other Ideas for me too, although I'm waiting for more details a priorties) and they can let me know exactly what they want.

    Do you think I should keep the 'select into' tblNoXXXXX as a temporary measure or make it a permenant feature of the database?  I am likely to need this selection in various other reports; I thought it might be a good idea to do this and then index the colPK.  I intend to write some SPs to upload new data files, to the main data table, so I could include populating and indexing this at the same time.

    Cheers

    Geoff

     

     

     

     

  • 1. I think so, yes. 2. I guess this depends on your requirements and the amount of data.

     

  • Thanks for your help Jesper :-)>

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply