pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Masaru Sugawara <rk73@echna.ne.jp>
To: aannddrree@libero.it
Cc: pgsql-sql@postgresql.org
Subject: Re: left join and where
Date: Sat, 05 Jan 2002 00:18:46 +0900
Message-ID: <20020104211134.4181.RK73@echna.ne.jp> (raw)
In-Reply-To: <c837c0fc.0112271228.1ce47b3a@posting.google.com>
References: <c837c0fc.0112271228.1ce47b3a@posting.google.com>

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




view thread (2+ messages)

Message-ID: <20020104211134.4181.RK73@echna.ne.jp>
Permalink:  ../20020104211134.4181.RK73@echna.ne.jp/
Also on:    postgresql.org/message-id/20020104211134.4181.RK73@echna.ne.jp

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rk73@echna.ne.jp, aannddrree@libero.it
  Subject: Re: left join and where
  In-Reply-To: <20020104211134.4181.RK73@echna.ne.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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