0

I have a table with redundant values in the first two columns:

sqlite> select * from amino where "lebensmittel" = 'Walnüsse';
Lebensmittel  Protein pro 100g  Histidin  Isoleucin  Leucin   Lysin    Methionin  Cystein  Phenylalanin  Tyrosin  Threonin  Tryptophan  Valin 
------------  ----------------  --------  ---------  -------  -------  ---------  -------  ------------  -------  --------  ----------  ------
Walnüsse      15,23 g           391 mg                                                                                                        
Walnüsse      15,23 g                     625 mg                                                                                              
Walnüsse      15,23 g                                1170 mg                                                                                  
Walnüsse      15,23 g                                         0,424 g                                                                         
Walnüsse      15,23 g                                                  236 mg                                                                 
Walnüsse      15,23 g                                                             208 mg                                                      
Walnüsse      15,23 g                                                                      711 mg                                             
Walnüsse      15,23 g                                                                                    406 mg                               
Walnüsse      15,23 g                                                                                             596 mg                      
Walnüsse      15,23 g                                                                                                       170 mg            
Walnüsse      15,23 g                                                                                                                   753 mg

I tried to merge the columns with coalesce, but it works only for the first column:

sqlite> select
  "Lebensmittel",
  "Protein pro 100g",
  coalesce("Histidin", null) as "Histidin",
  coalesce("Isoleucin", null) as "Isoleucin",
  coalesce("Leucin", null) as "Leucin",
  coalesce("Lysin", null) as "Lysin",
  coalesce("Methionin", null) as "Methionin",
  coalesce("Cystein", null) as "Cystein",
  coalesce("Phenylalanin", null) as "Phenylalanin",
  coalesce("Tyrosin", null) as "Tyrosin",
  coalesce("Threonin", null) as "Threonin",
  coalesce("Tryptophan", null) as "Tryptophan",
  coalesce("Valin", null) as "Valin"
from amino
where Lebensmittel = 'Walnüsse'
group by "Lebensmittel", "Protein pro 100g";
Lebensmittel  Protein pro 100g  Histidin  Isoleucin  Leucin  Lysin  Methionin  Cystein  Phenylalanin  Tyrosin  Threonin  Tryptophan  Valin
------------  ----------------  --------  ---------  ------  -----  ---------  -------  ------------  -------  --------  ----------  -----
Walnüsse      15,23 g           391 mg                                                                                                    

Can anybody explain what is wrong with my query?

1
  • Your query is wrong because you can only COALESCE values from the same row - take a look here - if it's the correct answer, I'll write it up!
    – Vérace
    Jul 8 at 19:12

1 Answer 1

0

I seems that coalesce can not be used as an aggregate function. But group_concat works fine:

select
  "Lebensmittel",
  "Protein pro 100g",
  group_concat("Histidin", '') as "Histidin",
  group_concat("Isoleucin", '') as "Isoleucin",
  group_concat("Leucin", '') as "Leucin",
  group_concat("Lysin", '') as "Lysin",
  group_concat("Methionin", '') as "Methionin",
  group_concat("Cystein", '') as "Cystein",
  group_concat("Phenylalanin", '') as "Phenylalanin",
  group_concat("Tyrosin", '') as "Tyrosin",
  group_concat("Threonin", '') as "Threonin",
  group_concat("Tryptophan", '') as "Tryptophan",
  group_concat("Valin", '') as "Valin"
from amino
where Lebensmittel = 'Walnüsse'
group by "Lebensmittel", "Protein pro 100g";
1
  • Yes, the columns that are not in group by need to be invoked inside an aggregate function. My personal preference for cases like this is max, but group_concat may be a better option in case the aggregated column happens to have more than one value and you need to catch all of them.
    – Andriy M
    Jul 9 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.