I am trying to output the most recent Call_date. I have tried using the MAX function with no luck. Below I have tagged 3 images showing the database tables, my current code output and the required output. Underneath that is my current code. Any help is appreciated!
Database Tables - https://imgur.com/a/7ZPFO
Output we are looking for - https://imgur.com/a/k3idB
Output my code currently gives - https://imgur.com/a/H53vq
Here is what I have tried:
SELECT Staff.First_name, Staff.Last_name, call_date, taken_by
FROM Issue
JOIN Caller ON Issue.Caller_id = Caller.Caller_id
JOIN Staff ON Issue.Taken_by = Staff.Staff_code
WHERE Caller.First_name = 'Harry'
I would just add the following to the end of your query:
ORDER BY call_date DESC LIMIT 1
This will give you one row as a result. And that row will be the one with the most recent call_date.
Based on the code provided, you're only asking for 3 columns so it's a matter of a join. When you select the max call date you need to group by the other 2 non-aggregate columns. If the date column is of datatype Date or Datetime then this should work:
SELECT Caller.First_name, Caller.Last_name --from Caller_id
,MAX(Issue.call_date) AS call_date
FROM Issue INNER JOIN Caller ON Issue.Caller_id = Caller.Caller_id
WHERE Caller.First_name = 'Harry'
GROUP BY Caller.First_name, Caller.Last_name
Related
I know this has already been asked answered a thousend times. But I seem not to be able to resolve this.
I am trying to group by and order this query so I can join it as subquery to a bigger query
SELECT * FROM `ggorderlog`
WHERE `GGTITLE` LIKE '%Reklamation%'
ORDER BY `GGDATE` DESC, `ggorderlog`.`GGORDERID` DESC
This is the result from the ggorderlog table and the query above
GGTITLE GGOXID GGDATE GGORDERID GGTITLE User
Reklamation uniqueid1 2018.12.7 16:20:00 1 Reklamation created Max Mustermann
Reklamation uniqueid2 2018.12.7 16:24:00 1 Reklamation finished Maxine Musterfrau
Reklamation uniqueid3 2018.12.7 16:22:00 2 Reklamation created Max mustermann
Now what I want is to have this table be displayed so that for every GGORDERID I only see the latest entry. In order to give an overview over the User who has worked on this and the status of the ticket.
Like this:
GGTITLE GGOXID GGDATE GGORDERID GGTITLE User
Reklamation uniqueid2 2018.12.7 16:24:00 1 Reklamation finished Maxine Musterfrau
Reklamation uniqueid3 2018.12.7 16:22:00 2 Reklamation created Max mustermann
I tried standard group by with order by but mysql seem to do the group by first and give out a random column
I tried this but it still shows always a random date.
Select* from (
Select *
from ggorderlog as b
where GGTITLE like '%Reklamation%'
ORDER BY b.GGDATE DESC
) b2
group by b2.GGORDERID
I tried a lot of other suggestions with left itselfe or group_concat and then desolve again but nothing seems to work.
Can you try this ?
SELECT gg.*
FROM ggorderlog gg
INNER JOIN
(SELECT ggorderid, MAX(ggdate) AS maxggdate,oxid
FROM ggorderlog
GROUP BY ggorderid) groupedgg
ON gg.oxid = groupedgg.oxid
AND gg.ggdate = groupedgg.maxggdate
I have table rozpis_riesitelov which contains columns :
id_rozpisu_riesit, id_zam, id_projektu, id_ulohy.
I made query :
select id_zam, id_ulohy, count(*) as counted
from rozpis_riesitelov
group by id_zam
having id_ulohy in (1,2,8)
which shows me id of employee (id_zam) and how many times He was in project (id_ulohy is irrevelant but I had to select it beacuse of having clause). It shows me everyone in db but I am looking for employee with ID of 4 who is in 6 projects (Yes, I could do order by but I want to see max). When I do max of this query like this:
select max(counted)
from (select id_zam, id_ulohy, count(id_zam) as counted
from rozpis_riesitelov
group by id_zam
having id_ulohy in (1,2,8)) as riesitel
which shows me number 149 instead of 6.
So basically I only need to find employee that occurs in the most of the projects.
What's wrong with sorting by the COUNT() value, and limiting to one result?
SELECT `id_zam`,
`id_ulohy`,
COUNT(*) AS `counted`
FROM `rozpis_riesitelov `
WHERE `id_ulohy` IN ( 1, 2, 8 )
GROUP BY `id_zam`
ORDER BY `counted` DESC
LIMIT 1
Not sure exactly what you are trying to accomplish but you only use HAVING to filter on your aggregate like this:
HAVING COUNT(*) > 1
you should be able to move the condition to a WHERE clause and get correct max returned:
select max(counted)
from (select id_zam, count(id_zam) as counted
from rozpis_riesitelov
where id_ulohy in (1,2,8)
group by id_zam) as riesitel
From this post, enter link description here
I would like to improve the query
SELECT `BetType`,
count(`BetType`) AS COUNT,
sum(`BetAmount`) AS BetAmountTotal,
sum(`Payout`) AS PayoutTotal
FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
GROUP BY `BetType`
thanks to Sadikhasan, who helped on this query
I would like to add another row showing the totals of the columns
BetType Count BetAmount Total Payout Total
Handi 2 60000 950000
Homerun Count 4 10000 0
Total 6 70000 950000
this seems to be needing another SELECT statement but how would I put another row explicitly showing the "Total" string and getting the sum of the previously used columns with Aggregate Functions?
You can use WITH ROLLUP modifier to GROUP BY, which will give you another row with totals, but the column you group on (BetType) will show NULL for that row. But nothing stops you from using COALESCE() to replace that NULL with 'Total' string.
SELECT COALESCE(`BetType`,'Total') AS BetType,
COUNT(*) AS `Count`,
sum(BetAmount) AS BetAmountTotal,
sum(Payout) AS PayoutTotal
FROM betdb
WHERE betdb.MatchID=135
GROUP BY BetType WITH ROLLUP
I got a quite complex query (at least for me).
I want to create a list of users that are ready to be paid. There are 2 conditions that need to be met: order status should be 3 and the total should be more then 50. Currently I got this query (generated with Codeingiter active record):
SELECT `services_payments`.`consultant_id`
, `consultant_userdata`.`iban`
, `consultant_userdata`.`kvk`, `consultant_userdata`.`bic`
, `consultant_userdata`.`bankname`
, SUM(`services_payments`.`amount`) AS amount
FROM (`services_payments`)
JOIN `consultant_userdata`
ON `consultant_userdata`.`user_id` = `services_payments`.`consultant_id`
JOIN `services`
ON `services`.`id` = `services_payments`.`service_id`
WHERE `services`.`status` = 3
AND `services_payments`.`paid` = 0
HAVING `amount` > 50
The services_payments table contains the commissions, consultant_userdata contains the userdata and services keeps the order data. The current query only gives me 1 result while I'm expecting 4 results.
Could someone please tell me what I'm doing wrong and what would be the solution?
For ActiveRecord, rsanchez' answer would be more of
$this->db->group_by('services_payments.consultant_id, consultant_userdata.iban, consultant_userdata.kvk, consultant_userdata.bic, consultant_userdata.bankname');
I am working on a mysql query that will filter out certain occurrences dependent on how many rows are returned.
I am trying to filter out any support categories when the number of rows returned are 1, however leave the support category in when the result set turned is more than 1.
I originally had this idea however it seems as if it will not work.
SELECT stockmaster.description, SUM(salesorderdetails.quantity), stockmaster.categoryid as qty
FROM salesorderdetails, stockmaster
where salesorderdetails.stkcode=stockmaster.stockid
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M' THEN COUNT(*) >= 2 ELSE COUNT(*) = 1 END
Any help will be gratefully accepted.
Try this
SELECT *
FROM
(
SELECT stockmaster.description,
SUM(salesorderdetails.quantity),
stockmaster.categoryid as qty ,
COUNT(*) AS count
FROM salesorderdetails, stockmaster
where salesorderdetails.stkcode=stockmaster.stockid
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M'
) MAIN_DATA
WHERE MAIN_DATA.count >1