4

I need some help writing SQL statement for below requirements(FINAL REULT).

note: I am writing this SQL on SAP HANA system(database) using SQL syntax. The SQL syntax is normal universally used.

some abbreviations used for column names:

cust = customer
ctry = country
mth = month
HCostPP = Highest cost per period
HCtryPP = highest country per period
HCostAP = Highest cost over all periods
HCtryAP = highest country over all periods

i have granular data in my source table. By writing SQL like below, i get aggregated data:

SELECT distinct cust,ctry,mth,sum(cost)
FROM mytable
GROUP BY cust,ctry,mth

I get aggregated data like which i used further to get my required results:  
cust  ctry  mth     cost
c001   US  201506  -100
c001   DK  201506  -100
c001   DE  201506  -50
c001   FR  201507  -200
c001   UK  201507   -50

THE FINAL required RESULT i want to achieve should look like below:

cust   ctry  mth   cost  HCostPP HCtryPP HCostAP HCtryAP
c001   US  201506  -100  -100    DK      -200    FR
c001   DK  201506  -100  -100    DK      -200    FR
c001   DE  201506  -50   -100    DK      -200    FR
c001   FR  201507  -200  -200    FR      -200    FR
c001   UK  201507   -50  -200    FR      -200    FR

Explanation of required results

based on data group (cust,ctry,mth) need to get for which 
country COST were hightest 'within each month' (HCostPP , HCtryPP)
and then again 'over all months'(HCostAP , HCtryAP).

CATCH

for month 201506, -100 cost is same for both US and DK. 
In this case take either one e.g. DK or US (i am showing above to take DK)

WHAT I TRIED:
i understand that two left joins are needed. first left join should look like below to get HCostPP , HCtryPP:

LEFT SIDE                            RIGHT SIDE
cust   ctry  mth   cost          cust   ctry  mth   cost
c001   US  201506  -100          c001   DK  201506  -100
c001   DK  201506  -100          c001   FR  201507  -200
c001   DE  201506  -50
c001   FR  201507  -200
c001   UK  201507   -50

in order to get RIGHT SIDE table, when i write SQL like:

SELECT cust,ctry,mth, MIN(cost)
FROM
(
SELECT distinct cust,ctry,mth,sum(cost)
FROM mytable
GROUP BY cust,ctry,mth
)
GROUP BY cust,ctry,mth

i don't get the required result, i get:
cust   ctry  mth   cost
c001   US  201506  -100
c001   DK  201506  -100
c001   DE  201506  -50
c001   FR  201507  -200
c001   UK  201507   -50

if i do like: 
SELECT cust,mth, MIN(cost)
FROM
(
SELECT distinct cust,ctry,mth,sum(cost)
FROM mytable
GROUP BY cust,ctry,mth
)
GROUP BY cust,mth

then i get below and i lose 'cntry' column:
cust     mth   cost
c001     201506  -100
c001     201507  -200

further if i use INNER JOIN to get 'cntry' column:

SELECT cust,mth,ctry,cost FROM mytable AS 'main'
INNER JOIN (
SELECT cust,mth, MIN(cost) as cost1
FROM
(
SELECT distinct cust,ctry,mth,sum(cost)
FROM mytable
GROUP BY cust,ctry,mth
)
GROUP BY cust,mth ) AS 'sub'
ON main.cust=sub.cust, main.mnth=sub.mnth, main.cost=sub.cost1

then this gives me what is also not desired as 
it is giving me both rows i.e. for US and DK and i need only one here: 
cust   ctry  mth   cost
c001   US  201506  -100
c001   DK  201506  -100
c001   FR  201507  -200

I am thankful for any help in writing SQL to achieve the required results as outlined above (section FINAL REQUIRED RESULTS).

thanks for your help./Regards/NOMAN

nomann
  • 41
  • 3
  • Hi nomann, your final result table is wrong (i guess). I think the column entry in `HCtryPP ` in the two last rows should be `FR` and not `DK`, right? – V. Wolf Jul 06 '16 at 12:35
  • like so:`cust ctry mth cost HCostPP HCtryPP HCostAP HCtryAP c001 US 201506 -100 -100 DK -200 FR c001 DK 201506 -100 -100 DK -200 FR c001 DE 201506 -50 -100 DK -200 FR c001 FR 201507 -200 -200 FR -200 FR c001 UK 201507 -50 -200 FR -200 FR` – V. Wolf Jul 06 '16 at 12:36
  • Hi, thanks for pointing the mistake. I have edited 'final result' – nomann Jul 13 '16 at 17:11

1 Answers1

0

here is the first part, for the PerPeriod results you should you use an over (partion by) construction (which is very well described here.

select 
 t1.cust,
 t1.ctry,
 t1.mth,
 t1.cost,
 (select t3.ctry from mytable t3 order by t3.cost asc limit 1)  HCtryAP,
 min(t2.cost) HCostAP
from mytable t1, mytable t2
group by 1,2,3
order by 3, 4;
V. Wolf
  • 123
  • 1
  • 8
  • with `over (partition by)` you are referring to window functions. Since when does MySQL support them? (And what does that have to do with SAP HANA) –  Jul 06 '16 at 13:01