Skip to content
Advertisement

SQL query to get subtotal on the last row of bill number

I have written the following SQL query which gives the result not in a way I desire. The only change I want is that, I don’t want to repeat the subtotal on every single line, which is what my query is doing now:

Instead, I want it only on the last row of every unique bill_number:

The query is

I guess you get the idea of what I want from the images given

Advertisement

Answer

You can do this with row_number() and a CASE expression.

However, your query and result suggest that you have multiple rows sharing the same s.bill_num; to get a stable result, you need a column that can be used to unambigously order rows having duplicate values – let me assume s.id:

Note that I moved the condition on B.isexpress from the WHERE clause to the ON clause of the relevant LEFT JOIN; otherwise, this would filter out rows that have no match in bills – which does not seem to be your intent.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement