Solved

Mysql query to compare 2 columns nd multiple rows

Posted on 2008-10-28
3
658 Views
Last Modified: 2012-05-05
I have one table with 3 values:
tablex
sku_id       weight1        weight2
1234           1                   2
1234           1.4                2

I need to identify any sku_id in column sku_id that has more than one entry. After that I need identify every SKU that has identical values in weight2 but has different values in weight1.

IN the example above 1234 has multiple entries in the table and weight2 is equal for both entries. However weight1 does not equal the other weight1 for the other entry. It is not required that weight1 = weight2 just we identify any weight1 that is not equal to the other weight1 but both weight2 entries are equal.
0
Comment
Question by:azit2000
  • 3
3 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22826390
First part is easy, the below code will show all sku_id with more than 1 entry
select sku_id,count(*) as entries
from tablex
group by sku_id
having entries > 1

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
ID: 22826471
The next query is a bit more complicated. You could try with a self join:
select t1.sku_id,t1.weight2,
  t1.weight1 as t1_weight1,
  t2.weight1 as t2_weight1
from tablex as t1,tablex as t2
where 
  t1.sku_id = t2.sku_id and 
  t1.weight2 = t2.weight2 and
  t1.weight1 <> t2.weight1 

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23087051
Did my answer not resolve your issue?

http://www.experts-exchange.com/help.jsp#hi403
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL: Updating SubQuery Match Faster 9 53
PHP: concatenate query 13 79
MySQL Error Code 2 25
Combining Queries 7 29
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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