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.
| Author |
Topic |
|
seany
Starting Member
15 Posts |
Posted - 2008-02-01 : 14:35:03
|
| I am trying to write a select statement that will return the values of two columns from two different tables in the same statement. I believe the join word needs to be used somewhere, but I am having difficulty with it.Table1 (table name)Column1 (column name)------abTable2 (table name)Column2 (column name)------cdresults should be:a,cb,dThanks in advance. |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-01 : 15:29:45
|
| you need a way to correlate 1 row from one table with 1 (or more) rows in the other table. With no correlation you end up with a "cross join" where all possible combinations are returned:a,ca,db,cb,dAre there any other columns in the tables that tell you that rows with a and c are correlated with each other? Of do you simply want to go by the "ORDER" of the rows?what should the results be for this scenario:Table1 (table name)Column1 (column name)------acbTable2 (table name)Column2 (column name)------312Be One with the OptimizerTG |
 |
|
|
seany
Starting Member
15 Posts |
Posted - 2008-02-01 : 15:48:44
|
| Hi TG,No primary key, no foreign key. I simply want to do something like:select Column1,Column2 from Table1,Table2 |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-01 : 16:04:19
|
| answer my quesiton above "what should the results be for this scenario:" and I'll tell you if it's possible.Be One with the OptimizerTG |
 |
|
|
seany
Starting Member
15 Posts |
Posted - 2008-02-01 : 17:38:18
|
| based on your tables, the result should be:a,3c,1b,2 |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-01 : 17:58:51
|
| That's what I though you would say. Then no, unless you have some other column that acts as a sequence you can't do it. A table with no clustered index is just an un-ordered "heap". So there is no way to garauntee the order of the rows.Be One with the OptimizerTG |
 |
|
|
seany
Starting Member
15 Posts |
Posted - 2008-02-01 : 19:28:57
|
| Hi TG,Ok, so I guess I will ask it in another way because I don't think I was clear enough in terms of what I am really after:Let's say the scenerio is as follows:Table1 has column “projects”Table1Projects------------ProjaProjb Now there is also a table called “Proja” that has theversion numbersProjaVersionNumb------------V1.9V1.8V1.7 There is also a table called “Projb” that has theversion numbers ProjbVersionNumb-----------V1.2V1.1V1.0Can I run a select to give me the following output (Ijust want the first 3 values in the table Proja or the TableProjb). I need to first retrieve the value fromTable1 and then run the select on the Tables “Proja” or“Projb”Output:Proja,V1.9,V1.8,V1.7Projb,V1.2,V1.1,V1.0 |
 |
|
|
seany
Starting Member
15 Posts |
Posted - 2008-02-01 : 19:30:01
|
| I am running this in csharp so is there a way to get it in a one liner? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-01 : 23:14:42
|
quote: Originally posted by seany Hi TG,Ok, so I guess I will ask it in another way because I don't think I was clear enough in terms of what I am really after:Let's say the scenerio is as follows:Table1 has column “projects”Table1Projects------------ProjaProjb Now there is also a table called “Proja” that has theversion numbersProjaVersionNumb------------V1.9V1.8V1.7 There is also a table called “Projb” that has theversion numbers ProjbVersionNumb-----------V1.2V1.1V1.0Can I run a select to give me the following output (Ijust want the first 3 values in the table Proja or the TableProjb). I need to first retrieve the value fromTable1 and then run the select on the Tables “Proja” or“Projb”Output:Proja,V1.9,V1.8,V1.7Projb,V1.2,V1.1,V1.0
If you are using SQL 2005 then you can get this as follows:-SELECT t.Projects + ',' CASE WHEN t.Projects ='Proja' THEN LEFT(pal.Projalist,LEN(pal.Projalist-1)) WHEN t.Projects ='Projb' THEN LEFT(pbl.Projblist,LEN(pbl.Projblist-1)) ENDFROM Table tCROSS APPLY (SELECT VersionNumb + ',' [AS text()] FROM Proja FOR XML PATH(''))pal(Projalist)CROSS APPLY (SELECT VersionNumb + ',' [AS text()] FROM Projb FOR XML PATH(''))pbl(Projblist) |
 |
|
|
seany
Starting Member
15 Posts |
Posted - 2008-02-02 : 01:34:01
|
| I can't be specific about Proja or Projb because I don't know that these are the values from "table1". I need to be able to read the values from "Table1" one at a time and then run a query on each one of the values which will correspond to a table, in this case proja and projb. Once I have these values, I need to run a query to get the top 3 for each value from "table1". I don't know if this makes sense or not. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-02 : 02:21:55
|
| Any specific reasons why you went for such a design? I would have thought of keeping two tables for thisProjects-------ProjID Project1 Proja2 Projb...and ProjectVersion---------------ProjVerID ProjID Version1 1 V1.92 1 V1.83 1 V1.7 4 2 V1.25 2 V1.16 2 V1.0...... |
 |
|
|
seany
Starting Member
15 Posts |
Posted - 2008-02-02 : 02:33:18
|
| I might be confusing everyone in the way i am asking the question. I have one table that contains a list of values which these values reference their own tables. So, I have to get the list of the values from that table and loop through each one of the values which represent a table and get the top 3 values in each one of those tables. In my example, there is the table called "table1" which consists of two rows (proja and projb) but the table can grow to have projc, projd, etc. So in total, with my example, I have 3 tables. I hope this makes sense so far. At this point, once I know what the values from table1 are, I will then need to iterate through. Does that make sense? There is no specific reason why I did it like that, but now, that is the way the tables are set and that is what I have to work with. I am hoping that there is a way to get the information I need extracted. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-02 : 02:54:10
|
quote: Originally posted by seany I might be confusing everyone in the way i am asking the question. I have one table that contains a list of values which these values reference their own tables. So, I have to get the list of the values from that table and loop through each one of the values which represent a table and get the top 3 values in each one of those tables. In my example, there is the table called "table1" which consists of two rows (proja and projb) but the table can grow to have projc, projd, etc. So in total, with my example, I have 3 tables. I hope this makes sense so far. At this point, once I know what the values from table1 are, I will then need to iterate through. Does that make sense? There is no specific reason why I did it like that, but now, that is the way the tables are set and that is what I have to work with. I am hoping that there is a way to get the information I need extracted.
The probelm is now you dont know how many projects are in main table and so how many tables you need to link to for retrieval of data. If you had gone for a generic table approach (containing all project version information along with concerned project id) you will always be taking join with a single table and this would have helped you to get version info easily by linking with projectid of main table. Thats why i feel you are complicating the scenario with such a design. The only solution i can think of in your case is to use dynamic sql and retrieve the details inside a loop as you are not aware of projects inside the main table. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-02 : 03:32:04
|
Alternatievly, you can try something like this if you canCREATE TABLE #ProjVersions(ID int IDENTITY(1,1)Project varchar(20),Version varchar(20))INSERT INTO #ProjVersions(Project,Version)SELECT 'Proja',Version from ProjaUNION ALLSELECT 'Projb',Version from Projb...........SELECT t1.Projects + ',' + LEFT(vl.VersionList,LEN(vl.VersionList)-1)FROM Table1 t1CROSS APPLY (SELECT TOP 3 Version + ',' AS [text()] FROM #ProjVersions WHERE Project=t1.Projects FOR XML PATH(''))vl(VersionList) |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-02 : 10:14:04
|
Now it is perfectly clear. You want to:1) Pull an indeterminate number of table names from a table 2) for each of those tables: select from the table (assuming it exists and contains a column called VersionNumb) and return the "first three" version numbers. (What deterimines the "first 3"? Remeber, it can't be based on order in the table. I added a versionDate column in the example below. Transpose those 3 rows into a single comma seperated value.And oh, by the way, all that with just one line of code so you can call it from your csharp app.That's a reasonable request...unfortunately it has nothing to do with original post:quote: I am trying to write a select statement that will return the values of two columns from two different tables in the same statement. I believe the join word needs to be used somewhere, but I am having difficulty with it.
Now I know why this was all so familiar. We went through the same run-around on this post: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=96226seany, I'm glad to help but you really need to learn how to ask a question so as not to waste everyones time.Please try to post your future questions following this format:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxuse tempdbset nocount on------------------------------------------------------------------set upt the example--You should have done this for us seany!!!create table Table1 (Projects varchar(5))insert table1 (projects)select 'Proja' unionselect 'Projb'create table Proja (VersionNumb varchar(10), versionDate datetime)insert proja (versionNumb, versionDate)select 'V1.9', getdate() union select 'V1.8', getdate()-1 unionselect 'V1.7', getdate()-2create table Projb (VersionNumb varchar(10), versionDate datetime)insert projb (versionNumb, versiondate)select 'V1.2', getdate() union select 'V1.1', getdate()-1 unionselect 'V1.0', getdate()-2----------------------------------------------------------------goif object_id('dbo.PrjectVersions_select') > 0 drop proc dbo.PrjectVersions_selectgocreate proc dbo.PrjectVersions_selectasset nocount oncreate table #results (rowid int identity(1,1) ,projects varchar(5) ,versionsCSV varchar(30))declare @csv varchar(30) ,@sql NVarchar(200) ,@sql2 NVarchar(200) ,@params nVarchar(200) ,@tbl varchar(50)set @sql = ' select @csv = coalesce(@csv + '', '' + versionNumb, versionNumb) from ( select top 3 versionNumb from [tbl] order by versionDate desc ) d'set @params = '@csv varchar(30) output'--Get all [projects] from Table1 than exists as a table --and contains a column: [verionNumb]insert #results (projects)select t.projectsfrom Table1 tjoin information_schema.columns c on c.table_name = t.projects and c.column_name = 'versionNumb'--loop through all eligible proj tablesdeclare @rowid intselect @rowid = min(rowid) from #resultswhile @rowid is not nullbegin select @tbl = projects from #results where rowid = @rowid set @sql2 = replace(@sql, '[tbl]', @tbl) exec sp_executesql @sql2, @params, @csv = @csv output update #results set versionsCSV = @csv from #results where rowid = @rowid select @rowid = min(rowid) ,@csv = null from #results where rowid > @rowidend--return resultsselect projects ,versionsCSV from #resultsgo--Here is your "one liner" to call from your app exec dbo.PrjectVersions_selectgodrop table Projadrop table Projbdrop table Table1OUTPUT:projects versionsCSV -------- ------------------------------ Proja V1.9, V1.8, V1.7Projb V1.2, V1.1, V1.0Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|