Skip to content

Tag: sum

Conditional Grouping in SQL

This should be easy, but I’m having trouble with it. I have a results table that looks like this: Notice that the pct column sums to 1.0 for each store. Here’s the code to create this table: I’m trying to group the results by store, then by cust_id while creating a new category A&B if th…

Add additional column to the query output

I have the following query that is grabbing data from a table and combining all the products into one column and counting the quantity of that order. I need to add an additional column to the output though, how can I also add ship_date? This is how the original table is laid out: And this is how the query out…

My SUM query returns 2 rows, need some advise

I am getting 2 rows(9500) for 111 in the result, could you please advise on the right approach, I need like, Balance = (sum of bought – sum of sold) Query Answer You can use conditional aggregation: If you want this for only one account, you can add a where clause:

Count json tags in sql

I have this json strings And know i want the following output I wanted to do this with a sql query. If you know how to do this, thanks in advance! Answer If you’re not using MySQL 8, this is a bit more complicated. First you have to find a path to a name element that has the value phone

MySQL Debit/Credit cumulative balance

I would like a cumulative DR or CR balance for the following table: For the above table, a cumulative Balance column would be: Decreasing a DR below 0 makes it a CR Decreasing a CR below 0 makes it a DR Where balance is 0, value is 0 DR It could be calculated as CR’s being a negative value, but