I have simple table where i store userID, UserName, Email, RegistrationDate ...
RegistrationDate is DateTime Datatype
When i to to get the count of users for each date it get me 1 for each row as it each row has a unique dateTime stamp.
SELECT DATEPART(dd, RegistrationDate) AS DD, COUNT(userID)
FROM USER_Table
GROUP BY RegistrationDate
order by RegistrationDate DESC
ABove query get me each 100 rows if i have 100 users registered.
I want to get count based on date. i tried different thing but nothing seems to work.
Try this :
SELECT Cast(RegistrationDate AS DATE),
Count(userID)
FROM User_Table
GROUP BY Cast(RegistrationDate AS DATE)
ORDER BY Cast(RegistrationDate AS DATE)DESC
You need to group by CAST(RegistrationDate as Date)
SELECT CAST(RegistrationDate as Date) AS [Date], COUNT(userID)
FROM USER_Table
GROUP BY CAST(RegistrationDate as Date)
order by CAST(RegistrationDate as Date) DESC
SELECT cast(RegistrationDate as Date) AS DD, COUNT(userID)
FROM USER_Table
GROUP BY cast(RegistrationDate as Date)
order by cast(RegistrationDate as Date) DESC
Related
This is my query which is not returning the correct result ordered by RegistrationDate Desc:
SELECT
Team,
CONVERT(VARCHAR(10), RegistrationDate, 103) AS RegistrationDate,
FormFilledAt, CreationBy
FROM
Table_Candidate_Info
WHERE
Status = 'Completed'
GROUP BY
Team, CONVERT(VARCHAR(10), RegistrationDate, 103), FormFilledAt, CreationBy
ORDER BY
RegistrationDate DESC
If I will use this query, it's returning the correct order by RegistrationDate Desc
select *
from Table_Candidate_Info
order by RegistrationDate desc
I want above first query should be RegistrationDate order by Desc with group by query
Try
order by CONVERT(VARCHAR(10),RegistrationDate,103) desc
or better if you want really keep order by date (and not text) try this:
select Team, CONVERT(VARCHAR(10),RegistrationDate,103) as RegistrationDate, FormFilledAt,CreationBy
from (
Select Team, cast(RegistrationDate as date) as RegistrationDate ,FormFilledAt,CreationBy
from Table_Candidate_Info
where Status='Completed'
group by Team,cast(RegistrationDate as date) ,FormFilledAt,CreationBy
) tmp
order by RegistrationDate desc
Note: if you want group by date + time remove cast… as date
use distinct and CONVERT(VARCHAR(10),RegistrationDate,103) in order by clause
Select distinct Team,CONVERT(VARCHAR(10),RegistrationDate,103)as RegistrationDate ,FormFilledAt,CreationBy
from Table_Candidate_Info where Status='Completed'
order by CONVERT(VARCHAR(10),RegistrationDate,103) desc
Note: You don't need group by since you are not using any aggregated function
The reason why results are not ordered by RegistrationDate when you convert it to a varchar in Select clause is because Order By clause is logically processed after evaluation of Select clause.
Now in first query when you write
Select * from Table_Candidate_Info order by RegistrationDate desc
[Though writing * in select list is a very bad practice] format of RegistrationDate still remains date in Select clause which holds true for further logical processing phase of Order By clause. Hence 31.01.2019 comes first and 31.12.2018 later.
But when you convert it to varchar(10) to get a required format then actually Order By clause is ordering a Varchar and not Date. Hence 31/12/2018 comes first and 31/01/2019 comes after it.
To resolve the problem if you want to retain the formatting of datetime/date column in Select but Order By with Date value then simply cast the datetime column back to Date in Order by clause.
Pseudo code as:
select CONVERT(VARCHAR(10),RegistrationDate,103) as RegistrationDate from
Table_Candidate_Info
order by cast(RegistrationDate as Date) desc -- cast it back to date
Demo Link here: https://rextester.com/WMLQL78387
I am trying to find all record count for each day using query:
select cast(Timestamp_field as date), count(*) as cnt
from table1
group by 1
having cast(Timestamp_field as date) between date and date -10;
Timestamp_field is a timestamp and I am casting this to date. This; despite max value of Timestamp_field showing 2016-09-20 12:31:38.000000, doesn't return any record. Any idea why?
My guess is that the problem is the between. Perhaps this will work for you:
select cast(Timestamp_field as date), count(*)
from table1
group by 1
having cast(Timestamp_field as date) between date - 10 and date;
The smaller value should go first for the between comparands.
Note: You should do the filtering before the group by, not after:
select cast(Timestamp_field as date), count(*)
from table1
where cast(Timestamp_field as date) between date - 10 and date;
group by 1
I am trying to write a statement and just a bit puzzled what is the best way to put it together. So I am doing a UNION on a number of tables and then from there I want to produce as the output a count for the UserID within that day.
So I will have numerous tables union such as:
Order ID, USERID, DATE, Task Completed.
UNION
Order ID, USERID, DATE, Task Completed
etc
Above is layout of the table which will have 4 tables union together with same names.
Then statement output I want is for a count of USERID that occurred within the last 24 hours.
So output should be:
USERID--- COUNT OUTPUT-- DATE
I was attempting a WHERE statement but think the output is not what I am after exactly, just thinking if anyone can point me in the right direction and if there is alternative way compared to the union? Maybe a joint could be a better alternative, any help be appreciated.
I will eventually then put this into a SSRS report, so it gets updated daily.
You can try this:
select USERID, count(*) as [COUNT], cast(DATE as date) as [DATE]
from
(select USERID, DATE From SomeTable1
union all
select USERID, DATE From SomeTable2
....
) t
where DATE <= GETDATE() AND DATE >= DATEADD(hh, -24, GETDATE())
group by USERID, cast(DATE as date)
First, you should use union all rather than union. Second, you need to aggregate and use count distinct to get what you want:
So, the query you want is something like:
select count(distinct userid)
from ((select date, userid
from table1
where date >= '2015-05-26'
) union all
(select date, userid
from table2
where date >= '2015-05-26'
) union all
(select date, userid
from table3
where date >= '2015-05-26'
)
) du
Note that this hardcodes the date. In SQL Server, you would do something like:
date >= cast(getdate() - 1 as date)
And in MySQL
date >= date_sub(curdate(), interval 1 day)
EDIT:
I read the question as wanting a single day. It is easy enough to extend to all days:
select cast(date as date) as dte, count(distinct userid)
from ((select date, userid
from table1
) union all
(select date, userid
from table2
) union all
(select date, userid
from table3
)
) du
group by cast(date as date)
order by dte;
For even more readability, you could use a CTE:
;WITH cte_CTEName AS(
SELECT UserID, Date, [Task Completed] FROM Table1
UNION
SELECT UserID, Date, [Task Completed] FROM Table2
etc
)
SELECT COUNT(UserID) AS [Count] FROM cte_CTEName
WHERE Date <= GETDATE() AND Date >= DATEADD(hh, -24, GETDATE())
I think this is what you are trying to achieve...
Select
UserID,
Date,
Count(1)
from
(Select *
from table1
Union All
Select *
from table2
Union All
Select *
from table3
Union All
Select *
from table4
) a
Group by
Userid,
Date
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
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