TSQL -> Sales Reports using TSQL syntax. Part 1

The mission is to analize sales information coming from two tables: Invoices and Invoices-Lines. First table include information from Invoice header (like Issue Date, Total Sum, CustomerID ...), Invoice Lines table include list of Items which was selled (Item Name, Price, Quantity ...). Table "Invoices" is referenced to table "InvLines" by column "DocKey". Type of reference is one-to-many: one record in "Invoices" reference to many records in "InvLines".

Invoices:
DocNoDocKeyDocDateDocTotalCustomerNameSlpCode...
110014/01/992375Angelina Alba11...
210122/01/991291.5Pestiana Oliviera11...
310214/02/9910200Enrice Durance10...
410310/03/99250Jessica Simpson8...

InvLines:
DocKeyItemCodeItemNameQnt...
1000036876Memorex 8.5Gb20...
10077003Panasonic DMR-E3...
1015003258Philips DVP64245...


Four sales reports:

1. Select invoices that sold any kind of "Sony" products :
SELECT a.DocNo, a.CustomerID, a.DocTotal, a.DocDate
FROM Invoice AS a INNER JOIN InvLines AS b ON a.DocKey=b.DocKey
WHERE b.ItemName LIKE '%Sony%';

/* As you see join is made here by "DocKey" field */


2. Now your boss want to get Montly Sales of "Sony" products in 1999:
SELECT (CASE Month(a.DocDate)
  WHEN 1 THEN 'JAN' WHEN 2 THEN 'FEB' WHEN 3 THEN 'MAR'
  WHEN 4 THEN 'APR' WHEN 5 THEN 'MAY' WHEN 6 THEN 'JUN'
  WHEN 7 THEN 'JUL' WHEN 8 THEN 'AUG' WHEN 9 THEN 'SEP'
  WHEN 10 THEN 'OCT' WHEN 11 THEN 'NOV' WHEN 12 THEN 'DEC'
ELSE 'NNN' END) AS The_Month, SUM(a.DocTotal) AS Income
FROM (SELECT a.DocNo, a.CustomerID, a.DocTotal, a.DocDate
FROM Invoice AS a INNER JOIN InvLines AS b ON a.DocKey = b.DocKey
WHERE Year(a.DocDate) = 1999) AS a
GROUP BY Month(a.DocDate);
/* We used here subquery inside brackets. Then groupe result set by Month. Monthes numbers converted to names using CASE function */


The result will be like this:
The_MonthIncome
JAN7993.1
FEB20775
MAR750
......


3. Now suppose that the company launched two advertizing projects:
  1 - global TV advertizing
  2 - community radio

Your management wants to monitor the eficiency of these two projects. Every invoice issued was attributed to relevat project; field "projectid". Report have to retrieve monthly sales of 1999 separated by project:
SELECT b.project_title, Month(a.DocDate) AS The_Month,
SUM(a.DocTotal) AS Income
FROM (SELECT a.projectid, a.DocNo, a.CustomerID,
a.DocTotal, a.DocDate
FROM Invoice AS a INNER JOIN InvLines AS b ON a.DocKey = b.DocKey
WHERE Year(a.DocDate) = 1999) AS a INNER JOIN AdvProjects AS b
ON a.projectid = b.projectid
GROUP BY b.project_title, Month(a.DocDate)
ORDER BY b.project_title, Month(a.DocDate);


The result will be like this:
project_titleThe_MonthIncome
community radio11951.6
community radio2375
community radio3250
.........
global TV advertizing16041.5
global TV advertizing220400
global TV advertizing3500
.........


3. The same query can be converted to TSQL matrix using Group By + CASE function:
SELECT c.project_title,
  SUM(CASE c.Mon WHEN 1 THEN c.Income ELSE 0 END) AS Jan,
  SUM(CASE c.Mon WHEN 2 THEN c.Income ELSE 0 END) AS Feb,
  SUM(CASE c.Mon WHEN 3 THEN c.Income ELSE 0 END) AS Mar,
  SUM(CASE c.Mon WHEN 4 THEN c.Income ELSE 0 END) AS Apr,
  SUM(CASE c.Mon WHEN 5 THEN c.Income ELSE 0 END) AS May,
  SUM(CASE c.Mon WHEN 6 THEN c.Income ELSE 0 END) AS Jun,
  SUM(CASE c.Mon WHEN 7 THEN c.Income ELSE 0 END) AS Jul,
  SUM(CASE c.Mon WHEN 8 THEN c.Income ELSE 0 END) AS Aug,
  SUM(CASE c.Mon WHEN 9 THEN c.Income ELSE 0 END) AS Sep,
  SUM(CASE c.Mon WHEN 10 THEN c.Income ELSE 0 END) AS Oct,
  SUM(CASE c.Mon WHEN 11 THEN c.Income ELSE 0 END) AS Nov,
  SUM(CASE c.Mon WHEN 12 THEN c.Income ELSE 0 END) AS Dec
FROM
  (SELECT Month(a.DocDate) AS Mon,
  b.project_title, SUM(a.DocTotal) AS Income
  FROM
  (SELECT a.projectid, a.DocTotal, a.DocDate
  FROM Invoice AS a INNER JOIN InvLines AS b     ON a.DocKey = b.DocKey
  WHERE Year(a.DocDate) = 1999) AS a INNER JOIN AdvProjects AS b
  ON a.projectid = b.projectid
  GROUP BY Month(a.DocDate), b.project_title) AS c
GROUP BY c.project_title


The result will be like this:
project_titleJanFebMarAprMayJunJulAugSepOctNovDec
community radio1951.637525035006900183746175200467.51250375  
global TV advertizing6041.520400500     400       19082.5   12500


sqlexamples.info