Solved

mssql t-sql select only min or max value from temp table

Posted on 2009-05-05
5
791 Views
Last Modified: 2013-11-30
I have a script shown below, that selects only people with identical last name and address (street only) as well as their casino net worth (theo win).  I want to select out of this temp table ONLY the lowest theo win so I can delete them from my report temp table and am stuck.. Keep in mind, its finding the duplicate addresses and last name, i just want to do something like:

delete from #report where (this is where it should be based on select from another temp table from my script below as I would change the script below to a select into #other_table) for an example...

Thanks for your help!

select r.player_id,r.firstname,r.lastname,r.address1a, r.totaltwin

from   #report r

    inner join

       (

        select lastname,address1a

        from   #report

        group by lastname,address1a

        having count(*) > 1

        ) dr

    on  r.lastname = dr.lastname

    and r.address1a = dr.address1a

Open in new window

0
Comment
Question by:smyers051972
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24306389
select r.player_id,r.firstname,r.lastname,r.address1a, r.totaltwin
from   #report r
    inner join
       (
        select lastname,address1a
        from   #report
        group by lastname,address1a
        having count(*) > 1
        ) dr
    on  r.lastname = dr.lastname
    and r.address1a = dr.address1a
WHERE r.totaltWin  =  (SELECT MIN(totalTWin) from #Report )
0
 
LVL 1

Author Comment

by:smyers051972
ID: 24306490
Whats funny is I tried it that way but in reverse, selected the MAX values, but got only 1 result.  When I tried it that way it selected 0 records.  What I am trying to do is eval the last name, and address, if they are matching records then I would want to turn around and delete those records from the #report table.

This would be a mailing list...
0
 
LVL 1

Author Comment

by:smyers051972
ID: 24306497
I think the way you mentioned above was only selecting the absolute record not each of the two records that is lowest but its strange I have 0 results from that.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24306570


select r.player_id,r.firstname,r.lastname,r.address1a, r.totaltwin, dr.minTotalTWin
from   #report r
    inner join
       (
        select lastname,address1a, MIN(totalTWin) minTotalTWin
        from   #report
        group by lastname,address1a
        having count(*) > 1
        ) dr
    on  r.lastname = dr.lastname
    and r.address1a = dr.address1a
   -- and r.totaltwin =  dr.minTotalTWin

if you want to delete the entries with minimum TotalTwin, then uncomment the last line
0
 
LVL 1

Author Closing Comment

by:smyers051972
ID: 31578091
Perfect. Thanks a bunch!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now