MSSQL Sum by EntryDate - sql

I have a table called "Data". With columns: "Number" and "EntryDate".
The EntryDate is Datetime( Y-m-d h:i:s ).
I need to calculate the sum of all entries from a date ignore h:i:s, group them by the date.
Example:
Number | EntryDate
-------------------
23 | 2018-10-01 13:22:10.520
25 | 2018-10-01 11:16:09.533
So basically I need to SUM the Number from 2018-10-01.
I have tried several variations but nothing seems to work, for example:
SELECT
SUM(Number) as 'Sum',
EntryDate AS DATE
FROM Data
GROUP BY EntryDate

Use cast() function for converting datetime to date
SELECT
SUM(Number) as 'Sum', cast(EntryDate as date) AS `DATE`
FROM Data
GROUP BY cast(EntryDate as date

Your date is at the moment in the datetime format, hence if you select date within your select query, you wont really get the date, instead you would get the respective datetimes.
What you can do is Convert the EntryDate as date:
Try:
select sum(number) as 'Sum', convert(date,EntryDate) as 'Date'
from Data
group by convert(date,EntryDate)
Should work.
Go seek more information from here https://www.w3schools.com/sql/func_sqlserver_convert.asp
Cheers

Related

How do I create a new column showing difference between maximum date in table and date in row?

I need two columns: 1 showing 'date' and the other showing 'maximum date in table - date in row'.
I kept getting a zero in the 'datediff' column, and thought a nested select would work.
SELECT date, DATEDIFF(max_date, date) AS datediff
(SELECT MAX(date) AS max_date
FROM mytable)
FROM mytable
GROUP BY date
Currently getting this error from the above code : mismatched input '(' expecting {, ';'}(line 2, pos 2)
Correct format in the end would be:
date | datediff
--------------------------
2021-08-28 | 0
2021-07-26 | 28
2021-07-23 | 31
2021-08-11 | 17
If you want the date difference, you can use:
SELECT date, DATEDIFF(MAX(date) OVER (), date) AS datediff
FROM mytable
GROUP BY date
You can do this using the analytic function MAX() Over()
SELECT date, MAX(date) OVER() - date FROM mytable;
Tried this here on sqlfiddle

How to filter date with where on SQLite with '2021-07-31 13:53:26' format?

I wanted to take just the year and month from '2021-07-31 13:53:26' and group them based on count values.
i tried the date, datetime, strftime functions.
Date and Datetime resulting null. strftime result something, but i cant group the Year and Month i get with the count i want, resulting null again
Here is the preview of the data.
expected result example is like '2021-07' with the count of how many times this year and month occurs
This is the syntax i tried with strftime:
select strftime('%Y%m', started_at) year_month, count(year_month) from bike_trip
group by year_month
Thank You
Sqlite doesn't have a date data type so you will need to do string comparison to achieve this.
with d as (
select '2021-07-31 13:53:26' as d, 'A' val union all
select '2021-08-30 13:53:26' as d, 'B' val
)
select substr(d,1,4) as yyyy, substr(d,6,2) as mm, count(*)
from d
group by substr(d,1,4), substr(d,6,2)
in your query:
select substr(started_at,1,4) as yyyy, substr(started_at,6,2) as mm, count(*)
from bike_trip
group by substr(started_at,1,4), substr(started_at,6,2)
Use a CTE to get your answer.
with
-- uncomment to test
/*bike_trip(started_at) as (
values
('2021-07-31 13:53:26'),
('2021-07-17 19:06:01'),
('2021-08-30 13:53:26')
),*/
bike_months(year_month) as (
select strftime('%Y-%m', started_at) year_month from bike_trip
)
select year_month, count(year_month) count_year_month from bike_months
group by year_month;
Output:
year_month|count_year_month
2021-07|2
2021-08|1

How to group by date from datetime and order by date time

What I am trying to do is group by date only from a datetime column and then ordering by the datetime column.
Below SQL is grouping by the date from the datetime column with the minimum time ( I am assuming )
SELECT MIN(CAST(Date AS DATE))
FROM [TEST].[dbo].[PROD]
GROUP BY CAST(Date AS DATE)
Output
DATE
15/01/2018
16/01/2018
17/01/2018
18/01/2018
Is there a way where I can add the order by ‘Date’ and show the date and time in the results?
Ideal output
DATE
15/01/2018 18:52
16/01/2018 18:52
17/01/2018 18:52
18/01/2018 18:52
is this possible? if so could someone show me please.
Remove the cast in the select():
SELECT MIN(Date)
FROM [TEST].[dbo].[PROD]
GROUP BY CAST(Date AS DATE)
ORDER BY MIN(Date);
By the way "Date" is a really bad name for a column that has a time component.

Select count for each specific date

I have the following need:
I need to count the number of times each id activated from all dates.
Let's say the table looks like this:
tbl_activates
PersonId int,
ActivatedDate datetime
The result set should look something like this:
counted_activation | ActivatedDate
5 | 2009-04-30
7 | 2009-04-29
5 | 2009-04-28
7 | 2009-04-27
... and so on
Anyone know how to do this the best possible way? The date comes in the following format '2011-09-06 15:47:52.110', I need to relate only to the date without the time. (summary for each date)
you can use count(distinct .. )
and if the ActivatedDate is datetime you can get the date part
select Cast(ActivatedDate AS date), count(distinct id)
from my_table
group by ast(ActivatedDate AS date)
You can use to_char function to remove the time from date
select count(*) counted_activation,
to_char(activatedDate,"yyyy-mm-dd") ActDate
from table1
group by to_char(activatedDate,"yyyy-mm-dd");
Use 'GROUP BY' and 'COUNT'. Use CONVERT method to convert datetime to Date only
SELECT CONVERT(DATE,activatedate), COUNT(userId)
FROM [table]
GROUP BY CONVERT(DATE,InvoiceDate)

SUM of total per month

I have this table with two columns:
Price Date
45.00 12/06/2015 12:32:54 AM
455.98 22/06/2015 11:00:32 AM
32.00 08/07/2015 09:11:45 AM
98.00 11/07/2015 19:22:32 PM
(the date is in the format DD/MM/YYYY)
I need to get the sum of prices grouped by month and for this I need to find a code in sqlite that cut some parts of that date only getting the month (...06...)(...07...). Below is my code used for some parts of the SELECT:
SELECT SUM(CAST(price as INTEGER)) as TOTALPERMONTH FROM cart_history GROUP BY ....... ORDER BY ID
How I can grouped by some parts of that row?
Use STRFTIME function
select SUM(CAST(price as INTEGER)) as Total,
strftime('%m', dateField) as Month
From yourtable
Group by strftime('%m', dateField)
use HAVING clause.
SELECT SUM(CAST(price as INTEGER)) as TOTALPERMONTH
FROM cart_history,Date
GROUP BY Date
HAVING Date IN('your dates ')'