473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Help with Query To combine results from two Columns

Hi All.

I have two Columns column1 and column2. i have to run a query with some value from colum1 depending on it will select result from coloumn2 and if that result is present in coloumn 1 it will again select something from coloum2 again if that new result is present in coloum 1 it will select something from colum2 . i have to combine all this in one column without repeating values.

something like.

Column1 Column2 Column3

2100 2200 V
2200 2307 V
2200 2308 M
2307 2400 V

now if i run my query where column3='V'

then the result should be

2100
2200
2307
2400

values from both the columns in one column(i may skip the first value 2100) becouse i allready know it

please help
Apr 8 '09 #1
8 3952
ck9663
2,878 Expert 2GB
Looks like a CTE would be your option

--- CK
Apr 8 '09 #2
can you give me a little example or something please ?
Apr 9 '09 #3
I'm not really sure what you are after but you can get the result set you want in your example with a UNION statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT    Column1 FROM table WHERE Column3 = 'V'
  2. UNION
  3. SELECT    Column2 FROM table WHERE Column3 = 'V'
Apr 9 '09 #4
no that wont help me.

Actually we have a number of starting point followed by a chain in those columns for ex/;
2100 2200 V
2105 2200 V
2106 2200 V
2100 2205 M
2200 2307 V
2200 2308 M
2106 2800 M
2307 2400 V
2200 2100 M
2200 2106 M

the union will give me 2100,2105,2106,2307,2200,2400

but i wanna get 2100,2200,2307,2400
for ex: my query starts with where cl1=2100 ans cl3=V

then it will go to cl1 select 2100 and check from cl2 and select 2200 and becouse 2200 is also in cl1 and for that cl3='V' it will select 2307 and then 2400 so result will be 2100,2200, 2307,2400

please help..
Apr 9 '09 #5
ck9663
2,878 Expert 2GB
Search SQL Server Common Table Expression in google.

--- CK
Apr 9 '09 #6
yeah i did that. tnx a lot, that was very useful
Apr 10 '09 #7
Hi, try this

declare @table table (col1 int, col2 int ,col3 varchar)
insert into @table values (2100, 2200, 'V')
insert into @table values (2200, 2307, 'V')
insert into @table values (2200, 2308, 'm')
insert into @table values (2307, 2400, 'V')
insert into @table values (2400, 2500, 'V')
insert into @table values (2600, 2700, 'V')
insert into @table values (2700, 2800, 'V')
select col1 from @table where col3='v'
union
select col2 from @table where col2 in (select col1 from @table where col3='v')

Kunal
Apr 13 '09 #8
Tnx a lot kunal , its work. i tried the similer approach, but with a join as they have given in CTE in msdn.
Apr 13 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Glenn Carr | last post by:
I'm trying to count the number of records in 'game_dates' where the columns home_team_id or away_team_id have the same value. E.g., i want to know the number of records for each team_id where...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
5
by: Alicia | last post by:
Hello everyone based on the data, I created a union query which produces this. SELECT ,,, 0 As ClosedCount FROM UNION SELECT ,, 0 AS OpenedCount, FROM ORDER BY , ;
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
8
by: GeorgeSmiley | last post by:
Does anyone know of a way, via VBA, to set the screen position of query results to a particular top, left position? I've glanced at API techniques but cannot find exactly what will do the trick....
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
0
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
1
by: csolomon | last post by:
Hello: I am using two queries to get one result set. The issue is, I return no data when I combine them into one query. I have listed both queries, as well as the 3rd query that shows them...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.