September 19, 2013 at 3:58 am
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.
September 19, 2013 at 4:28 am
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
September 19, 2013 at 11:47 am
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
Consultant - Straight Path Solutions
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
September 23, 2013 at 2:56 am
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply