I have a calendar and user_result table and I need to join these two queries.
calendar query
SELECT `week`, `date`, `time`, COUNT(*) as count
FROM `calendar`
WHERE `week` = 1
GROUP BY `date`
ORDER BY `date` DESC
and the result is
{"week":"1","date":"2014-08-21","time":"15:30:00","count":"4"}, {"week":"1","date":"2014-08-20","time":"17:30:00","count":"12"}
user_result query
SELECT `date`, SUM(`point`) as score
FROM `user_result`
WHERE `user_id` = 1
AND `date` = '2014-08-20'
and the result is just score 3
My goal is to always show calendar even if the user isn't present in the user_result table, but if he is, SUM his points for that day where calendar.date = user_result.date. Result should be:
{"week":"1","date":"2014-08-21","time":"15:30:00","count":"4","score":"3"}, {"week":"1","date":"2014-08-20","time":"17:30:00","count":"12","score":"0"}
I have tried this query below, but the result is just one row and unexpected count
SELECT c.`week`, c.`date`, c.`time`, COUNT(*) as count, SUM(p.`point`) as score
FROM `calendar` c
INNER JOIN `user_result` p ON c.`date` = p.`date`
WHERE c.`week` = 1
AND p.`user_id` = 1
GROUP BY c.`date`
ORDER BY c.`date` DESC
{"week":"1","date":"2014-08-20","time":"17:30:00","count":"4","score":"9"}
SQL Fiddle
ow sorry, i was edited, and i was try at your sqlfiddle, if you want to show all date from calendar you can use LEFT JOIN, but if you want to show just the same date between calendar and result you can use INNER JOIN, note: in this case INNER JOIN just show 1 result, and LEFT JOIN show 2 results
SELECT c.`week`, p.user_id, c.`date`, c.`time`, COUNT(*) as count, p.score
FROM `calendar` c
LEFT JOIN
(
SELECT `date`, SUM(`point`) score, user_id
FROM `result`
group by `date`
) p ON c.`date` = p.`date`
WHERE c.`week` = 1
GROUP BY c.`date`
ORDER BY c.`date` DESC
I put a pre-aggreate query / group by date as a select for the one person you were interested in... then did a left-join to it. Also, your column names of week, date and time (IMO) are poor choice column names as they can appear to be too close to reserved keywords in MySQL. They are not, but could be confusing..
SELECT
c.week,
c.date,
c.time,
coalesce( OnePerson.PointEntries, 0 ) as count,
coalesce( OnePerson.totPoints, 0 ) as score
FROM
calendar c
LEFT JOIN ( select
r.week,
r.date,
COUNT(*) as PointEntries,
SUM( r.point ) as totPoints
from
result r
where
r.week = 1
AND r.user_id = 1
group by
r.week,
r.date ) OnePerson
ON c.week = OnePerson.week
AND c.date = OnePerson.date
WHERE
c.week = 1
GROUP BY
c.date
ORDER BY
c.date DESC
Posted code to SQLFiddle
Related
I have a query that is to pull at max one entry per day per specified id. The query is returning multiple values per day despite max being specified. I need some assistance tweaking this so that I only get one entry per day. The image shows a snippet of the data that is returned.
SELECT a.*
FROM turtle_derby AS a
INNER JOIN (SELECT turtle_id, DATE(`date`) AS day_date, MAX(`date`) AS maxdate
FROM turtle_derby
GROUP BY turtle_id, day_date) AS groupedtt
ON a.turtle_id = groupedtt.turtle_id
AND a.turtle_id = '175846'
AND a.`date` = groupedtt.maxdate
AND a.`date` > '2018-07-26'
ORDER BY `a`.`date`
Your data has multiple rows are at the same time of the day. If that time is the last time of the day, you'll get all of them. You need to get the maximum of two columns, date and loc_id. See SQL : Using GROUP BY and MAX on multiple columns
SELECT a.*
FROM turtle_derby AS a
INNER JOIN (
SELECT t2.turtle_id, MAX(t2.loc_id) AS max_id
FROM (
SELECT turtle_id, MAX(date) AS maxdate
FROM turtle_derby AS
GROUP BY turtle_id, DATE(date)
) AS t1
INNER JOIN turtle_derby AS t2
ON t1.turtle_id = t2.turtle_id AND t1.maxdate = t2.date
GROUP BY t1.turtle_id, t1.maxdate
) AS groupedtt
ON a.turtle_id = groupedtt.turtle_id
AND a.loc_id = groupedtt.maxloc
WHERE a.turtle_id = '175846'
AND a.`date` > '2018-07-26'
ORDER BY `a`.`date`
I have this code:
SELECT a.id, a.to_id, a.from_id, a.seen, a.date, a.message
FROM `Chat_messages` a
INNER JOIN (
SELECT MAX( `id` ) AS id
FROM `Chat_messages` AS `alt`
WHERE `alt`.`to_id` =7
OR `alt`.`from_id` =7
GROUP BY `to_id` , `from_id`
)b ON a.id = b.id
returning:
So, I want to get conversations (sent and received messages) of an user and latest message of it.
Latest message works ok, but the problem is that I get 2 rows from messages received (#1 and #2) and 2 rows from messages sent (#3 and #4), but I only need 2 results, because there are 2 conversations.
The best way to pick out the row that holds the latest sent message and the row that holds the latest received message respectively, is using the row_number() window function. Unfortunately, MySql does not support window functions, so I think it's best to use two nested SELECT's:
SELECT z.id, max(z.to_id), max(z.from_id), max(z.seen), max(z.date), max(z.message)
FROM chat_messages z
LEFT JOIN
(SELECT x.from_id, max(date) date
FROM chat_messages x
GROUP BY x.from_id) f
ON z.from_id = f.from_id AND z.date = f.date
LEFT JOIN
(SELECT y.to_id, max(date) date
FROM chat_messages y
GROUP BY y.to_id) t
ON z.to_id = t.to_id AND z.date = t.date
GROUP BY z.id
I do not recommend using max on ID's if you care about correctness in the long run.
You can group by least(to_id, from_id), greatest(to_id, from_id) to make sure conversions between 2 people are merged:
SELECT a.id, a.to_id, a.from_id, a.seen, a.date, a.message
FROM `Chat_messages` a
INNER JOIN (
SELECT MAX( `id` ) AS id
FROM `Chat_messages` AS `alt`
WHERE `alt`.`to_id` =7
OR `alt`.`from_id` =7
GROUP BY least(`to_id` , `from_id`), greatest(`to_id` , `from_id`)
)b ON a.id = b.id
I have the following tables in my DB
EVENT
ID, TITLE, ...
VOTES
ID, TYPE, ID_EVENT
COMMENTS
ID, COMMENT, ID_EVENT
DATES
ID, DATE, ID_EVENT
One EVENT has many actions, has many comments and has many dates.
I'm using following query to retrieve info from EVENTS table, and for each event retrieve the number of votes, the number of comments and each one of the dates. For events with one of their date = tomorrow (2015-04-03)
SELECT events.id,
events.title,
GROUP_CONCAT(dates.date) AS dates,
COUNT(distinct votes.id) AS votes,
COUNT(distinct comments.id) AS comments
FROM events
LEFT JOIN dates on dates.post_id = events.id
LEFT JOIN votes on votes.post_id = events.id AND votes.type = 1
LEFT JOIN comments on comments.votes_id = votes.id
WHERE dates.date = CURDATE() + INTERVAL 1 DAY
GROUP BY events.id
Result looks like this
id title dates votes comment
33 Event33 2015-04-03,2015-04-03,2015-04-03 4 0
39 Event39 2015-04-03 9 1
Why the dates column repeats the same date (tomorrow)??? The dates of Event33 should be 2015-04-01, 2015-04-02, 2015-04-03.
What is wrong?
You want the events that have one of their dates tomorrow. Your query does that but it also cuts off all other dates.
You need an extra join or an EXISTS subquery.
You also need a DISTINCT on the GROUP_CONCAT(), the same way you used it at the COUNT() aggregate:
SELECT events.id,
events.title,
GROUP_CONCAT(DISTINCT dates.date) AS dates,
COUNT(DISTINCT votes.id) AS votes,
COUNT(DISTINCT comments.id) AS comments
FROM events
LEFT JOIN dates ON dates.post_id = events.id
LEFT JOIN votes ON votes.post_id = events.id AND votes.type = 1
LEFT JOIN comments ON comments.votes_id = votes.id
WHERE EXISTS
( SELECT 1
FROM dates AS dd
WHERE dd.date = CURDATE() + INTERVAL 1 DAY
AND dd.post_id = events.id
)
GROUP BY events.id ;
Another way would be using inline subqueries. No need for GROUP BY or DISTINCT in this. A minor disadvantage in your case, is that the join to comments is through votes, so one subquery has an extra join:
SELECT e.id,
e.title,
( SELECT GROUP_CONCAT(d.date)
FROM dates AS d
WHERE d.post_id = e.id
) AS dates,
( SELECT COUNT(v.id)
FROM votes AS v
WHERE v.post_id = e.id AND v.type = 1
) AS votes,
( SELECT COUNT(c.id)
FROM comments AS c
JOIN votes AS v ON c.votes_id = v.id
WHERE v.post_id = e.id AND v.type = 1
) AS comments
FROM events AS e
WHERE EXISTS
( SELECT 1
FROM dates AS dd
WHERE dd.date = CURDATE() + INTERVAL 1 DAY
AND dd.post_id = events.id
) ;
LEFT JOIN
(
SELECT user_id, review, COUNT(user_id) totalCount
FROM reviews
GROUP BY user_id
) b ON b.user_id= b.user_id
I am trying to fit WHERE LENGTH(review) > 100 in this somewhere but every I put it, it gives me problems.
The sub-query above counts all total reviews by user_id. I simply want to add one more qualification. Only count reviews greater than 100 length.
On a side note, I've seen the function CHAR_LENGTH -- not sure if that i what I need either.
EDIT:
Here is complete query working perfectly as expected for my needs:
static public $top_users = "
SELECT u.username, u.score,
(COALESCE(a.totalCount, 0) * 4) +
(COALESCE(b.totalCount, 0) * 5) +
(COALESCE(c.totalCount, 0) * 1) +
(COALESCE(d.totalCount, 0) * 2) +
(COALESCE(u.friend_points, 0)) AS totalScore
FROM users u
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM items
GROUP BY user_id
) a ON a.user_id= u.user_id
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM reviews
GROUP BY user_id
) b ON b.user_id= u.user_id
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM ratings
GROUP BY user_id
) c ON c.user_id = u.user_id
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM comments
GROUP BY user_id
) d ON d.user_id = u.user_id
ORDER BY totalScore DESC LIMIT 25;";
LENGTH() returns the length of the string measured in bytes. You probably want CHAR_LENGTH() as it will give you the actual characters.
SELECT user_id, review, COUNT(user_id) totalCount
FROM reviews
WHERE CHAR_LENGTH(review) > 100
GROUP BY user_id, review
You're also not using GROUP BY correctly.
See the documentation
The query that you want is:
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount,
sum(case when length(review) > 100 then 1 else 0 end
) as NumLongReviews
FROM reviews
GROUP BY user_id
) b ON b.user_id= b.user_id
This counts both the reviews and the "long" reviews. That count is done using a case statement nested in a sum() function.
I have a table called Request.
Other tables are linked to the Request table through a request id.
There is a TwitterTweet table and a FacebookPost table.
So a single request can have 50 TwitterTweets and/or 20 FacebookPosts or any amount of Tweets/Posts
We can add them together for a total count of 70.
I'm trying to create a query that could tell me what is the request with the highest total count.
I know this is wrong:
(I attempted to just order them by the counts within the TwitterTweet, but it would not let me do an OUTER JOIN which I thought
would bring back the Count.count column. It forced me to do a Left Join for it to compile. My Logic was to do a join so
that the results were calculated for each row by the requestid)
SELECT r1.`id` AS requestid, r1 . *
FROM `Request` AS r1
LEFT JOIN
(SELECT COUNT( * ) AS count, rid
FROM
((SELECT `TwitterTweet`.`id` AS `smid` , `TwitterTweet`.`requestid` AS rid
FROM `TwitterTweet`
WHERE `TwitterTweet`.`requestid` = requestid
AND `TwitterTweet`.`active` =1) AS talias
)) AS Count ON ( Count.rid = requestid )
ORDER BY Count.count
*When I tried to add in the Facebook side it would not compile any more
(The concept is that the results are added from TwitterTweet with the results from FacebookPost
that are attached to the specific requestid which would give us a count. The entire result
set should be ordered by that count)
SELECT r1.`id` AS requestid, r1 . *
FROM `Request` AS r1
LEFT JOIN
(SELECT COUNT( * ) AS count, rid
FROM
((SELECT `TwitterTweet`.`id` AS `smid` , `TwitterTweet`.`requestid` AS rid
FROM `TwitterTweet`
WHERE `TwitterTweet`.`requestid` = requestid
AND `TwitterTweet`.`active` =1 ) AS talias
UNION All
(SELECT `FacebookPost`.`id` AS `smid`, `FacebookPost`.`requestid` AS rid
FROM `FacebookPost`
WHERE `FacebookPost`.`requestid` = requestid
AND `FacebookPost`.`active` = 1) as falias
)) AS Count ON ( Count.rid = requestid )
ORDER BY Count.count
I updated the Query with an attempt to add an alias:
SELECT rid, SUM(count) total_count
FROM
(
(SELECT COUNT(*) AS count, r.rid
FROM request r
JOIN TwitterTweet tt
ON r.id = tt.requestid
WHERE tt.active = 1
GROUP BY r.rid) AS twitter
UNION ALL
(SELECT COUNT(*) AS count, r.rid
FROM request r
JOIN FacebookPost fp
ON r.id = fp.requestid
WHERE fp.active = 1
GROUP BY r.rid ) AS fbook
)
GROUP BY rid
ORDER BY SUM(count) DESC
I made another adjustment to give the middle subquery an alias, but now I only get one row returned with a zero in the rid column and 5686 in the total_count column...the 5686 might be all of the results.
SELECT counts.rid, SUM(count) total_count
FROM
(
SELECT COUNT(*) AS count, r.requestid AS rid
FROM request r
JOIN TwitterTweet tt
ON r.id = tt.requestid
WHERE tt.active = 1
GROUP BY r.requestid
UNION ALL
SELECT COUNT(*) AS count, r.requestid AS rid
FROM request r
JOIN FacebookPost fp
ON r.id = fp.requestid
WHERE fp.active = 1
GROUP BY r.requestid
) AS counts
GROUP BY counts.rid
ORDER BY SUM(count) DESC
Got it!!!
Thanks for your help guys, I had to remove those joins on the request:
SELECT counts.rid, SUM(count) total_count
FROM
(
SELECT COUNT(*) AS count, tt.requestid AS rid
FROM TwitterTweet tt
WHERE tt.active = 1
GROUP BY tt.requestid
UNION ALL
SELECT COUNT(*) AS count, fp.requestid AS rid
FROM FacebookPost fp
WHERE fp.active = 1
GROUP BY fp.requestid
) AS counts
GROUP BY counts.rid
ORDER BY SUM(count) DESC
SELECT id, SUM(count) total_count
FROM
(
SELECT COUNT(*) AS count, r.id
FROM request r
JOIN TwitterTweet tt
ON r.id = tt.requestid
WHERE tt.active = 1
GROUP BY r.id
UNION ALL
SELECT COUNT(*) AS count, r.id
FROM request r
JOIN FacebookPost fp
ON r.id = fp.requestid
WHERE fp.active = 1
GROUP BY r.id
) sub
GROUP BY id
ORDER BY SUM(count) DESC
;