c# - Sqlite Get sum Columns based on another column -
i have sqlite table data given below.
id type amount ----------------------- 1 10 1 b 5 2 4 2 b 7 3 2 3 b 8
what wanted present in datagridview this
id b ------------- 1 10 5 2 4 7 3 2 8
this in sqlite database. need show data in datagridview. taking first dataset , using code loop through desired result table has lot of results , therefore process extremely slow.
can guys please tell me how can second result directly. have search not find appropriate sql query result
you can conditional aggregation
select id, sum(case when type = 'a' amount) sum(case when type = 'b' amount) b yourtable group id
another option join table itself
select id, t1.amount a, t2.amount b yourtable t1 join yourtable t2 on t1.id = t2.id t1.type = 'a' , t2.type = 'b'
the first option requires have 1 row per id / type, if that's case performs better. second 1 safer, joining table decrease performances
Comments
Post a Comment