Hello everyone
You can watch all my little scripts in C and Perl.
Enjoy !
MANGEZ
SELECT date, SUM(dollars) AS total_dollars,
SUM(SUM(dollars)) OVER(ORDER BY date ROWS UNBOUNDED PRECEDING) AS run_dollars,
SUM(quantity) AS total_qty,
SUM(SUM(quantity)) OVER(ORDER BY date ROWS UNBOUNDED PRECEDING) AS run_qty
FROM aroma.period a, aroma.sales b, aroma.product c
WHERE a.perkey = b.perkey
AND c.prodkey = b.prodkey
AND c.classkey = b.classkey
AND year = 2006
AND month = 'JAN'
AND prod_name = 'Aroma Roma'
GROUP BY date
ORDER BY date;
SUM(SUM(dollars)) OVER(ORDER BY date ROWS UNBOUNDED PRECEDING)
OVER = OLAP aggregation function
MOVING Average = moyenne sur quelques jours pour faire une courbe de la moyenne (moins en dents de scie)
SELECT date, SUM(dollars) AS total_dollars,
SUM(SUM(dollars)) OVER(PARTITION BY week ORDER BY date
ROWS UNBOUNDED PRECEDING) AS run_dollars,
SUM(quantity) AS total_qty,
SUM(SUM(quantity)) OVER(PARTITION BY week ORDER BY date
ROWS UNBOUNDED PRECEDING) AS run_qty
FROM aroma.period a, aroma.sales b, aroma.product c
WHERE a.perkey = b.perkey
AND c.prodkey = b.prodkey
AND c.classkey = b.classkey
AND year = 2006
AND month = 'JAN'
AND prod_name = 'Aroma Roma'
GROUP BY week, date
ORDER BY week, date;
PARTITION BY week = Faire la somme mais par semaine
SELECT date, SUM(dollars) AS total_dollars,
SUM(SUM(dollars)) OVER (PARTITION BY week ORDER BY date
ROWS UNBOUNDED PRECEDING) AS run_dollars,
SUM(quantity) AS total_qty,
SUM(SUM(quantity)) OVER(PARTITION BY week ORDER BY date
ROWS UNBOUNDED PRECEDING) AS run_qty, week
FROM aroma.period a, aroma.sales b, aroma.product c
WHERE a.perkey = b.perkey
AND c.prodkey = b.prodkey
AND c.classkey = b.classkey
AND year = 2006
AND month = 'JAN'
AND prod_name = 'Aroma Roma'
GROUP BY week, date
ORDER BY week, date;
SELECT prod_name, SUM(dollars) AS total_sales, SUM(quantity) AS total_qty,
DEC(sum(dollars)/sum(quantity), 7, 2) AS price
FROM aroma.product a, aroma.sales b, aroma.period c
WHERE a.prodkey = b.prodkey
AND a.classkey = b.classkey
AND c.perkey = b.perkey
AND year = 2004
GROUP BY prod_name
ORDER BY price;
dec(sum(dollars)/sum(quantity), 7, 2) AS price
SELECT t1.date, sales_cume_west, sales_cume_south,
sales_cume_west - sales_cume_south AS west_vs_south
FROM
(SELECT date, SUM(dollars) AS total_sales,
SUM(SUM(dollars)) OVER(ORDER BY date
ROWS UNBOUNDED PRECEDING) AS sales_cume_west
FROM aroma.market a,
aroma.store b,
aroma.sales c,
aroma.period d
WHERE a.mktkey = b.mktkey
AND b.storekey = c.storekey
AND d.perkey = c.perkey
AND year = 2006
AND month = 'MAR'
AND region = 'West'
GROUP BY date) AS t1
JOIN
(SELECT date, SUM(dollars) AS total_sales,
SUM(SUM(dollars)) OVER(ORDER BY date ROWS UNBOUNDED PRECEDING) AS sales_cume_south
FROM aroma.market a,
aroma.store b,
aroma.sales c,
aroma.period d
WHERE a.mktkey = b.mktkey
AND b.storekey = c.storekey
AND d.perkey = c.perkey
AND year = 2006
AND month = 'MAR'
AND region = 'South'
GROUP BY date) AS t2
ON t1.date = t2.date
ORDER BY date;
SELECT city, week, SUM(dollars) AS sales,
DEC(AVG(SUM(dollars)) OVER(partition by city
ORDER BY city, week ROWS 2 PRECEDING),7,2) AS mov_avg,
SUM(SUM(dollars)) OVER(PARTITION BY city
ORDER BY week ROWS unbounded PRECEDING) AS run_sales
FROM aroma.store a,
aroma.sales b,
aroma.period c
WHERE a.storekey = b.storekey
AND c.perkey = b.perkey
AND qtr = 'Q3_05'
AND city IN ('San Jose', 'Miami')
GROUP BY city, week;
ROWS n PRECEDING (ou on peut aussi dire : FOLLOWING )
SELECT date, SUM(quantity) AS day_qty,
DEC(SUM(SUM(quantity)) OVER(ORDER BY date
ROWS 6 PRECEDING),7,2) AS mov_sum
FROM aroma.sales a, aroma.period b, aroma.product c
WHERE b.perkey = a.perkey
AND c.classkey = a.classkey
AND c.prodkey = a.prodkey
AND year = 2006
AND month = 'MAR'
AND prod_name = 'Demitasse Ms'
GROUP BY date
ORDER BY date;
REPONSES :
# Total du montant des ventes par catégorie
select c.class_type, sum(s.dollars) as total
from aroma.class c, aroma.sales s, aroma.product p
where c.classkey = p.classkey
and p.prodkey = s.prodkey
and p.classkey = s.classkey
group by c.class_type
order by total desc
# Pourcentage du montant des ventes de chaque catégorie
select c.class_type, dec(100*sum(s.dollars)/(select sum(dollars) from aroma.sales),7,0) as part
from aroma.class c, aroma.sales s, aroma.product p
where c.classkey = p.classkey
and p.prodkey = s.prodkey
and p.classkey = s.classkey
group by c.class_type
order by part desc
# Classement des ventes par classes de produit
select c.class_type, dec(100*sum(s.dollars)/(select sum(dollars) from aroma.sales),7,0) as part, rank() over(order by sum(s.dollars) desc) as rang
from aroma.class c, aroma.sales s, aroma.product p
where c.classkey = p.classkey
and p.prodkey = s.prodkey
and p.classkey = s.classkey
group by c.class_type
order by part desc
# Produit le plus vendu, pourcentage correspondant
select p.prodkey, p.classkey, p.prod_name, p.pkg_type, dec(100*sum(s.dollars)/(select sum(dollars) from aroma.sales),7,0) as part
from aroma.sales s, aroma.product p
where p.prodkey = s.prodkey
and p.classkey = s.classkey
group by p.prodkey, p.classkey, p.prod_name, p.pkg_type
order by part desc
# Montant des ventes des produits par semaine
select p.prodkey, p.classkey, p.prod_name, p.pkg_type, t.week, sum(s.dollars) as montant
from aroma.sales s, aroma.product p, aroma.period t
where p.prodkey = s.prodkey
and p.classkey = s.classkey
and s.perkey = t.perkey
group by p.prodkey, p.classkey, p.prod_name, p.pkg_type,t.week
order by t.week
# Classement des produits par montant des ventes décroissant pour chaque semaine
select p.prodkey, p.classkey, p.prod_name, p.pkg_type, t.week, sum(s.dollars) as montant, rank() over(partition by week order by week, sum(s.dollars) desc) as rang
from aroma.sales s, aroma.product p, aroma.period t
where p.prodkey = s.prodkey
and p.classkey = s.classkey
and s.perkey = t.perkey
group by p.prodkey, p.classkey, p.prod_name, p.pkg_type,t.week
order by t.week, rang
# Classement moyen des produits en vente hebdomadaire
select t.prodkey, t.classkey, t.prod_name, t.pkg_type, avg(rang) as rang_moyen
from (select p.prodkey, p.classkey, p.prod_name, p.pkg_type, t.week, sum(s.dollars) as montant, rank() over(partition by week order by week, sum(s.dollars) desc) as rang
from aroma.sales s, aroma.product p, aroma.period t
where p.prodkey = s.prodkey
and p.classkey = s.classkey
and s.perkey = t.perkey
group by p.prodkey, p.classkey, p.prod_name, p.pkg_type,t.week
order by t.week, rang) as t(prodkey,classkey,prod_name,pkg_type,week,montant,rang)
group by t.prodkey,t.classkey, t.prod_name, t.pkg_type
# Montant moyen des ventes par jour de la semaine
select ti.day, avg(ti.montant) as moyenne
from (select t.day,sum(dollars)
from aroma.sales s, aroma.period t
where s.perkey = t.perkey
group by t.perkey,t.day) as ti(day,montant)
group by ti.day
order by moyenne desc