繁体   English   中英

如何在不使用插入查询的情况下在sql中插入虚拟行?

[英]how to insert dummy rows in sql without using insert query?

我有以下查询

select '2016' as yr,warehouse as product,sum(answertext) p1 ,
round((sum(abc)/(select sum(abc) from [table 1])*100),2) p2
 from [table 1]
group by warehouse
order by p1 desc
limit 10 

这显示以下输出

yr        product            prodnum    prodper

2016     Harness Extra      94427        10.4
2016     Lumax              54534         9.6
.. 
... 
..
...
..
..
..
2016   Capreno              534533        4.6

现在我必须手动为2015年和2014年的虚拟值插入prdonum和prodper的自定义值,我在另一个单词表中如何在单个查询中执行此操作? 任何帮助都会很棒? 它应该采用以下格式

    yr      product              prodnum      prodper

    2016   Harness Extra         94427       10.4  // from table
    2015   Harness Extra         32453       5.7  <- custom value to be inserted
    2014   Harness Extra         21215       2.3  <- custom value to be inserted
    2016   Lumax                 78994       8.76  // from table
    2015   Lumax                 43435       4.77 <- custom value to be inserted
    2014   Lumax                 15522       3.55 <- custom value to be inserted
    ..
    ..


    ..
<totally 30 rows>

如果你在另一个表中,可以使用union语句?

Select * from 
 (
select '2016' as yr,warehouse as product,sum(answertext) p1 ,
round((sum(abc)/(select sum(abc) from [table 1])*100),2) p2
         from [table 1]
        group by warehouse

UNION ALL            

    select '2015' as yr,warehouse as product,sum(answertext) p1 ,
    round((sum(abc)/(select sum(abc) from [table 1])*100),2) p2
     from [table 2]
    group by warehouse

    UNION ALL

    select '2014' as yr,warehouse as product,sum(answertext) p1 ,
    round((sum(abc)/(select sum(abc) from [table 1])*100),2) p2
     from [table 3]
    group by warehouse
    )a
 order by p1 desc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM