I have a 1 table database that has a list of advertisements.
I am trying to grab the LATEST advertisement for EACH resort.
I know it should probably be a table database, but how would I go about doing so.
Assuming the Id column is unique:
SELECT T3.*
FROM yourtable AS T3
JOIN
(
SELECT T2.resort, T2.date_added, MAX(T2.id) AS id
FROM yourtable AS T2
JOIN
(
SELECT resort, MAX(date_added) AS date_added
FROM yourtable
GROUP BY resort
) AS T1
ON T1.resort = T2.resort AND T1.date_added = T2.date_added
GROUP BY T2.resort, T2.date_added
) AS T4
ON T4.id = T3.id
select t.*
from YourTable t
join
(select resort, max(dateAdded) dateAdded
from YourTable
group by resort) m on t.dateAdded = m.dateAdded and t.resort = m.resort
order by t.resort
First group the rows by resort to find the max of dateAdded, then query (join) the rows that have the same dateAdded with the max.
One problem is, if the same resort b is added in the same time, twice or more, it will take only the first row. But I think is slightly possible.
Have a look at this article for selecting the "maximum" (say, most recent date) item from a group of items.
Use GROUP BY function
SELECT MAX(dateAdded), resort, linkToUrl FROM `your_table` GROUP BY resort
I think this should do it:
SELECT * FROM advertisements GROUP BY resort ORDER BY dateAdded DESC
Related
Yesterday I tried to retrieve data from my db table using 'user_id' as a criterion to limit the amount of data per user.
I tried to get data from table https://prnt.sc/p53zhp in format like this https://prnt.sc/p541wk and limit the number of output records for user_id where limit will be 2 (count(user_id) <= 2), but i don't understand how to do that. What kind of sql request can i use to get this data?
Assuming that your RDBMS, here is a solution yo select only the top 2 records per user. You can use ROW_NUMBER() in a subquery to rank records by id within groups of records having the same user_id, and the filter out unerelevant records in the outer query, like:
SELECT *
FROM (
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY id)
FROM mytable
) x WHERE rn <= 2
On earlier versions of MySQL, you could use self-LEFT JOIN the table and use GROUP BY and HAVING COUNT(...) < 2 to limit the results to first two records per group:
SELECT
t.id,
t.user_id,
t.vip,
t.title,
t.description,
t.data
FROM mytable t
LEFT JOIN mytable t1 ON t1.user_id = t.user_id AND t1.id > t.id
GROUP BY
t.id,
t.user_id,
t.vip,
t.title,
t.description,
t.data
HAVING COUNT(t1.id) < 2
I don't understand if your problem is a Transact-SQL or your code.
In SQL you can limit record with "LIMIT": https://www.w3schools.com/sql/sql_top.asp
In code, you can use a condition IF.
This is my table structure
and this is my dataset
What I want is query that gets data ordered by date desc and group by id_patient
so the result in the dataset example should be like this:
I would go with limit clause with subquery since you have PK :
select *
from table t
where id = (select t1.id
from table t1
where t1.id_patient = t.id_patient
order by t1.date desc
limit 1
);
However, if single patient has multiple same dates then this would produce only single records based on date.
SELECT * from rdv a JOIN (SELECT id_patient,MAX(date) date FROM rdv GROUP by id_patient ) b on a.id_patient = b.id_patient and a.date = b.date
If you want the latest record for each patient, then you are not looking for an aggregation. I would often approach this with a correlated subquery:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.id_patient = t.id_patient);
SELECT *
FROM table
GROUP BY group by id_patient
ordered by DATE(date) desc;
I am having trouble with this query and am hoping someone can help.
SELECT
myTable.id,
myTable.subject,
myTable.upvotes,
myTable.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = myTable.id) AS comments_count
FROM myTable
Basically I have a table with posts and comments, the comments thread is tied to the id of the original post. In my query I want to show how many relpies (how many threads = id) from all rows for the current id/row.
I hope that makes sense :)
You need to specify the the table with new alias to match in your subquery other wise it will match the thread with id from same table
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = m.id) AS comments_count
FROM myTable m
Or Better to use LEFT JOIN
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
COUNT(mm.id) AS comments_count
FROM myTable m
LEFT JOIN myTable mm ON(mm.thread = m.id)
GROUP BY m.id
I have Problems with a select statement, as a little help here are the important columns:
Table1
ID NAME
TABLE 2
ID U_ID COUNTER
The ID of Table 1 Matches the U_ID of Table 2. Table 2 contains many entries for the same u_id.
What I want to do is to get the Name of the "user" (table 1) who has in sum the max. counter.
What I got since now is the join of the tables (Where clause depends on other rows which are not important for the problem).
Can anyone help me on this issue?
So what you need is an aggregate of an aggregate (max of sum of column). The easiest will be to create a view providing the sum and u_id end then select the max of it:
create view table2sums
as
select u_id, sum(counter) as total
from table2
group by u_id;
and then
select t1.name
from table1 t1, table2sums t2
where t1.id = t2.u_id
and t2.total >= all (
select total
from table2sums
)
In this special case you can also do it directly:
select t1.name
from table1 t1, table2 t2
where t1.id = t2.u_id
group by t1.name
having sum(t2.counter) >= all (
select sum(counter)
from table2
group by t2.u_id
)
NOTE: The other proposed solutions will show a better performance. My solution only selects the name (which is what you said you wanted) and works in any RDBMS.
There exist RDBMS without the LIMIT possibility.
In the end, I'd say: regard my solution as educational, the others as practical
SELECT name,
SUM(counter) as counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY counter DESC
LIMIT 1
You can try this:
SELECT name, SUM(counter) as total_counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY total_counter DESC
LIMIT 1
Working Demo: http://sqlfiddle.com/#!2/45419/4
I need a little help to count and group some MySql table records according of a range of dates given.
The table strcutre is this:
In advance, thank you for your help
This shoud give you the result that you need:
SELECT
d1.dt,
COUNT(DISTINCT t1.recid) AS departures,
COUNT(DISTINCT t2.recid) AS returns,
COUNT(DISTINCT t1.recid) + COUNT(DISTINCT t2.recid) AS total
FROM (SELECT departure_date AS dt FROM yourtable
UNION SELECT return_date FROM yourtable) d1
LEFT JOIN yourtable t1 ON d1.dt = t1.departure_date
LEFT JOIN yourtable t2 ON d1.dt = t2.return_date
GROUP BY
d1.dt
The first subquery will return all dates, not duplicated, present in your table (departures and returns).
I'm then trying to join each date to a departure date, using a LEFT JOIN. I'm then counting the DISTINCT t1.recid that make the join succeed..
I'm then trying to join each date to a return date, using LEFT JOIN. Total is the sum of both counts.
Fiddle is here.