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 |
12many
Starting Member
9 Posts |
Posted - 2013-04-10 : 08:46:55
|
Hi thereI Have this TableId Time Name1 08:00 Ian2 08:05 Lindsey3 08:10 Sally4 08:15 Lisa5 08:20 IanHow can i Select only the ons that say Ian So Far i have this "SELECT * FROM tbl HAVING COUNT(Name) > 1" this is incorrect,I believe, Because i don't have Count(Name) in the main query Many thanks |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-04-10 : 08:48:30
|
Select name,count(1)From yourTableNameGroup BY name Having count(1)>1CheersMIK |
|
|
12many
Starting Member
9 Posts |
Posted - 2013-04-10 : 09:40:01
|
Hi Mik Thanks for your help Sadly that didn't work for me, i need the 'Id' as Well If i do that all the counts come out as one The idea of what im trying to do is this For 'Ian' he has two records one early in the morning and one later i want to be able to mark the Later one as it is not needed My desired Out Put Would be something like ID Time Name1 08:00 Ian5 08:20 IanMany Thanks IanRegards Ian |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-04-10 : 10:41:37
|
[code]select ID, Time, Namefrom( select *, COUNT(*) over (partition by Name) as N from yourTableName) s where N > 1;[/code] |
|
|
UnemployedInOz
Yak Posting Veteran
54 Posts |
Posted - 2013-04-10 : 18:18:08
|
SELECT * FROM Table where Name = 'Ian'Your question does not mention counting the entries |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-04-10 : 18:29:06
|
quote: Originally posted by UnemployedInOz SELECT * FROM Table where Name = 'Ian'Your question does not mention counting the entries
True, but the subject line asks for "selecting rows where there are multiples of one Field" |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-11 : 02:31:12
|
[code]SELECT ID,Time,NameFROM(SELECT *,COUNT(1) OVER (PARTITION BY Name) AS OccFROM Table)tWHERE Occ>1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
12many
Starting Member
9 Posts |
Posted - 2013-04-12 : 03:17:26
|
quote: True, but the subject line asks for "selecting rows where there are multiples of one Field"
Cheers james k totally correct ;)Hi Guys thanks for for all the help defiantly given me a good couple of thing too try out Many thanks indeed :)Ian |
|
|
|
|
|
|
|