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 |
|
Azaera
Starting Member
5 Posts |
Posted - 2009-05-21 : 18:44:33
|
| Hiya.I'm trying to get a nice concat and union to work togheter, but gets a little bit of trouble, so a bit of help would be apprecitated.I have to get the firstname and lastname togheter with a count into one single column and then union them with different movies.Banged my head now, and since it is school, I would prefer the kicks right rather then full code.The best I've been able to do is:SELECT First_Name + ' ' + Last_Name + ' har hyrt ' ,(SELECT COUNT(CAST(person_id AS Varchar(5))) FROM Lending where person_id=2), 'st film(er)' FROM person WHERE id=2;SELECT Lending.Lending_TimePoint, Movie.TitleFROM Lending INNER JOIN MovieON Lending.Item_id = Movie.IdWHERE Person_id=2ORDER BY Lending.Lending_TimePoint;But this gives me three columns in the first code bit but dislikes the union tpo the second since I don't have equal numbers of expressions. |
|
|
kfluffie
Posting Yak Master
103 Posts |
Posted - 2009-05-22 : 01:21:38
|
Hi!Good to see another Swede here!Okay, first of all, what do you really want to do? What is the expected output?SELECT First_Name + ' ' + Last_Name + ' har hyrt ' ,(SELECT COUNT(CAST(person_id AS Varchar(5))) FROM Lending where person_id=2), 'st film(er)' FROM person WHERE id=2; I suppose the output is something like:Column 1: "First and Last name har hyrt:"Column 2: 5Column 3: "st film(er)"Everything is not concatenated, they are seperated by a comma. Try this one:SELECT First_Name + ' ' + Last_Name + ' har hyrt ' + (SELECT COUNT(CAST(person_id AS Varchar(5))) FROM Lending where person_id=2)+ 'st film(er)' FROM person WHERE id=2; Also, you mention UNION. Then you need to have equal number of columns from each query and the same datatype. Read more on that topic here:http://msdn.microsoft.com/en-us/library/ms180026.aspxBest Regards,KFluffie |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-22 : 02:06:41
|
quote: Originally posted by Azaera Hiya.I'm trying to get a nice concat and union to work togheter, but gets a little bit of trouble, so a bit of help would be apprecitated.I have to get the firstname and lastname togheter with a count into one single column and then union them with different movies.Banged my head now, and since it is school, I would prefer the kicks right rather then full code.The best I've been able to do is:SELECT First_Name + ' ' + Last_Name + ' har hyrt ' ,(SELECT COUNT(CAST(person_id AS Varchar(5))) FROM Lending where person_id=2), 'st film(er)' FROM person WHERE id=2;SELECT Lending.Lending_TimePoint, Movie.TitleFROM Lending INNER JOIN MovieON Lending.Item_id = Movie.IdWHERE Person_id=2ORDER BY Lending.Lending_TimePoint;But this gives me three columns in the first code bit but dislikes the union tpo the second since I don't have equal numbers of expressions.
your both queries doesnt seem to return same type of fields. one of filed in first query is person while one in second is lending point. can i ask whats the purpose of combining these using union? also you need to have equal columns on both sides of union so if you dont have enough columns use NULL as gap filler for extra columns in other query |
 |
|
|
Azaera
Starting Member
5 Posts |
Posted - 2009-05-22 : 03:16:04
|
| The output is from three tables do I need to get the lenders name (From Person) and a count on all movies he has hired (from lending).Then I need to combine that with the Lending Timepoint from the table called Movie.The output I need would be like:Fullname +has rented+ COUNT + movies.Date MovieName SELECT First_Name + ' ' + Last_Name + ' har hyrt ' + (SELECT COUNT(CAST(person_id AS Varchar(5))) FROM Lending where person_id=2)+ 'st film(er)' FROM person WHERE id=2;gave me the following error: Conversion failed when converting the varchar value 'Peter Andersson har hyrt ' to data type int. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-22 : 03:19:36
|
| [code]SELECT First_Name + ' ' + Last_Name + ' har hyrt ' + CAST((SELECT COUNT(CAST(person_id AS Varchar(5))) FROM Lending where person_id=2) AS varchar(10))+ 'st film(er)' FROM person WHERE id=2;[/code] |
 |
|
|
Azaera
Starting Member
5 Posts |
Posted - 2009-05-22 : 03:46:56
|
| Thank you Visakh. That code worked perfect :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-22 : 03:49:51
|
| welcome |
 |
|
|
|
|
|
|
|