sql reporting

  • I am working on a report Which would be like following

    This is what i have:

    company_no  dept_no count

    1  2 10

    1  3 11

    1  4 39

    2  1 15

    3  2 99

    3  3 74

    I would like to show this data as below:

    company_no  dept_no count

    1  1 0

    1  2 10

    1  3 11

    1  4 39

    2  1 15

    2  2 0

    2  3 0

    2  4 0

    3  1 0

    3  2 99

    3  3 74

    3  4 0

    How can I do this.

    Thanks.

  • Hi,

      What exactly you want to show on report?do u wnat to repeat dept_no with count? or just count for each dept_no?

  • SELECT

    FROM

    ORDER BY company_no  dept_no

    Have fun

    Steve

    We need men who can dream of things that never were.

  • For each company we have 4 departments.

    Suppose for company 1 we have 2 departments and 2 and 3 and they have dept_no_count is 23 and 34 respectively.

    But in my report If I group the data the repprt would be like this

    company_id   dept_no    dept_no count

    1                   2                    23

    1                   3                     34

    Because i dont have dara for dept 1 and 4.

    But i shoul have a report like

    company_id   dept_no    dept_no count

    1                   1                    0

    1                   2                    23

    1                   3                     34

    1                   4                    0

    Thanks.

  • Do you have a table listing departments for each company?

    Assuming there are always 4 depts for each company as in your example:

    SELECT counts.company_no, counts.dept_no, counts.count

    FROM (

       SELECT dept_no = 1

       UNION SELECT dept_no = 2

       UNION SELECT dept_no = 3

       UNION SELECT dept_no = 4

       ) depts

    left join counts on (counts.dept_no = depts.dept_no)

    ORDER BY company_no, dept_no

  • Just one minor change to snow.surfer on the select list use:

     SELECT IsNull(counts.company_no,'?') as Company_no, depts.dept_no, IsNull(counts.count,0) as Counts FROM ...

    HTH

     


    * Noel

Viewing 6 posts - 1 through 5 (of 5 total)

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