다음은 sqlite에서 롤업을 에뮬레이션하는 방법입니다. 2007 년 데이터베이스 forum에서 부분합과 총합계에 대해 묻는 질문에 답을 얻지 못했습니다. 단순화 된 사례로 요약 해 보겠습니다.
다른 필드들 중에서 Ticker_Symbol (텍스트), Underlying (텍스트) 및 Quantity (정수)를 포함하는 테이블 스톡이 있습니다. 이 예제에서는 충분합니다.
롤업이 sqlite가에서 다음 호출을 사용하여 에뮬레이션 할 수는 :
select Ticker_Symbol,Underlying,Quantity from (
select '1' orderCol, Ticker_Symbol,Underlying,Quantity from stocks
union
select '2' orderCol, Ticker_Symbol, 'Subtotal' as Underlying, Sum(Quantity) as Quantity from stocks
group by Ticker_Symbol
union
select '99' orderCol, '_' as Ticker_Symbol, 'GrandTotal' as Underlying, sum(Quantity) as Quantity from stocks)
as t1 order by case when orderCol=99 then 1 else 0 end, Ticker_Symbol, orderCol;
이 다음과 유사한 출력을 생성합니다 : 불행하게도
|Ticker_Symbol |Underlying|Quantity|
|-------------------|----------|--------|
AAPL AAPL 500
AAPL AAPL 1000
AAPL AAPL 2000
AAPL Subtotal 3500
AAPL140222P00500000 AAPL 10
AAPL140222P00500000 Subtotal 10
IBM140322C00180000 IBM 25
IBM140322C00180000 Subtotal 25
R140222C00067500 R 10
R140222C00067500 Subtotal 10
VLCCF VLCCF 300
VLCCF VLCCF 2000
VLCCF Subtotal 2300
_ GrandTotal 5845
, 나는 TICKER_SYMBOL를 사용하지 않도록 할 수있는 방법을 찾을 수 없습니다 . 이상적으로, 현재 Ticker_Symbol을 'Subtotal'(또는 GrandTotal)로 바꾸는 것이 좋겠지 만 작동하지 않습니다. 또한 GrandTotal이 실제로 마지막 행에 표시되는지 확인하기 위해 "_"의 사용에 유의하십시오.
다른 사람들에게 도움이 되었으면 좋겠다.