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 |
|
oap
Yak Posting Veteran
60 Posts |
Posted - 2012-09-18 : 23:16:22
|
| This has been asked many times before and the more answers I read, the more confused I become.My table:lcreator // locid // issue // statusJoe, 12, missing photos, NotifyJim Bob, 1344, missing information, NotifyMary Joe, 33, needs to update, NotifyJim Bob, 444, blurry photos, NotifyBetty, 32, blurry photos, NotifyJim Bob, 1244, no information on location, NotifyI want to read from mytable, all records where status = 'Notify' (which means the person will be receiving an e-mail to let them know they need to fix their photos).The 'issue' will be what they need to fix and the 'locid' is a location ID. The 'lcreator' is the location creator's name.I'm trying to only send out ONE message per user to tell them that they have to fix their photos. Currently I am receiving multiple results for the same name if lcreator has multiple entries of the same person.eg.Jim BobJim BobJim BobMary JoeBettyThis is my code:strsql = "select distinct lcreator,locid,issue from mytable where status = 'Notify'"while not objRS.eoftofld = objrs("lcreator")lcn = objrs("locid")reason = objrs("issue").....<snip>It is giving me multiple results for lcreator when I want distinct ones. I believe this is due to SQL looking through all rows so it is including 'locid' and 'issue' in the query. I don't want this.I need to return the results from locid and issue columns (some of which will be duplicate) but only ONE result per LCREATOR. It doesn't matter which result it is, as long as only ONE Jim Bob, one Betty, one Mary Joe gets returned.Once the person receives a message, they can view ALL records under their name through another SELECT statement I've got working.Help would be gretly appreciated. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-09-19 : 02:28:53
|
Try this:select t1.lcreator,t1.locid,t1.issue from mytable t1join (select lcreator,max(locid) as locid from mytable where status = 'Notify' group by lcreator)t2on t1.locid = t2.locid Too old to Rock'n'Roll too young to die. |
 |
|
|
|
|
|
|
|