IF(GROUPING(id)=1, "Total", name) returns the same wrong "name" across all rows, except for the total case
2
votes
3
answers
74
views
I'm using MySQL 8.0 and performing select query with totals and sub-totals, hence it's about
with rollup
statement.
The problem is that the expression IF(GROUPING(id)=1, "Total revenue", diagnosis_name)
is returning the same value for diagnosis_name
in all rows except for those where the grouping id is 1 (i.e., the "Total revenue" rows). Moreover, if I put a fake condition without GROUPING
(ex. 0=0) the result is correct. Same happens if I take the diagnosis_name
without the conditional statement at all.
Similar problem happens with group_concat
, I wonder if the reasons are the same.
**Here is the query result, the _true
columns shows the correct value**
| id | appointment_id_true | appointment_id | appointment_diagnosis | revenue | appointment_diagnosis_true |
| ----- | ------------------- | -------------- | --------------------- | ------- | -------------------------- |
| 2 | 1 | 5 | Туберкулез | 350.00 | Грипп |
| 2 | 12 | 5 | Туберкулез | 0.00 | Туберкулез |
| 2 | | 1, 12 | Total revenue | 350.00 | Туберкулез |
| 3 | 11 | 5 | Туберкулез | 0.00 | Диарея |
| 3 | | 11 | Total revenue | 0.00 | Диарея |
| 4 | 5 | 5 | Туберкулез | 120.00 | Наркотическая зависимость |
| 4 | | 5 | Total revenue | 120.00 | Наркотическая зависимость |
| Total | | 1, 5, 11, 12 | Total revenue | 470.00 | Наркотическая зависимость |
Not sure if it's good idea to put the SQL query here, so [View on DB Fiddle](https://www.db-fiddle.com/f/kcMvHR2FyKaQvm7QNVnBAt/4)
What is the coveats and how can I make it work in a way sql query describes?
Asked by lostsoul
(21 rep)
Apr 22, 2023, 09:12 PM
Last activity: Apr 29, 2023, 07:23 PM
Last activity: Apr 29, 2023, 07:23 PM