2017-05-01 77 views
0

我對我的查詢沒有什麼問題,我想在單選語句中顯示2 union all,但只有union all正在工作。來自同一表-H2數據庫的兩個聯盟

工作查詢:

select '' as Total, product, price,sellprice, 
openingstock as openingStock, openingstock*price as op_value, 
receipts as receipts, receipts*price as re_value, 
totalstock as totalstock, totalstock*price as ts_value, 
sales as sales, sales*sellprice as s_value, 
return as returns,return*sellprice as rt_value, 
closingstock as closingstock, closingstock*price as cl_value 
from purchase_table where date between '2017-04-01' and '2017-05-30' 
union all 
select orgname , 'Total', sum(price),sum(sellprice), 
sum(openingstock) as openingStock, sum(openingstock*price) as op_value, 
sum(receipts) as receipts, sum(receipts*price) as re_value, 
sum(totalstock) as totalstock, sum(totalstock*price) as ts_value, 
sum(sales) as sales, sum(sales*sellprice) as s_value, 
sum(return) as returns,sum(return*sellprice) as rt_value, 
sum(closingstock) as closingstock, sum(closingstock*price) as cl_value 
from purchase_table where date between '2017-04-01' and '2017-05-30' group by orgname order by closingstock asc 

當我添加另一個聯盟所有它不工作

select '' as Total, product, price,sellprice, 
openingstock as openingStock, openingstock*price as op_value, 
receipts as receipts, receipts*price as re_value, 
totalstock as totalstock, totalstock*price as ts_value, 
sales as sales, sales*sellprice as s_value, 
return as returns,return*sellprice as rt_value, 
closingstock as closingstock, closingstock*price as cl_value 
from purchase_table where date between '2017-04-01' and '2017-05-30' 
union all 
select orgname , 'Total', sum(price),sum(sellprice), 
sum(openingstock) as openingStock, sum(openingstock*price) as op_value, 
sum(receipts) as receipts, sum(receipts*price) as re_value, 
sum(totalstock) as totalstock, sum(totalstock*price) as ts_value, 
sum(sales) as sales, sum(sales*sellprice) as s_value, 
sum(return) as returns,sum(return*sellprice) as rt_value, 
sum(closingstock) as closingstock, sum(closingstock*price) as cl_value 
from purchase_table where date between '2017-04-01' and '2017-05-30' group by orgname order by closingstock asc 

union all 
select '' , '', sum(price),sum(sellprice), 
sum(openingstock) as openingStock, sum(openingstock*price) as op_value, 
sum(receipts) as receipts, sum(receipts*price) as re_value, 
sum(totalstock) as totalstock, sum(totalstock*price) as ts_value, 
sum(sales) as sales, sum(sales*sellprice) as s_value, 
sum(return) as returns,sum(return*sellprice) as rt_value, 
sum(closingstock) as closingstock, sum(closingstock*price) as cl_value 
from purchase_table 

我究竟做錯了什麼,請由第二幫助

回答

0

刪除訂單,並從別名第二個第三選擇

select 
    '' as Total 
    , product 
    , price 
    , sellprice 
    , openingstock as openingStock 
    , openingstock*price as op_value 
    , receipts as receipts 
    , receipts*price as re_value 
    , totalstock as totalstock 
    , totalstock*price as ts_value 
    , sales as sales 
    , sales*sellprice as s_value 
    , return as returns 
    , return*sellprice as rt_value 
    , closingstock as closingstock 
    , closingstock*price as cl_value 
from purchase_table 
where date between '2017-04-01' and '2017-05-30' 

union all 

select 
    orgname 
    , 'Total' 
    , sum(price) 
    , sum(sellprice) 
    , sum(openingstock) 
    , sum(openingstock*price) 
    , sum(receipts) 
    , sum(receipts*price) 
    , sum(totalstock) 
    , sum(totalstock*price) 
    , sum(sales) 
    , sum(sales*sellprice) 
    , sum(return) 
    , sum(return*sellprice) 
    , sum(closingstock) 
    , sum(closingstock*price)  

from purchase_table 
where date between '2017-04-01' and '2017-05-30' 
group by orgname 


union all 

select 
    orgname 
    , 'Total' 
    , sum(price) 
    , sum(sellprice) 
    , sum(openingstock) 
    , sum(openingstock*price) 
    , sum(receipts) 
    , sum(receipts*price) 
    , sum(totalstock) 
    , sum(totalstock*price) 
    , sum(sales) 
    , sum(sales*sellprice) 
    , sum(return) 
    , sum(return*sellprice) 
    , sum(closingstock) 
    , sum(closingstock*price) 
from purchase_table 

,如果你需要的二階比使用子查詢

select 
     '' as Total 
     , product 
     , price 
     , sellprice 
     , openingstock as openingStock 
     , openingstock*price as op_value 
     , receipts as receipts 
     , receipts*price as re_value 
     , totalstock as totalstock 
     , totalstock*price as ts_value 
     , sales as sales 
     , sales*sellprice as s_value 
     , return as returns 
     , return*sellprice as rt_value 
     , closingstock as closingstock 
     , closingstock*price as cl_value 
    from purchase_table 
    where date between '2017-04-01' and '2017-05-30' 

    union all 
    select * from 
    (select 
     orgname 
     , 'Total' 
     , sum(price) 
     , sum(sellprice) 
     , sum(openingstock) 
     , sum(openingstock*price) 
     , sum(receipts) 
     , sum(receipts*price) 
     , sum(totalstock) 
     , sum(totalstock*price) 
     , sum(sales) 
     , sum(sales*sellprice) 
     , sum(return) 
     , sum(return*sellprice) 
     , sum(closingstock) closingstock 
     , sum(closingstock*price)  

    from purchase_table 
    where date between '2017-04-01' and '2017-05-30' 
    group by orgname ) t order by t.closingstock asc 


    union all 

    select 
    orgname 
    , 'Total' 
    , sum(price) 
    , sum(sellprice) 
    , sum(openingstock) 
    , sum(openingstock*price) 
    , sum(receipts) 
    , sum(receipts*price) 
    , sum(totalstock) 
    , sum(totalstock*price) 
    , sum(sales) 
    , sum(sales*sellprice) 
    , sum(return) 
    , sum(return*sellprice) 
    , sum(closingstock) 
    , sum(closingstock*price) 
    from purchase_table 
+0

感謝的迴應,但我想在第二工會ASC選擇語句 –

+0

答案更新增加了選擇,訂購第二有序 – scaisEdge

0

嘿scaisEdge由第二select聲明和替換它曾在去年select聲明移除順序!工作對我來說

select '' as Total, product, price,sellprice, 
openingstock as openingStock, openingstock*price as op_value, 
receipts as receipts, receipts*price as re_value, 
totalstock as totalstock, totalstock*price as ts_value, 
sales as sales, sales*sellprice as s_value, 
return as returns,return*sellprice as rt_value, 
closingstock as closingstock, closingstock*price as cl_value 
from purchase_table where date between '2017-04-01' and '2017-05-30' 
union all 
select orgname , 'Total', sum(price),sum(sellprice), 
sum(openingstock) as openingStock, sum(openingstock*price) as op_value, 
sum(receipts) as receipts, sum(receipts*price) as re_value, 
sum(totalstock) as totalstock, sum(totalstock*price) as ts_value, 
sum(sales) as sales, sum(sales*sellprice) as s_value, 
sum(return) as returns,sum(return*sellprice) as rt_value, 
sum(closingstock) as closingstock, sum(closingstock*price) as cl_value 
from purchase_table where date between '2017-04-01' and '2017-05-30' group by orgname 

union all 
select '' , '', sum(price),sum(sellprice), 
sum(openingstock) as openingStock, sum(openingstock*price) as op_value, 
sum(receipts) as receipts, sum(receipts*price) as re_value, 
sum(totalstock) as totalstock, sum(totalstock*price) as ts_value, 
sum(sales) as sales, sum(sales*sellprice) as s_value, 
sum(return) as returns,sum(return*sellprice) as rt_value, 
sum(closingstock) as closingstock, sum(closingstock*price) as cl_value 
from purchase_table order by closingstock asc 
相關問題