Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with Select Statment

Author  Topic 

ljp099
Yak Posting Veteran

79 Posts

Posted - 2008-03-08 : 13:08:31
Im trying to get a count of all user logins to display in a report - I have a column, count, which has a value of 1 for each record entered.


Select firstname, lastname, count(count) as TotalLogins Order by TotalLogins. But the count is always appearing as 1.

Report should look like:

John Smith 132
Jane Doe 101
Doris Day 99


dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-08 : 13:12:13
Well, you don't need a separate column that holds the value 1 to do such a thing.

Select firstname,lastname,count(*)
FROM TotalLogins
GROUP by firstname,lastname
ORDER by first name desc, lastname desc


If you must use your "Count" column

Select firstname,lastname,SUM([count])
FROM TotalLogins
GROUP by firstname,lastname
ORDER by first name desc, lastname desc




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

ljp099
Yak Posting Veteran

79 Posts

Posted - 2008-03-08 : 13:47:04
My query is still returning 1 for the count(*) totals. I tried to truncate the sql statement in my first posting. I am joing tables (bersin_users and bersin_users_loginhistory) to get data and counts. Here is the query Im currently running, works except for the TotalLogins column displays 1 for each record.

SELECT bersin_users.first_name, bersin_users.last_name, bersin_users.title, bersin_users.email, bersin_users.organization,
bersin_users_loginhistory.date_inserted, bersin_users.uID, SUM(bersin_users_loginhistory.count) AS TotalLogins
FROM bersin_users INNER JOIN
bersin_users_loginhistory ON bersin_users.uID = bersin_users_loginhistory.bersin_uid
WHERE (bersin_users_loginhistory.date_inserted >= @FromDate) AND (bersin_users_loginhistory.date_inserted <= @ToDate)
GROUP BY bersin_users.first_name, bersin_users.last_name, bersin_users.title, bersin_users.email, bersin_users.organization,
bersin_users_loginhistory.date_inserted, bersin_users.uID
ORDER BY TotalLogins
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-08 : 13:57:36
Well, that is quite different now isn't it.

I would presume this is happening simply because each row in your select statement is entirely unique, thus negating the attempt to aggregate.



SELECT bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
--bersin_users_loginhistory.date_inserted,
bersin_users.uID,
SUM(bersin_users_loginhistory.[count]) AS TotalLogins
FROM bersin_users INNER JOIN bersin_users_loginhistory
ON bersin_users.uID = bersin_users_loginhistory.bersin_uid
WHERE (bersin_users_loginhistory.date_inserted >= @FromDate)
AND (bersin_users_loginhistory.date_inserted <= @ToDate)
GROUP BY bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
-- bersin_users_loginhistory.date_inserted,
bersin_users.uID
ORDER BY TotalLogins


I am guessing that my bolded, and commented out columns above are the reason.


You should eliminate the column named "count" from the table. It is a waste of space, and is named using a keyword...both not very wise.



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

ljp099
Yak Posting Veteran

79 Posts

Posted - 2008-03-08 : 14:44:50
Why cant I run the query with the date_inserted column? So I cant run a query which simply holds an aggregate count in a column - wouldnt a select subquery perform this?

Show me users name, email, company name, date of last login and the number of total logins for the user - this is an impossible query to run in t-sql?

John Smith john@smith.com Acme Inc. 03/08/2008 100
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-08 : 15:19:36
What exactly did I say is impossible?
quote:

wouldnt a select subquery perform this?



It could do this, but you didn't do a select subquery.

If you want to aggregate a count, or a sum of a column called count, you have to group the records. If each row is unique, there is no aggregation.

You need started this with a simple count/sum query. Then it was a joined table query and more columns.

I didn't say you "CAN'T" use the loginDate_inserted column, I only said that it may be the reason you are coming back with unique rows and thus no total of the column.

I can't see your data. I don't know what you are actually trying to produce.

quote:
Why cant I run the query with the date_inserted column?


I didn't say you can't. I said that it may be the reason you cannot aggregate the data, since this is likely unique.

quote:
So I cant run a query which simply holds an aggregate count in a column


You didn't say the column held an aggregate count. I presumed you had the value of 1 in each ROW. Not an aggregate. Probably because of

quote:

I have a column, count, which has a value of 1 for each record entered.


Which clearly is not an aggregate. It is the value of 1 entered in each row, which as I said, is kind of useless to do.

If the [Count] column contains a larger number, or is updated with an actual aggregate.That is different..specifically:

quote:
Select firstname, lastname, count(count) ....

COUNTING a column, and returning unique rows will return a 1. If you want the total of a value, you would use SUM. SUM of a column called "count", and returning unique rows, will also return 1.

Illustrated here:

Create Table #T (ID int identity(1,1) not null,letter char(1) not null,number int not null)

Insert #T (letter,number)
Select 'A',1 UNION ALL
Select 'A',1 UNION ALL
Select 'B',2 UNION ALL
Select 'C',3 UNION ALL
Select 'C',1


Select ID, letter, count(number)
FROM #t
Group by ID, letter
/*
1 A 1
2 A 1
3 B 1
4 C 1
5 C 1
*/
Select ID,letter, sum(number)
FROM #t
Group by ID, Letter
/*
1 A 1
2 A 1
3 B 2
4 C 3
5 C 1
*/


Select letter,count(number)
FROM #t
Group by letter
/*
A 2
B 1
C 2
*/


Select letter,sum(number)
FROM #t
Group by letter
/*
A 2
B 2
C 4
*/

Drop Table #t


quote:

Show me users name, email, company name, date of last login and the number of total logins for the user - this is an impossible query to run in t-sql?

John Smith john@smith.com Acme Inc. 03/08/2008 100



is much different than your original post. While I can be helpful, I can't read minds, so sniping back at me for not providing you correct results for your unstated criteria.

You provided only desired results, but no sample data. Prior to this response, You never indicated you ALSO needed the "last login date"

Perhaps this will be better?


SELECT bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
max(bersin_users_loginhistory.date_inserted) as LastLogin,
bersin_users.uID,
SUM(bersin_users_loginhistory.[count]) AS TotalLogins
FROM bersin_users INNER JOIN bersin_users_loginhistory
ON bersin_users.uID = bersin_users_loginhistory.bersin_uid
WHERE (bersin_users_loginhistory.date_inserted >= @FromDate)
AND (bersin_users_loginhistory.date_inserted <= @ToDate)
GROUP BY bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
-- bersin_users_loginhistory.date_inserted,
bersin_users.uID
ORDER BY TotalLogins



Again, I don't know what your data looks like, or how it is structured, or exactly what you need to accomplish. I can only go off what you provide.




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-08 : 15:27:44
quote:
Originally posted by ljp099

Im trying to get a count of all user logins to display in a report - I have a column, count, which has a value of 1 for each record entered.


Select firstname, lastname, count(count) as TotalLogins Order by TotalLogins. But the count is always appearing as 1.

Report should look like:

John Smith 132
Jane Doe 101
Doris Day 99



Obviously, THAT query will never run. And from the looks of all the query examples, we're only guessing what you want.

Please post the actual query you want help with.

--Jeff Moden
Go to Top of Page

ljp099
Yak Posting Veteran

79 Posts

Posted - 2008-03-08 : 16:06:11
Heres the query I need help modifying - so that the last column in the query shows the number of Total Logins for a user:

SELECT bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
--bersin_users_loginhistory.date_inserted,
bersin_users.uID,
SUM(bersin_users_loginhistory.[count]) AS TotalLogins
FROM bersin_users INNER JOIN bersin_users_loginhistory
ON bersin_users.uID = bersin_users_loginhistory.bersin_uid
WHERE (bersin_users_loginhistory.date_inserted >= @FromDate)
AND (bersin_users_loginhistory.date_inserted <= @ToDate)
GROUP BY bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
-- bersin_users_loginhistory.date_inserted,
bersin_users.uID
ORDER BY TotalLogins

TotalLogins would show the total number of times a user has logged in (the sum of all their logins).

Thanks


Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-08 : 16:37:58
quote:
Originally posted by ljp099

Heres the query I need help modifying - so that the last column in the query shows the number of Total Logins for a user:

SELECT bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
--bersin_users_loginhistory.date_inserted,
bersin_users.uID,
SUM(bersin_users_loginhistory.[count]) AS TotalLogins
FROM bersin_users INNER JOIN bersin_users_loginhistory
ON bersin_users.uID = bersin_users_loginhistory.bersin_uid
WHERE (bersin_users_loginhistory.date_inserted >= @FromDate)
AND (bersin_users_loginhistory.date_inserted <= @ToDate)
GROUP BY bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
-- bersin_users_loginhistory.date_inserted,
bersin_users.uID
ORDER BY TotalLogins

TotalLogins would show the total number of times a user has logged in (the sum of all their logins).

Thanks







Did you bother reading my last post?

That column does not contain the number of times a user has logged in as written. it contains the number 1 in each row as per your very first post on the topic.

What about the query above, that you just posted (which is the one I gave you) does not work?

Does it still provide only 1 in the total column?

As a TEST, run the below and see if it returns a proper Sum.


SELECT bersin_users.uID,
SUM(bersin_users_loginhistory.[count]) AS TotalLogins
FROM bersin_users INNER JOIN bersin_users_loginhistory
ON bersin_users.uID = bersin_users_loginhistory.bersin_uid
WHERE (bersin_users_loginhistory.date_inserted >= @FromDate)
AND (bersin_users_loginhistory.date_inserted <= @ToDate)
GROUP by bersin_users.uID
ORDER BY TotalLogins





Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

ljp099
Yak Posting Veteran

79 Posts

Posted - 2008-03-08 : 17:28:41
Thanks for your help. Your query did exactly what I was looking for:

SELECT bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
--bersin_users_loginhistory.date_inserted,
bersin_users.uID,
SUM(bersin_users_loginhistory.[count]) AS TotalLogins
FROM bersin_users INNER JOIN bersin_users_loginhistory
ON bersin_users.uID = bersin_users_loginhistory.bersin_uid
WHERE (bersin_users_loginhistory.date_inserted >= @FromDate)
AND (bersin_users_loginhistory.date_inserted <= @ToDate)
GROUP BY bersin_users.first_name,
bersin_users.last_name,
bersin_users.title,
bersin_users.email,
bersin_users.organization,
-- bersin_users_loginhistory.date_inserted,
bersin_users.uID
ORDER BY TotalLogins
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-08 : 17:38:03
So my original reply, indicating that the date_inserted was causing unique rows was correct?

quote:

Why cant I run the query with the date_inserted column?



I guess you know that answer now...

Happy to help!





Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-08 : 18:56:52
@ljp099:
i feel i have to ask you this:
Do you fully understand why your original query did't work and why the one provided by dataguru1971 did?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-03-08 : 19:23:51
ljp099 -- maybe it will be helpful for you to read this:

http://weblogs.sqlteam.com/jeffs/archive/2007/07/20/60261.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-08 : 20:34:08
I still can't get over the COUNT column... guess you don't ever have to worry about indexing that bad boy ;-)

--Jeff Moden
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-03-09 : 01:11:19
The "count" columns of 1s is what Kimball advocates in his "datawarehouses for dummies" seminars.

e4 d5 xd5 Nf6
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-09 : 08:34:03
@blindman:
i'm no data warehouse expert so i have to ask:
why?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-09 : 10:39:37
quote:
Originally posted by blindman

The "count" columns of 1s is what Kimball advocates in his "datawarehouses for dummies" seminars.


Curious... does Kimball explain why?

--Jeff Moden
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-03-09 : 10:56:47
I just cant see a practical need for storing that...it seems to predicate using sum instead of count aggregation. Either way, you have to do GROUP BY, so I am not sure the relevance...i can't imagine that one is faster than the other.



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-09 : 10:59:46
neither is faster... so i was pondering the same question.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-03-09 : 12:25:52
The people I talked to who had been to his seminar said it was supposedly more efficient. Never made much sense to me.

e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -