SQL to get amounts by type [message #642680] |
Thu, 17 September 2015 04:19 |
oteixeira
Messages: 33 Registered: May 2007
|
Member |
|
|
Hi to all.
As an accountant, VAT has no much secrets for me. However, Tax Authorities sometimes request weird reports and I cannot say that I'm an sqlplus expert (far from it).
Please have a look to the uploaded file TwoTables.jpg
My goal is to use sqlplus to pick the data in the yellow table and return the data as shown on blue table.
Many thanks in advance for any kind help.
Octavio
|
|
|
|
Re: SQL to get amounts by type [message #642682 is a reply to message #642681] |
Thu, 17 September 2015 04:49 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
cookiemonster wrote on Thu, 17 September 2015 15:00Use listagg, sum and group by
But OP doesn't want aggregation of amount column here, he wants it to be pivoted.
|
|
|
|
|
|
|
Re: SQL to get amounts by type [message #642688 is a reply to message #642685] |
Thu, 17 September 2015 06:04 |
oteixeira
Messages: 33 Registered: May 2007
|
Member |
|
|
@sandeep_orafaq - I see. I have to reformulate the question. Please consider that the third (last) line of yellow table is of type 'V'. Yes, 3 types instead of only 2.
Sorry for the mess...
Octavio
|
|
|
|
|
|
|
|
Re: SQL to get amounts by type [message #642708 is a reply to message #642706] |
Thu, 17 September 2015 08:53 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
I've not idea what type of file that .dat file is but it's not plain text.
You should just copy and paste a create table statement and insert statements directly in code tags.
|
|
|
Re: SQL to get amounts by type [message #642713 is a reply to message #642708] |
Thu, 17 September 2015 09:20 |
oteixeira
Messages: 33 Registered: May 2007
|
Member |
|
|
Here are the create and insert statements:
CREATE TABLE TEST_CASE
(REC_NUMBER varchar2(20),
REC_AMOUNT number,
CASH_REC_ID number,
CCID number,
COMMENTS varchar2(20),
D_AMOUNT number)
INSERT INTO TEST_CASE(REC_NUMBER, REC_AMOUNT,CASH_REC_ID, CCID, COMMENTS, D_AMOUNT)
SELECT '363_2015', 26.87,366388,109740,'V',2.24 from dual
UNION ALL SELECT '363_2015', 26.87,366388,125191,'B',15.51 from dual
UNION ALL SELECT '363_2015', 26.87,366388,125047,'I',8.96 from dual
UNION ALL SELECT '670_15', 4.53,366640,125485,'I',4.53 from dual
Thank you
Octavio
|
|
|
|
Re: SQL to get amounts by type [message #642716 is a reply to message #642715] |
Thu, 17 September 2015 09:42 |
oteixeira
Messages: 33 Registered: May 2007
|
Member |
|
|
For each REC_NUMBER, D_AMOUNT with COMMENTS = 'B' and 'V' must be shown on column AMOUNT1 and AMOUNT2, respectively. For each REC_NUMBER, D_AMOUNT with COMMENT = 'I' must be shown in AMOUNT1 and AMOUNT2 must show zero.
Thanks for your patiente.
Octavio
|
|
|
|
|
|
|
|
Re: SQL to get amounts by type [message #642722 is a reply to message #642719] |
Thu, 17 September 2015 10:28 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Looks like you need sum, case and union.
I think it's this:
SELECT rec_number, rec_amount, sum(CASE WHEN comments = 'B' THEN d_amount END) d_amount1,
sum(CASE WHEN comments = 'V' THEN d_amount END) d_amount2
FROM test_case
WHERE comments IN ('B', 'V')
GROUP BY rec_number, rec_amount
UNION ALL
SELECT rec_number, rec_amount, SUM(d_amount), 0
FROM test_case
WHERE comments = 'I'
GROUP BY rec_number, rec_amount
|
|
|
|
|
|