473,804 Members | 3,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Query To combine results from two Columns

57 New Member
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 3979
ck9663
2,878 Recognized Expert Specialist
Looks like a CTE would be your option

--- CK
Apr 8 '09 #2
vineetbindal
57 New Member
can you give me a little example or something please ?
Apr 9 '09 #3
Uncle Dickie
67 New Member
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
vineetbindal
57 New Member
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 Recognized Expert Specialist
Search SQL Server Common Table Expression in google.

--- CK
Apr 9 '09 #6
vineetbindal
57 New Member
yeah i did that. tnx a lot, that was very useful
Apr 10 '09 #7
kunalsmehta
6 New Member
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
vineetbindal
57 New Member
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
3140
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 SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
3
3481
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 team_id is home_team_id or away_team_id. I'm doing this in two separate select statements now. Example: SELECT count(home_team_id),home_team_id FROM games WHERE league_id = 218 and ((home_score IS NOT NULL OR away_score IS NOT NULL) OR...
9
4361
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 predecessor, I hasten to add) so that each day it creates a copy of the record for each company, changes the date to today's date, and prompts the user for any changes of ratings on that day. The resulting data table grows by approx 600 records per...
5
1925
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
4852
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 SalesManName AT Alan Time
8
4916
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. I have a database with large amounts of historical data for which many queries have been built. If I used Access reports for output, I'd have to build many reports that I don't want to do. For this and other reasons, my approach to inquiries...
9
3949
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 - I have read every post out there and spent hours trying to figure out the problem with no success whatsoever - I have constrained the problem to one form however, and I think it's hiding somewhere in my code associated with this form, which is...
0
4130
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 'zContractDefault'. For some reason it is only returning the first 11 chars of the column name? Any help would be greatly appreciated... This query searches a DB and determines which columns are 'Invalid' if the column name is >30 and...
1
3167
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 combined. **Max Date/Time Query-Finds the most recent updated record based on the time and date (Correct Results Returned) SELECT .panelNumber, Max(.locDate) AS MaxOflocDate, Max(.completionTime) AS MaxOfcompletionTime, .jobNumber, .panelID FROM , ...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.