Solved

SQL Question

Posted on 2009-03-31
10
181 Views
Last Modified: 2012-05-06
Hello

I have some table like this:


A                      B
1                      1
1                      3
1                      2
2                      2
2                      1
2                      3

Now I want only these rows, where b is the highest value for A

So i want:

1                      3
2                      3

But it's not always 3 the highest value

How can I do that?

Thanks
0
Comment
Question by:Ruttensoft
  • 5
  • 4
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24026837
this will do:
select t.*
  from yourtable t
 where t.b = ( select max(i.b) from yourtable i where i.a= t.a)

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 24026871
try this.
SELECT A,MAX(B) AS B

  FROM YourTable

 GROUP BY A

Open in new window

0
 

Author Comment

by:Ruttensoft
ID: 24026896
Hm, so mytable is  tbl_back_checkliste_aktiv, a = tbl_back_checkliste_aktiv_checkliste_typid, b = tbl_back_checkliste_aktiv_checkliste_version gives me this, right:

select t.*
from tbl_back_checkliste_aktiv t
 where t.tbl_back_checkliste_aktiv_checkliste_version = (select max(i.tbl_back_checkliste_aktiv_checkliste_version) from tbl_back_checkliste_aktiv i where i.tbl_back_checkliste_aktiv_checkliste_typid= t.tbl_back_checkliste_aktiv_checkliste_typid)

But the query says:

There was an error parsing the query. [ Token line number = 3,Token line offset = 59,Token in error = select ]

What have I done wrong?
0
 

Author Comment

by:Ruttensoft
ID: 24026901
Thanks Sarath, that did it!
0
 

Author Comment

by:Ruttensoft
ID: 24026918
Ups, another thing: I have other columns in the table, so column d, e and f.

How can I add them to this query?
0
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.

 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 24026969

SELECT T1.*

  FROM YourTable T1

 INNER JOIN (SELECT A,MAX(B) AS B FROM YourTable GROUP BY A) AS T2

    ON T1.A = T2.A AND T1.B = T2.B

Open in new window

0
 

Author Comment

by:Ruttensoft
ID: 24026981
Boooooaa nice, thanks!!
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24026996
I also updated your original query :)

SELECT T1.*

  FROM tbl_back_checkliste_aktiv T1

 INNER JOIN (SELECT tbl_back_checkliste_aktiv_checkliste_typid,

                    MAX(tbl_back_checkliste_aktiv_checkliste_version) AS tbl_back_checkliste_aktiv_checkliste_version

               FROM tbl_back_checkliste_aktiv

              GROUP BY tbl_back_checkliste_aktiv_checkliste_typid) AS T2

    ON T1.tbl_back_checkliste_aktiv_checkliste_typid = T2.tbl_back_checkliste_aktiv_checkliste_typid 

   AND T1.tbl_back_checkliste_aktiv_checkliste_version = T2.tbl_back_checkliste_aktiv_checkliste_version

Open in new window

0
 

Author Comment

by:Ruttensoft
ID: 24027000
Thanks a lot!
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24027009
you are welcome
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

23 Experts available now in Live!

Get 1:1 Help Now