When I used the
SELECT * FROM reports WHERE staff='$username' GROUP BY taskid
on my query, I get a result of the first row from that group.
What do I need to add to get the result from the last row of that group?
last row means having id greater than the other row from that group.
I tried adding
ORDER BY id DESC
or
ORDER BY id
but it did not return the intended result.
You are using a group by function without any aggregate functions. You probably want to do an order by instead (No group by in the query):
SELECT * FROM reports WHERE staff='$username' order BY taskid desc;
Group By functions are commonly used when you want to use an aggregate function on a particular column (such as get an average row value, or a sum) and the like. If you are not using any aggregate function, then using Group By will not do anything.
If you only want to get one row from the query you can add a limit clause like this:
SELECT * FROM reports WHERE staff='$username' order BY taskid desc limit 1;
if you want the "last" row per group, then you need to specify which field defines uniqueness (i.e. what you mean by "first/last row") and then isolate those rows in a subquery.
e.g.
this gets you the max(id) for each group
SELECT taskid, max(id) as max_id FROM reports
WHERE staff ='$username'
GROUP BY taskid
and this gets the entire row(s):
select * from reports where id
in
(
SELECT max(id) as max_id FROM reports
WHERE staff='$username'
GROUP BY taskid
) x
This of course assumes that id is unique and assigned in ascending order and that therefore max(id) indicates the last row per group.
Alternatively you could rewrite this using a join:
select * from reports r
inner join
(
SELECT max(id) as max_id FROM reports
WHERE staff='$username'
GROUP BY taskid
) x
on r.id = x.max_id
Related
i need to create query with group by and order by, and i dont know how to do it.
query should return one record for the newest date for existing device_serial_number. enter image description here
so i would to get id 591 nad 592
solution can be in sql or the best way it will be in symfony, through query builder etc.
There are many ways to accomplish what you want.
First Way
The oldest way to select first, best, worst, whatever within a group is with a correlated subquery:
Select * from mytable outer
Where created_at = (
Select max(created_at)
from mytable inner
Where inner.device_serial_number = outer.device_serial_number
)
Second Way
Use a subselect to find earliest dates for all devices, them join back to the original table to filter:
Select a.*
From mytable a Inner Join
(Select device_serial_number, max(created_at) as latedate
From mytable b
Group By device_serial_number
) b
On a.device_serial_number=b.device_serial_number
And a.created_at=b.latedate
Third way
Use a window function to rank order all the dates and then pick the number one ranking.
Select * From (
Select *
, rank() Over (Partition By device_serial_number Order by created_at desc) as myrank
From mytable
)
Where myrank=1
Notice that while these 3 solutions use different aspects of SQL, they all have a common analytical approach. They are all two step processes whose first (inner) part involves finding the most recent created_at date for each device_serial_number and then reapplying that result back to the original table in the second (outer) part.
Here is
I want to show rows with same stop name one time..
How i use Query and While Loop
I see you have and id column. Assuming that it is unique you can do this all in sql query, no need for while loop.
You will need 2 queries; first will get the maximum (could be minimum also) available id of only one distinct stop name, the second is a join query with the first results and the main table. Something like this:
select * from tablename
inner join
(
select stop, max(id) as id from tablename
group by stop
)
as uniqueIDs
on tablename.id=uniqueIDs.id
u may try this..this will help you to fetch duplicates from table
SELECT tablename.stop FROM tablename INNER JOIN
(SELECT stop FROM tablename GROUP BY stop HAVING COUNT(id) > 1) dup
ON tablename.stop = dup.stop;
I am not sure if this is possible or not
I have a table with orders and userid
I need to do a SELECT query with ORDER BY the least number of orders of a customer
Example:
userid1 has 10 orders
userid2 has 5 orders
Then SELECT should show userid2 at the top
Thanks in advance
If you have orders as ordersCount i mean as a field which keeps counts of orders then
Select * FROM YourTable order by orders ASC
Else if you have a record for each order then
Select * FROM YourTable Group by UserID order by count(*) ASC;
Apply limit like this
Select *
FROM YourTable
Group by UserID
order by count(*) ASC
limit startIndex, fetchSize;
where startIndex and fetchSize are int that define your limit
You need to group by the userid so you can count orders by user.
SELECT userid, count(1) FROM orders GROUP BY userid ORDER BY count(1) asc
You can accomplish that using GROUP BY and ordering by COUNT().
It would be something like that.
SELECT userid, COUNT(*) qt
FROM orders
GROUP BY userid
ORDER BY qt
If each order is in its own row, however, you need to aggregate them:
SELECT * from MYTABLENAME GROUP BY userid ORDER BY COUNT(*)
Count is what you're looking for
SELECT count(theField) as theCount, *
FROM theTable
GROUP BY theField
ORDER BY theCount
You can select the COUNT of a column and give it a name, then use that name to sort by. Here's an example:
SELECT
p.*,
(SELECT COUNT(o.order_id) FROM orders AS o WHERE p.partron_id=p.patron_id) AS orderCount
FROM
patrons AS p
WHERE
...
ORDER BY
orderCount ASC
UPDATE: Just realized this is only useful if you have orders in a separate table than patrons. Disregard if you are only looking at a single table.
This query expects you to have two tables, a patron table with patron information like name, and a orders table that has a patron_id column that matches the identity column in the patron table.
The advantage to doing this is that you can request patron information at the same time, so you don't need to run two queries in PHP if you're going to use that information.
I'm using a database to store results of an election with the columns id, candidate, post_time and result. Results are put in the database during 'counting the votes'. When a new update is available, a new entry will be inserted.
From this database, I would like to create a table with the most recent results (MAX post_time) per candidate (GROUP BY candidate), ordered by result (ORDER BY result).
How can I translate this to a working SQL-statement?
(I've tried mysql order and groupby without success)
I've tried:
SELECT *, MAX(time_post)
FROM [database]
GROUP BY candidate
HAVING MAX(time_post) = time_post
ORDER BY result
Assuming that you don't have multiple results per candidate at same time, next should work:
select r.candiate, r.result
from results r
inner join (
select candidate, max(post_time) as ptime
from results
group by candidate
) r2 on r2.candiate=r.candidate and r2.ptime=r.post_time
order by r.result
Note that MAX will not select the record with the maximum time, but it will select the maximum value from any record. So
SELECT MAX(a), MAX(b) FROM example
where exmple contains the two records a=1, b=2 and a=4, b=0, will result in a=4, b=2, which wasn't in the data. You should probably create a view with the latest votes only from each candidate, then query that. For performance, it may be sensible to use a materialized view.
Is the post_time likely to be the same for all the most recent results? Also does each candidate only appear once per post_time?
This could be achieved by just using a SELECT statement. Is there a reason you need the results in a new table?
If each candidate only appears once per post_time:
SELECT candidate, result
FROM table
WHERE post_time = (SELECT MAX(post_time) FROM table)
If you want to count how many times a candidate appears in the table for the last post_time:
SELECT candidate, count(result) as ResultCount
FROM table
WHERE post_time = (SELECT MAX(post_time) FROM table)
GROUP BY candidate
By what i see from ur attempts i'd think you should use this
SELECT MAX(post_time) FROM `table` GROUP BY candidate ORDER BY result
but the MAX statment only return a single value therefore i dont see why ORDER BY would be needed.
if you want multiple results try looking up the TOP statment
One way (tied results shown):
SELECT t.*
FROM tableX AS t
JOIN
( SELECT candidate
, MAX(time_post) AS time_post
FROM tableX
GROUP BY candidate
) AS m
ON (m.candidate, m.time_post) = (t.candidate, t.time_post)
ORDER BY t.result
and another one (no ties, only one row per candidate shown):
SELECT t.*
FROM
( SELECT DICTINCT candidate
FROM tableX
) AS d
JOIN
tableX AS t
ON t.PK = --- the Primary Key of the table, here
( SELECT ti.PK --- and here
FROM tableX AS ti
WHERE ti.candidate = d.candidate
ORDER ti.time_post DESC
LIMIT 1
)
ORDER BY t.result
I have two queries, the only difference being the GROUP BY clause
SELECT * FROM `packages_sorted_YHZ` WHERE `hotel_city` = 'Montego Bay'
ORDER BY `deal_score` DESC
LIMIT 0,3;
SELECT * FROM `packages_sorted_YHZ` WHERE `hotel_city` = 'Montego Bay'
GROUP BY `hotel_name`
ORDER BY `deal_score` DESC
LIMIT 0,3;
The first query returns the first result with a deal_score of 75 but the second query returns the first result with the deal_score of just 72.
I would have thought that regardless of the GROUP BY clause, the first result would have the highest deal score possible (75)
The purpose of the GROUP BY clause is to optionally select a unique hotel_name for each result.
Does anyone know what I'm doing wrong here.
Without being able to look at all the data, my best guess is that Group By is merging the data and giving you an arbitrary value that matches the Where clause. This will happen if hotel name isn't unique, and you won't be given the maximum score unless you specifically query for it.
Try putting a Max() around deal_score. In MySQL, Group By can be used way too easily, I like how MSSQL enforces the use of aggregate functions and grouping by every field that isn't aggregated. Try this query:
SELECT `hotel_name`, MAX( `deal_score` ) AS `max_score` FROM `packages_sorted_YHZ` WHERE `hotel_city` = 'Montego Bay'
GROUP BY `hotel_name`
ORDER BY `max_score` DESC
LIMIT 0,3;
It looks like you are facing some very MySql specific issue. In theory, your second query is not valid and should return an error. But MySQL allows for selection of so called hidden columns - the columns that are not mentioned in a group by clause and not aggregated.
As stated in manual, hidden columns values are indeterminate, but in practice it usually picks up the first row walking the index used, regardless of sorting specified by ORDER BY, as sorting is performed after the grouping.
This is vendor-specific issue, so your second query should always fail if used to query other RDBMS. The correct implementation should be something like
SELECT max(`deal_score`) as maxdeal, `hotel_name` FROM `packages_sorted_YHZ` WHERE `hotel_city` = 'Montego Bay'
GROUP BY `hotel_name`
ORDER BY maxdeal
LIMIT 0,3;
You should not use GROUP BY but instead DISTINCT since you want a unique hotel_name.
example:
SELECT DISTINCT hotel_name -- add other fields here
FROM `packages_sorted_YHZ`
WHERE `hotel_city` = 'Montego Bay'
ORDER BY `deal_score` DESC
LIMIT 0,3;
SELECT max(deal_score) as maxdealscore, `hotel_name` * FROM `packages_sorted_YHZ` WHERE `hotel_city` = 'Montego Bay'
GROUP BY `hotel_name`
ORDER BY `deal_score` DESC
LIMIT 0,3;