从单行显示两行

时间:2016-04-18 05:34:41

标签: sql

这是给定的表格:

enter image description here

我需要在不创建基于上表的临时表的情况下获取此数据:

enter image description here

我们无法使用临时表。我只需要用sql查询显示数据。

5 个答案:

答案 0 :(得分:0)

试试这个

SELECT company name,'No' as value, clicks  as data
from table1
union all
SELECT company,'Yes', (clicks - impression)
from table1
order by name,val

答案 1 :(得分:0)

您可以使用UNION ALL取消对表格的删除:

SELECT
    company,
    'No' AS val,
    impression AS data
FROM tbl
UNION ALL
SELECT
    company,
    'Yes' AS val,
    clicks - impression AS data
FROM tbl
ORDER BY company, val

答案 2 :(得分:0)

你需要UNION ALL

SELECT company,'No' val, impression as data from your_table
union all
SELECT company,'Yes' val, clicks-impression as data from your_table

答案 3 :(得分:0)

select company as name, 'No' as val, impression as data
from tbl
union
select company as name, 'Yes' as val, clicks - impression as data
from tbl

答案 4 :(得分:0)

SELECT COMPANY AS NAME, 'YES' AS VAL, (CLICKS-IMPRESSIONS) AS DATA
FROM ORIGINAL_TABLE
UNION
SELECT COMPANY AS NAME, 'NO' AS VAL, IMPRESSIONS AS DATA
FROM ORIGINAL_TABLE

如果你想要它们排序:试试这个:

SELECT A.NAME, A.VAL, A.DATA FROM
(SELECT COMPANY AS NAME, 'YES' AS VAL, (CLICKS-IMPRESSIONS) AS DATA
FROM OR_TABLE
UNION
SELECT COMPANY AS NAME, 'NO' AS VAL, IMPRESSIONS AS DATA
FROM OR_TABLE) A
ORDER BY A.NAME ASC;