How to modify SQL query to get latest date - sql

I need to obtain the latest order date in statsbestproducts module, I added it to the SQL query but I obtain the first order date (between two dates). How to change it for latest dates? And also how to remove the time from this date?

... MIN(o.date_add) AS `date` ...

Related

How to select SQL Server data based on today's date and time?

I'm using SQL Server to COUNT some data which originates from a HTML table. I want to COUNT total rows in the database based on today's date. And then after the date hits tomorrow's date, to set the COUNT value back to zero and to restart the count back from the start.
Is there a query that can help me fetch data based on current date and time?
Something like this:
SELECT COUNT(data_id)
FROM table1
WHERE clock_time BETWEEN 000000 AND 235959
AND date_time IS TODAY's DATE (or something like that)
GROUP BY XXX
ORDER BY XXX
After the clock hits tomorrow's date, I want to reset the COUNT back to zero, and to start a new count back from 00:00:00.
I know there is NOW() query but as far as I know it only shows the date.
Someone told me I could use WHERE DATE(date)=CURDATE() but the SQL Server won't work with that.
Thank you...
You can use convert function like this
where convert(Date,date_time)= CONVERT(Date,GETDATE())
Need not to use time as you want today's data.
Try this one
SELECT COUNT(data_id) FROM table1
WHERE convert(date,date_time) = getdate()

Converting date-time format and querying based on a date condition in SQL

I have around 20,000 entries in a SQL table for which a date column is of the form
YYYY-MM-DD HH-SS. I would like to convert this format to a YYYY-MM-DD format so I can run a query on all of the entries that will count the number of entries based on
a) the month under which they fall
b) the day
I'm new to SQL and not sure if there is a way to loop through all of the entries and check based on the required criteria; and as such, would greatly appreciate any help.
I unfortunately, cannot send a screenshot of the table since the data is classified.
You don't need to change the data in the table. Most databases have year() and month() functions, so you could do:
select year(datecol), month(datecol), count(*)
from sqltable
group by year(datecol), month(datecol)
order by year(datecol), month(datecol);
If these specific functions are not available, then I'm sure your database supports something similar.

Retrieving how many transactions were made on a date in SQL?

I have a table named Sales and a column within it named Date. I'm simply trying to find how many sales were made on a specific date. My intuition was to use something like this:
SELECT COUNT(Date) FROM Sales WHERE Date='2015-04-04'
this should count all sales that were made on that date, but that returns 0. What am I doing wrong?
While it is difficult to be precise without table definitions or an indication of what RDBMS you are using, it is likely that Date is a time/date stamp, and that the result you want would be obtained either by looking for a range from the beginning of the day to the end of the day in your WHERE clause, or by truncating Date down to a date without the time before comparing it to a date.
Try the below once.
select count(*) from <t.n> where date like '2015-04-04%';
When you want to find the count of rows based on a field (Date) You need to Group By over it like this:
SELECT Date, COUNT(*)
FROM Sales
GROUP BY Date
Now you have all count of rows for each Date.
Type and Value of Date is important in the result of the above query.
For example in SQL Server your best try is to convert a DateTime field to varchar and then check it as the result of CONVERT like this:
SELECT COUNT(*)
FROM Sales
WHERE CONVERT(VARCHAR, Date, 111) = '2015/04/04'

Sql query to find difference between date

I need a sql query which will find the difference between two dates . I have already tried the between clause query but It include the Starting date . But I need a query that exclude the start date . and so please provide me a different one.
You can use the DATEDIFF method
E.g:
SELECT DATEDIFF(SELECT DATE_ADD(start_date,INTERVAL 1 DAY),end_date);
If you want to exclude the start_date, or change the interval as per your requirements

PostgreSQL - GROUP BY timestamp values?

I've got a table with purchase orders stored in it. Each row has a timestamp indicating when the order was placed. I'd like to be able to create a report indicating the number of purchases each day, month, or year. I figured I would do a simple SELECT COUNT(xxx) FROM tbl_orders GROUP BY tbl_orders.purchase_time and get the value, but it turns out I can't GROUP BY a timestamp column.
Is there another way to accomplish this? I'd ideally like a flexible solution so I could use whatever timeframe I needed (hourly, monthly, weekly, etc.) Thanks for any suggestions you can give!
This does the trick without the date_trunc function (easier to read).
// 2014
select created_on::DATE from users group by created_on::DATE
// updated September 2018 (thanks to #wegry)
select created_on::DATE as co from users group by co
What we're doing here is casting the original value into a DATE rendering the time data in this value inconsequential.
Grouping by a timestamp column works fine for me here, keeping in mind that even a 1-microsecond difference will prevent two rows from being grouped together.
To group by larger time periods, group by an expression on the timestamp column that returns an appropriately truncated value. date_trunc can be useful here, as can to_char.