Count query grouping by date ignoring time - sql

I have a DATE variable that should be grouped ignoring the time in this query:
SELECT COUNT(*), update_date
FROM table1
HAVING count(update_date)>1
GROUP BY update_date
ORDER BY update_date desc
The result is right but I need to group them by the DAY and not by seconds since the date format of the field is DD/MM/YYYY HH:MM:SS. I would like to compare only the DD/MM/YYYY, ignoring the time of the update, by day.

Use trunc() to set the time part to 00:00:00:
SELECT COUNT(*), trunc(update_date) as update_date
FROM table1
GROUP BY trunc(update_date)
HAVING count(trunc(update_date))>1
ORDER BY trunc(update_date) desc

Related

How to summarize dates in a table with different timestamps

When I ask this question, I get multiple hits on divcode because there are different timestamps in pickdate. Is there a way to get the question to summarize the date so I get 1 value per division?
/*
Söker Vikt och volym Snitt
*/
select TO_CHAR(ROUND(O08T1.pickdate),'YYYY-MM-DD') as date_1,
O08T1.divcode,
sum(O08T1.calcwght) as Vikt, sum(O08T1.calcvol) as Volym,
count(*) as AntalOrder,
avg(O08T1.calcwght) as avg_vikt,
avg(O08T1.calcvol) as avg_Vol
from O08T1
where O08T1.pickdate >= #('Från datum',#DATE)
group by O08T1.pickdate, O08T1.divcode
order by Pickdate DESC
If you want the values grouped by day then don't use GROUP BY pickdate as that will group by each instant and have values down to an accuracy of a second. Instead, GROUP BY TRUNC(pickdate) which will truncate values to the start of the day and will aggregate all values on the same day together (and then, also, use TRUNC in the first line and in the ORDER BY):
select TO_CHAR(TRUNC(pickdate),'YYYY-MM-DD') as date_1,
divcode,
sum(calcwght) as Vikt,
sum(calcvol) as Volym,
count(*) as AntalOrder,
avg(calcwght) as avg_vikt,
avg(calcvol) as avg_Vol
from O08T1
where pickdate >= #('Från datum',#DATE)
group by TRUNC(pickdate),
divcode
order by TRUNC(pickdate) DESC

SQL count distinct # of calls 6 months prior to create date

Am trying to figure out the SQL to:
count # of distinct calls
made on an account 6 months prior to the account being created
I also need to CAST the date field.
I'm thinking something like:
case when (call_date as date format 'MM/DD/YYYY')
between (create_date as date format 'MM/DD/YYYY') and
(ADD_MONTHS, (create_date as date format 'MM/DD/YYYY), -6)
then COUNT (DISTINCT call_nbr) as calls
Here's a snippet of the data i am working with. The answer I require 3 Calls.
Note: both dates are flagged in the db table as DATE format.
Call_Nbr.....Call Date......Create Date
12345........03/14/2020....07/23/2020.....include in result set
12345........03/14/2020....07/23/2020.....exclude in result set
45678........02/14/2020....07/23/2020.....include in result set
91011........01/20/2020....07/23/2020.....include in result set
91211........01/24/2020....07/23/2020.....exclude in result set
12345........11/14/2019....07/23/2020.....exclude in result set
I think you want:
select count(distinct call_nbr) no_calls
from mytable
where call_date >= add_months(create_date, -6)
If you have a column that represnets the account_id, then you can use a group by clause to get the count of calls per account:
select account_id, count(distinct call_nbr) no_calls
from mytable
where call_date >= add_months(create_date, -6)
group by account_id
Edit: it seems like you want conditional aggregation instead:
select
account_id,
count(distinct case when call_date >= add_months(create_date, -6) then call_nbr end) no_calls
from mytable
group by account_id

Oracle sql query not showing specific date

This is my first time with oracle database. So I save data with date 30/04/20 and I want to retrieve it. So I use SELECT * FROM USER_ACTION WHERE ACTION_DATE_TIME <= '30-APR-20' order by ACTION_DATE_TIME desc but no data with date 30/04/20 are shown. However when I use SELECT * FROM USER_ACTION WHERE ACTION_DATE_TIME <= '01-MAY-20' order by ACTION_DATE_TIME desc, I can see the data. Is there anyway that I can get date with exact date? no need to put extra +1 day to get it.
This is result when use 30-APR-20:
This is result when use 01-MAY-20:
Given that your ACTION_DATE_TIME column be a datetime, with time component, if you want to include 30th April 2020 proper, you should be using this inequality:
SELECT *
FROM USER_ACTION
WHERE ACTION_DATE_TIME < date '2020-05-01'
ORDER BY ACTION_DATE_TIME DESC;
This will include all dates strictly less than 1st May 2020, which include all of 30th April 2020.
If the date value is coming from the outside, then just add one day to it:
SELECT *
FROM USER_ACTION
WHERE ACTION_DATE_TIME < date '2020-05-01' + 1
ORDER BY ACTION_DATE_TIME DESC;
use trunc to convert date time to date as below
SELECT *
FROM USER_ACTION
WHERE TRUNC(ACTION_DATE_TIME) <= '30-APR-20'
order by ACTION_DATE_TIME desc

Latest date from date, month, year

I have an SQLite DB with date, month, year fields in integers (I believe they should have used a date field but the choice wasn't mine). I would like to select the row whose date value is the latest. What is the best query to do that?
select * from your_table
order by year, month, `date` desc
limit 1

How do I get a maximium daily value of a numerical field over a year in SQL

How do I get a maximium daily value of a numerical field over a year in MS-SQL
This would query the daily maximum of value over 2008:
select
datepart(dayofyear,datecolumn)
, max(value)
from yourtable
where '2008-01-01' <= datecolumn and datecolumn < '2009-01-01'
group by datepart(dayofyear,datecolumn)
Or the daily maximum over each year:
select
datepart(year,datecolumn),
, datepart(dayofyear,datecolumn)
, max(value)
from yourtable
group by datepart(year,datecolumn), datepart(dayofyear,datecolumn)
Or the day(s) with the highest value in a year:
select
Year = datepart(year,datecolumn),
, DayOfYear = datepart(dayofyear,datecolumn)
, MaxValue = max(MaxValue)
from yourtable
inner join (
select
Year = datepart(year,datecolumn),
, MaxValue = max(value)
from yourtable
group by datepart(year,datecolumn)
) sub on
sub.Year = yourtable.datepart(year,datecolumn)
and sub.MaxValue = yourtable.value
group by
datepart(year,datecolumn),
datepart(dayofyear,datecolumn)
You didn't mention which RDBMS or SQL dialect you're using. The following will work with T-SQL (MS SQL Server). It may require some modifications for other dialects since date functions tend to change a lot between them.
SELECT
DATEPART(dy, my_date),
MAX(my_number)
FROM
My_Table
WHERE
my_date >= '2008-01-01' AND
my_date < '2009-01-01'
GROUP BY
DATEPART(dy, my_date)
The DAY function could be any function or combination of functions which gives you the days in the format that you're looking to get.
Also, if there are days with no rows at all then they will not be returned. If you need those days as well with a NULL or the highest value from the previous day then the query would need to be altered a bit.
Something like
SELECT dateadd(dd,0, datediff(dd,0,datetime)) as day, MAX(value)
FROM table GROUP BY dateadd(dd,0, datediff(dd,0,datetime)) WHERE
datetime < '2009-01-01' AND datetime > '2007-12-31'
Assuming datetime is your date column, dateadd(dd,0, datediff(dd,0,datetime)) will extract only the date part, and then you can group by that value to get a maximum daily value. There might be a prettier way to get only the date part though.
You can also use the between construct to avoid the less than and greater than.
Group on the date, use the max delegate to get the highest value for each date, sort on the value, and get the first record.
Example:
select top 1 theDate, max(theValue)
from TheTable
group by theDate
order by max(theValue) desc
(The date field needs to only contain a date for this grouping to work, i.e. the time component has to be zero.)
If you need to limit the query for a specific year, use a starting and ending date in a where claues:
select top 1 theDate, max(theValue)
from TheTable
where theDate between '2008-01-01' and '2008-12-13'
group by theDate
order by max(theValue) desc