Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How I can join two records of the same table?

Author  Topic 

pedro_cr
Starting Member

2 Posts

Posted - 2009-09-30 : 17:22:31
Hi,

Thanks in advance for the help.

I need to create in a single SELECT a query that return me two values of the same table. I mean, I have for example the next table:

OPD_ID
CATEGORY_TYPE
PRICE

Now I have the next records

OPD_ID,CATEGORY_TYPE,PRICE
1,FC,100
1,FS,150

And I need to have the following result after the query

OPD_ID,COST PRICE,SELL PRICE
1,100,150

Please help me, I need to do this urgently in my work

Thanks


jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2009-09-30 : 17:50:26
If CATEGORY_TYPE defines whether or not the row is the cost price (FC) or sell price (FS), then you can reference the table twice using a self-join. For example:

SELECT a.OPD_ID, a.Price AS [Cost Price], b.Price AS [Sell Price]
FROM table a
INNER JOIN table b ON b.OPD_ID = a.OPD_ID AND b.CATEGORY_TYPE = 'FS'
WHERE a.CATEGORY_TYPE = 'FC';

Go to Top of Page

pedro_cr
Starting Member

2 Posts

Posted - 2009-09-30 : 18:47:56
Thank for the answer, but I have few questions:

What I use to replace table b??

I'm not sure if i don't explain clearly, both of the records are in the same table, so I can't use the same name.

I read in some places about to create an alias to the "2nd" table, but how I can create an alias in the same SELECT?

Pedro
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 01:33:12
Seems like what you need is


SELECT OPD_ID,
MAX(CASE WHEN CATEGORY_TYPE='FC' THEN PRICE ELSE NULL END) AS COST_PRICE,
MAX(CASE WHEN CATEGORY_TYPE='FS' THEN PRICE ELSE NULL END) AS SELL_PRICE
FROM tablename
GROUP BY OPD_ID

Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2009-10-01 : 14:49:52
In the query I gave you - replace 'table' with the name of your actual table. I have used the alias 'a' for the first table and 'b' for the second instance of the same table. To make it clearer - if your table name is 'ThisIsMyTable' then the query would be:

SELECT a.OPD_ID, a.Price AS [Cost Price], b.Price AS [Sell Price]
FROM ThisIsMyTable a
INNER JOIN ThisIsMyTable b ON b.OPD_ID = a.OPD_ID AND b.CATEGORY_TYPE = 'FS'
WHERE a.CATEGORY_TYPE = 'FC';

You could also use the AS clause to alias (I usually don't include it). That would be:

SELECT a.OPD_ID, a.Price AS [Cost Price], b.Price AS [Sell Price]
FROM ThisIsMyTable AS a
INNER JOIN ThisIsMyTable AS b ON b.OPD_ID = a.OPD_ID AND b.CATEGORY_TYPE = 'FS'
WHERE a.CATEGORY_TYPE = 'FC';
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 15:01:41
quote:
Originally posted by jeffw8713

In the query I gave you - replace 'table' with the name of your actual table. I have used the alias 'a' for the first table and 'b' for the second instance of the same table. To make it clearer - if your table name is 'ThisIsMyTable' then the query would be:

SELECT a.OPD_ID, a.Price AS [Cost Price], b.Price AS [Sell Price]
FROM ThisIsMyTable a
INNER JOIN ThisIsMyTable b ON b.OPD_ID = a.OPD_ID AND b.CATEGORY_TYPE = 'FS'
WHERE a.CATEGORY_TYPE = 'FC';

You could also use the AS clause to alias (I usually don't include it). That would be:

SELECT a.OPD_ID, a.Price AS [Cost Price], b.Price AS [Sell Price]
FROM ThisIsMyTable AS a
INNER JOIN ThisIsMyTable AS b ON b.OPD_ID = a.OPD_ID AND b.CATEGORY_TYPE = 'FS'
WHERE a.CATEGORY_TYPE = 'FC';



isnt it a matter of just cross tabbing? whats the need of join here?
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2009-10-01 : 15:23:31
visakh16

A row with a category_type of 'FS' defines a sell price, a row with a category_type of 'FC' defines a cost price. You can either join to the table for each type, or build a group by cross tab and a SUM(CASE ...) expressions. I prefer joining since it makes more sense to me - but, others will prefer using a cross tab. If there is a requirement to actually SUM the values and you have multiple rows with a category_type = 'FC' or 'FS' then the cross tab would be better.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 15:24:51
quote:
Originally posted by jeffw8713

visakh16

A row with a category_type of 'FS' defines a sell price, a row with a category_type of 'FC' defines a cost price. You can either join to the table for each type, or build a group by cross tab and a SUM(CASE ...) expressions. I prefer joining since it makes more sense to me - but, others will prefer using a cross tab. If there is a requirement to actually SUM the values and you have multiple rows with a category_type = 'FC' or 'FS' then the cross tab would be better.


ok...thanks for clarification
Go to Top of Page
   

- Advertisement -