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)
Related
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
I have a table with this structure:
id | timestamp | barcode
The timestamp is datetime and the barcode is a full barcode, from which I need only the first 9 digits.
I need to get the count for each product for each day with one query.
So I basically need a result like:
date | item number | count
-----------+-------------+--------
12.02.2019 | 827384950 | 32
Item number is left(barcode, 9).
You can try below - using cast() to convert timestamp to date and then add that in group by
select cast([timestamp] as date),left(barcode, 9) as itemnumber,count(*)
from tablename
group by cast([timestamp] as date),left(barcode, 9)
this query has better performance
select date, itemnumber, count(*) from
(select cast([timestamp] as date) as date,left(barcode, 9) as itemnumber
from tablename) a
group by date,itemnumber
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
I am using PostgreSQL version 8.1. I have a table as follows:
datetime | usage
-----------------------+----------
2015-12-16 02:01:45+00 | 71.615
2015-12-16 03:14:42+00 | 43.000
2015-12-16 01:51:43+00 | 25.111
2015-12-17 02:05:26+00 | 94.087
I would like to add the integer values in the usage column based on the date in the datetime column.
Simply, I would like the output to look as below:
datetime | usage
-----------------------+----------
2015-12-16 | 139.726
2015-12-17 | 94.087
I have tried SELECT dateTime::DATE, usage, SUM(usage) FROM tableName GROUP BY dateTime::DATE, lngusage; which does not perform as expected. Any assistance would be appreciated. Thanks in advance.
Below query should give you the desired result:
select to_char(timestamp, 'YYYY-MM-DD') as time, sum(usage)
from table
group by time
This one is for postgreSQL, I see you added MySQL also.
SELECT
dt
SUM(usage),
FROM (
SELECT
DATE_TRUNC('day', datetime) dt,
usage
FROM
tableName
) t
GROUP BY
dt
SELECT to_char(datetime, 'format'), sum(usage)
FROM table
group by to_char(datetime, 'format')
In addition you could a window function.
SELECT DATETIME
,SUM(USAGE) OVER(PARTITION BY CAST(datetime AS DATE) ORDER BY datetime) AS Usage
FROM TableName
I have a date that looks like this: 2014-10-01 12:35:29.440
the table looks like this:
ORDER 1 | 2014-07-31 00:00:00.000
ORDER 2 | 2015-07-31 00:00:00.000
sorry i wanted ORDER 2 to show up.. As my get date returns todays date and that is GREATER than 2014-07-31 00:00:00.000
Here is what i have tried:
SELECT TOP 1 NAME
FROM ORDER_DATES
WHERE GETDATE() > ORDER_DATE
ORDER BY NAME DESC
Your question still isn't quite worded in a way that is conducive to what you need... but I think I understand what you want now based on the comments.
Based on the comment:
IF it doesnt match the date then it needs to return the next row.
Which is ORDER 2
Something like this should work:
SELECT TOP 1 name
FROM ORDER_DATES o
INNER JOIN (
-- This subquery finds the first date that occurs *after* the current date
SELECT MIN(ORDER_DATE) AS ORDER_DATE
FROM ORDER_DATES
WHERE ORDER_DATE > GETDATE()
) minDateAfterToday ON o.ORDER_DATE = minDateAfterToday.ORDER_DATE
ORDER BY name
This would work a lot better if you had an ID field in the table, but this should work with the given data, you'll potentially run into issues if you have two orders on the exact same date.
EDIT:
here's a fiddle showing the query in action:
http://sqlfiddle.com/#!6/f3057/1
DATEDIFF will come handy, also you have to order by ORDER_DATE:
SELECT TOP 1 NAME
FROM ORDER_DATES
WHERE DATEDIFF(DAY,ORDER_DATE,GETDATE())>0
ORDER BY ORDER_DATE DESC
You can write as:
SELECT NAME
FROM ORDER_DATES
WHERE cast(GETDATE()as date) > cast (ORDER_DATE as date)
ORDER BY NAME DESC
Demo
Check if you are querying against right table
declare #dt datetime = cast('2014-10-01 12:35:29.440' as datetime), #dt2 datetime= cast('2014-07-31 00:00:00.000' as datetime);
print(case when #dt > #dt2 then 1 else 0 end);
This piece of script shows output 1 i.e. condition should match for ORDER 1.
Verify if you are missing some thing.
Edit as per change to original question:
here the condition needed be reverted as date value is in future which is greater than current date
new query will be as
SELECT TOP 1 NAME
FROM ORDER_DATES
WHERE ORDER_DATE > GETDATE()
ORDER BY NAME DESC