Let the table contains the fields id,amount,createdDate where createdDate is in timeStamp
**1.**em.createNamedQuery("select Sum(t.amount) from tablename t
WHERE cast(t.createdDate as date) = :createdDate")**
Let the createdDate be '2012-3-23'
Now the result would be the sum of all the amount on the particular date('2012-3-23') from time 00:00:00 to 23:59:59 .
The another Way is by
**2.em.createNamedQuery("select Sum(t.amount) from tablename t
WHERE t.createdDate between :fromDate and :todate")**
The values of fromDate is'2012-3-23 0:0:0' and todate as'2012-3-23 23:59:59'
Now the result would be the sum of all the amount on the particular date('2012-3-23') from time 00:00:00 to 23:59:59 .
The above two queries work successful.
Is there any other way to get the result without using
1.cast in the query
2.Giving the fromdate and todate with the specified time values as mentioned above.
Use DATE() function:
select DATE(createdDate), SUM(t.amount) from tableName t
where DATE(createdDate) =:createdDate
Use DATE function.
SELECT SUM(t.amount) FROM tablename t WHERE DATE(createdDate) = :createdDate
Related
I execute this query
SELECT * FROM graph WHERE ean IN ('00000000166330') group by DAY(created_at);
Getting those results:
# id, ean, avg_price, created_at
'58', '00000000166330', '2799.0000', '2020-06-11 16:43:27'
I want to change the date format returned of the created_at field.
I would like to get only the date, not the hour, and with the format: Day, month, Year.
My guess is that DATE_FORMAT should be used, but how to use it, grouping also by day?
Example here
You are not doing any aggregation, so remove group by
You can replace the operator IN with = because you are comparing against 1 value only
Use the function DATE() to get only the date part from created_at
You need a correlated subquery in the WHERE clause to get the row with the minimum id (since it does not matter whic row will be returned) of each day:
SELECT g.id, g.ean, g.avg_price, DATE_FORMAT(g.created_at, '%d-%m-%Y') created_at
FROM graph g
WHERE g.ean = '00000000166330'
AND g.id = (SELECT MIN(id) FROM graph WHERE ean = g.ean AND DATE(created_at) = DATE(g.created_at))
See the demo.
If you want distinct values without aggregation function you should use DISTINCT and for date you can use the date_format() function
SELECT DISTINCT DAY(created_at)
, date_format(date(created_at),'%d, %m, %Y') , id, avg_price
FROM graph WHERE ean = '00000000166330';
and when you have only a value you should use = and not IN operator.
I have a table that roughly looks like this. There are thousands of rows.
booking_date checkin_date ...some other columns .... booking_value
22-mar-2016 29-mar-2016 ........................... $150
01-apr-2016 17-may-2016 ........................... $500
09-apr-2016 09-apr-2016 ........................... $222
17-apr-2016 23-apr-2016 ........................... $75
19-apr-2016 31-july-2016 ........................... $690
03-May-2016 07-May-2016 ............................. $301
.
.
.
.
I am trying to calculate number of bookings per day and the value of bookings per day in April 2016. The second part is to calculate for how many bookings the booking_date and checking_date were the same.
I am very new to SQL. I can formulate the logic in paper, but can't seem to figure out how to proceed with the code.
I recommend the following query:
SELECT CAST(booking_date AS DATE), COUNT(*) as Number_of_Booking,
SUM(CAST(booking_date AS DATE) = CAST(checkin_date AS DATE)) as count_with_same_date,
SUM(booking_value) as booking_value
FROM t
WHERE booking_date >= '2016-04-01' AND
booking_date < '2016-05-01'
GROUP BY CAST(booking_date AS DATE);
In particular, note the filtering on the dates. The direct comparisons allow MySQL to use an index.
The calculation of the number on the same date uses a nice feature of MySQL where boolean values are treated as numbers in a numeric context.
You can try this below code-
SELECT CAST(booking_date AS DATE),
COUNT(*) Number_of_Booking,
COUNT(
CASE
WHEN CAST(booking_date AS DATE)
= CAST(checkin_date AS DATE) THEN 1
ELSE NULL
END
) count_with_same_date,
SUM(booking_value) booking_value -- Booking value has to be Number field
FROM your_table
WHERE YEAR(booking_date ) = 2016
AND MONTH(booking_date ) = 4
GROUP BY CAST(booking_date AS DATE)
For the first question you can try
Select booking_date
,count(*) as Number_of_bookings
,Sum(booking_value) as value
From table_name
Where booking_date between '01-apr-2016' and '30-apr-2016'
group by booking_date:
Or you can use month() and year() function in filter.
For the second question try,
Select booking_date
,checkin_date
,count(*)
from table_name
where booking_date=checkin_date
group by booking_date, checkin_date
I want to retrieve mysql table data if the data created_at Datetime column equal to tomorrow date, for example:
SELECT * FROM sales_order where created_at = tomorrow_date;
You can use the following solution, using DATEDIFF and DATE_ADD:
SELECT *
FROM sales_order
WHERE DATEDIFF(created_at, DATE_ADD(CURDATE(), INTERVAL 1 DAY)) = 0;
or a simpler solution only using DATEDIFF:
SELECT *
FROM sales_order
WHERE DATEDIFF(created_at, CURDATE()) = 1
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation. - from MySQL docs.
SELECT * FROM sales_order where created_at = CURDATE() + 1;
This is my SQL statement which pulls back all the fills I've had in a certain time frame. Is there a way to get the list to come up and also pull the SUM of all of them?
SELECT customerName, date, gallons
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
ORDER BY CONVERT(DATE, date) ASC
First, I would write your query as:
SELECT customerName, date, gallons
FROM addFill
WHERE date >= '2015-03-03'
ORDER BY date ASC;
I see no value in ordering by the date and not the date/time component. Also, you might as well just use a recognizable date format for the comparison.
If you want the sum as well, then that is tricky. One method uses rollup:
SELECT customerName, date, SUM(gallons) as gallons
FROM addFill
WHERE date >= '2015-03-03'
GROUP BY customerName, date with rollup
sums of fills per customer since '3-3-2015'
SELECT customerName, date, sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
GROUP BY customerName
sum of fills for all customers since '3-3-2015'
SELECT sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
I have a user_entry table which contains a date field. data type is datetime.
data base is mysql.
I want a count of current date and current month and all data of current date.
How can I get this?
I tried below query but it's not working.
select * from table
where DATE(date) = CURDATE()
SELECT date FROM `test` WHERE date = CURDATE()
or
SELECT date FROM `test` WHERE date = DATE('2016-04-04')
it's work.
if you want the number of matches:
SELECT COUNT(date) from test WHERE date = CURDATE()
What is the data type of field 'date'?
To obtain the DAY/MONTH you can use the corresponding functions
SELECT MONTH(date), DAY(date) from test
Moreover, you can use groups to create a complete report
SELECT COUNT(date), date from test GROUP BY DAY(date), MONTH(date)
i used below query and it works for me.
SELECT *
FROM `user_entry`
WHERE DATE( DATE ) = DATE( NOW( ) )