i have next query
SELECT *
FROM(
SELECT
ID,
SUM(points) AS SUMMARY
FROM my_table
GROUP BY ID
ORDER BY SUMMARY DESC
) t1
WHERE SUMMARY>=(SELECT
SUMMARY
FROM (
SELECT
ID,
SUM(points) AS SUMMARY
FROM my_table
GROUP BY ID
ORDER BY SUMMARY DESC
) t2
WHERE ID=1234)
How can I remove duplicate query or reuse selection results?
Maybe my request is completely incorrect?
I'm pretty sure your query is identical to:
SELECT ID, SUM(points) AS SUMMARY
FROM my_table
GROUP BY ID
HAVING SUMMARY >= (SELECT SUM(points) FROM my_table WHERE ID=1234)
ORDER BY SUMMARY DESC
SQL Fiddle demonstration
Related
select
OrderNo,
Sum(QtyIn) as QuantityIn,
Sum(QtyOut) as QuantityOut
from
tbl_Assign
group by
OrderNo
I want to select * from table also group by from table. How to do it?
To group by on all columns with a sum you cannot use *, you have to list all of the columns out and every column that isn't a function like Sum must be included in the group by.
So if you have other fields in your database such as OrderName, OrderedBy you can perform a group by like this:
Select
OrderNo,
OrderName,
OrderBy,
Sum(QtyIn) as QuantityIn,
Sum(QtyOut) as QuantityOut
From
tbl_Assign
Group By
OrderNo, OrderName, OrderBy
The following will create one row for every row in the tbl_Assign.
Each row will also show the summary information for the order.
This might not be what you need, but it's useful to understand it anyway.
SELECT T1.*, T2.*
FROM
( select * FROM tbl_Assign ) AS T1
LEFT JOIN ( select
OrderNo,
Sum(QtyIn) as QuantityIn,
Sum(QtyOut) as QuantityOut
from
tbl_Assign
group by
OrderNo
) AS T2
ON T1.OrderNo = T2.OrderNo
Harvey
I have the following table structure
and i want the result to be
Here is the query which i tried
select * from table where userid IN(201,202,203,204,205)
group by userid
order by messageid desc
But i dint get the latest records based on messageid.
I need to write this in a single query as i must use order by clause
Please explain my mistake and provide a solution
You can join the table to itself using the max of the messageid if I'm understanding your question correctly:
select t.messageid, t.userid, t.data
from yourtable t
join (
select max(messageid) maxmessageid, userid
from yourtable
where userid in (201,202,203,204,205)
group by userid
) t2 on t.userid = t2.userid and t.messageid = t2.maxmessageid
order by t.messageid desc
SQL Fiddle Demo
Edit: Here's an alternative approach using IN:
select messageid, userid, data
from yourtable
where messageid in (
select max(messageid) maxmessageid
from yourtable
where userid in (201,202,203,204,205)
group by userid
)
order by messageid desc
More Fiddle
SELECT yourtable.*
FROM
yourtable INNER JOIN (SELECT userid, MAX(messageid) max_messageid
FROM yourtable
WHERE userid IN (201,202,203,204,205)
GROUP BY userid) mx
ON yourtable.messageid=mx.max_messageid
AND yourtable.userid=mx.userid
I would like to get all the rows from the two users with the greatest number of rows, that is, the two users with the greatest activity in a log table.
I have only found next solution: first, get the number of rows for every user, an limit it to 2:
SELECT userid, count(*) AS n_of_rows FROM my_table GROUP BY userid LIMIT 2;
Then, from the source code I'm querying the database (Python for example), query the database to get the rows of each user:
SELECT * FROM my_table where userid = $userid
Is it the best/elegant solution, taking into account SQL language itself and database performance?
Thanks!
I think what you're looking for is something like
select * from my_table where userid in
(select userid from my_table
group by userid
order by count(*) desc
limit 2)
To get the rows and keep the order, use a join with aggregation:
select t.*
from my_table t join
(select userid, count(*) as cnt
from my_table
group by userid
order by count(*) desc
limit 2
) top2
on t.userid = top2.userid
order by top2.cnt desc, userid;
Try this:
SELECT TOP 2 userid, count(*) AS n_of_rows
FROM my_table
GROUP BY userid
ORDER BY count(*) desc
how can i make something like this work?
INSERT INTO age.page(domain,title_count,youtube_count,ipaddress,updated)
SELECT * FROM
(
SELECT domain,
COUNT(domain) AS titlecount,
(SELECT COUNT(*) FROM table2 WHERE title = table1.title) AS YoutubeCount, ipaddress
NOW() AS timeNow
FROM table1
GROUP BY domain
ORDER BY title DESC
) a;
I want to use a subquery to get a count of a different table but use the same field from the main query.
the reason i want to do this is so i dont have to run two queries instead its only one.
You can do this COUNT in a subquery and then JOIN it with the first table:
INSERT INTO age.page(domain, title_count, youtube_count, ipaddress, updated)
SELECT * FROM
(
SELECT
domain,
COUNT(domain) AS titlecount,
t2.titlecount AS YoutubeCount,
ipaddress,
NOW() AS timeNow
FROM table1
INNER JOIN
(
SELECT title, COUNT(*) Titlecount
FROM table2
GROUP BY title
) AS t2 ON t2.title = table1.title
GROUP BY domain
ORDER BY table1.title DESC
) a;
I have two tables, lets say table1 and table2 with common columns, id and update_date. I am looking to get the id's and update_date based on latest update_date in descending order. I have used 'union' and 'order by' together which gave the results in descending order of update_date but there are duplicate id's which I am not sure how to get rid of.
My query is like,
(select id,update_date from table1 where [condition])
UNION
(select id,update_date from table2 where [condition])
order by update_date desc;
I can just get rid of the duplicate id's by adding select distinct id from (above query) as temp; but the problem is that I need the update_date too.
Can anyone suggest how to get rid of duplicates and still get both id and update_date information.
Assuming you want the latest update out of duplicates this one should work:
SELECT id, max(update_date) AS last_update
FROM
( (select id,update_date from table1 where [conditions])
UNION
(select id,update_date from table2 where [conditions]) ) both_tables
GROUP BY id
ORDER by last_update DESC
Wrap the query in a DISTINCT block:
SELECT DISTINCT * FROM (
select id,update_date from table1 where [condition]
UNION
select id,update_date from table2 where [condition]
)
order by update_date desc;
Limit the second query's results:
select id, update_date
from table1
where [conditions]
union
select id, update_date
from table2
where [conditions]
and id not in (select id from table1 where [conditions])