Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

SUM from two tables, then sort by publisher and country

$
0
0
Good morning people! 

I have two different sales tables and i want to SUM the quantity and FEE for storeX and storeY from book_sales(isa), with quantity from all sales in the financial_report(xo). Im having trouble with both, and i dont know which JOIN i should use.

The quantity calculation does not give any results and the fee calculation gives a number that is way to high. What is wrong here?

ALTER PROCEDURE [dbo].[Report] @startDate VARCHAR(10), 
                                   @endDate   VARCHAR(10) 
AS 
    SELECT BB.name  AS 'Publisher', 
           Sum(CASE 
                 WHEN isa.report_source = 'storeX' 
                      or isa.report_source = 'storeY' THEN 
                 isa.quantity * xo.quantity 
               END) AS 'Total quantity', 
           Sum(CASE 
                 WHEN isa.sales_price > 69 
                      AND isa.report_source = 'storeX' THEN 
                 6 * 1.25 * isa.quantity 
                 WHEN isa.sales_price <= 69 
                      AND isa.report_source = 'storeX' THEN 
                 3 * 1.25 * isa.quantity 
                 WHEN isa.sales_price <= 19 
                      AND isa.report_source = 'storeX' THEN 
                 1 * 1.25 * isa.quantity 
                 WHEN isa.sales_price > 69 
                      AND isa.report_source = 'storeY' THEN 
                 6 * 1.25 * isa.quantity 
                 WHEN isa.sales_price <= 69 
                      AND isa.report_source = 'storeY' THEN 
                 3 * 1.25 * isa.quantity 
                 WHEN isa.sales_price <= 19 
                      AND isa.report_source = 'storeY' THEN 
                 1 * 1.25 * isa.quantity 
                 WHEN xo.sales_price > 69 
                      AND bb.country = 'NOR' THEN 6 * 1.25 * xo.quantity 
                 WHEN xo.sales_price <= 69 
                      AND bb.country = 'NOR' THEN 3 * 1.25 * xo.quantity 
                 WHEN xo.sales_price > 69 
                      AND bb.country <> 'NOR' THEN 6 * xo.quantity 
                 WHEN xo.sales_price <= 69 
                      AND bb.country <> 'NOR' THEN 3 * xo.quantity 
               END) AS 'Fee inc VAT(tot)' 
    FROM   book_sales AS isa 
           INNER JOIN store AS BV 
                   ON bv.store_id = isa.store_id 
           INNER JOIN financial_report AS xo 
                   ON xo.identifiers = isa.identifiers 
           LEFT OUTER JOIN publisher AS BB 
                        ON bb.publisher_id = bk.publisher_id 
    WHERE  isa.sales_date >= CONVERT(DATETIME, @startDate, 20) 
           AND isa.sales_date < Dateadd(day, 1, ( CONVERT(DATETIME, @endDate, 20 
                                                  ) )) 
    GROUP  BY bb.name, 
              bb.country 


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>