Re: left join and where

Поиск
Список
Период
Сортировка
От Masaru Sugawara
Тема Re: left join and where
Дата
Msg-id 20020104211134.4181.RK73@echna.ne.jp
обсуждение исходный текст
Ответ на left join and where  (aannddrree@libero.it (andrea))
Список 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, alsoAccess 97), the following query could get what you
wouldexpect.  Since,however, the table of Vendite has very small example, I'm not sure whether"v0.anno IS NULL" that I
citefrom 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
ASv0         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
JOINvendite AS v0 ON c0.colore = v0.colore WHERE v0.anno = 1 OR v0.anno IS NULL GROUP BY c0.colore, v0.annoUNIONSELECT
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



В списке pgsql-sql по дате отправления:

Предыдущее
От: "Unnikrishnan Menon"
Дата:
Сообщение: Execute dynamically generated query???
Следующее
От: Andrew Perrin
Дата:
Сообщение: Telling how many records are joined