Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to Write Query for This Expand / Collapse
Author
Message
Posted Thursday, September 19, 2013 3:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, February 14, 2014 7:24 PM
Points: 25, Visits: 40
i want to write a query to get data of two different columns with two different conditions from a single table.
How
Can anybody please Help me Out.
Post #1496284
Posted Thursday, September 19, 2013 4:28 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, April 2, 2014 8:05 AM
Points: 285, Visits: 334
Hi
I'm not sure what you are looking for, but try this:

SELECT        t1.Col1,t2.Col2 
FROM SomeTable As t1 CROSS JOIN
SomeTable AS t2
WHERE (t1.Colx = 'Your_condition') AND (t2.Coly = 'Your_next_condition')



Br.
Mike
Post #1496296
Posted Thursday, September 19, 2013 11:47 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:33 AM
Points: 11,221, Visits: 12,981
If you were to share the DDL for the table (or an example) and the conditions you want it would it easier to help you out.

Michal has shared one possible solution for one interpretation of your question. I'd do something like this:

Select col1, col2 from table1 where col3 = 1 or col4 = 1;

I actually interpreted your question to mean that for one condition you wanted to return one column and for another condition a different column like this:

Select CASE WHEN col3 = 1 then col1 ELSE col2 END as returnedData FROM table 1;





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1496526
Posted Monday, September 23, 2013 2:56 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 4:14 AM
Points: 3,618, Visits: 5,254
Something like this perhaps?

WITH SampleData ([Key], Col1, Col2) AS (
SELECT 1, 'ABC', 'DEF'
UNION ALL SELECT 2, 'GFI', 'XYZ')
SELECT Col1=MAX(CASE WHEN [Key]=Key1 THEN a.Col1 END)
,Col2=MAX(CASE WHEN [Key]=Key2 THEN a.Col2 END )
FROM SampleData a
CROSS APPLY (SELECT 1, 2) b([Key1], [Key2])
WHERE [Key] IN ([Key1], [Key2]);





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1497297
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse