Inside of my Auctions table, I have a column called Auction_StartDate. The value of a row is like this: 2012-10-27 13:45:30.
I need a query that will return the next closest date and time after that. So if the next Auction_StartDate is 2012-10-27 18:30:00, it should return that before the date turns to 2012-10-28.
If you mean to do this for every row, try this:
SELECT a1.id,
(SELECT MIN(a2.Auction_StartDate)
FROM Auctions a2
WHERE a2.Auction_StartDate > a1.Auction_StartDate) AS nextStartDate
FROM Auctions a1
You can use MIN to find the closest value without using LIMIT and ORDER BY clause.
SELECT MIN(DATE(Auction_StartDate)) closestDate
FROM Auctions
WHERE DATE(Auction_StartDate) > '2012-10-27'
SQLfiddle Demo
May be this one helps
SELECT DATE(Auction_StartDate) closestDate
FROM Auctions
WHERE DATE(Auction_StartDate) > '2012-10-27'
order by Auction_StartDate ASC
limit 1
SELECT (case when Hour(StartDate)>=12 then DATE_ADD(StartDate,
INTERVAL 1 DAY) else StartDate end) as 'date' FROM table
------------------------------
pleaes add your column name where is static date :
est on : http://sqlfiddle.com/#!2/b8435/19
SELECT (case when Hour(StartDate )>=12 then
DATE_FORMAT( DATE_ADD(StartDate ,INTERVAL 1 DAY), '%Y-%m-%d')
else DATE_FORMAT(StartDate , '%Y-%m-%d') end) as 'date' from tabel
Related
I have a query in MariaDB 10.3 database where there is a field called "expiration_date" that stores a unix timestamp, but if there is no data in the field the default is set to "0".
I'm trying to use a WHERE clause to check the current date against the expiration_date to filter out any records that are past the expiration_date. Below is what I have.
SELECT entry_id, title, (CASE WHEN expiration_date = "0" THEN CURDATE() + INTERVAL 1 DAY ELSE FROM_UNIXTIME(expiration_date, "%Y-%m-%d") END) AS expiration_date
FROM channel_titles
WHERE CURDATE() < expiration_date
This returns and empty result set... what am I missing?
There's a very simple solution to this and it only requires you to change two things from your original query:
The first part is your column (CASE expression) alias - you should define your alias with something not similar to any of the column names present in the table. From your query, you have a column expiration_datein your table and you also set an alias for your CASE expression with expiration_date as well and since you're using WHERE, the query will definitely do the lookup based on your table expiration_date column instead of your CASE expression. Rename that alias to something like exp_date... but doing WHERE exp_date ... will return you an error. Refer to the second point below.
The second part is your WHERE - since you're doing lookup from a CASE expression (or perhaps custom generated value/column) with newly assigned alias of exp_date, you can't use it in WHERE.. well, unless you make the query as a subquery/derived table then do the WHERE outside.. but you don't need to. You only need to change WHERE to HAVING and you should be able to use the exp_date and get your result.
So, with those two changes, your query should be something like this:
SELECT entry_id, title,
(CASE WHEN expiration_date = "0" THEN CURDATE() + INTERVAL 1 DAY ELSE
FROM_UNIXTIME(expiration_date, "%Y-%m-%d") END) AS exp_date
FROM channel_titles
HAVING CURDATE() < exp_date;
demo fiddle
You're trying to use an alias of expiration_date from your CASE statement in your WHERE clause.
Two problems with this:
You cannot use column aliases in the WHERE clause. Refer to this post here.
WHERE happens before SELECT in the execution chain.
Your alias matches an actual column name in your table, so your
WHERE clause is not throwing an error regarding your alias, its
comparing the current date to the expiration_date column in the table,
thus, throwing off your expected result.
Solutions:
If you want to use the alias in your WHERE clause, there are a few options for you to force SQL to handle the SELECT before the WHERE clause.
You can use a subquery (or subselect) to force logical order of
operation by using parentheses:
SELECT
a.entry_id,
a.title,
a.expiration_date
FROM
(SELECT
entry_id,
title,
(CASE WHEN expiration_date = 0 THEN CURDATE() + INTERVAL 1 DAY ELSE FROM_UNIXTIME(expiration_date, '%Y-%m-%d') END) AS expiration_date
FROM channel_titles
) a
WHERE CURDATE() < a.expiration_date
You can declare your alias in a Common Table Expression (CTE), then SELECT it FROM the CTE:
WITH cte AS (SELECT
entry_id,
title,
(CASE WHEN expiration_date = 0 THEN CURDATE() + INTERVAL 1 DAY ELSE FROM_UNIXTIME(expiration_date, '%Y-%m-%d') END) AS expiration_date
FROM channel_titles)
SELECT
entry_id,
title,
expiration_date
FROM cte
WHERE CURDATE() < expiration_date
You can disregard using your alias entirely in your WHERE clause and plug in the logic from your SELECT statement directly into your WHERE clause. However, this may appear redundant from a readability perspective; also, extra processing should be considered when using this approach as well, but if you have a small data set this method will work just fine:
SELECT
entry_id,
title,
(CASE WHEN expiration_date = 0 THEN CURDATE() + INTERVAL 1 DAY ELSE FROM_UNIXTIME(expiration_date, '%Y-%m-%d') END) AS expiration_date
FROM channel_titles
WHERE CURDATE() < (CASE WHEN expiration_date = 0 THEN CURDATE() + INTERVAL 1 DAY ELSE FROM_UNIXTIME(expiration_date, '%Y-%m-%d') END)
Input:
entry_id
title
expiration_date
expiration_date_date
1
test1
1695513600
2023-09-24
2
test2
0
2022-09-15
3
test3
1662768000
2022-09-10
Output:
entry_id
title
expiration_date
1
test1
2023-09-24
2
test2
2022-09-15
db<>fiddle here.
i want to add new column in my select statement
SELECT name,line,style,operation,
7to8am,8to9am,9to10am,10to11am,11to12am,
1to2pm,2to3pm,3to4pm,4to5pm,5to6pm,6to7pm,7to8pm,8to9pm,9to10pm,10to11pm,11to12pm,
sum(7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm
+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+
8to9pm+9to10pm+10to11pm+11to12pm) as DailyTotal,id from new_hourly GROUP By line
i want to add a new column that will show sum of DailyTotal that the day is today
This is my sql backup file http://www.uploadmb.com/dw.php?id=1446536983
Please help me! thank you so much!
So you want one additional columns that adds only lines where datee equals current date?
I'd go for case-when expression in this case:
coalesce(
sum(
case
when datee = CURDATE()
then 7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+8to9pm+9to10pm+10to11pm+11to12pm
else null
end
)
,0) as TodaysTotal
That's summing up only those rows where column datee is curdate() and returning 0 if no rows at all are present for today.
Full SQL:
SELECT name,line,style,operation,
7to8am,8to9am,9to10am,10to11am,11to12am,
1to2pm,2to3pm,3to4pm,4to5pm,5to6pm,6to7pm,7to8pm,8to9pm,9to10pm,10to11pm,11to12pm,
sum(7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm
+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+
8to9pm+9to10pm+10to11pm+11to12pm) as DailyTotal,
coalesce(
sum(
case
when datee = CURDATE()
then 7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+8to9pm+9to10pm+10to11pm+11to12pm
else null
end
)
,0) as TodaysTotal
,id from new_hourly GROUP By line
I have a table
id user Visitor timestamp
13 username abc 2014-01-16 15:01:44
I have to 'Count' total visitors for a 'User' for last seven days group by date(not timestamp)
SELECT count(*) from tableA WHERE user=username GROUPBY __How to do it__ LIMIT for last seven day from today.
If any day no visitor came so, no row would be there so it should show 0.
What would be correct QUERY?
There is no need to GROUP BY resultset, you need to count visits for a week (with unspecified user). Try this:
SELECT
COUNT(*)
FROM
`table`
WHERE
`timestamp` >= (NOW() - INTERVAL 7 DAY);
If you need to track visits for a specified user, then try this:
SELECT
DATE(`timestamp`) as `date`,
COUNT(*) as `count`
FROM
`table`
WHERE
(`timestamp` >= (NOW() - INTERVAL 7 DAY))
AND
(`user` = 'username')
GROUP BY
`date`;
MySQL DATE() function reference.
Try this:
SELECT DATE(a.timestamp), COUNT(*)
FROM tableA a
WHERE a.user='username' AND DATEDIFF(NOW(), DATE(a.timestamp)) <= 7
GROUP BY DATE(a.timestamp);
i think it's work :)
SELECT Count(*)
from table A
WHERE user = username AND DATEDIFF(NOW(),timestamp)<=7
The following code is producing a list with the non-expired rows on top, then the ones with unknown expiry date and at the end the already expired (all of them in ascending order). The problem is that I want the last block of already expired rows to be in descending order so it displays the rows that expired more recently on top of that block without altering the order of the other top blocks.
Basically, I am trying to find a way to incorporate two "ORDER BY" clauses within the same recordset...
Any ideas? Thanks
SELECT *
FROM prueba
WHERE UPPER(CONCAT(Company,Deal,keywords,Type,Expiry,Name)) LIKE UPPER(%s)
ORDER BY (CASE
WHEN prueba.Expiry = 'UNKNOWN' THEN 1
WHEN prueba.Expiry < CURRENT_DATE THEN 2
END)
, prueba.Expiry ASC
Try this
DEMO FIDDLE
SELECT * FROM t
order by case
when expiry = 'Unknown' Then 1
WHEN expiry >= CURRENT_DATE THEN 0
ELSE 2 END,
CASE WHEN expiry >= CURRENT_DATE THEN expiry END,
CASE WHEN expiry < CURRENT_DATE THEN expiry END desc
Separate those records that have expired into another SELECT clause, then UNION ALL:
(SELECT *
FROM prueba
WHERE UPPER(CONCAT(Company,Deal,keywords,Type,Expiry,Name)) LIKE UPPER(%s)
AND prueba.Expiry > CURRENT_DATE
ORDER BY prueba.Expiry DESC)
UNION ALL
(SELECT *
FROM prueba
WHERE UPPER(CONCAT(Company,Deal,keywords,Type,Expiry,Name)) LIKE UPPER(%s)
AND prueba.Expiry = 'UNKNOWN')
UNION ALL
(SELECT *
FROM prueba
WHERE UPPER(CONCAT(Company,Deal,keywords,Type,Expiry,Name)) LIKE UPPER(%s)
AND prueba.Expiry < CURRENT_DATE
ORDER BY prueba.Expiry DESC)
Say I want to SELECT all records between two dates plus one record before and one record after that date? All records are ordered by date.
You could use a union combined with the limit statement. Something like what's below (untested, don't have access to mysql).
(select column from table where datefield > startdate and datefield < stopdate)
union
(select column from table where datefield < startdate order by datefield desc limit 1)
union
(select column from table where datefield > stopdate order by datefield limit 1)
This will give you the next row regardless of where it falls date-wise.
Thanks for syntax fix, ponies.
(select * from t where date < start_date order by date desc limit 1)
union (select * FROM t WHERE date between start_date and end_date)
union (select * from t where date > end_date order by date asc limit 1)
You can use functions to add or subtract values, like this:
select * from table where field1 < ADDDATE( CURTIME() , INTERVAL 1 DAY)
Check this link where there are some examples.
SELECT *
FROM table
WHERE date BETWEEN DATE_ADD(current_date(), INTERAL -1 DAY)
AND DATE_ADD(current_date(), INTERVAL 1 DAY);