A SQL query where a count will be returned by date_created
SELECT COUNT(*) FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
How would I get the count ordered by date?
If you want to get the results as count(*) per date in two columns you need to include the date in the select and use it in a group by clause:
SELECT DATE(date_created) AS "Date Created", COUNT(*) AS "Count"
FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
GROUP BY DATE(date_created)
ORDER BY DATE(date_created)
As the date_created includes a time component you might want to cast it to just a date unless all dates have the exact same time - how you do this depends on what database you are using, it could be cast(date_created as date), DATE(date_created) or something similar.
This should work on most databases:
SELECT date_created, COUNT(*) FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
GROUP BY date_created
ORDER BY date_created
Related
I have a table like this, I hope to count the number of ids by month. I used the following code but it does not work.
id date_time
1390880502018723840,2021-05-08
1390881127930372100,2021-05-08
1390881498270736386,2021-05-08
SELECT twitter.tweets.id
WHERE Month(twitter.tweets.date_time)=01 AND Year(twitter.tweets.date_time)=2021 ;
you have to use count() function and to_char to get year month part of date in one column:
SELECT count(witter.tweets.id)
WHERE to_char(twitter.tweets.date_time,'YYYY-MM')= '2021-01';
you can generalize it for all the month/year by using group by :
SELECT to_char(twitter.tweets.date_time,'YYYY-MM') , count(witter.tweets.id)
group by to_char(twitter.tweets.date_time,'YYYY-MM');
To get counts for all months since Jan 2021:
SELECT date_trunc('month', date_time), count(*)
FROM twitter.tweets
WHERE date_time >= '2021-01-01'
GROUP BY 1;
If id can be NULL (which should be disallowed for an id column), use the slightly more expensive count(id) instead.
Count of distinct IDs:
SELECT date_trunc('month', date_time), count(DISTINCT id)
FROM twitter.tweets
WHERE date_time >= '2021-01-01'
GROUP BY 1;
For only Jan 2021:
SELECT count(DISTINCT id)
FROM twitter.tweets
WHERE date_time >= '2021-01-01'
WHERE date_time < '2021-02-01';
I have a table (DATA_RECORDS) in a database which contains multiple records for the same date, but at different times, running from 2015-2018. What I am trying to do is select all records within a given date range and then select the latest record available for each date. The current code I have in SQL is:
SELECT NAME, DATE_LOADED, R_ID
FROM DATA_RECORDS
WHERE ((DATE_LOADED>=to_date('01/12/2018 00:00:00', 'dd/mm/yyyy HH24:MI:SS'))
AND (DATE_LOADED<=to_date('31/12/2018 23:59:59', 'dd/mm/yyyy HH24:MI:SS')))
ORDER BY DATE_LOADED DESC;
Where the column names are 'NAME','DATE_LOADED' and 'R_ID'.
The above gives the following results:
NAME |DATE_LOADED |R_ID
-------------------------------------
RECORD_1 |31/12/2018 17:36:38 |1234
RECORD_2 |31/12/2018 10:15:11 |1235
RECORD_3 |30/12/2018 16:45:23 |1236
RECORD_4 |30/12/2018 09:06:54 |1237
RECORD_5 |30/12/2018 07:53:30 |1238
etc... As you can see, there is also not a consistent number of uploads per day.
What I want is to select
NAME |DATE_LOADED |R_ID
-------------------------------------
RECORD_1 |31/12/2018 17:36:38 |1234
RECORD_3 |30/12/2018 16:45:23 |1236
I'm very new to SQL so any help would be appreciated.
N.B: I'm using Oracle SQL Developer and I only have read-only access to the database so I cannot create any new tables or modify the current table.
I would write this logic as:
SELECT NAME, DATE_LOADED, R_ID
FROM DATA_RECORDS
WHERE DATE_LOADED >= DATE '2018-01-12' AND
DATE_LODED < DATE '2018-12-31'
ORDER BY DATE_LOADED DESC;
Then a simple method is ROW_NUMBER() -- along with extracting only the date from the date/time value:
SELECT NAME, DATE_LOADED, R_ID
FROM (SELECT NAME, DATE_LOADED, R_ID ,
ROW_NUMBER() OVER (PARTITION BY TRUNC(DATE_LOADED) ORDER BY DATE_LOADED DESC) as seqnum
FROM DATA_RECORDS
WHERE DATE_LOADED >= DATE '2018-01-12' AND
DATE_LODED < DATE '2018-12-31'
) dr
WHERE seqnum = 1
ORDER BY DATE_LOADED DESC;
YOu can use correlated subquery
select * from tablename a where date in
(select max(DATE_LOADED) from tablename b where cast(a.DATE_LOADED as date)=cast(b.DATE_LOADED as date)) and
((DATE_LOADED>=to_date('01/12/2018 00:00:00', 'dd/mm/yyyy HH24:MI:SS'))
AND (DATE_LOADED<=to_date('31/12/2018 23:59:59', 'dd/mm/yyyy HH24:MI:SS')))
Using
https://data.seattle.gov/Public-Safety/PDRs-After-using-City-of-Seattle-Public-Records-Re/wj44-r6br/data I want the know on each date the number of public disclosure requests were open. This means per date I want the number of requests created before or same day as date and don't have a close date after the date.
I copied it to https://data.world/timacbackup/seattle-police-public-disclosure-requests where I can use SQL.
The closest I've gotten is
SELECT CAST(seattle_police_records_requests.request_create_date AS DATE) AS the_date,
count(*)
FROM seattle_police_records_requests
GROUP BY CAST(seattle_police_records_requests.request_create_date AS DATE)
ORDER BY the_date DESC;
I tried
SELECT CAST(request_create_date AS DATE) AS the_date,
count((
SELECT request_create_date
FROM seattle_police_records_requests AS t
WHERE CAST(t.request_create_date AS DATE) < d.request_create_date
))
FROM seattle_police_records_requests AS d
GROUP BY CAST(request_create_date AS DATE)
ORDER BY the_date DESC;
but get unknown table 'd' for the count subquery.
The last query I tried is
WITH dates
AS (
SELECT CAST(request_create_date AS DATE) AS create_date,
CAST(request_closed_date AS DATE) AS closed_date
FROM seattle_police_records_requests
),
create_dates
AS (
SELECT DISTINCT CAST(request_create_date AS DATE) AS create_date
FROM seattle_police_records_requests
)
SELECT create_dates.create_date,
COUNT(*)
FROM dates
INNER JOIN create_dates ON dates.create_date = create_dates.create_date
GROUP BY create_dates.create_date
HAVING dates.create_date <= create_dates.create_date
ORDER BY create_dates.create_date DESC
and basically it's just counting # of requested opened on given day not all that were open as of given day.
After importing the "created" and "closed" values into SQL Server as datetime columns I was able to generate the counts per day like so:
WITH
given_dates AS
(
SELECT DISTINCT CAST(created AS DATE) AS given_date
FROM seattle_police_records_requests
)
SELECT
given_date,
(
SELECT COUNT(*)
FROM seattle_police_records_requests
WHERE created <= DATEADD(DAY, 1, given_date) AND (closed > given_date OR closed IS NULL)
) AS num_open
FROM given_dates
ORDER BY given_date;
The DATEADD was necessary to include requests opened during that day, since the comparison of a date and a datetime implies that the date value is midnight (i.e., the very beginning of that day).
Im trying to write a SQL request that'll fetch the user in my database, but here's the trick, if there's the same email twice, i only want to fetch the most recent one ( i have a time stamp in my db )
Here's the SQL CODE
SELECT cs_login, cs_email_opt_out_did
FROM dbo.individual
WHERE insert_date >= '2016-12-05 00:00:00' and insert_date < dateadd(day,1,'2016-12-05 23:59:59')
cs_login is the email adress i want to check for duplicate
cs_email_opt_oud_did is a boolean which values doesn't matter
insert_date is the timestamp on which i want to check the latest date
My problem is i have 2 email address user#test.com with a different cs_email_opt_out_did value. I don't how to could i select the one with the most recent date value from the insert_date colums
You could use the row_number() function inside a cte like this:
with cte as (
select
cs_login
, cs_email_opt_out_did
, rn = row_number() over (partition by cs_login order by insert_date desc)
from dbo.individual
where insert_date >= '2016-12-05 00:00:00'
and insert_date < dateadd(day,1,'2016-12-05 23:59:59')
)
select
cs_login
, cs_email_opt_out_did
from cte
where rn = 1;
Just get the record with the MAX(insert_date)
SELECT i.cs_login, i.cs_email_opt_out_did
FROM dbo.individual i
INNER JOIN
(SELECT cs_login, MAX(insert_date) dt FROM dbo.individual GROUP BY cs_login) as i2
ON i2.dt = i.insert_date and i2.cs_login = i.cs_login
WHERE i.insert_date >= '2016-12-05 00:00:00' and i.insert_date < dateadd(day,1,'2016-12-05 23:59:59')
You could also use the the With Ties clause. No need for a sub-query or cte
Select Top 1 With Ties
cs_login
, cs_email_opt_out_did
From dbo.individual
Where insert_date >= '2016-12-05 00:00:00' and insert_date < dateadd(day,1,'2016-12-05 23:59:59')
Order By Row_Number() over(Partition By cs_login Order By insert_date desc)
You can use Ranking windows function like DENSE_RANK
SELECT cs_login, cs_email_opt_out_did FROM (
SELECT cs_login,cs_email_opt_out_did,insert_date, DENSE_RANK() OVER (PARTITION BY cs_login ORDER BY insert_date Desc) as [Rank]
FROM dbo.individual
WHERE insert_date >= '2016-12-05 00:00:00' and insert_date < dateadd(day,1,'2016-12-05 23:59:59')) AS T1
WHERE T1.[Rank] = 1
you can also use other functions like RANK AND ROW_NUMBER
Having a table with a column like: mydate DATETIME ...
I have a query such as:
SELECT SUM(foo), mydate FROM a_table GROUP BY a_table.mydate;
This will group by the full datetime, including hours and minutes. I wish to make the group by, only by the date YYYY/MM/DD not by the YYYY/MM/DD/HH/mm.
How to do this?
Cast the datetime to a date, then GROUP BY using this syntax:
SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table.mydate);
Or you can GROUP BY the alias as #orlandu63 suggested:
SELECT SUM(foo), DATE(mydate) DateOnly FROM a_table GROUP BY DateOnly;
Though I don't think it'll make any difference to performance, it is a little clearer.
I found that I needed to group by the month and year so neither of the above worked for me. Instead I used date_format
SELECT date
FROM blog
GROUP BY DATE_FORMAT(date, "%m-%y")
ORDER BY YEAR(date) DESC, MONTH(date) DESC
Or:
SELECT SUM(foo), DATE(mydate) mydate FROM a_table GROUP BY mydate;
More efficient (I think.) Because you don't have to cast mydate twice per row.
SELECT SUM(No), HOUR(dateofissue)
FROM tablename
WHERE dateofissue>='2011-07-30'
GROUP BY HOUR(dateofissue)
It will give the hour by sum from a particular day!
this worked for me
select
CONVERT(date, CONVERT(VARCHAR(10),sd.Date,112)) as Date,
sd.CodId as CodId,
p.Description ,
sum(sd.Quantity)as Quantity,
sum(sd.TotalQuantityXPriceWithIva) as TotalWithIva
from
SaleDetails sd
join Sales s on sd.SaleId = s.SaleId
join Products p on sd.ProductId = p.ProductId
Where
(
sd.Date >=' 1/1/2021 00:00:00'
and sd.Date <= '26/10/2021 23:59:59'
and p.BarCode = '7790628000034'
and ((s.VoucherTypeId >= 16 and s.VoucherTypeId <= 18)
or s.VoucherTypeId = 32 ))
group by
CONVERT(VARCHAR(10),sd.Date,112),
sd.CodId ,
p.Description
order by CONVERT(VARCHAR(10),sd.Date,112) desc