![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi All -- I have a lot of experience with programming databases and the like, but I cannot for the life of me remember how to do this. I have been tasked with creating a summary report in MS Access that will show the Classified and Unclassified Roads in a Road System. Table: tblRoad The relevant Fields in this table are: District (Text) -- PK Road_System (Text) Road_Number (Text) Classified_Road (Yes/No) The Grouping should be something like this: District "Alpha" Road System "Foo1" Classified Roads: 33 Unclassified Roads: 89 Road System "Foo2" Classified Roads: 102 Unclassified Roads: 462 Road System "Foo3" Classified Roads: 0 Unclassified Roads: 22 District "Beta" Road System "Foo4" Classified Roads: 1298 Unclassified Roads: 3 You get the idea. The District is the primary key in this query, and the query should display how many roads are classified and how many roads are unclassified in a given Road System in a given District. I have done some reading and screwing around with SQL and I'm pretty sure I need to use the Count() function and group by Road System and District, but nothing I've tried works. Actually that's not true. I wrote a tremendously complicated UNION query that does work, sort of, but I KNOW there has to be a simpler solution. I mean, reports like these are the reason they invented SQL in the first place! And I'm pretty sure that I did something like this a long time ago. I just can't remember how I did it. Can anyone throw me a bone, here? TIA, Greg |
#3
| |||
| |||
|
|
Hi All -- |
#4
| |||
| |||
|
|
"Greg Burns" <gburns (AT) gci (DOT) net> wrote Hi All -- [...] how about create table roads ( district char(10) not null primary key, system char(10) not null, number char(10) not null, classified char(1) not null, check (classified in ('y', 'n')) ) select district, system, count(case when classified = 'y' then 1 else null end) as classified, count(case when classified = 'n' then 1 else null end) as nonclassified, count(1) as nr_of_roads from roads group by district, system if you would like a "total" over district as well you can either use "grouping sets" - that is if your db supports them -, or construct them your self as select district, system, count(case when classified = 'y' then 1 else null end) as classified, count(case when classified = 'n' then 1 else null end) as nonclassified, count(1) as nr_of_roads from roads group by district, system union all select district, 'Total' as system, count(case when classified = 'y' then 1 else null end) as classified, count(case when classified = 'n' then 1 else null end) as nonclassified, count(1) as nr_of_roads from roads group by district, 'Total' |
#5
| |||
| |||
|
|
Hi All -- I have a lot of experience with programming databases and the like, but I cannot for the life of me remember how to do this. I have been tasked with creating a summary report in MS Access that will show the Classified and Unclassified Roads in a Road System. Table: tblRoad The relevant Fields in this table are: District (Text) -- PK Road_System (Text) Road_Number (Text) Classified_Road (Yes/No) The Grouping should be something like this: District "Alpha" Road System "Foo1" Classified Roads: 33 Unclassified Roads: 89 Road System "Foo2" Classified Roads: 102 Unclassified Roads: 462 Road System "Foo3" Classified Roads: 0 Unclassified Roads: 22 District "Beta" Road System "Foo4" Classified Roads: 1298 Unclassified Roads: 3 You get the idea. The District is the primary key in this query, and the query should display how many roads are classified and how many roads are unclassified in a given Road System in a given District. I have done some reading and screwing around with SQL and I'm pretty sure I need to use the Count() function and group by Road System and District, but nothing I've tried works. Actually that's not true. I wrote a tremendously complicated UNION query that does work, sort of, but I KNOW there has to be a simpler solution. I mean, reports like these are the reason they invented SQL in the first place! And I'm pretty sure that I did something like this a long time ago. I just can't remember how I did it. Can anyone throw me a bone, here? TIA, Greg |
#6
| |||
| |||
|
|
select t0.district, t0.system, nvl(class,0) as classified, nvl(unclass,0) as unclassified I assume nvl is the oracle way of saying coalesce? |
|
[...] where t0.district = t1.district(+) and t0.system = t1.system(+) and t0.district = t2.district(+) and t0.system = t2.system(+) I assume (+) is an oracle way of saying ... left/right outer join ... |
![]() |
| Thread Tools | |
| Display Modes | |
| |