Solved

Neeq a SQL query

Posted on 2011-03-10
9
224 Views
Last Modified: 2012-06-27
Experts,
As per my attachment, could you please get me a query?
Thanks in advance.
test.docx
0
Comment
Question by:Tpaul_10
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 9

Expert Comment

by:sshah254
Comment Utility
What decides if 1233 or 1234 from Table1 should be selected?

Ss
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 50 total points
Comment Utility

select A.Column1, b.Column1, MAX(A.Column3) [Column3]
from Table1 A
inner join Table2 B on B.Column1 = A.Column2
group by A.Column1, b.Column1
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
I presume you need to review the specs ... because I don't see an explanation why table2 is actually needed, and I see no explanation on why row2 from table1 is returned and why not row1.

meanwhile, you might want to read this article, I presume it will help you:
http://www.experts-exchange.com/A_3203.html
0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
My mistake


select MAX(A.Column1) [Table1 Column1], B.Column1 [table2 Column1], MAX(A.Column3) [Column3]
from Table1 A
inner join Table2 B on B.Column1 = A.Column2
group by B.Column1
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Tpaul_10
Comment Utility
Basically I need the max value from table1 and for some rows, it is returning the both the rows even though I have used max(column1) in my query. Not sure why
Thanks
0
 
LVL 9

Expert Comment

by:sshah254
Comment Utility
ewangoya,

We don't know why he is selecting one row over the other - maybe it should be max, maybe min, maybe first, maybe last ... :-)

Ss
0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility

A quick look at the results tells me its max values
0
 
LVL 40

Accepted Solution

by:
Sharath earned 200 total points
Comment Utility
try this.
SELECT t1.Column1, 
       t2.Column1, 
       t1.Column3 
  FROM table1 t1 
       JOIN (  SELECT Column2, 
                      MAX(Column1) Column1 
                 FROM table1 
             GROUP BY Column2) AS t11 
         ON t1.Column1 = t11.Column1 
            AND t1.Column2 = t11.Column2 
       JOIN table2 t2 
         ON t1.Column2 = t2.Column2

Open in new window

0
 

Author Closing Comment

by:Tpaul_10
Comment Utility
Thanks for all your help guys and just to let you know I don't have to review my specs since it was pretty straight forward and I didn't specify all the columns in my tables and I wanted the maximum value.

Sharath's solution gave me the correct results. once again THANKS to all for quick response.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

12 Experts available now in Live!

Get 1:1 Help Now