Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to show that how much user loggedin my site between two date like 01/02/2015 to 22/03/2015 and my query is:
"select LoggedIn,count(LoggedInFrom) AS Device from User_log_Info where LoggedIn between '03/20/2015 00:00:00' And '03/22/2015 23:59:59' And LoggedInFrom = 0 GROUP BY LoggedIn order by LoggedIn asc "

the result is :
HTML
id          LoggedIn        Device

1   2015-03-20 23:14:00     1
2   2015-03-21 07:13:00     1
3   2015-03-21 11:22:00     1
4   2015-03-21 11:24:00     1
5   2015-03-21 11:35:00     1
6   2015-03-22 14:07:00     1


but i want to show like this:
HTML
id  	LoggedIn  Device

1   2015-03-20 	     1		
2	2015-03-21 	     4
3	2015-03-22 	     1


how can i do this?
Posted
Updated 22-Mar-15 19:29pm
v2
Comments
PIEBALDconsult 23-Mar-15 1:50am    
GROUP BY and COUNT
And I hope you are not storing dates as strings.

Abhinav gave correct solution, but there are minor syntax errors in it.

SQL
SELECT CAST(LoggedIn AS DATE)  LoggedIn
       ,COUNT(LoggedInFrom) AS Device
FROM User_log_Info
WHERE LoggedIn BETWEEN '03/20/2015 00:00:00' AND '03/22/2015 23:59:59'
      AND LoggedInFrom = 1
GROUP BY CAST(LoggedIn AS DATE)
ORDER BY LoggedIn ASC


Note that I changed one condition LoggedInFrom = 1 to get data as per the sample provided.
 
Share this answer
 
Comments
Member 9361273 23-Mar-15 5:37am    
Yes its working well but it also show the data of 03/23/2015 though i want to show data between '03/20/2015 00:00:00' AND '03/22/2015 23:59:59'
Saral S Stalin 23-Mar-15 6:01am    
That's a surprise. Plesae post table schema and sample data and the query you are running. Let me check.
Member 9361273 24-Mar-15 2:54am    
Column Name datatype Allow nulls
ID int no
LoggedIn smalldatetime no
LoggedInFrom tinyint no

this is the table design
and sample data is shown below:

ID LoggedIn LoggedInFrom
1 2015-02-20 11:48:00 1
2 2013-02-20 11:48:00 2
3 2013-02-21 11:48:00 2


and my my query is:
SELECT CAST(LoggedIn AS DATE) LoggedIn
,COUNT(LoggedInFrom) AS Device
FROM User_log_Info
WHERE LoggedIn BETWEEN '03/20/2015 00:00:00' AND '03/22/2015 23:59:59'
AND LoggedInFrom = 1
GROUP BY CAST(LoggedIn AS DATE)
ORDER BY LoggedIn ASC



Cast LoggedIn to date ->
select LoggedIn,count(LoggedInFrom) AS Device from User_log_Info where LoggedIn between '03/20/2015 00:00:00' And '03/22/2015 23:59:59' And LoggedInFrom = 0 GROUP BY CAST(LoggedIn) AS DATE) order by LoggedIn asc
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900