在复杂的Select语句中将列显示为行 - sql

时间:2016-03-08 04:52:59

标签: sql-server select cross-apply

我有两个表(一个基表和一个数据表),我使用OUTER APPLY合并来获取结果。

tblBase看起来像这样:

+------+------+
| IDnu | Name |
+------+------+
|  1   | abc  |
|  2   | cde  |
|  3   | efg  |
|  4   | rfl  |
+------+------+

tblData就是这样:

+------+--------+--------+--------+-------------+
| IDNu | Price1 | Price2 | Price3 | ProductType |
+------+--------+--------+--------+-------------+
|    1 |     10 |     15 |     20 | Old         |
|    2 |     10 |     20 |     30 | Refurbished |
|    3 |     20 |     30 |     40 | New         |
|    1 |     20 |     15 |     20 | New         |
|    2 |     20 |     10 |     30 | Old         |
+------+--------+--------+--------+-------------+

根据少数条件计算tblData的当前查询如下:

Select IDNu, Name, t2.PNew, t2.POld FROM tblBase as t1
OUTER APPLY
(
SELECT
  SUM (CASE WHEN ProductType = 'New' THEN Price1 + Price2 ELSE 0 END) AS PNew, 
  SUM (CASE WHEN ProductType = 'Old' THEN Price2 + Price3 ELSE 0 END) AS POld
FROM tblData
WHERE IDNu = t1.IDNu
GROUP BY IDNu
) t2

以上查询结果为:

+------+------------+------+------+
| IDNu |    Name    | PNew | POld |
+------+------------+------+------+
|    1 | abc        | 35   | 35   |
|    2 | cde        | 0    | 40   |
|    3 | efg        | 50   | 0    |
|    4 | rfl        | NULL | NULL |
+------+------------+------+------+

现在我的问题是,不是在两列中显示PNew和POld,而是在行中显示它们?像这样:

+------+------------+-------------+-------+
| IDNu |    Name    | ProductType | Price |
+------+------------+-------------+-------+
|    1 | abc        | PNew        | 35    |
|    2 | cde        | PNew        | 0     |
|    3 | efg        | PNew        | 50    |
|    4 | rfl        | PNew        | NULL  |
|    1 | abc        | POld        | 35    |
|    2 | cde        | POld        | 40    |
|    3 | efg        | POld        | 0     |
|    4 | rfl        | POld        | NULL  |
+------+------------+-------------+-------+

3 个答案:

答案 0 :(得分:1)

select b.IDNu, b.Name, pt.ProductType, d.Price
from
    tblBase as b cross join (select 'New' ProductType union all select 'Old') pt
    left outer join
    (
        select
            ProductType,
            sum(case ProductType
                    when 'New' then Price1 + Price2
                    when 'Old' then Price2 + Price3 end) as Price
        from tblData
        where ProductType in ('New', 'Old') /* not strictly necessary but maybe faster */
        group by IDNu, ProductType
    ) d
        on d.IDNu = p.IDNu and d.ProductType = pt.ProductType
order by b.IDNu, pt.ProductType

或......

select
    b.IDNu, b.Name, pt.ProductType,
    sum(case pt.ProductType
            when 'New' then d.Price1 + d.Price2
            when 'Old' then d.Price2 + d.Price3 end) as Price
from
    tblBase as b
    cross join
    (select 'New' ProductType union all select 'Old') pt
    left outer join
    tblData d on d.IDNu = p.IDNu and d.ProductType = pt.ProductType
group by b.IDNu, pt.ProductType
order by b.IDNu, pt.ProductType

在您的输出中,您混合了一些零和空输出,其中没有数据。当然,翻译这些空值的常用方法是使用coalesce()。你也有" PNew"和" POld"作为你的输出值,但我不确定这是故意的。一个简单的case将处理输出select子句中的那些。

答案 1 :(得分:1)

你可以试试这个:

def get_wins():
    data = raw_input('Enter the number of wins for each day, separated by spaces: ')

    wins = [int(win) for win in data.split() if win.isdigit()]
    return sum(wins)

答案 2 :(得分:1)

简单的方法是在外部应用表中取消隐藏:

Select 
    IDNu,Name,t2.ProductType,t2.Price
FROM 
    tblBase as t1
    OUTER APPLY (
        SELECT ProductType='New',SUM(Price1+Price2) AS Price
        FROM tblData
        WHERE IDNu=t1.IDNu AND ProductType='New'
        UNION ALL
        SELECT ProductType='Old',SUM(Price2+Price3) AS Price
        FROM tblData
        WHERE IDNu=t1.IDNu AND ProductType='Old'
    ) t2