I have this WHERE clause:
Where Year(CreateDate) >= '2016'
and b.colors1 In ('Yellow','Red')
Or b.colors2 In ('Yellow','Red')
Or b.colors3 In ('Yellow','Red')
I only want to return CreateDate >= 2016 but I'm getting data outside of '2016' because of the Or statement. How can I fix this?
Try this and year is Not String
Where Year(CreateDate) >= 2016
and (b.colors1 In ('Yellow','Red')
Or b.colors2 In ('Yellow','Red')
Or b.colors3 In ('Yellow','Red') )
Related
Why can't I use that subquery which returns a group error ?
SELECT hs.dateFin, hs.codeAdherent, hs.codeArticle
FROM hs
WHERE hs.codeFamilleArticle IN ('CNI', 'COT', 'ABO', 'ABOW',
'CNIW', 'O&T', 'EPH', 'TAX')
AND codeAdherent != 0
AND MAX(hs.dateFin) BETWEEN '2017-01-01'
AND '2017-12-31'
GROUP BY hs.codeAdherent
The same data exists for 2018-01-01 and 2018-12-31 but I only want to get the ones that end in 2017.
Here under a sample of table which contains 140000 raws (not all columns are showed).
codeAdherent A has data for 2018, 2017, 2016.
codeAdherent B has data for2018, 2017
codeAdherent C only for 2017.
If I do a select on 2017 I get all three codeAdherent, then the MAX BETWEEN will exclude A and B... But that's doesn't work
You can use NOT EXISTS to check if no record exists for 2018:
SELECT dateFin, codeAdherent, codeArticle
FROM hs AS t
WHERE codeFamilleArticle IN ('CNI', 'COT', 'ABO', 'ABOW', 'CNIW', 'O&T', 'EPH', 'TAX')
AND codeAdherent != 0
-- filter 2017 rows
AND dateFin >= '2017-01-01'
AND dateFin < '2018-01-01'
-- filter rows where 2018 data does not exist
AND NOT EXISTS (
SELECT 1
FROM hs
WHERE codeAdherent = t.codeAdherent
AND dateFin >= '2018-01-01'
)
You can do it like this:
HAVING YEAR(MAX(hs.dateFin)) = 2017
You cannot use aggregate functions like Max() inside Where clause. You can simply modify your where condition to include dates in year 2017 only, and then determine Max() date after group by.
SELECT MAX(hs.dateFin), hs.codeAdherent, hs.codeArticle
FROM hs
WHERE hs.codeFamilleArticle IN ('CNI', 'COT', 'ABO', 'ABOW',
'CNIW', 'O&T', 'EPH', 'TAX')
AND hs.codeAdherent != 0
WHERE hs.dateFin BETWEEN '2017-01-01'
AND '2017-12-31'
GROUP BY hs.codeAdherent, hs.codeArticle
I wrote a query which counts the customer's PPPOE breaks. It works perfectly in this case - in the output file I can see the customer's name and how many time his PPPOE connection was dropped:
select
username,
count(username) as db,
DATE_FORMAT(acctstarttime, '%Y-%m-%d')
from radius_db.radacct
where DATE_FORMAT(acctstarttime, '%Y-%m-%d') = '2018-01-02'
group by username
order by db desc;
I tried to make it automatic so I modified the WHERE statement, but in this case in the output I see only the customers whose PPPOE was only dropped once and the others not.
select
username,
count(username) as db,
DATE_FORMAT(acctstarttime, '%Y-%m-%d')
from radius_db.radacct
where DATE_FORMAT(acctstarttime, '%Y-%m-%d') = date_format(curdate(),'%Y-%m-%d')
group by username
order by db desc into outfile '/var/lib/mysql-files/pppoe-"
, DATE_FORMAT( NOW(), '%Y%m%d%m%s')
, ".txt'";
Assuming you acctstarttime column is datetime column you could filter by date() and curdate()
select
username,
count(username) as db,
DATE_FORMAT(acctstarttime, '%Y-%m-%d')
from radius_db.radacct
where DATE(acctstarttime) =curdate()
group by username
order by db desc into outfile '/var/lib/mysql-files/pppoe-"
, DATE_FORMAT( NOW(), '%Y%m%d%m%s')
, ".txt'";
and be careful .. because where DATE_FORMAT(acctstarttime, '%Y-%m-%d') = '2018-01-02' mean 2018 January 2 not 2018 February 1 as curdate()
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;
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;
I am new to MySQL. I have some code below which works to extract the year from a datefield, but it brings back all years from the table, ie 2011, 2012, 2013, where as I only want 2013. This is the basic query that works for the years:
SELECT EXTRACT(YEAR FROM startDate) FROM events_events where EventOwnerId = 206
This is my query where I tried to create an alias and return only 2013 years but it doesn't work:
SELECT EXTRACT(YEAR FROM startDate) as d_year
FROM events_events
where EventOwnerId = 206 AND d_year = 2013
As documented under Problems with Column Aliases:
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.
You could repeat the EXTRACT() function within your WHERE clause:
SELECT EXTRACT(YEAR FROM startDate) AS d_year
FROM events_events
WHERE EventOwnerId = 206
AND EXTRACT(YEAR FROM startDate) = 2013
However this is not particularly efficient, as MySQL must calculate the result of the function for every record (which necessitates a full table scan). If an index on startDate is to be used, it would be better to filter for dates within the desired range:
SELECT EXTRACT(YEAR FROM startDate) AS d_year
FROM events_events
WHERE EventOwnerId = 206
AND startDate >= '2013-01-01'
AND startDate < '2014-01-01'
This should work
SELECT EXTRACT(YEAR FROM startDate) as d_year
FROM events_events
where EventOwnerId = 206 AND EXTRACT(YEAR from startDate) = 2013