Im having trouble with this query.
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission
WHERE adm.adm_ReferralDate >= '01/01/2014 00:00:00' AND adm.adm_ReferralDate <= '31/12/2014 00:00:00'
AND adm.adm_PriorSurgery = 'Yes'
AND adm.adm_Consultant <> ''
GROUP BY adm_Consultant
ERROR: General error
this works though, but returns the null values as-well
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission
GROUP BY adm_Consultant
I tried the HAVING clause instead of the WHERE clause, but still it fails.
Please help.
here was my reading material.
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement. The result is a BIGINT value.
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count
You are forgetting to create alias adm
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission adm
WHERE adm.adm_ReferralDate >= '01/01/2014 00:00:00' AND
adm.adm_ReferralDate <= '31/12/2014 00:00:00'
AND adm.adm_PriorSurgery = 'Yes'
AND adm.adm_Consultant <> ''
GROUP BY adm_Consultant
Try using ISO standard date formats:
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission adm
WHERE adm.adm_ReferralDate >= '2014-01-01' AND adm.adm_ReferralDate <= '2014-12-31' AND
adm.adm_PriorSurgery = 'Yes' AND
adm.adm_Consultant <> ''
GROUP BY adm_Consultant;
Related
I'm trying to add a condition in my mySQL statement where if the status is >= 6 then it only shlows the dates between fit_appointment_start_date and if the status is <= 5 then it uses the appointment_start_date condition instead. Is there a way to do this in Mysql?
At the moment i'm just doing an (and or) which is bringing back all the records in the condition, but i need it to change the condition depending on the status
This is my SQL.
SELECT j.client_first_name,
u.home_postcode,
j.engineer_id,
j.deposit_taken,
j.men,
j.time_taken,
j.potential_instalation_date,
j.id,
j.status,
j.postcode,
j.calendar_notes,
j.hasinvoice,
u.color,
j.fit_appointment_start_date,
j.fit_appointment_end_date,
j.client_surname,
j.appointment_start_date,
j.appointment_end_date,
u.username AS engineer_username,
j.potential_instalation_notes
FROM jobs j
INNER JOIN users u
ON u.id = j.engineer_id
INNER JOIN status s
ON s.id = j.status
WHERE u.active = 1
AND ( j.fit_appointment_start_date BETWEEN
'2021-05-04 00:00:00' AND '2021-05-04 23:59:59'
OR j.appointment_start_date BETWEEN
'2021-05-04 00:00:00' AND '2021-05-04 23:59:59' )
ORDER BY engineer_username
if the status is >= 6 then it only shlows the dates between fit_appointment_start_date and if the status is <= 5 then it uses the appointment_start_date condition instead.
...
AND CASE WHEN status >= 6
THEN j.fit_appointment_start_date BETWEEN
'2021-05-04 00:00:00' AND '2021-05-04 23:59:59'
WHEN status <= 5
THEN j.appointment_start_date BETWEEN
'2021-05-04 00:00:00' AND '2021-05-04 23:59:59'
END
...
PS. The rows with status IS NULL won't be selected.
This is my query
WHERE id = 14 AND start_time BETWEEN '2019-10-24 00:00:00' AND '2019-12-12 23:59:59'
ORDER BY created_date LIMIT 0 , 10
When I run this query then it returns me data of this data also. -> 2019-10-23T19:23:41.000Z
Issue: When I pass the 2019-10-24 then why it gives me data of 2019-10-23 date?
Note: start_time has a data type -> datetime in db.
It's not a issue by the way it's correct output.
Try this way
DATE_FORMAT(start_time, "%Y-%m-%d %H:%i:%s") as start_time
Due to diffrent formate, It may confused you.
use less than or equal to '<=' or greater than or equal to '>=' operator instead of BETWEEN.
Use this condition in your query.
WHERE id = 14 AND DATE(start_time) >= DATE('2019-10-24 00:00:00') AND DATE(start_time) <= DATE('2019-12-12 23:59:59') ORDER BY created_date LIMIT 0 , 10
OR
WHERE id = 14 AND DATE(start_time) >= '2019-10-24' AND DATE(start_time) <= '2019-12-12') ORDER BY created_date LIMIT 0 , 10
I have the following query:
SELECT DATE(utimestamp) as utimestamp, name, data*2000000 from tData
where utimestamp BETWEEN '2016-01-01 00:00:00' AND '2016-04-16 00:00:00'
AND name = 'Valor2' and data>20
group by YEAR(utimestamp), MONTH(utimestamp), name
union
SELECT DATE(utimestamp) as utimestamp, name, data*0.1 from tData
where utimestamp BETWEEN '2016-01-01 00:00:00' AND '2016-04-16 00:00:00'
AND name = 'Valor1' and data>20
group by YEAR(utimestamp), MONTH(utimestamp), name
order by utimestamp asc
Is there a more efficient way of operating with 'data'? Is there a way of doing this without using UNION?
You can try to use case when then:
SELECT DATE(utimestamp) as utimestamp, name,
case when name = 'Valor1' then data*0.1
when name = 'Valor2' then data*2000000
end
from tData
where utimestamp BETWEEN '2016-01-01 00:00:00' AND '2016-04-16 00:00:00'
and data>20
group by YEAR(utimestamp), MONTH(utimestamp), name
order by utimestamp asc
The query in your question is strange, because it has a math calculation without an aggregation function. And, you are aggregating by year and month, but not including them in the query.
I would be inclined to put the values in two separate columns, with the year and month explicitly defined in the query:
select year(utimestamp), month(utimestamp),
sum(case when name = 'Valor1' then data*0.01 end) as valor1,
sum(case when name = 'Valor2' then data*2000000 end) as valor2
from tData
where utimestamp between '2016-01-01' and '2016-04-16' and
name in ('Valor1', 'Valor2') and
data > 20
group by year(utimestamp), month(utimestamp)
order by max(utimestamp);
I'm really blocked at an advanced query, if someone can help me
I have a table mysql that looks like:
customers(id, appointment_date1, appointment_date2, appointment_date3, appointment_date4)
I'm looking for a query that list me what is the next most recent appointment
Before I do this query :
SELECT CASE
WHEN (customers.appointment_date1 != '0000-00-00' AND DATE(customers.appointment_date1) >= CURDATE()) THEN customers.appointment_date1
WHEN (customers.appointment_date2 != '0000-00-00' AND DATE(customers.appointment_date2) >= CURDATE()) THEN customers.appointment_date2
WHEN (customers.appointment_date3 != '0000-00-00' AND DATE(customers.appointment_date3) >= CURDATE()) THEN customers.appointment_date3
WHEN (customers.appointment_date4 != '0000-00-00' AND DATE(customers.appointment_date4) >= CURDATE()) THEN customers.appointment_date4
END as appointment
ORDER BY appointment ASC
But it's wrong, it doesn't work correctly.
Anyone can help?
Thanks
I'd use nested mysql if() functions in select clause, like :
select *
from(
select if(date1<date2&&date1>curdate(),date1,
if(date2<date3&&date2>curdate(),date2,
if(date3>curdate(),date3, 'nothing')
)
) as date
from dates
) as dates
order by dates.date desc;
EDIT : as per Zika's comment
SELECT IF(LEAST(
IFNULL(date1,'0000-00-00'),
IFNULL(date2,'0000-00-00'),
IFNULL(date3,'0000-00-00')
)!='0000-00-00',
LEAST(
IFNULL(date1,'0000-00-00'),
IFNULL(date2,'0000-00-00'),
IFNULL(date3,'0000-00-00')
),
'aucune date'
)
FROM dates;
I need to make query that selects data between two dates but exclude weekdays and some other days that are for example public holidays.
SELECT
`tblproduction`.`OperaterID`, `tblproduction`.`OperationID`,
SUM(`tblproduction`.`TotalProduced`) AS TotalProduced,
SUM(`tblproduction`.`TotalProducedOperator`) AS TotalProducedOp,
'Normal working day' AS DayType
FROM `tblproduction`
WHERE tblproduction.StartDateTime >= '2015-02-01 00:00:00' AND (tblproduction.StartDateTime <= '2015-02-28 23:59:59') AND tblproduction.OperaterID = 10
AND (DAYOFWEEK(tblproduction.StartDateTime) IN (1,7)) AND (DATE(tblproduction.StartDateTime) NOT IN (SELECT HolidayDate FROM tblholidays))
GROUP BY `tblproduction`.`OperaterID`, `tblproduction`.`OperationID`
UNION ALL
SELECT
`tblproduction`.`OperaterID`, `tblproduction`.`OperationID`,
SUM(`tblproduction`.`TotalProduced`) AS TotalProduced,
SUM(`tblproduction`.`TotalProducedOperator`) AS TotalProducedOp,
'Weekend' AS DayType
FROM `tblproduction`
WHERE tblproduction.StartDateTime >= '2015-02-01 00:00:00' AND (tblproduction.StartDateTime <= '2015-02-28 23:59:59') AND tblproduction.OperaterID = 10
AND (DAYOFWEEK(tblproduction.StartDateTime) NOT IN (1,7)) AND (DATE(tblproduction.StartDateTime) NOT IN (SELECT HolidayDate FROM tblholidays))
GROUP BY `tblproduction`.`OperaterID`, `tblproduction`.`OperationID`
For that purpose i have table with predefined dates that represents public holidays named tblHolidays. One of criteria in sql query is avoid counting pieces made during the holiday date. By running this query is steel includes those dates that are holidays. What should I change in query?
Maybe start with this...
SELECT p.OperaterID
, p.OperationID
, SUM(p.TotalProduced) TotalProduced
, SUM(p.TotalProducedOperator) TotalProducedOp
, CASE WHEN DAYOFWEEK(p.startdatetime) IN (1,7) THEN 'Normal working day' ELSE 'Weekend' END DayType
FROM tblproduction p
WHERE p.StartDateTime >= '2015-02-01 00:00:00' AND p.StartDateTime <= '2015-02-28 23:59:59'
AND p.OperaterID = 10
AND DATE(p.StartDateTime) NOT IN (SELECT HolidayDate FROM tblholidays)
GROUP
BY p.OperaterID
, p.OperationID
, CASE WHEN DAYOFWEEK(p.startdatetime) IN (1,7) THEN 'Normal working day' ELSE 'Weekend' END