Question

I've been working this query for a couple of days and can't seem to get it right :/ I'm taking quarterly transaction sums and adding them to get the yearly sum. Then I'm calculating the variance year over year, from this year and last. Lastly, for each account, I'm counting the total that whose variance is LTE -1, GTE 1, and between -1 AND 1. To work with our graphics software I need this as three distinct records all from a single, compound SELECT statement. Any advice would be appreciated. Thanks in advance!

Here is the SQL to print as a single row. Receipts_Total is a positive integer, YR is a year, 2012 or 2013.

SELECT 
    Year,
    COUNT(NEGATIVES) AS SUM_OF_NEGATIVES
    , COUNT(POSITIVES) AS SUM_OF_POSITIVES
    , COUNT(FLATS) AS SUM_OF_FLATS
FROM(
    SELECT 
        Year,
        CASE WHEN (Percentage <= -1) THEN 'NEGATIVE' END AS NEGATIVES 
        , CASE WHEN (Percentage >= 1) THEN 'POSITIVE' END AS POSITIVES
        , CASE WHEN ((Percentage > -1) AND (Percentage < 1)) THEN 'FLAT' END AS FLATS
        , CASE WHEN (1 = 1) THEN 4 END AS TOTAL_COUNT
    FROM(
        SELECT 
            '2013' AS Year,
            ((((1.0 * SUM(QR_Dom1.Receipts_Total)) - (1.0 * SUM(QR_Dom.Receipts_Total))) / (1.0 * SUM(QR_Dom.Receipts_Total))) * 100) AS Percentage 

        FROM  QR_Dom INNER JOIN Contacts ON QR_Dom.ID = Contacts.ID INNER JOIN QR_Dom QR_Dom1 ON Contacts.ID = QR_Dom1.ID 

        WHERE ((QR_Dom.YR LIKE '%2012%' AND QR_Dom1.YR LIKE '%2013%')) 

        GROUP BY Contacts.ID, Contacts.COMPANY 

        HAVING (Sum(QR_Dom.Receipts_Total) <> 0 AND Sum(QR_Dom1.Receipts_Total) <> 0)

    ) AS T1

) AS T2
GROUP BY YEAR

Sample QR_Dom table:

1111    2012    5000
2222    2012    5000
3333    2012    5000
4444    2012    5000
5555    2012    5000
6666    2012    5000
7777    2012    5000
8888    2012    5000
9999    2012    5000
1212    2012    5000
1111    2013    4000
2222    2013    3000
3333    2013    4999
4444    2013    5001
5555    2013    6000
6666    2013    7000
7777    2013    8000
8888    2013    2000
9999    2013    1000
1212    2013    1500

Sample Contacts table:

1111    Nike
2222    Beer
3333    Phone
4444    Car
5555    Yankees
6666    Cat
7777    Cereal
8888    DVD
9999    Toe
1212    Globe

Expected Output:

NEGATIVE         2013    5
FLAT             2013    2
POSITIVE         2013    3
Était-ce utile?

La solution

Can't test this on SQL Server 2005, but common table expression should simplify things somewhat;

WITH cte AS (
  SELECT(b.receipts_total*1.0 - a.receipts_total*1.0) / (a.receipts_total*1.0) * 100 perc
  FROM qr_dom a JOIN qr_dom b ON a.id = b.id AND a.yr = 2012 AND b.yr = 2013
)
SELECT
  2013 AS year,
  SUM(CASE WHEN perc >  1 THEN 1 ELSE 0 END) positive,
  SUM(CASE WHEN perc < -1 THEN 1 ELSE 0 END) negative,
  SUM(CASE WHEN perc <= 1 AND perc >= -1 THEN 1 ELSE 0 END) flat
FROM cte

An SQLfiddle to test with.

If you really need the results in separate rows, you could rewrite the query to use UNION ALL between 3 separate queries against the table expression.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top