how to select with group by order by and desc? - mysql

i want to select the data in a table such that it should group it by userid except one value in that column and order by date time and desc.
The problem i am getting is the grouped items are not ordering by date and time and in desc manner.
I mean the grouped item is showing earlier row.
How can i do that.
This is what i have done.
SELECT * FROM `tbljobs`
GROUP BY user_id
UNION ALL
SELECT * FROM tbljobs
WHERE user_id = '1'
ORDER BY date_time DESC
LIMIT 20"
where '1' is should not be grouped.

Your ORDER BY is only executed on the second statement. You have to use braces to order the whole results:
(SELECT *
FROM `tbljobs`
GROUP BY user_id)
UNION ALL
(SELECT *
FROM tbljobs
WHERE user_id = '1')
ORDER BY date_time DESC

Thank you friends for your suggestions. Finally I created the solution by myself after a lot of effort.
(
SELECT *
FROM (
SELECT *
FROM tbljobs
ORDER BY date_time desc
) AS A
WHERE user_id <> '1' group by user_id
)
UNION ALL
(
SELECT *
FROM tbljobs
WHERE user_id=1
)
ORDER BY date_time DESC

Related

mysql count return multiple rows in DESC order

I have read count only returns one row, however if you use group by you can get multiple rows returned as such
The code that returned the result above is
SELECT event_id, COUNT( event_id ) AS nr FROM picks GROUP BY
event_id DESC LIMIT 0 , 30
However I now need to find a way to order it by DESC order. I have tried order by but then I only get 1 row back and it looks like I cant use DESC with group by. Looking for advice
UPDATE
The following give me the result as displayed in image below
SELECT event_id, COUNT( event_id ) AS nr
FROM picks
GROUP BY event_id
Order by event_id DESC
LIMIT 0 , 30
Descending Order
SELECT event_id, COUNT( event_id ) AS nr
FROM picks
GROUP BY event_id
Order by event_id DESC
LIMIT 0 , 30
Ascending Order //This is the default sort order
SELECT event_id, COUNT( event_id ) AS nr
FROM picks
GROUP BY event_id
Order by event_id
LIMIT 0 , 30

Mysql complex query, group by with limit

I've the following table
store_visits: (store_id, city_id, date, visits, ...)
I want to select the maximum 5 stores ordered by visits.
SELECT X.*
FROM (
SELECT
store_id, SUM(visits) as sum_visits FROM store_visits
WHERE
(date <= '2014-06-28' AND date >= '2014-06-27')
AND
store_visits.city_id = 2
GROUP BY
store_id
ORDER BY
sum_visits desc
) X
LIMIT 5
I was wondering if there's a way to enhance the query to eleminate the temporary table and filesort.
Try this:
SELECT store_id, SUM(visits) AS sum_visits
FROM store_visits sv
WHERE sv.date <= '2014-06-28' AND sv.date >= '2014-06-27' AND sv.city_id = 2
GROUP BY store_id
ORDER BY sum_visits DESC LIMIT 5

Selecting most recent MySQL rows by MAX(time) WHERE time <= x

I am selecting the most recent entries of a MySQL table with:
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
ORDER BY most_recent DESC
My problem is, if I want to limit the maximum time with:
WHERE time <= nnn
The query does not work anymore. Is there a solution for with without a subquery?
Thanks in advance!
you can do it with subquery :
select t.userID, max(t.time)
from
(
select userID, time
from tableName
where time <= nnn
) t
group by t.userID
or simply :
select userID, max(time)
from tableName
where time <= nnn
group by userID
Use the HAVING clause like so:
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
HAVING MAX(time) <= nnn
ORDER BY most_recent DESC

Most common hour query?

I have this table:
ID(INT) DATE(DATETIME)
Under the DATE column there are a lot of different dates, and I want to figure out the most common hour between all the rows of the table, regardless of the day.
How can I do that with a MySQL query?
SELECT HOUR(date) AS hr, COUNT(*) AS cnt
FROM yourtable
GROUP BY hr
ORDER BY cnt DESC
LIMIT 1
relevant docs: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
Try this -
SELECT HOUR(`DATE`) AS `hour`, COUNT(*)
FROM `table`
GROUP BY `hour`
You could do a query like:
SELECT COUNT(daterow) AS occurrences FROM table GROUP BY daterow ORDER BY occurrences DESC LIMIT 1;
SELECT COUNT( id ) , HOUR( date )
FROM test
GROUP BY HOUR( date )
ORDER BY COUNT( id ) DESC
LIMIT 1

Sum results of a few queries and then find top 5 in SQL

I have 3 queries:
table: pageview
SELECT event_id, count(*) AS pageviews
FROM pageview
GROUP BY event_id
ORDER BY pageviews DESC, rand()
LIMIT 1000
table: upvote
SELECT event_id, count(*) AS upvotes
FROM upvote
GROUP BY event_id
ORDER BY upvotes DESC, rand()
LIMIT 1000
table: attending
SELECT event_id, count(*) AS attendants
FROM attending
GROUP BY event_id
ORDER BY attendants DESC, rand()
LIMIT 1000
I'd like to combine the event_ids of all 3 queries ordered by amount and then choose the top 5. How do I do that?
EDIT: HERE IS WHAT I DID TO MAKE IT HAPPEN:
SELECT event_id, sum(amount) AS total
FROM (
(SELECT event_id, count(*) AS amount
FROM pageview
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*) as amount
FROM upvote
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*) as amount
FROM attending
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
) x
GROUP BY 1
ORDER BY sum(amount) DESC
LIMIT 5;
To UNION the resulting rows of all three queries and then pick the 5 rows with the highest amount:
(SELECT event_id, count(*) AS amount
FROM pageview
GROUP BY event_id
ORDER BY pageviews DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*)
FROM upvote
GROUP BY event_id
ORDER BY upvotes DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*)
FROM attending
GROUP BY event_id
ORDER BY attendants DESC, rand()
LIMIT 1000)
ORDER BY 2 DESC
LIMIT 5;
The manual:
To apply ORDER BY or LIMIT to an individual SELECT, place the
clause inside the parentheses that enclose the SELECT.
UNION ALL to keep duplicates.
To add the counts for every event_id:
SELECT event_id, sum(amount) AS total
FROM (
(SELECT event_id, count(*) AS amount
FROM pageview
GROUP BY event_id
ORDER BY pageviews DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*)
FROM upvote
GROUP BY event_id
ORDER BY upvotes DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*)
FROM attending
GROUP BY event_id
ORDER BY attendants DESC, rand()
LIMIT 1000)
) x
GROUP BY 1
ORDER BY sum(amount) DESC
LIMIT 5;
The tricky part here is that not every event_id will be present in all three base queries. So take care that a JOIN does not lose rows completely and additions don't turn out NULL.
Use UNION ALL, not UNION. You don't want to remove identical rows, you want to add them up.
x is a table alias and shorthand for AS x. It is required for for a subquery to have a name. Can be any other name here.
The SOL feature FULL OUTER JOIN is not implemented in MySQL (last time I checked), so you have to make do with UNION. FULL OUTER JOIN would join all three base queries without losing rows.
Answer to follow-up question
SELECT event_id, sum(amount) AS total
FROM (
(SELECT event_id, count(*) / 100 AS amount
FROM pageview ... )
UNION ALL
(SELECT event_id, count(*) * 5
FROM upvote ... )
UNION ALL
(SELECT event_id, count(*) * 10
FROM attending ... )
) x
GROUP BY 1
ORDER BY sum(amount) DESC
LIMIT 5;
Or, to use the base counts in multiple ways:
SELECT event_id
,sum(CASE source
WHEN 'p' THEN amount / 100
WHEN 'u' THEN amount * 5
WHEN 'a' THEN amount * 10
ELSE 0
END) AS total
FROM (
(SELECT event_id, 'p'::text AS source, count(*) AS amount
FROM pageview ... )
UNION ALL
(SELECT event_id, 'u'::text, count(*)
FROM upvote ... )
UNION ALL
(SELECT event_id, 'a'::text, count(*)
FROM attending ... )
) x
GROUP BY 1
ORDER BY 2 DESC
LIMIT 5;