简体   繁体   English

带子查询的SQL内部联接带条件返回差值

[英]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. 我正在尝试获取两个表,一个表包含2016年数据,一个表包含2015年数据,并减去每列中的单元格以在新表中仅显示大于或等于10,000的差,四舍五入到最接近的第100位。

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. 我无法添加任何其他条件来过滤结果以显示> = 10000或四舍五入到第100位。

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. 经过研究,看来我的JOIN需要一个子查询来显示我想要的内容,但是我已经搞砸了几个小时,添加子项时似乎无法显示任何内容。 Any assistance would be great. 任何帮助都会很棒。 Here is what I have that works without the >= 10000 and rounding: 这是我没有> = 10000和四舍五入的情况:

     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) 看看下面的查询可能会有所帮助(使用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: 如果希望单元格显示空白而不是值,请在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 严格来说,else null是不必要的,我只是为了您的学习利益而输入

If you want to only show rows where the difference is greater than ten k put this in on the end of your query: 如果只想显示相差大于10 k的行,则在查询末尾输入:

 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. 如果只想显示所有年份都超过10k的行,请为其他年份添加类似的过滤器,并用AND分隔。 If you want to show rows where any year was over ten k, separate them with OR 如果要显示任何一年超过10k的行,请使用OR分隔行

To round values to the nearest 100 (ie 12345 becomes 12300) I believe you would use 将值四舍五入到最接近的100(即12345变为12300),我相信您会使用

ROUND(12345,-2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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