there is a table

enter image description here

do not pay attention to field names

how to reduce it to mind

enter image description here

the most trivial is to group by column and join as many times as there are columns, but I think there is a more convenient solution

  • No need to lead to such a view in SQL. For SQL is not intended to display the number of columns that are undefined in the initial query. To make the usual grouping by day with counting quantities with issuing vertically, and horizontally deployed on the client - Mike
  • can be more? group on the basis of what? - des1roer
  • a solution to the forehead of something like select 'пришло' as type,пришел, count(1) from table group by пришел union select 'болеет',болезнь,count(1) from table group by болезнь union ... if you want of course, for one reading the table can be done, but there you have to mess with IF - Mike

1 answer 1

This is called table transposition. Read http://easy4web.ru/?p=1099 - here chewed how to do it.

UPD: Although not exactly transposing in the direct sense of the term, the essence is the same.

  • and how to implement grouping by day? - des1roer
  • There are more versatile options here.stackoverflow.com/questions/42644/… - only the request will be complicated, I don’t undertake to generate it on the go. - Goncharov Alexander
  • there is a tin full. but even if I flip the data I don’t sort them by dates that aren’t - des1roer
  • @ des1roer: well, either a hard sign or non-flexibility, or we do it not on the side of sql - but on the side of the client, which receives data from sql. Of course, I would not do pornography and choose option # 3, and if there is no access to the client’s code, they’ll say to the progers who do it in a good way) - Goncharov Alexander