SQL内部联接w / sub查询返回差异w / criteria

时间:2017-08-07 05:36:41

标签: mysql sql join subquery

我在做什么:

我正在尝试使用两个表,一个包含2016数据,另一个包含2015数据,并减去每列中的单元格以仅显示大于或等于10,000的差异,四舍五入到最接近的第100位,在新表。

问题:

我能够弹出新表格,只显示减法部分的正确数量。我无法添加任何其他条件来过滤结果以显示> = 10000或舍入到第100个点。

经过研究,看起来我的JOIN需要一个子查询才能显示我想要的内容,但是我已经乱了几个小时了,而且当我添加一个sub时,我似乎无法显示它。任何援助都会很棒。这是我的工作,没有> = 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;

2 个答案:

答案 0 :(得分:0)

看看它可能有用的下面的查询(使用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 .......

修改

    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

答案 1 :(得分:0)

如果您希望单元格显示空白而不是值,请在SELECT:

下使用这样的模式
    CASE WHEN `currentdata`.`2015` - `prioryeardata`.`2015` >= 10000 THEN 
    `currentdata`.`2015` - `prioryeardata`.`2015` ELSE NULL END AS '2015_Difference'

严格来说,else null是不必要的,我只是为了你的学习利益而把它放进去

如果您只想显示差异大于10的行,请将其放在查询的末尾:

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

如果您只想显示所有年份超过10 k的行,请为AND分隔其他年份的类似过滤器。如果要显示任何年份超过10 k的行,请用OR

分隔

要将值舍入到最接近的100(即12345变为12300),我相信你会使用

ROUND(12345,-2)