简体   繁体   中英

SQL Inner Join w/ sub query to return difference w/criteria

What am I doing:

I'm attempting to take two tables, one with 2016 data and one with 2015 data, and subtract the cells in each column to display only the differences greater than or equal to 10,000, rounded to the nearest 100th place, in a new table.

The Issue:

I am able to get the new table to pop up with the correct amounts displayed for the subtraction part only. I'm not able to add any additional criteria to filter the results to display the >= 10000 or the rounding to the 100th spot.

After research it looks like my JOIN needs a subquery to display what i would like, but I've been messing around with it for hours now and I can't seem to get it to display anything when I add a sub. Any assistance would be great. Here is what I have that works without the >= 10000 and rounding:

     SELECT
        `prioryeardata`.location,
        `currentdata`.`2010` - `prioryeardata`.`2010` AS '2010_Difference',
        `currentdata`.`2011` - `prioryeardata`.`2011` AS '2011_Difference',
        `currentdata`.`2012` - `prioryeardata`.`2012` AS '2012_Difference',
        `currentdata`.`2013` - `prioryeardata`.`2013` AS '2013_Difference',
        `currentdata`.`2014` - `prioryeardata`.`2014` AS '2014_Difference',
        `currentdata`.`2015` - `prioryeardata`.`2015` AS '2015_Difference'
    FROM `prioryeardata` 
    JOIN `currentdata`
    ON `prioryeardata`.location = `currentdata`.location;

Have a look at the below query it may help (using sql-server)

select location,Round([2010_Difference],3).[2010_Difference],Round([2011_Difference],3)[2011_Difference]
            ,Round([2012_Difference],3)[2012_Difference],Round([2013_Difference],3)[2013_Difference]
            ,Round([2014_Difference],3)[2014_Difference],Round([2015_Difference],3)[2015_Difference] from 
 ( SELECT
    prioryeardata.location,
    currentdata.year2010 - prioryeardata.year2010 AS [2010_Difference],
    currentdata.year2011 - prioryeardata.year2011 AS [2011_Difference],
    currentdata.year2012 - prioryeardata.year2012 AS [2012_Difference],
    currentdata.year2013 - prioryeardata.year2013 AS [2013_Difference],
    currentdata.year2014 - prioryeardata.year2014 AS [2014_Difference],
    currentdata.year2015 - prioryeardata.year2015 AS [2015_Difference]
FROM prioryeardata 
JOIN currentdata
ON prioryeardata.location = currentdata.location

) t where t.[2015_Difference]>=10000  --or .......

Edit

    select location,Round([2010_Difference],3).[2010_Difference],Round([2011_Difference],3)[2011_Difference]
                ,Round([2012_Difference],3)[2012_Difference],Round([2013_Difference],3)[2013_Difference]
                ,Round([2014_Difference],3)[2014_Difference],Round([2015_Difference],3)[2015_Difference]

     from  

      (select t.location
             ,case when [2010_Difference]>10000 then [2010_Difference] Else 0 End as [2010_Difference]
             ,case when [2011_Difference]>10000 then [2011_Difference] Else 0 End as [2011_Difference]
             ,case when [2012_Difference]>10000 then [2012_Difference] Else 0 End as [2012_Difference]
             ,case when [2013_Difference]>10000 then [2013_Difference] Else 0 End as [2013_Difference]
             ,case when [2014_Difference]>10000 then [2014_Difference] Else 0 End as [2014_Difference]
             ,case when [2015_Difference]>10000 then [2015_Difference] Else 0 End as [2015_Difference]

          from        
                 ( SELECT
                    prioryeardata.location,
                    currentdata.year2010 - prioryeardata.year2010 AS [2010_Difference],
                    currentdata.year2011 - prioryeardata.year2011 AS [2011_Difference],
                    currentdata.year2012 - prioryeardata.year2012 AS [2012_Difference],
                    currentdata.year2013 - prioryeardata.year2013 AS [2013_Difference],
                    currentdata.year2014 - prioryeardata.year2014 AS [2014_Difference],
                    currentdata.year2015 - prioryeardata.year2015 AS [2015_Difference]
                FROM prioryeardata 
                JOIN currentdata
                ON prioryeardata.location = currentdata.location

                ) t where t.[2010_Difference]>=10000  or t.[2011_Difference]>=10000 or t.[2012_Difference]>=10000 
                      or t.[2013_Difference]>=10000 or t.[2014_Difference]>=10000 or t.[2015_Difference]>=10000

      )tt

If you want cells to show blank instead of a value, use a pattern like this under your SELECT:

    CASE WHEN `currentdata`.`2015` - `prioryeardata`.`2015` >= 10000 THEN 
    `currentdata`.`2015` - `prioryeardata`.`2015` ELSE NULL END AS '2015_Difference'

strictly speaking the else null is unnecessary, I just put it in for your learning benefit

If you want to only show rows where the difference is greater than ten k put this in on the end of your query:

 WHERE
    `currentdata`.`2015` - `prioryeardata`.`2015` >= 10000

If you want to only show rows where all years were over ten k, add similar filters for other years separated by AND. If you want to show rows where any year was over ten k, separate them with OR

To round values to the nearest 100 (ie 12345 becomes 12300) I believe you would use

ROUND(12345,-2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM