| 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_IDCATEGORY_TYPEPRICENow I have the next recordsOPD_ID,CATEGORY_TYPE,PRICE1,FC,1001,FS,150And I need to have the following result after the queryOPD_ID,COST PRICE,SELL PRICE1,100,150Please help me, I need to do this urgently in my workThanks |
|
|
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 aINNER JOIN table b ON b.OPD_ID = a.OPD_ID AND b.CATEGORY_TYPE = 'FS'WHERE a.CATEGORY_TYPE = 'FC'; |
 |
|
|
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 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-01 : 01:33:12
|
Seems like what you need isSELECT 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_PRICEFROM tablenameGROUP BY OPD_ID |
 |
|
|
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 aINNER 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 aINNER JOIN ThisIsMyTable AS b ON b.OPD_ID = a.OPD_ID AND b.CATEGORY_TYPE = 'FS'WHERE a.CATEGORY_TYPE = 'FC'; |
 |
|
|
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 aINNER 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 aINNER 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? |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
|