TSQL可以使用命名行進行多列拆分嗎?

[英]TSQL Multiple column unpivot with named rows possible?


I know there are several unpivot / cross apply discussions here but I was not able to find any discussion that covers my problem. What I've got so far is the following:

我知道這里有幾個unpivot / cross apply討論,但我無法找到任何涵蓋我的問題的討論。到目前為止我得到的是以下內容:

SELECT Perc, Salary
FROM (
    SELECT jobid, Salary_10 AS Perc10, Salary_25 AS Perc25, [Salary_Median] AS Median
    FROM vCalculatedView
    WHERE JobID = '1'
    GROUP BY JobID, SourceID, Salary_10, Salary_25, [Salary_Median]
) a
UNPIVOT (
    Salary FOR Perc IN (Perc10, Perc25, Median)
) AS calc1

Now, what I would like is to add several other columns, eg. one named Bonus which I also want to put in Perc10, Perc25 and Median Rows.

現在,我想要添加其他幾個列,例如。一個名為Bonus,我也想把它放在Perc10,Perc25和Median Rows中。

As an alternative, I also made a query with cross apply, but here, it seems as if you can not "force" sort the rows like you can with unpivot. In other words, I can not have a custom sort, but only a sort that is according to a number within the table, if I am correct? At least, here I do get the result like I wish to have, but the rows are in a wrong order and I do not have the rows names like Perc10 etc. which would be nice.

作為替代方案,我也使用交叉應用進行了查詢,但是在這里,似乎你不能像強制轉換那樣“強制”對行進行排序。換句話說,我不能有一個自定義排序,但只有一個根據表中的數字排序,如果我是正確的?至少,在這里我得到的結果就像我希望的那樣,但是行的順序錯誤而且我沒有Perc10等行名稱,這樣會很好。

SELECT crossapplied.Salary,
       crossapplied.Bonus
FROM vCalculatedView v
CROSS APPLY (
    VALUES
          (Salary_10, Bonus_10)
        , (Salary_25, Bonus_25)
        , (Salary_Median, Bonus_Median)
) crossapplied (Salary, Bonus)
WHERE JobID = '1'
GROUP BY crossapplied.Salary,
         crossapplied.Bonus

Perc stands for Percentile here.

Perc在這里代表Percentile。

Output is intended to be something like this:

輸出意圖是這樣的:

+--------------+---------+-------+
| Calculation  | Salary  | Bonus |
+--------------+---------+-------+
| Perc10       |      25 |     5 |
| Perc25       |      35 |    10 |
| Median       |      27 |     8 |
+--------------+---------+-------+

Do I miss something or did I something wrong? I'm using MSSQL 2014, output is going into SSRS. Thanks a lot for any hint in advance!

我錯過了什么或者我做錯了什么嗎?我正在使用MSSQL 2014,輸出正在進入SSRS。非常感謝任何提前提示!

Edit for clarification: The Unpivot-Method gives the following output:

編輯說明:Unpivot-Method提供以下輸出:

    +--------------+---------+
    | Calculation  | Salary  |
    +--------------+---------+
    | Perc10       |      25 |
    | Perc25       |      35 |
    | Median       |      27 |
    +--------------+---------+

so it lacks the column "Bonus" here.

所以它缺少“Bonus”欄目。

The Cross-Apply-Method gives the following output:

Cross-Apply-Method提供以下輸出:

+---------+-------+
| Salary  | Bonus |
+---------+-------+
|      35 |    10 |
|      25 |     5 |
|      27 |     8 |
+---------+-------+

So if you compare it to the intended output, you'll notice that the column "Calculation" is missing and the row sorting is wrong (note that the line 25 | 5 is in the second row instead of the first).

因此,如果將其與預期輸出進行比較,您會注意到“計算”列缺失且行排序錯誤(請注意,第25行是第2行而不是第1行)。

Edit 2: View's definition and sample data: The view basically just adds computed columns of the table. In the table, I've got Columns like Salary and Bonus for each JobID. The View then just computes the percentiles like this:

編輯2:視圖的定義和示例數據:視圖基本上只是添加了表的計算列。在表格中,我為每個JobID提供了薪水和獎金等列。 View然后只計算這樣的百分位數:

Select 
    Percentile_Cont(0.1)
    within group (order by Salary)
    over (partition by jobID) as Salary_10,

    Percentile_Cont(0.25)
    within group (order by Salary)
    over (partition by jobID) as Salary_25
from Tabelle

So the output is like:

所以輸出如下:

+----+-------+---------+-----------+-----------+
| ID | JobID | Salary  | Salary_10 | Salary_25 |
+----+-------+---------+-----------+-----------+
|  1 |     1 |     100 |        60 |        70 |
|  2 |     1 |     100 |        60 |        70 |
|  3 |     2 |     150 |        88 |       130 |
|  4 |     3 |      70 |        40 |        55 |
+----+-------+---------+-----------+-----------+

In the end, the view will be parameterized in a stored procedure.

最后,視圖將在存儲過程中進行參數化。

1 个解决方案

#1


1  

Might this be your approach?

這可能是你的方法嗎?

After your edits I understand, that your solution with CROSS APPLY would comes back with the right data, but not in the correct output. You can add constant values to your VALUES and do the sorting in a wrapper SELECT:

在您的編輯之后我明白,您使用CROSS APPLY的解決方案將返回正確的數據,但不會返回正確的輸出。您可以向VALUES添加常量值,並在包裝​​器SELECT中進行排序:

SELECT wrapped.Calculation,
       wrapped.Salary,
       wrapped.Bonus
FROM
(
    SELECT crossapplied.*
    FROM vCalculatedView v
    CROSS APPLY (
        VALUES
              (1,'Perc10',Salary_10, Bonus_10)
            , (2,'Perc25',Salary_25, Bonus_25)
            , (3,'Median',Salary_Median, Bonus_Median)
    ) crossapplied (SortOrder,Calculation,Salary, Bonus)
    WHERE JobID = '1'
    GROUP BY crossapplied.SortOrder,
             crossapplied.Calculation,
             crossapplied.Salary,
             crossapplied.Bonus
) AS wrapped
ORDER BY wrapped.SortOrder

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/03/22/72f4c62625f6452838cd4f38cdc8b2cc.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com