MySQL Sum grouping issue - mysql

I have a table containing jobs to be invoiced. Each row contains two columns, 'value' and 'group'. Like this
ID Value Group
1 2000.00 1
2 2000.00 1
3 1000.00 0
4 1000.00 0
What I need to do is combine the values in Rows 1 and 2 (because they have the same group number), then return rows 3 and 4 as normal (so, not grouped together):
4000 //Rows 1 and 2 combined
1000 //Row 3 returned as whole value
1000 //Row 4 returned as whole value
I've tried to use GROUP BY in the query, so something like
SELECT SUM(Value) AS totalValue FROM table GROUP BY Group
However this returns
4000
2000 //Row 3 and 4 combined
It's combining Row 3 and 4 because they share the same Group number, its grouping them together.
My problem is, I don't want them grouped together. I want them to return separately as they have a value of zero. Is there any way for me to do this?

SELECT Group AS Unique_ID, SUM(Value) AS totalValue FROM table WHERE Group>0 GROUP BY Group
UNION
SELECT id AS Unique_ID, Value As totalValue FROM table WHERE Group=0

Related

How to show all MIN of SUM(column) that has the same min value?

I've been trying to get MIN of SUM of a column by doing subqueries, however in a case where I have two same SUM values, my query only returns me one of them. Is there a way to get all of them to be shown?
So for example this table is called quantity,
date product_id quantity_start quantity_end
1/1/2020 1 10 5
1/1/2020 2 10 5
1/1/2020 3 12 1
2/2/2020 1 10 5
2/2/2020 2 11 6
2/2/2020 3 14 1
my query would be
SELECT product_id, MIN(Total) as Minimum
FROM (SELECT product_id, SUM(quantity_start - quantity_end) as Total
FROM quantity
GROUP BY product_id)T
But this will return me only one min value while there are two since both product 1 and 2 will have total of 10 from my subquery. Is there a way to write it such that it shows me both ?
Thanks!
You are correctly grouping by your sub query, but the outer query also needs a group by so:
SELECT product_id, MIN(Total) as Minimum
FROM (SELECT product_id, SUM(quantity_start - quantity_end) as Total
FROM quantity
GROUP BY product_id)T
GROUP BY product_id;
See it working here: http://sqlfiddle.com/#!9/97d7724/1

Group by with sum doesn't return correct result

Say a table has this schema :
grp | number
1 | 10
1 | 10
1 | 10
2 | 30
2 | 30
3 | 20
Note that each unique grp has a unique number even if there are more than 1 grp. I'm looking to sum all numbers for each unique grp.
So I want to group my table by grp to have this :
grp | number
1 | 10
2 | 30
3 | 20
And then get the sum which is now 60, but without grouping it gets me 110 as it calculates the sum of everything without grouping. All in one query, with no sub-queries if possible.
I've tried doing the following :
SELECT sum(number) as f
FROM ...
WHERE ...
GROUP BY grp
But this doesn't work, it returns multiple results and not the single result of the sum. What am I doing wrong?
You can use subquery to select unique records & do the sum:
select sum(number)
from (select distinct grp, number
from table t
) t;
If you group by the group, then you'll get one result for each group. And it won't take into account the fact that you only want to use the value from each group once.
To get your desired result, taking one row from each group, you first need to make a subquery selecting DISTINCT group/number combinations from the table, and then SUM that.
SELECT
sum(`number`) as f
FROM
(SELECT DISTINCT `grp`, `number` FROM table1) g
This will output 60.
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=8a3b346041731a4b4c85f4e151c10f70

mysql how to use column count when same column can have 2 different values

I have an cutomer table where i store some customer error data
I would like to find different count on the same column.
Errorid custName ErrorReported
----------------------------------
1 abc Dead_battery
2 xyz Low_voltage
3 ann Dead_battery
4 ben Dead_battery
5 max Low_voltage
The result of sql query should be count and ErrorReportedtype
count(*) Errortype
------------------------
3 Dead_battery
2 Low_voltage
If you group by the column to be unique you can use aggregate functions like count on every group
select count(*), ErrorReported as ErrorType
from your_table
group by ErrorReported

Access Totals Query Not Necessarily Returning First Record

I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.

Suggest the query to fetch the required result in one query with performance

I am able to fetch the results using the temp tables or looping over this table but I want to translate the following table into the required result in one query with great performance.
Order Table
OrdNbr LineNbr ItemName Qty
1 1 Pen 1
1 2 Pencil 2
1 3 Scale 2
2 5 Bottle 2
3 3 Pen 10
3 1 Pencil 5
Required Result:
OrdNbr OrdNbrFirstLineNbr ItemName Qty AllLineNumbers
1 1-1 Pen 1 1,2,3
2 2-5 Bottle 2 5
3 3-1 Pencil 5 1,3
OrdNbr and LineNbr are primary key for order table. I want to fetch only first record for the same OrdNbr.
Logic to get the result:
Find the distinct order number and get the lowest line number for the individual order number. Now display the order number, lowest line number and details for that lowest line number in that order. I want two extra derived fields OrdNbrFirstLineNbr and AllLineNumbers.
select OrdNbr,
LineNbr as OrdNbrFirstLineNbr,
ItemName,
Qty
from (
select *,
row_number() over (partition by OrdNbr order by LineNbr) as rn
from "Order" -- need to quote this, because order is a reserved word
) t
where rn = 1
Try this:
SELECT OrdNbr, LineNbr, ItemName, Qty
FROM Order
GROUP BY OrdNbr
ORDER BY LineNbr;