| Author |
Topic |
|
fredong
Yak Posting Veteran
80 Posts |
Posted - 2004-01-05 : 17:17:49
|
| I am trying to select the just 1 row on the primary key table and multiple row on the joined tables which has the primary keys that has become the foriegn keys. Please help. Thanks. Below is my exampleA table-----------pokey podate trantype Amt123 1/01/04 101 10000.00B table----------polinekey itemkey Amt pokey001 ABC123 2500 123002 DEF456 2500 123003 GHI789 5000 123so A table is joined with B table by the pokey so if you do equal it will give with 3 rows of pokeys,podate,trantype, and Amt from A table which I am trying to remove the duplicate from A. I need to do a select statement with a result format look like below because this is eventually going to be a text file: (column headings can be disregarded).Thanks123 1/01/04 101 1000.00001 ABC123 2500 123002 DEF456 2500 123003 GHI789 5000 123 k |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-01-05 : 17:26:11
|
| You are trying to put two result sets together that don't match up with data types. You can't do that without converting them to VARCHAR or something like that. You can use a UNION if the data types are the same for the columns in each table.SELECT a.pokey, podate, trantype, polinekey, itemkey, b.amtFROM TableA aINNER JOIN TableB b ON a.pokey = b.pokeyWHERE a.pokey = 123You might consider Reporting Services for SQL Server 2000 to do this for you since it probably should be done on the presentation side. Or some other reporting tool like Crystal Reports.Tara |
 |
|
|
fredong
Yak Posting Veteran
80 Posts |
Posted - 2004-01-05 : 17:34:49
|
| well they are not the same columns in both tables because the first table is the primary tables and the second table is a secondary table (normalized table). Is it possible to insert into a temp table and get the result format I wanted? Do you have any ideas?Thanks.k |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-01-05 : 17:40:49
|
| I realize why they don't have the same column names. I was saying they don't have the same data types. So you have to explicitly CONVERT them in order to combine them into one result set.You can combine them with a UNION as long as you CONVERT the columns to the same data type.SELECT TOP 1 CONVERT(VARCHAR(50), pokey), CONVERT(VARCHAR(50), podate), CONVERT(VARCHAR(50), trantype), CONVERT(VARCHAR(50), amt)FROM TableAWHERE pokey = 123UNIONSELECT CONVERT(VARCHAR(50), polinekey), CONVERT(VARCHAR(50), itemkey), CONVERT(VARCHAR(50), amt), CONVERT(VARCHAR(50), pokey)FROM TableBWHERE pokey = 123Tara |
 |
|
|
fredong
Yak Posting Veteran
80 Posts |
Posted - 2004-01-05 : 18:19:31
|
| this is the result that I got which is good but I need the last row(from the primary table) to become the first row. any ideas.Thanks. 001 4557 2500.00 123002 5083 2500.00 123003 4738 5000.00 123123 Nov 19 2002 3:32PM 1101 10000.000k |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-01-05 : 18:56:03
|
| To order result sets, use ORDER BY.Tara |
 |
|
|
fredong
Yak Posting Veteran
80 Posts |
Posted - 2004-01-06 : 11:25:42
|
| I meant how I can get the resutlt to look like this123 Nov 19 2002 3:32PM 1101 10000.000001 4557 2500.00 123002 5083 2500.00 123003 4738 5000.00 123instead of this001 4557 2500.00 123002 5083 2500.00 123003 4738 5000.00 123123 Nov 19 2002 3:32PM 1101 10000.000I don't understand how would you use order by in the Union operator. Please advise.Thanks.k |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-01-06 : 12:06:21
|
| When you have a UNION, you might need to use the ordinal positions of the columns since the column names are different.For example,ORDER BY 1That says order by the first column of the SELECT statement, and it will do it for the entire result set and not just the first or second part. Your values could get messed up though since 123 might be smaller than your polinekey. If that is the case, then put the results of the individual selects in the temp table. Obviously do the primary table first. The temp table would have generic data types such as VARCHAR.Tara |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2004-01-06 : 14:46:31
|
| If you always want the results from one table to appear before the other then you can put a constant in both parts of the UNION to identify which table it came from and then order by that. So it would look something likeSELECT CONVERT......., 1 as SortOrder...UNION...SELECT CONVERT........, 2 as SortOrderORDER BY SortOrderor if there are multiple "sets" then maybeORDER BY 4, 5, 1to sort by pokey, table, keyfield--------------------------------------------------------------Find more words of wisdom at [url]http://weblogs.sqlteam.com/markc[/url] |
 |
|
|
|