0% found this document useful (0 votes)
68 views2 pages

Rapoarte PostgreSQL

The document discusses three solutions for writing a SQL query to generate a sales report by product that includes totals for years 2008, 2009, 2010, and a grand total. The first solution uses CASE statements in the SELECT clause. The second uses scalar subqueries. The third uses subqueries in the FROM clause with left joins to link the subquery results by product code.

Uploaded by

Olga Junior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Rapoarte PostgreSQL

The document discusses three solutions for writing a SQL query to generate a sales report by product that includes totals for years 2008, 2009, 2010, and a grand total. The first solution uses CASE statements in the SELECT clause. The second uses scalar subqueries. The third uses subqueries in the FROM clause with left joins to link the subquery results by product code.

Uploaded by

Olga Junior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Redactati interogarea SQL pentru a obtine un raport similar celui de mai jos:

Vanzari
Produs

2008

2009

2010

Total

Produs 1
Produs 2
Produs 3
...

1. Solutia 1 - CASE
select denpr, sum(case when extract(year from datafact)=2008 then
cantitate*pretunit
else 0 end) as v_2008, sum(case when extract(year from
datafact)=2009 then cantitate*pretunit
else 0 end) as v_2009, sum(case when extract(year from
datafact)=2010 then cantitate*pretunit
else 0 end) as v_2010,
sum(cantitate*pretunit) as total
from facturi f inner join liniifact lf on f.nrfact=lf.nrfact
inner join produse p on p.codpr=lf.codpr
where extract(year from datafact) between 2008 and 2010
group by denpr order by 1

2. Solutia 2 - subconsultari (scalare) in clauza SELECT


select distinct denpr,
( select sum(cantitate*pretunit) from facturi f
inner join liniifact lf on f.nrfact=lf.nrfact
where extract(year from datafact) = 2008 and lf.codpr=p1.codpr) as v_2008,
( select sum(cantitate*pretunit) from facturi f
inner join liniifact lf on f.nrfact=lf.nrfact
where extract(year from datafact) = 2009 and lf.codpr=p1.codpr) as v_2009,
( select sum(cantitate*pretunit) from facturi f
inner join liniifact lf on f.nrfact=lf.nrfact
where extract(year from datafact) = 2010 and lf.codpr=p1.codpr) as v_2010,
( select sum(cantitate*pretunit) from facturi f
inner join liniifact lf on f.nrfact=lf.nrfact
where extract(year from datafact) between 2008 and 2010 and
lf.codpr=p1.codpr ) as total from facturi f1 inner join
liniifact lf1 on f1.nrfact=lf1.nrfact inner join produse p1 on
p1.codpr=lf1.codpr where extract(year from datafact) between 2008
and 2010 order by 1

2. Solutia 3 - subconsultari in clauza FROM


select distinct denpr, t2008.v_2008, t2009.v_2009, t2010.v_2010, t_total.total
from facturi f1 inner join liniifact lf1 on f1.nrfact=lf1.nrfact inner join
produse p1 on p1.codpr=lf1.codpr left join ( select codpr,
sum(cantitate*pretunit) as v_2008
from facturi f inner join liniifact lf on f.nrfact=lf.nrfact
where extract(year from datafact) = 2008
group by codpr

) t2008 on t2008.codpr = p1.codpr


left join (
select codpr, sum(cantitate*pretunit) as v_2009 from facturi
f inner join liniifact lf on f.nrfact=lf.nrfact where
extract(year from datafact) = 2009 group by codpr
) t2009 on t2009.codpr = p1.codpr
left join (
select codpr, sum(cantitate*pretunit) as v_2010 from facturi
f inner join liniifact lf on f.nrfact=lf.nrfact where
extract(year from datafact) = 2010 group by codpr
) t2010 on t2010.codpr = p1.codpr
left join (
select codpr, sum(cantitate*pretunit) as total from facturi f
inner join liniifact lf on f.nrfact=lf.nrfact where
extract(year from datafact) between 2008 and 2010 group by
codpr
) t_total on t_total.codpr = p1.codpr
where extract(year from datafact) between 2008 and 2010
order by 1

You might also like