Re: [SQL] How pull

2007-09-24 Thread Matt Magoffin
> On 23/09/2007, Matt Magoffin <[EMAIL PROTECTED]> wrote:
>> Hello,
>>
>> My SQL skills are limited and I'm struggling with a query where I want
>> to
>> return a single item of an aggregate join. The query looks like this:
>>
>> select
>>   (case
>> when agg.avg_rating is null then 0.0
>> when agg.avg_rating < 0.75 then 0.5
>> when agg.avg_rating < 1.25 then 1.0
>> when agg.avg_rating < 1.75 then 1.5
>> when agg.avg_rating < 2.25 then 2.0
>> when agg.avg_rating < 2.75 then 2.5
>> when agg.avg_rating < 3.25 then 3.0
>> when agg.avg_rating < 3.75 then 3.5
>> when agg.avg_rating < 4.25 then 4.0
>> when agg.avg_rating < 4.75 then 4.5
>> else 5.0
>>   end) as avg_rating,
>>   count(item.itemid) as item_count
>> from media_item item
>> inner join (
>>   select rating.mediaitem_userrating_hjid as ritemid,
>> avg(rating.rating) as avg_rating
>>   from media_item_rating rating, media_item item
>>   where rating.mediaitem_userrating_hjid = item.itemid
>>   group by rating.mediaitem_userrating_hjid
>> ) as agg
>> on item.itemid = agg.ritemid
>> group by avg_rating
>> order by avg_rating desc
>>
>> and a sample of results is this:
>>
>>  avg_rating | item_count
>> +
>> 5.0 | 21
>> 4.0 | 33
>> 3.0 | 13
>> 2.0 |  4
>> 1.0 |  1
>>
>> What I want as well is the ID of the item (and possibly it's avg_rating
>> value) from the "agg" join with the highest avg_rating for each output
>> row... something like this
>>
>>  avg_rating | item_count | item_id | item_rating
>> +---
>> 5.0 | 21 | 109890  | 4.9
>> 4.0 | 33 | 89201   | 4.1
>> 3.0 | 13 | 119029  | 2.8
>> 2.0 |  4 | 182999  | 2.2
>> 1.0 |  1 | 1929| 1.0
>>
>> So the intention in this example is that item #109890 has an average
>> rating of 4.9 and that is the highest rating within the > 4.75 rating
>> group.
>>
>> If anyone had any tips I'd greatly appreciate it.
>>
>
> create ranking function to make queries look simpler:
>
> create or replace function ranking_group(numeric) returns numeric as
> $$ select case
>   when $1 < 0.3456 then 'quite small'
>   ...
> end $$ language sql immutable;
>(I'd make it STRICT, but you allow null rankings)
>
> 1st way: DISTINCT ON + subquery
>
> select *, (select count(*) from rating where rating_group(rating) =
> subq.rating_group ) as rating_group_size
> from (
> select distinct on (rating_group)
> rating_group(r.rating),
> r.item_id as best_rated_item_id,
> r.rating as best_rating
> from rating r
> order by rating_group desc, r.rating desc
> ) subq;
>
>
> 2nd way (faster - actually 2 x faster)
> using FIRST aggregate to calculate all in one pass
>
> create function first(numeric,numeric) returns numeric as 'select $1'
> language sql immutable strict;
> create function first(integer,integer) returns integer as 'select $1'
> language sql immutable strict;
> create aggregate first( integer ) ( SFUNC = first, STYPE = integer );
> create aggregate first ( numeric ) ( SFUNC = first, STYPE = numeric );
>
> select
>   rating_group(rating),
>   count(*) as num_ratings,
>   first(item_id) as best_rated_item_id,
>   first(rating) as best_rating
> from ( select * from rating order by rating desc ) ordered_ratings
> group by rating_group
> order by rating_group desc;
>
>
> note: if you can, get rid of null ratings. what are they supposed to
> mean? they make things a bit more complicated.


Thanks very much for the helpful tips, Filip, you understand perfectly
what I'm trying to do. For those nulls, they are not actually needed with
the query I gave, you are right. I have another version of this query that
I started with that uses a left outer join instead of the inner join to
produce another grouping for all items that have no rating set (thus the
null value).

An aggregate function like first() is precisely what I need, however I am
looking for a fairly portable solution I can also use in other databases
(Derby and MySQL at a minimum). Unfortunately I don't think either of
these suggestions will work for those systems because they don't support
DISTINCT ON or custom SQL aggregate functions as nicely as Postgres does.

Also, the "avg_rating" I'm using is itself an aggregate value, but I don't
see how your second solution handles this? The rating group values come
from  the avg() calculation on a rating table, like

 ratinguser | rating | item_id | idx
++-+-
   1098 |  2 |   11016 |   1
  2 |  3 |   11016 |   0
 43 |  5 |9021 |   2
  2 |  4 |9021 |   1
   1098 |  2 |9021 |   0

So here the rating for item 11016 is 2.5 and for 9021 it is 3.66.

In addition, this is not my entire SQL statement, because I need another
join to narrow the item results first... so the 

[SQL] foreign key problem

2007-09-24 Thread ivan marchesini
Dear users,
I'm trying to create constraints into a db they give me from access...
I'm using pgadmin to create a new foreign key for the relationship
between two table..

I have a table called "generalita" wich contains a column called
"cod_carg":
___
select cod_carg from generalita group by cod_carg;
 cod_carg
--

 2
 0
 1
(4 righe)
___


I have another table, called "diz_carg" that contain, into a column
called "cod_carg" (that is a primary key), the domain values for the
"cod_carg" column of "generalita":
__
select cod_carg from diz_carg group by cod_carg;
 cod_carg
--
 1
 0
 2
(3 righe)
__



so when I try to create the foreign key I obtain:
ALTER TABLE generalita ADD CONSTRAINT fkey_diz_cod_carg FOREIGN KEY
(cod_carg) REFERENCES diz_carg (cod_carg) ON UPDATE NO ACTION ON DELETE
NO ACTION;


ERROR:  insert or update on table "generalita" violates foreign key
constraint "fkey_diz_cod_carg"
DETAIL:  Key (cod_carg)=(  ) is not present in table "diz_carg".



can I allow null values for "generalita.cod_carg" although it is a
foreign key for "diz_carg.cod_carg"?

I apologize if it is a stupid question   :-(

many thanks to all




-- 
Ti prego di cercare di non inviarmi files .doc, .xls, .ppt, .dwg.
Preferisco formati liberi.
Please try to avoid to send me  .doc, .xls, .ppt, .dwg files.
I prefer free formats.
http://it.wikipedia.org/wiki/Formato_aperto
http://en.wikipedia.org/wiki/Open_format

Ivan Marchesini
Department of Civil and Environmental Engineering
University of Perugia
Via G. Duranti 93/a 
06125
Perugia (Italy)
e-mail: [EMAIL PROTECTED]
[EMAIL PROTECTED]
tel: +39(0)755853760
fax (university): +39(0)755853756
fax (home): +39(0)5782830887
jabber: [EMAIL PROTECTED]


---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faq


Re: [SQL] Many databases

2007-09-24 Thread Erik Jones


On Sep 23, 2007, at 11:56 PM, Erik Jones wrote:



On Sep 21, 2007, at 6:16 PM, Stefan Arentz wrote:


Well I'm just toying with an idea. The problem I'm facing is that I
would like clients to only see the tuples that they have created and
own. I guess I'll just skip direct sql access to the db and write  
some

API on top of it that manages the data. Not a big deal but it
complicates things :-)



You could do the same thing with views on those tables.  One  
problem with multiple databases is keeping global meta data for all  
of your customers together in a simple way.


Oh, yeah, I forgot to mention separate schemas as well.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com



---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

  http://www.postgresql.org/docs/faq


[SQL] problems with copy

2007-09-24 Thread chester c young
I'm getting lots of delimited files from Excel and MySQL users that,
mid-file, begin truncating lines if ending in null values.

for example:
1781: "one","two","three",,
1782: "one","two","three",,
1783: "one","two","three",,

(delimited files from Open Office are well behaved)

is there any way to handle this apparently standard aberration in PG?



  

Fussy? Opinionated? Impossible to please? Perfect.  Join Yahoo!'s user panel 
and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 


---(end of broadcast)---
TIP 6: explain analyze is your friend