Can somebody guide me how to get the count for the answer of this query?
SELECT wo.Client_id
FROM wish_order wo,
wish_order_fruit wof
WHERE wo.wish_order_id = wof.wish_order_id
GROUP BY wof.wish_order_id
HAVING Count(wo.Client_id) > 1;
Use a derived table to get the # of rows returned by the group by query
select count(*) from (
select wo.Client_id
from wish_order wo, wish_order_fruit wof
where wo.wish_order_id = wof.wish_order_id
Group by wof.wish_order_id
having Count(wo.Client_id) >1
) t1
Related
I am trying to produce a result that shows duplicates in a table. One method I found for getting duplicates and showing them is to run the select statement again through an inner join. However, one of my columns needs to be the result of a function, and the only thing I can think to do is use an alias, however I can't use the alias twice in a SELECT statement.
I am not sure what the best way to run this code for getting the duplicates I need.
My code below
SELECT EXTRACT(YEAR_MONTH FROM date) as 'ndate', a.transponderID
FROM dispondo_prod_disposition.event a
inner JOIN (SELECT EXTRACT(YEAR_MONTH FROM date) as ???,
transponderID, COUNT(*)
FROM dispondo_prod_disposition.event
GROUP BY mdate, transponderID
HAVING count(*) > 1 ) b
ON ndate = ???
AND a.transponderID = b.transponderID
ORDER BY b.transponderID
SELECT b.ndate, transponderID
FROM dispondo_prod_disposition.event a
INNER JOIN ( SELECT EXTRACT(YEAR_MONTH FROM date) as ndate,
transponderID
FROM dispondo_prod_disposition.event
GROUP BY 1, 2
HAVING COUNT(*) > 1 ) b USING (transponderID)
WHERE b.ndate = ??? -- for example, WHERE b.ndate = 202201
ORDER BY transponderID
This is my query :
SELECT DISTINCT MAX(nb_played), id_person,id_list FROM `t_stats` ORDER BY MAX(nb_played) DESC;
i want to return the max value of nb played for every id list, then return the id person.
so basically, The best nb_played of every id list, with the id_person.
Can't manage to figure it out. anyone could help me ?
Use a GROUP BY query:
SELECT t1.*
FROM t_stats t1
INNER JOIN
(
SELECT id_list, MAX(nb_played) AS max_nb_played
FROM t_stats
GROUP BY id_list
) t2
ON t1.id_list = t2.id_list AND t1.nb_played = t2.max_nb_played;
You can also use a correlated subquery:
select s.*
from t_stats s
where s.nb_played = (select max(s2.nb_played)
from t_status s2
where s2.id_list = s.id_list
);
This can take advantage of an index on t_status(id_list, nb_played), so the performance can be better than using group by.
I have data like
|id|id_barang|in|out|stock|
|1|1|2|0|2|
|2|1|2|0|4|
|3|1|0|1|3|
|4|2|2|0|2|
|5|2|1|0|3|
|6|2|0|1|2|
I want result is group by id_barang
|Id_barang|sum(in)|sum(out)|first(stock)|
|1|4|1|2|
|2|3|1|2|
You can use the MIN function for the first stock. It works similarly to SUM or COUNT.
You could try something like:
SELECT Id_barang, SUM(in), SUM(out), FIRST(stock)
FROM data
GROUP BY Id_barang
Unfortunately Mysql doesn't support Window functions. Try this
SELECT b.id_barang,
sum_in,
sum_out,
a.stock
FROM yourtable A
INNER JOIN (SELECT id_barang,
Sum(in) sum_in,
Sum(OUT) sum_out,
Min(id) AS min_id
FROM yourtable
GROUP BY id_barang) B
ON a.id_barang = b.id_barang
AND a.id = b.min_id
this query fetch all record of table group by Id_barang. its give first record of stock.
SELECT t1.Id_barang, sum(t1.in), sum(t1.out), (select min(t2.stock)
from table_name t2 where t2.Id_barang = t1.Id_barang)
FROM table_name t1
GROUP BY Id_barang
you can make query this way :
SELECT id_barang,SUM(in) as total_in,SUM(out) as total_out,stock
FROM stock
GROUP BY id_barang
if you found any error then you must change your column name, i think 'in' and 'out' is not valid. you change 'in' into 'in_change' and 'out' into 'out_change'. try with this column it works as you want. i did try....
SELECT id_barang,SUM(in_change) as total_in,SUM(out_change) as total_out,stock
FROM stock
GROUP BY id_barang
I have the following 2 queries.
Query 1 :
select distinct(thread_id) from records where client_name='MyClient'
Query 2 :
select max(thread_no) from records
where thread_id='loop_result_from_above_query' AND action='Reviewed'
Is it possible to combine them into a single query ?
The second query is run on every result of the first query.
Thank you.
See attached image of a small snippet of mysql records.
I need a single mysql query to output only records which have action="MyAction" as the latest records for a given set of thread_ids. In the sample data set : record with Sr: 7201
I hope this helps in helping me :)
SELECT client_name, thread_id, MAX(thread_no) max_thread
FROM records
WHERE action='Reviewed' AND client_name='MyClient'
GROUP BY client_name, thread_id
UPDATE 1
SELECT a.*
FROM records a
INNER JOIN
(
SELECT thread_id, max(sr) max_sr
FROM records
GROUP BY thread_id
) b ON a.thread_id = b.thread_id AND
a.sr = b.max_sr
WHERE a.action = 'MyAction'
You can use SELF JOIN, but it is not advisable and will impact your query performance. Please check below query for your reference
SELECT DISTINCT r1.thread_id, MAX(r2.thread_no) from records r1 LEFT JOIN records r2 ON r2.thread_id=r1.thread_id WHERE r1.client_name='MyClient' AND r2.action='Reviewed'
SELECT a.maxthreadid,
b.maxthreadno
FROM (SELECT DISTINCT( thread_id ) AS MaxThreadId
FROM records
WHERE client_name = 'MyClient') a
CROSS JOIN (SELECT Max(thread_no) AS MaxThreadNo
FROM records
WHERE thread_id = 'loop_result_from_above_query'
AND action = 'Reviewed') b
Try this.
SELECT *
FROM (SELECT Row_number()
OVER (
partition BY thread_id
ORDER BY thread_no) no,
Max(thread_no)
OVER(
partition BY thread_id ) Maxthread_no,
thread_id,
action,
client_name
FROM records
Where client_name = 'MyClient') AS T1
WHERE no = 1
AND action = 'Reviewed'
How could I count rows from a SELECT query as a value?
Such as
SELECT FUCNTIONIMLOOKINGFOR(SELECT * FROM anothertable) AS count FROM table;
So that count is an integer of how many rows the subquery SELECT * FROM anothertable returns.
EDIT
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(f.FlagTime)
FROM Flags as f
JOIN Posts as p
ON p.PostPID = f.FlagPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY PostTime DESC
LIMIT 0, 30
SELECT ( SELECT COUNT(id) FROM aTable ) as count FROM table
I assume your example is a truncated version of your actual query, so perhaps you should post what you are after to get a, possibly, more optimal query.
EDIT
Working directly from my brain, something like this should be more optimal.
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep, COUNT(v.FlagTime) as postFlags
FROM Flags as f
JOIN Posts as p ON p.PostPID = f.FlagPID
JOIN Users AS u ON p.PostUID = u.UserUID
LIMIT 0, 30
GROUP BY p.PostPID
ORDER BY PostTime DESC
You can say
SELECT COUNT(*) FROM anothertable
which will return a numeric value, which you can use in another query, such as in the select list of another query, or as a condition in another query.
SELECT someVariable FROM table
WHERE (SELECT COUNT(*) FROM anotherTable) > 5
OR
SELECT someVariable, (SELECT COUNT(*) FROM anotherTable) as count FROM table