[SQL] Returning Multiple Values from CASE statement?

2003-05-30 Thread C F
Hello,
This is probably an odd request, but I have my reasons :)  Basically, what I need to do is return *multiple* values each for a number of criteria.  Imagine being able to return multiple values for each CASE statement... that's what I'm trying to do.  I can solve this with subqueries, but as you'll see it's *really* ugly, and I'm sure there's a better way (performance is key).  The other way obviously is to repeat the CASE statement for each value I want to return but that seems inefficient also.  So I was wondering if someone could take a look at my query and slap me up side the head and set me straight?
 
Here's the kind of query that would *like* to be able to do.
 
select   (case when column1 = column2 then column5,column6) as alias5,alias6,  (case when column3 = column4 then column7,column8) as alias7,alias8from  mytable;
 
and here's the only ugly way I know how to do it..
 
select   a.*,  (case a.alias2 = true then b.column5) as alias5,  (case a.alias2 = true then b.column6) as alias6,  (case a.alias4 = true then b.column7) as alias7,  (case a.alias4 = true then b.column8) as alias8from  (select  (case when column1 = column2 then true else false end) as alias2,  (case when column3 = column4 then true else false end) as alias4  from  mytable) a,  mytable bwhere  a.id = b.id;
Thanks!!
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).

[SQL] SQL Help

2003-05-31 Thread C F
Hello,
I already tried this same basic question with no response  maybe I was too wordy.  So here it is simplified what's the best way to write this query?  I'm open to using stored procedures, but even then I don't know how I would conditionally populate a resultset (refcursor).  Notice that in the first three cases, the _expression_ is the exact same, only the return value is different.  This seems inefficient
 
select   (case when column1 = column2 then column3 end) as alias1,  (case when column1 = column2 then column4 end) as alias2,  (case when column1 = column2 then column5 end) as alias3,  (case when column6 = column7 then column8 end) as alias4from  mytable;
Any ideas? 
Thanks!
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).

Re: [SQL] CASE returning multiple values (was SQL Help)

2003-05-31 Thread C F

I was afraid someone was going to ask that :)
Okay, I'll do my best at explaining where I'm coming from
I'm working on a mapping application it is user-configurable.  What this means (as it pertains to this disucssion) is that the through a configuration file, the user is able to define the rules that the application will use to determine which geometries *and* attributes to pull from PG at various scales.  These 'rules' that the user defines also contain other specifics such as how to symbolize the geometry, how to label the geometry etc.  All of these parameters can either be hard coded by the user into the configuration file, or they can define an _expression_ that will be used to dynamically pull it from the database.  On top of all of this, we have two more levels of queries.
So... at the very top level, the user can define an _expression_ that will determine that everything queried from this table will match these criteria... this is my WHERE clause.
Then below this level, various rules can be defined... each rule can have another definition that evaluates into a SQL _expression_.  Now, I could make each rule an entirely separate query, but for one, they all share the exact same top level WHERE clause, and two, there could potentially be many many rules which I would think would cause severe performance issues.  Let me give you an example...
Let's say we're mapping cities of the United States based on population...  In other words, I want to symbolize the cities on the map based on population (larger symbol for larger populations, smaller symbol for smaller populations, etc).  I also want to show the city names of the larger cities *only*.  So, what the client application (client to PostgreSQL) needs is; the city location, which rules evaluate to true, and the city names of those larger cities (defined by a rule).
 
We have a table of cities of the world.  So the top level filter (that all rules will share) is, "COUNTRY = 'USA'".
 
Rule 1 says that cities with a population over 1,000,000 will have a large symbol and be labeled with the city name.  So the sql could look like this...
select longitude, latitude, city_name from city where country = 'USA' and population > 100;
 
... seems easy enough, but remember we can have an infinite number of rules (not really inifinite, but you get the point). So
 
Rule2 says that cities with a population under 1,000,000 will have a small symbol (note, we do not care about the city name here).  So, by itself, the SQL could look like this...
select longitude, latitude from city where country = 'USA' and population < 100;
 
Okay, for this simple example, I would have no problem doing two different queries (this example is extremely simplified compared to what is possible/likely).  But what if the user wanted to give a different symbol for every population in 100,000 increments?  If our range of populations was 100,000 to 5,000,000 that would be 50 queries!  Not only would it be 50 queries, but it would be 50 queries using a nearly identical WHERE clause.  So I thought it would be more efficient to combine the queries into something like the following...
 
select
  longitude,
  latitutde,
  (case when population > 100 then true else false end) as rule1,
  (case when population > 100 then city_name end) as label1,
  (case when population < 100 then true else false end) as rule2
where
  country = 'USA' 
;
 
I could just only concern the SQL with the boolean values for the rules, and return all city names, and let the application simply discard them, but that seems like not a good thing to do for very large resultsets (and again, this is overly simplified, we could have many such columns full of uncessary data being returned).  And by the way, that query cannot be written as something like...

  (case when population > 100 then 'rule1' when population < 100 then 'rule2' end) as rules
... because the rules are NOT mutually exclusive, there can many positives.
 
Anyway, hopefully I didn't leave anything important out.  It sounds like there's no obvious solution to avoiding multiple evaluations of the test expressions.  The rules are relatively static once the config files are read in, so I could conceivably create stored procedures with a bunch of IF statements at that time.  However, I'm not sure if in PG there is a way to dynamically populate the resulting recordset on the fly.  I can think of 10 different ways accomplish what I'm trying to do, but hopefully someone has some ideas on what would be the best performing.
Sorry if it's information overload, but you tried to answer my questions, so I thought I should at least try to answer yours :)
Any thoughts much appreciated.
You could write a set returning function, but you'd just end up doing the same thing. Can you explain what it is you're trying to acheive - real fields/schemas etc?-- Richard Huxton
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).