0

i am trying to aggregate two columns in my result table but I can't get my head around it... I have a star schema and the following query:

select 1und1.SubCategory,count(industry_facts.NACE_ID), statistics_2016.businesses 
from industry_facts, 1und1, statistics_2016
where 1und1.NACE_ID = industry_facts.NACE_ID
and statistics_2016.Stat_ID = industry_facts.Stat_ID
group by 1und1.SubCategory ASC, statistics_2016.businesses

The result table give back the correct results but in this style:

enter image description here

This is the Schema:

enter image description here

I can't figure out a way to aggregate it per SubCategory...So that it looks like Accomodation, 660, 1004390

Tried it with rollup at the and of the group by clause, but that only aggregates the 2nd column, the 3rd always gives back "NULL"

Do you have any ideas? Help is much appreciated!

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181

2 Answers2

0

Remove business from the GROUP BY. Also, never use commas in the FROM clause. Always use proper, explicit JOIN syntax:

select u.SubCategory, count(i.NACE_ID)
from industry_facts join i
     1und1 u
     on u.NACE_ID = i.NACE_ID join
     statistics_2016 s 
     on s.Stat_ID = i.Stat_ID
group by u.SubCategory ASC;

If you want the businesses, then you can use group_concat(s.businesses) in the SELECT or group_concat(distinct s.businesses).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Sorry to bug you again, I tried to adapt your advice to my code and I got this: select 1und1.SubCategory, count(1und1.NACE_ID), group_concat(statistics_2016.businesses) from industry_facts join 1und1 on 1und1.NACE_ID = industry_facts.NACE_ID join statistics_2016 on statistics_2016.Stat_ID = industry_facts.Stat_ID group by 1und1.SubCategory Works good, but the group_concat only states values like 37760, 37760, 123465, ....so a lot of doublets and not summarized – Tobias Laifer Apr 30 '17 at 14:50
0

Try with this code:

   select 1und1.SubCategory,count(industry_facts.NACE_ID), statistics_2016.businesses 
    from industry_facts inner join 1und1
    on industry_facts.NACE_ID=1und1.NACE_ID
    join statistics_2016
    on statistics_2016.Stat_ID = industry_facts.Stat_ID
    group by 1und1.SubCategory,statistics_2016.businesses
    order by 1und1.SubCategory
Fahmi
  • 37,315
  • 5
  • 22
  • 31
  • Thanks Fahima, but that is returning the same as the result table above. Is it even possible tu sum up both colums? – Tobias Laifer May 01 '17 at 09:08
  • what is your business column here? Is it a measure column or key column? if measure then you can sum it up and remove business from group by. Hope it will give u as u expect – Fahmi May 01 '17 at 15:29
  • It is a measure, but when I sum it up, I think it sums up a lot duplicates...For example with the SubCategory "Accomodation" I should get a count of NACE_ID of 660 and a subtotal sum of 1.004.390 The count of NACE_ID is correct, but the sum it gives back is like 17.000.000 haha. Maybe I have a logical flaw in the design of my model and that's why it can not sum up the businesses...when I do group_concat (distinct (statistics_2016.businesses)) I get the string back correctly...But then I can't sum up strings unfortunately lol – Tobias Laifer May 01 '17 at 15:47