2017-08-07 66 views
0

我在做什麼:SQL內加入W /子查詢返回的區別W /標準

我試圖把兩個表,一個用2016點的數據和一個與2015年的數據,並減去在每個單元列以僅顯示大於或等於10,000的差異,四捨五入到最接近的第100位,在新表格中。

的問題:

我能夠獲得新的表格,表格中只有減法部分顯示正確的金額彈出。我無法添加任何附加條件來過濾結果以顯示> = 10000或舍入到第100個點。

經過研究,它看起來像我的JOIN需要一個子查詢來顯示我想要的,但我已經搞亂它幾個小時了,我似乎無法讓它顯示任何東西,當我添加一個子。任何援助將是偉大的。以下是我有沒有> = 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; 
+3

添加樣本數據和預期的結果.. –

+0

只需使用您現有的查詢作爲子查詢,然後附加一個where子句,即SELECT位置,.. FROM(SELECT ...),其中ABS(2015_Difference)> 10000.您可以添加舍入相同時間,或在子查詢 –

+0

你是什麼意思,由> = 10000和「四捨五入到第100個點」? – Squirrel

回答

0

看一看下面的查詢,它可以幫助(使用SQL服務器)

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 
+0

謝謝你這是有幫助的。圓的部分工作得很好,我只是有WHERE數據填充正確的問題。當我使用OR命令時,它仍然給我的值不符合> = 10000條件。 – Delbudge

+0

如果你需要所有差異超過10000,同時使用「和」 –

+0

我已編輯查詢用例,如果差異不超過10000 –

0

如果您希望單元格顯示空白而不是值,請在您的選擇下使用下面的圖案:

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

嚴格地說否則空是不必要的,我只是把它放在你的學習效益

如果你想只顯示行,其中的差別大於十ķ把這個在您的查詢的末尾:

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

如果您只想顯示所有年份均超過10 k的行,請在由AND隔開的其他年份中添加類似的過濾器。如果你想要顯示的行其中任何一年是十幾K,他們有分開的或

爲了圓值到最近的100(即12345變爲12300),我相信你會用

ROUND(12345,-2)