I have two select-queries which both give correct results:
SELECT SUM(value) AS "sum1" FROM table GROUP BY id1
and
SELECT SUM(value) AS "sum2" FROM table GROUP BY id1, id2
sum1 is the sum of all sum2-items and I want a query that gives me a result of sum1 and a GROUP_CONCAT of all sum2-items. But how can I define the GROUP BY inside the GROUP_CONCAT for the sum2-items element?
SELECT SUM(value) AS "sum1", GROUP_CONACAT(SUM(value) AS "sum2" ... (?))
FROM table GROUP BY id1
You should solve it using sub query, use the following query, i believe it will solve your problem
select sum(t.part_sum) as sum1, group_concat(t.part_sum) as sum2 from (select sum(value) as part_sum from table group by id1, id2) as t
Related
I have mysql table like this
I want to get row that has minimum 2 or more than 2 (multiple) row only from this table, so the result would be like this
What do i do?
thank you
Use GROUP BY and HAVING clauses
SELECT t.* FROM my_table t
JOIN (
SELECT cust_id, MIN(transaction_no) AS transaction_no
FROM my_table
GROUP BY cust_id
HAVING COUNT(cust_id) > 1
) agg ON t.transaction_no = agg.transaction_no
I have basic table looking like:
When I am using the Query:
SELECT *, SUM(cr) AS cr, SUM(dr) AS dr FROM my_table GROUP BY id
I am getting:
and that's correct!
What's the proper query to get (each sum in different row):
I already tried GROUP BY ID,CR,DR and GROUP BY CR,DR,ID but with not the results that I wanted. (I don't care if the 0 values are also NULL)
You can do:
select id, sum(dr) as dr, 0 as cr from my_table group by id
union all
select id, 0, sum(cr) from my_table group by id
order by id, dr desc
I have a mysql table with following columns:
Id1, id2, timestamp
The id2 is an auto increment entry. The id2 is not unique. so you may have a following rows:
1,12, 983475
2,12, 092348
3,23, 987455
4,23, 908457
I need to get following rows where the timestamp is the latest on the id2.
ie the results will be:
1,12,983475
3,23,987455
Also, the numbers 987455 and 983475 are just fictitious...
Help please...
You can use a subquery with the max() aggregate for this:
select y.id1, y.id2, y.timestamp
from yourtable y
join (select id2, max(timestamp) maxtimestamp
from yourtable
group by id2
) y2 on y.id2 = y2.id2 and y.timestamp = y2.maxtimestamp
This could possibly return ties -- if you truly want a single row per distinct id2, then you can use user-defined variables to establish a row number.
SELECT id2 FROM table ORDER BY max(timestamp) DESC LIMIT 0, 1
Select and restrict using max(timestamp)
e.g. select * from table where timestamp=(select max(timestamp) from table
How would I find the average from this query using SQL:
SELECT count(col1) as count FROM table1 GROUP BY col1 HAVING count > 1
I'm trying to find the average number of rows per col1
So far I managed to find the total amount of rows per col1, now I just need the avg
select avg( c )
from ( SELECT count(col1) as c FROM table1 GROUP BY col1 HAVING count > 1 )
For table you need alias and change count to count(*).
Query:
SELECT avg(t1.c) AS avgcol1
FROM
(SELECT count(col1) AS c
FROM table1
GROUP BY col1 HAVING COUNT(*) > 1) t1
I have a table like this:
jobid, orderid
And with some data inside:
jobid, orderid
1245, 6767
1235, 9058
6783, 6767
4991, 6767
9512, 9058
5123, 1234
Now I want the following output:
jobid, orderid, orderid(total)
1245, 6767, 3
1235, 9058, 2
6783, 6767, 3
4991, 6767, 3
9512, 9058, 2
5123, 1234, 1
Now, the COUNT() doesn't work the way I want to, and I probably need some group by but I don't know how.
Thanks in advance.
It looks like you're trying to get rows which look like jobid, orderid, number of times that orderid appears. For that, you could use a subquery:
SELECT jobid, orderid,
(SELECT COUNT(*) FROM
MY_TABLE INR
WHERE INR.orderid = OTR.orderid) as "orderid(total)"
FROM MY_TABLE OTR
Why are doing it this way? You will be doing a lot of redundant countings and put a lot of unnecessary pressure on your server. I would do this with two queries:
SELECT jobid, orderid FROM my_table
to get the complete list, and then:
SELECT orderid, COUNT(*) FROM my_table GROUP BY orderid
to get the total count for each orderid. Then combine these two results in your application. This will be much faster than your solution.
SELECT jobid, orderid, count(orderid)
FROM sometable
GROUP BY orderid, jobid
SELECT t.jobid
, t.orderid
, grp.orderid_total
FROM
tableX AS t
JOIN
( SELECT orderid
, COUNT(*) AS orderid_total
FROM tableX
GROUP BY orderid
) AS grp
ON grp.orderid = t.orderid
select jobid, orderid, count(*) from table group by orderid;