Solved

Sql select statement inner join

Posted on 2016-07-27
4
38 Views
Last Modified: 2016-07-27
I want to return all value that match the following criteria.

Return  all value from column A that are identical   where we have one row in Column B value is  'Export B' and Column C = 0.0  also a another from that a value from Column B is Export  and Column C is 10.00

The result should be 21282

Column A--- Column B ---Column C
21282       ---      Export  --- 10.0
21282       ---      Export b  --- 0.0
21281       ---      Export  --- 10.0
0
Comment
Question by:yguyon28
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 41732051
there will be several syntaxes to solve this, and depending on the real final requested output adifferent basic solution can be chosen.
select t.fieldA
 from yourtable t
 where t.fieldB = 'export'
   and t.fieldC = 0
 and exists( select null
   from yourtable o
   where o.fieldA = t.fieldA
     and o.fieldB = 'export b'
      and o.fieldC = 10
)

i may have mistyped some values, I presume you got the point of my suggestion
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41732108
I think you can achieve it using group by and having clauses like this:
Select ColumnA
From table1
Where (ColumnB='Export' And ColumnC = 10.0)
OR
(ColumnB='Export b' and ColumnC =0.0)
Group by ColumnA
HAVING COUNT(*) = 2

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 41732110
Not sure if I understand your problem correctly. Is this what you are looking for?
SELECT *
  FROM your_table
 WHERE (ColumnB = 'Export B' AND ColumnC = 0)
    OR (ColumnB = 'Export' AND ColumnC = 10)

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 41732118
Well. Based on Paul's reply, If there are ColumnA values duplicated for first or second condition.
SELECT ColumnA
  FROM your_table
 WHERE (ColumnB = 'Export B' AND ColumnC = 0)
    OR (ColumnB = 'Export' AND ColumnC = 10)
 GROUP BY ColumnA
HAVING COUNT(DISTINCT CASE WHEN ColumnB = 'Export B' AND ColumnC = 0 THEN 1 WHEN ColumnB = 'Export' AND ColumnC = 10 THEN 2 END) = 2

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

13 Experts available now in Live!

Get 1:1 Help Now