Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference Column to Row

Status
Not open for further replies.

bugzLue

IS-IT--Management
Feb 27, 2001
57
US
Can anyone help me with code for SQL or Access?
I need to be able to reference one column to the primary column and output the row from the primary column.

so if po_id is 2 then it will go over to ID column look for 2 and row 2 prints out.




ID(PK) Name Address CSZ PO_ID PE_ID
1 Bla bla 123 bla Any 999999 1 4
2 Ha HA 234 ha Mine 88888 1 3
3 ME me 345 City NY 11111 2 1
4 See 456 SD Ca 99211 3 2


Thank if you can help Live to learn or die trying
 
declare @VariablePassedIn int -- i am assuming

select b.name, b.address, b.csz
from TableName as a, SameTableName as b
where a.PO_ID = @VariablePassedIn
and b.ID = a.PO_ID

Print ....

remember, TableName and SameTableName should be the same table ie. Employee

This may work! Thanks

J. Kusch
 
Thanks for your help
But this pulls no Records. What I am trying to do is the number in PO_ID is linked to the same number in ID.
example:
ID Name PO_ID PE_ID
1 your mothers name
2 Your Fathers name
3 You 2 1
4 You Sibling 2 1

Instead of entering the name of your parents the PO_ID and PE_ID referances them back to the ID.

Thank you Live to learn or die trying
 
You may want to give the following a try(I have not tried it).
Assuming to determine the details of po_id for a specific @ID passed as a parameter.

Select a.name as your_name
, a.po_id as your_po_id
, (select b.name as your_po_name
from tableName b
where b.id = (select po_id
from tableName c
where c.id = @ID
)
)
from tableName a
where a.id = @ID



If @ID= 3 then the result would look like
your_name = "You"
your_po_id = "2"
your_po_Name = "Your Fathers Name"

Thanks.
 
Also, another approach could be

select a.name as your_name
, a.po_id as your_po_id
, b.name as your_fathers_name
from
tablename a left outer join tablename b
on a.po_id = b.id


This also may work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top