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.
| Author |
Topic |
|
madlinux
Starting Member
5 Posts |
Posted - 2009-01-26 : 19:35:32
|
| System that collects login information for users to network. Everytime a user logins in the username and computer name are written to a database. In a view I can link the logon tbl to the corresponding workstation tbl and see the rows match sucessfully but since the logon information is captured every login I see many duplicates. What I have been trying to do is create my view to only display the username to the computer that they have logged into the most. I am still at the "SQL Learning Stage" so I feel that it is neccessary to post a request for a little "bump" in the right direction so I can find this information this year :-) In advance any help would be greatly apprecaited. |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-26 : 22:39:09
|
| Please post sample data and expected output. |
 |
|
|
madlinux
Starting Member
5 Posts |
Posted - 2009-01-26 : 23:14:59
|
| SamAccount CompName_________________________Adam COMP1Adam COMP1Adam COMP1Adam COMP2Bob COMP3Bob COMP3Bob COMP4Bob COMP4Bob COMP4Dan COMP5Dan COMP6Dan COMP6Dan COMP6Dan COMP6Dan COMP6______________________Expected Result:Adam COMP1Bob COMP4Dan COMP6 |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-26 : 23:23:44
|
| declare @temp table ( SamAccount varchar(32),CompName varchar(32))insert into @temp select 'Adam', 'COMP1' union allselect 'Adam', 'COMP1' union allselect 'Adam', 'COMP1' union allselect 'Adam', 'COMP2' union allselect 'Bob', 'COMP3' union allselect 'Bob', 'COMP3' union allselect 'Bob', 'COMP4' union allselect 'Bob', 'COMP4' union allselect 'Bob', 'COMP4' union allselect 'Dan', 'COMP5' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' select samaccount,compname from ( select *, row_number() over ( partition by samaccount order by compname) as rn from @temp ) twhere rn = 3 |
 |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-26 : 23:30:30
|
| Hi Dear,Is this something you expected..CodeDECLARE @TEST TABLE(SamAccount varchar(50),CompName varchar(50))INSERT INTO @TESTSELECT 'Adam','COMP1' UNION ALLSELECT 'Adam','COMP1' UNION ALLSELECT 'Adam','COMP1' UNION ALLSELECT 'Adam','COMP2' UNION ALLSELECT 'Bob','COMP3' UNION ALLSELECT 'Bob','COMP3' UNION ALLSELECT 'Bob','COMP4' UNION ALLSELECT 'Bob','COMP4' UNION ALLSELECT 'Bob','COMP4' UNION ALLSELECT 'Dan','COMP5' UNION ALLSELECT 'Dan','COMP6' UNION ALLSELECT 'Dan','COMP6' UNION ALLSELECT 'Dan','COMP6' UNION ALLSELECT 'Dan','COMP6' UNION ALLSELECT 'Dan','COMP6'SELECT * FROM @TESTSELECT SamAccount,MAX(CompName) FROM @TESTGROUP BY SamAccountRegardsThiyagarajanwww.sqlhunt.blogspot.com |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-26 : 23:35:54
|
| Don't u Have any audit columns in your table like Identity column or Datecolumn ??? |
 |
|
|
madlinux
Starting Member
5 Posts |
Posted - 2009-01-26 : 23:42:42
|
| I do have columns that show date, not to sure what you mean by identity.There is one item I did not mention, there are 5 million rows in the logon table. Is it feasible to use the examples above in this situation?I am going to try both of the helpful responses on a small example of what I am trying to accomplish to see if I can understand the logic better. Thanks :-) |
 |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-26 : 23:46:58
|
| IDENTITY Column is nothing but a column in which the value(integer) is autogenerated by the SQL to maintain the uniqueness of a table.RegardsThiyagarajanwww.sqlhunt.blogspot.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-27 : 00:30:46
|
quote: Originally posted by madlinux I do have columns that show date, not to sure what you mean by identity.There is one item I did not mention, there are 5 million rows in the logon table. Is it feasible to use the examples above in this situation?I am going to try both of the helpful responses on a small example of what I am trying to accomplish to see if I can understand the logic better. Thanks :-)
is it an audit field like datemodified (one which gets autoupdated on insert/updates?). then this will be enoughselect samaccount,compname from ( select *, row_number() over ( partition by samaccount order by datecol desc) as rn from @temp ) twhere rn = 1 |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-27 : 00:34:35
|
| Hi try thisDECLARE @TEST TABLE(SamAccount varchar(50),CompName varchar(50))INSERT INTO @TESTSELECT 'Adam','COMPt' UNION ALLSELECT 'Adam','COMPt' UNION ALLSELECT 'Adam','COMP1' UNION ALLSELECT 'Adam','COMP2' UNION ALLSELECT 'Bob','COMPy' UNION ALLSELECT 'Bob','COMPx' UNION ALLSELECT 'Bob','COMPx' UNION ALLSELECT 'Bob','COMPx' UNION ALLSELECT 'Bob','COMP4' UNION ALLSELECT 'Dan','COMP5' UNION ALLSELECT 'Dan','COMP6' UNION ALLSELECT 'Dan','COMP6' UNION ALLSELECT 'Dan','COMP9' UNION ALLSELECT 'Dan','COMP9' UNION ALLSELECT 'Dan','COMP9' select distinct t1.samaccount,t1.compname from @test t1inner join ( select row_number() over ( partition by samaccount order by count(compname) desc) as rn,compname from @test group by samaccount,compname ) ton t.compname = t1.compname and t.rn = 1 |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-27 : 00:39:09
|
quote: Originally posted by visakh16
quote: Originally posted by madlinux I do have columns that show date, not to sure what you mean by identity.There is one item I did not mention, there are 5 million rows in the logon table. Is it feasible to use the examples above in this situation?I am going to try both of the helpful responses on a small example of what I am trying to accomplish to see if I can understand the logic better. Thanks :-)
is it an audit field like datemodified (one which gets autoupdated on insert/updates?). then this will be enoughselect samaccount,compname from ( select *, row_number() over ( partition by samaccount order by datecol desc) as rn from @temp ) twhere rn = 1
Hi Visakh,I think what MadLinux wants is Username and companyname which is occured max times in his table...Isn't it...? I guessed it from His Expected Result.. |
 |
|
|
Kokkula
Starting Member
41 Posts |
Posted - 2009-01-28 : 06:07:16
|
| Hello,Try thisdeclare @temp table ( SamAccount varchar(32),CompName varchar(32))insert into @temp select 'Adam', 'COMP1' union allselect 'Adam', 'COMP1' union allselect 'Adam', 'COMP1' union allselect 'Adam', 'COMP2' union allselect 'Bob', 'COMP3' union allselect 'Bob', 'COMP3' union allselect 'Bob', 'COMP4' union allselect 'Bob', 'COMP4' union allselect 'Bob', 'COMP4' union allselect 'Dan', 'COMP5' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' SELECT * FROM @TempSELECT A.SamAccount, A.CompNameFROM @Temp AGROUP BY SamAccount, CompNameHAVING COUNT(1) = (SELECT MAX(Cnt) FROM(SELECT SamAccount, CompName, COUNT(1) AS CntFROM @TempGROUP BY SamAccount, CompName)BWHERE A.SamAccount = B.SamAccount)--Thanks,Pavan |
 |
|
|
madlinux
Starting Member
5 Posts |
Posted - 2009-02-02 : 16:29:21
|
| Hello, back to this one - I have been trying to understand how I can use the identity column to my advantage, like I said there is almost 5 million rows in the table so wrapping each row in a SELECT statement seems not the way to go. Can someone provide an example of using a identity column to "loop" through? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-03 : 09:33:23
|
quote: Originally posted by madlinux Hello, back to this one - I have been trying to understand how I can use the identity column to my advantage, like I said there is almost 5 million rows in the table so wrapping each row in a SELECT statement seems not the way to go. Can someone provide an example of using a identity column to "loop" through?
why do you want to loop through? wont a batch executing query (set based) be enough? |
 |
|
|
madlinux
Starting Member
5 Posts |
Posted - 2009-02-04 : 01:21:51
|
quote: Originally posted by Nageswar9 declare @temp table ( SamAccount varchar(32),CompName varchar(32))insert into @temp select 'Adam', 'COMP1' union allselect 'Adam', 'COMP1' union allselect 'Adam', 'COMP1' union allselect 'Adam', 'COMP2' union allselect 'Bob', 'COMP3' union allselect 'Bob', 'COMP3' union allselect 'Bob', 'COMP4' union allselect 'Bob', 'COMP4' union allselect 'Bob', 'COMP4' union allselect 'Dan', 'COMP5' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' union allselect 'Dan', 'COMP6' select samaccount,compname from ( select *, row_number() over ( partition by samaccount order by compname) as rn from @temp ) twhere rn = 3
This was what I needed, after banging my head on the wall (remember I am a SQL noob) I realized thatSELECT SamAccount,MAX(CompName) FROM @TESTGROUP BY SamAccountWorked like I needed it to, funny when one over complicates a problem and the answer was right in front of them :-) Thanks for everyone's help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-04 : 03:12:48
|
| this will just return you alphabetically latest company name linked to account,is that waht you were looking at? |
 |
|
|
|
|
|
|
|