I have table_1 with the following data:
| STORE | Add | dis | Cnt |
+-------+-----+-----+-----+
| 101 | X | abc | 2 |
| 101 | X | null| 3 |
| 101 | X |pqrd | 4 |
| 101 | X | null| 1 |
| 102 | y | null| 1 |
| 102 | y | xyz | 3 |
| 102 | y | pqr | 4 |
| 102 | y | null| 2 |
I tried to build a query to get data from table_1 where [dis] is not null and [cnt] should be minumum. So my result should looks like below:
| STORE | Add | dis | Cnt |
+-------+-----+-----+-----+
| 101 | X | abc | 2 |
| 102 | y | xyz | 3 |
My query looks like below :
SELECT store,add,dis,min(TMPLT_PRIORITY_NMBR)
FROM table_1 group by store,add;
But I get the following error:
Column 'dis' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I use [dis] in GROUP BY clause, I get the wrong result and giving max(dis) or min(dis) also provides the wrong result.
What would be the solution for this issue?