pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
left join and where
2+ messages / 2 participants
[nested] [flat]

* left join and where
@ 2001-12-27 20:28 andrea <aannddrree@libero.it>
  2002-01-04 15:18 ` Re: left join and where Masaru Sugawara <rk73@echna.ne.jp>
  0 siblings, 1 reply; 2+ messages in thread

From: aannddrree@libero.it @ 2001-12-27 20:28 UTC (permalink / raw)
  To: pgsql-sql

hello,
I'm a beginner and I have a trouble with SQL and Access 97
I try to explain the problem:

I have a table colori:

ID  colore
1   red
2   blu
3   green

and a table Vendite

ID colore anno quantita
1   red    1     10
2   blu    1     20
3   green  2     30

I want a query that return all "quantita" but only for a year (for example
if year =1, I don't extarct green)
The result must be:

colore anno quantita
red     1     10
blu     1     20          
green       
------------------------------------

I use this SQL code

SELECT colori.colore, vendite.anno, sum(vendite.quantita)
FROM colori
LEFT JOIN vendite ON colori.colore=vendite.colore
WHERE vendite.anno=1 OR vendite.anno Is Null
GROUP BY colori.colore, vendite.anno

But the result is

colore anno quantita
red     1     10
blu     1     20          

How can I get the correct result

Thank's for the help

Andrea



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: left join and where
  2001-12-27 20:28 left join and where andrea <aannddrree@libero.it>
@ 2002-01-04 15:18 ` Masaru Sugawara <rk73@echna.ne.jp>
  0 siblings, 0 replies; 2+ messages in thread

From: Masaru Sugawara @ 2002-01-04 15:18 UTC (permalink / raw)
  To: aannddrree@libero.it; +Cc: pgsql-sql

On 27 Dec 2001 12:28:43 -0800
aannddrree@libero.it (andrea) wrote:

> I'm a beginner and I have a trouble with SQL and Access 97
> I try to explain the problem:
> 
> I have a table colori:
> 
> ID  colore
> 1   red
> 2   blu
> 3   green
> 
> and a table Vendite
> 
> ID colore anno quantita
> 1   red    1     10
> 2   blu    1     20
> 3   green  2     30
> 
> I want a query that return all "quantita" but only for a year (for example
> if year =1, I don't extarct green)
> The result must be:
> 
> colore anno quantita
> red     1     10
> blu     1     20          
> green       
> ------------------------------------
> 
> I use this SQL code
> 
> SELECT colori.colore, vendite.anno, sum(vendite.quantita)
> FROM colori
> LEFT JOIN vendite ON colori.colore=vendite.colore
> WHERE vendite.anno=1 OR vendite.anno Is Null
> GROUP BY colori.colore, vendite.anno
> 
> But the result is
> 
> colore anno quantita
> red     1     10
> blu     1     20          
> 
> How can I get the correct result

 Since most of DB software's support subqueries (not to mention, also
 Access 97), the following query could get what you would expect.  Since,
 however, the table of Vendite has very small example, I'm not sure whether
 "v0.anno IS NULL" that I cite from yours is actually needed or not. 


 SELECT c1.colore, v1.anno, SUM(v1.quantita) AS quantita
   FROM colori AS c1 LEFT JOIN 
        (SELECT v0.* FROM vendite AS v0
          WHERE v0.anno = 1 OR v0.anno IS NULL) AS v1
        ON c1.colore = v1.colore
  GROUP BY c1.colore, v1.anno
 ;



 In case of no using subqueries, here is:
 
 SELECT c0.colore, v0.anno, Sum(v0.quantita) AS quantita
   FROM colori AS c0 LEFT JOIN vendite AS v0 ON c0.colore = v0.colore
  WHERE v0.anno = 1 OR v0.anno IS NULL
  GROUP BY c0.colore, v0.anno
 UNION
 SELECT c1.colore, NULL, NULL
   FROM colori AS c1 LEFT JOIN vendite AS v1 ON c1.colore = v1.colore
  WHERE v1.anno > 1
 ;


Regards,
Masaru Sugawara




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-01-04 15:18 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-12-27 20:28 left join and where andrea <aannddrree@libero.it>
2002-01-04 15:18 ` Masaru Sugawara <rk73@echna.ne.jp>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox