2017-05-01 1 views
0

단일 쿼리에서 2 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 
012 3,516,

및 부속 선택

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 
를 사용하는 것보다 2 차를 필요로하는 경우
+0

감사 일했다,하지만 난 두 번째 주문에 대한 두 번째 조합에서 오름차순을 선택 문 선별을 추가하는 업데이트 –

+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 
관련 문제