Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to display a number of entries on my c# Form application. I have a sql query that gives me the result. However this sql query is joining two other tables. Here is the query:
SQL
SELECT  
People.FirstName, 
People.MiddleName,      
People.LastName, 
People.Class, 
LogonStats.DateTime, 
LogonStats.IP

FROM         
LogonStats INNER JOIN People ON LogonStats.Logon = People.ID

WHERE DateTime >= DATEADD(day,-7, GETDATE())

ORDER BY DateTime DESC


So what basically does this query is that gives me the people who visit the website in last 1 week. I'd like to know how many people visited. I can user Count() for one table however I couldn't make it work on this query. Is there anyway to make it work?
Posted

1 solution

This will add the number of unique users to your query
SQL
WITH stats AS (
    SELECT  
            LogonStats.DateTime, 
            LogonStats.IP
            LogonStats.Logon
    FROM    LogonStats
    WHERE   DateTime >= DATEADD(day,-7, GETDATE())
    )
,cnt AS (
    SELECT  Count(DISTINCT LogonStats.Logon) Unique_users
    FROM    stats
    )
SELECT  
        People.FirstName, 
        People.MiddleName,      
        People.LastName, 
        People.Class, 
        Stats.DateTime, 
        Stats.IP,
        Unique_users
FROM    Stats
JOIN    People ON LogonStats.Logon = People.ID
JOIN    cnt
ORDER BY DateTime DESC
 
Share this answer
 

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