I am lost in MySQL documentation. I have a table with votes - it has these columns
id
song_id
user_id
created
I cannot find the query which will process the information and output the 10 most voted songs in a given time period. What is it?
SELECT id, song_id, COUNT(1) AS total
FROM votes
WHERE created BETWEEN [user_defined_start_date] AND [user_defined_end_date]
GROUP BY song_id
ORDER BY total DESC
LIMIT 10;
How exactly are you storing the votes?
Replace the words in [] below with the variables you want.
select song_id
from [table]
where created > to_date('[the date you want]', '[the format you want]') and created < to_date('[the date you want]', '[the format you want]')
order by [votes]
limit 10;
Related
I have a user_certifications table, with these props:
int user_id
int cert_id
timestamp last_updated
So, a user can be listed multiple times with a variety of certs. I have a list of user_ids for which I need to get ONLY the most recently updated record. So for one user it would be:
SELECT user_id, last_updated
FROM user_certifications
WHERE user_id == x
ORDER BY last_updated DESC
LIMIT 1
How do I do this efficiently if I need ONLY the last dated entry for each of a number of users. E.g. similar query, but WHERE user_id IN (x,y,z) and returning one entry per user, with the latest date?
P.S. - I apologize for the title, I don't know how to word this.
Use distinct on:
SELECT DISTINCT ON (user_id), uc.*
FROM user_certifications uc
ORDER BY user_id, last_updated DESC
I have a Postgresql table with about 50 million entries. Now i want to find two id's, by looking for the first time and the last time a timestamp appears, to give me a range of entries.
Finding the "first" id takes about 100 milliseconds.
But finding the second id takes about 3 minutes.
Query for finding the first id
SELECT id
FROM transactions
WHERE "hashBlock" =
(SELECT hash
FROM blocks
WHERE n_time > 1262300400
ORDER BY id ASC
LIMTI 1)
ORDER BY id ASC
LIMTI 1
Query for finding the second id
Select id
FROM transactions
WHERE "hashBlock" =
(SELECT hash
FROM blocks
WHERE n_time < 1306879200
ORDER BY id DESC
LIMTI 1)
ORDER BY id DESC
LIMTI 1
I guess the longer runtime result in the query going from the first id until it find an id which satisfies the query and the second one starting at the last id.
Is there any way to speed up the second query?
I would create the following indexes, if you don't have them already:
create index ix1 on transactions ("hashBlock", id);
create index ix2 on blocks (n_time, id, hash);
Hi all I am using SQL server.
I have one table that has a whole list of details on cars and events that have happened with those cars.
What I need is to be able to pick out the last entry for each vehicle based on their (Reg_No) registration number.
I have the following to work with
Table name = UnitHistory
Columns = indx (This is just the primary key, with increment)
Transdate(This is my date time column) and have Reg_No (Unique to each vehicle) .
There are about 45 vehicles with registration numbers if that helps?
I have looked at different examples but they all seem to have another table to work with.
Please help me. Thanks in advance for the help
WITH cte
AS
(
SELECT *,
ROW_NUMBER() OVER
(
PARTITION BY Reg_No
ORDER BY Transdate DESC
) AS RowNumber
FROM unithistory
)
SELECT *
FROM cte
WHERE RowNumber = 1
If you only need the index and the transdatem and they are both incremental (I am assuming that a later date corresponds to a higher index number) then the simplest query would be:
SELECT Reg_No, MAX(indx), MAX(Transdate)
FROM UnitHistory
GROUP BY Reg_No
If you want all data for a known Reg_No, you can use Dd2's answer
If you want a list of all Reg_No's with thier data, you will need a subquery
I am new to SQL. I have a table which contains information about ads. Which function should I use, to find id of all ads posted within the past three days?
For Oracle DB, use this,
SELECT id
FROM tablename
WHERE TRUNC(pdate) >= TRUNC(SYSDATE) - 3;
SQLFIDDLE DEMO
Select ID --the id field
From tblAds --the ads table
WHERE Ad_Date >= DATEADD(day,-3,getDate()) --the filter. I am assuming Ad-Date is the name of the ad's date field
order by Ad_Date ASC -- order it
Assume a table with the following columns:
pri_id, item_id, comment, date
What I want to have is a SQL query that will delete any records, for a specific item_id that are older than a given date, BUT only as long as there are more than 15 rows for that item_id.
This will be used to purge out comment records older than 1 year for the items but I still want to keep at least 15 records at any given time. This way if I had one comment for 10 years it would never get deleted but if I had 100 comments over the last 5 days I'd only keep the newest 15 records. These are of course arbitrary record counts and date timeframes for this example.
I'd like to find a very generic way of doing this that would work in mysql, oracle, postgres etc. I'm using phps adodb library for DB abstraction so I'd like it to work well with that if possible.
Something like this should work for you:
delete
from
MyTable
where
item_id in
(
select
item_id
from
MyTable
group by
item_id
having
count(item_id) > 15
)
and
Date < #tDate
You want to keep at least 15 of them always, correct? So:
DELETE
FROM CommentTable
WHERE CommentId NOT IN (
SELECT TOP 15 CommentId
FROM CommentTable
WHERE ItemId=#ItemId
AND CommentDate < #Date
ORDER BY CommentDate DESC
)
AND ItemId=#ItemId
AND CommentDate < #Date
Is this what you're looking for?
DELETE
[MyTable]
WHERE
[item_id] = 100 and
(SELECT COUNT(*) FROM [MyTable] WHERE [item_id] = 100) > 15
I'm a MS SQL Server guy, but i think it should work elsewhere.