Thread: Doubt about SELECT

Doubt about SELECT

From
SydMosh
Date:
Hi, i'm kinda new on postgresql, so i have a doubt, i'm trying to make a
query look like this:
http://img90.imageshack.us/img90/9440/consultaen.jpg

I feel like it is something so simple, show the "SUM(price_serv) AS total"
on a new line, but i just can't find the way. I've read the postgresql
manual over and over and i couldn't find anything there.

I'm sure you guys can help me.
--
View this message in context: http://www.nabble.com/Doubt-about-SELECT-tp22769832p22769832.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: Doubt about SELECT

From
Jeff Ross
Date:
SydMosh wrote:
> Hi, i'm kinda new on postgresql, so i have a doubt, i'm trying to make a
> query look like this:
> http://img90.imageshack.us/img90/9440/consultaen.jpg
>
> I feel like it is something so simple, show the "SUM(price_serv) AS total"
> on a new line, but i just can't find the way. I've read the postgresql
> manual over and over and i couldn't find anything there.
>
> I'm sure you guys can help me.
>
You want two queries, joined with a union all.

create temp table test(person_id integer, service_id integer, name text,
address text, price_serv integer);

insert into test values(1,40,'Bob Cobb','85 Cob Court, Cheyenne, WY
82001',380);
insert into test values(1,40,'Bob Cobb','85 Cob Court, Cheyenne, WY
82001',220);

    select
        person_id,
        service_id,
        name,
        address,
        price_serv,
        null as "total"
        from test
    union all
    select
        null as person_id,
        null as service_id,
        null as name,
        null as address,
        null as price_serv,
        sum(price_serv) as "total"
        from test;

 person_id | service_id |   name   |             address              |
price_serv | total
-----------+------------+----------+----------------------------------+------------+-------
         1 |         40 | Bob Cobb | 85 Cob Court, Cheyenne, WY 82001
|        380 |
         1 |         37 | Bob Cobb | 85 Cob Court, Cheyenne, WY 82001
|        220 |
           |            |          |
|            |   600
(3 rows)


If you want to normalize your data, name and address should be in
another table so the query ends up more like

    select
        test.person_id,
        test.service_id,
        people.name,
        people.address,
        test.price_serv,
        null as "total"
        from test, people where test.person_id = people.person_id
    union all
    select
        null as person_id,
        null as service_id,
        null as name,
        null as address,
        null as price_serv,
        sum(price_serv) as "total"
        from test;

Hope that helps,

Jeff Ross


[Q] LOG: failed to commit client_encoding

From
"V S P"
Date:
Hi,
I am seeing this error in my concole (winXP) log
using Pg 8.3 UTF-8 encoding

client is Pg ODBC latest,
every time I open a connection I set
client encoding to UTF-8 (to make sure my C++ app is getting via
ascii version of pgODBC the UTF-8 strings).

"SET client_encoding='UTF8'"

What does this LOG string mean, and what should I do?

if there is a place with all the error messages and explanation
I would certainly appreciate the link (I just tried regular searches
but could not find it).



thank you in advance,
Vlad
--
  V S P
  toreason@fastmail.fm

--
http://www.fastmail.fm - Email service worth paying for. Try it for free


Re: [Q] LOG: failed to commit client_encoding

From
Tom Lane
Date:
"V S P" <toreason@fastmail.fm> writes:
> What does this LOG string mean, and what should I do?

That's supposed to be a can't-happen case.  How did you provoke it
exactly?  (No, you didn't provide enough detail for someone else
to reproduce it...)

Are you by any chance using database encoding mule_internal?
That's the only case I'm aware of where I'd expect setting
client_encoding to utf8 to fail.

            regards, tom lane