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 |
2revup
Posting Yak Master
112 Posts |
Posted - 2014-01-27 : 23:00:46
|
Guys I am having a great deal of trouble with a query and trying to get the data that I need. I am trying to get each login, location, supervisor_name and in this it must have the details showing the latest column no duplicates of the logins.I have the problem at the moment that its showing some login's that are duplicates because they may have different supervisor_names. Here is what I am using:select agent_login_id,location,supervisor_name,title,MAX(EFFECTIVE_start_DAY from agentsnewwhere location in ('AU','US','EU')and (Title like '%Support%%Engineer%' or Title like '%Enterprise%')and (EFFECTIVE_END_DAY >= '2013/06/01' or EFFECTIVE_END_DAY is null)group by agent_login_id,location,supervisor_name,titleSome results:agent_login_id location supervisor_name Start_dateLogin1 US Manager1 2012-07-30Login2 AU Manager2 NULLLogin3 US Manager3 2013-08-05Login3 US Manager4 2012-10-01 Login3 US Manager3 NULLLogin4 EU Manager5 NULLNote login3, this is repeated three times, I need to some how show only this one. While it seems easy enough to just target the NULL, not all of the logins will be in the same situation, in that they may not have NULL, and it may have a date.Let me know if you need more information. |
|
sqlsaga
Yak Posting Veteran
93 Posts |
Posted - 2014-01-28 : 00:50:04
|
If you have posted some DDL of the tables involved and sample DML, it would have been so easier to answer this query.. Now its more of a guessing game. First thing Login3 is repeating 3 times because it has different manager names and dates and since you are looking for a distinct record, the given record is also a distinct one. So I hope if you are looking for the better answer, I guess you have to post the DML related to the agentsnew table and you will get response like a quick charm...Visit www.sqlsaga.com for more t-sql snippets and BI related how to's. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-28 : 03:27:51
|
[code]SELECT agent_login_id,location,supervisor_name,title,EFFECTIVE_start_DAY FROM(select agent_login_id,location,supervisor_name,title,EFFECTIVE_start_DAY ,ROW_NUMBER() OVER (PARTITION BY agent_login_id ORDER BY COALESCE(EFFECTIVE_start_DAY,0) DESC) AS Seqfrom agentsnewwhere location in ('AU','US','EU')and (Title like '%Support%%Engineer%' or Title like '%Enterprise%')and (EFFECTIVE_END_DAY >= '2013/06/01' or EFFECTIVE_END_DAY is null)group by agent_login_id,location,supervisor_name,title)tWHERE Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
2revup
Posting Yak Master
112 Posts |
Posted - 2014-01-28 : 16:29:46
|
perfect... as usual. visakh16 do you have any good guides that you could point me to around 'ROW_NUMBER() OVER (PARTITION BY' I would like to try and understand more about how it works. |
|
|
sqlsaga
Yak Posting Veteran
93 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|
|
|