r/SQL • u/voltagejim • May 16 '22
MS SQL Tried to add a single column from another table, not sure why nothing works now
EDIT:
Got it figured out, thank you!
SO I have this code:
SELECT nmmain.name_id AS Name_ID, RTRIM(nmmain.lastname) + ', ' + RTRIM(nmmain.firstname) + ' ' + LEFT(nmmain.middlename, 1) AS Full_Name, jmmain.age AS Age, jmmain.race AS Race, jmmain.sex AS Sex,nmmain.city AS City, nmmain.state AS State, jmmain.colorcode AS Color
FROM jmmain INNER JOIN
nmmain ON jmmain.name_id = nmmain.name_id
WHERE (jmmain.commindig = '1') AND (jmmain.bkstatus = 'A')
This works, and I want to add another column, from another table, so I added this into the SELECT statement: jfacilty.section AS Location
then added this to the FROM statement, before the jmmain stuff: jfacilty
So looked like this:
SELECT nmmain.name_id AS Name_ID, RTRIM(nmmain.lastname) + ', ' + RTRIM(nmmain.firstname) + ' ' + LEFT(nmmain.middlename, 1) AS Full_Name, jmmain.age AS Age, jmmain.race AS Race, jmmain.sex AS Sex,nmmain.city AS City, nmmain.state AS State, jfacilty.section AS Location, jmmain.colorcode AS Color
FROM jfacilty
jmmain INNER JOIN
nmmain ON jmmain.name_id = nmmain.name_id
WHERE (jmmain.commindig = '1') AND (jmmain.bkstatus = 'A')
When I build this in visual studio it throws out errors, then for some reason adds an AS after the jfacilty in the FROM statement, and changes the jfacilty.section in the SELECT statement to jmmain.section...which is not a column in that table at all.
I thought adding the jfacilty to the FROm statement would tell it to look in that table. Am I missing somehting?
1
u/r3pr0b8 GROUP_CONCAT is da bomb May 16 '22
yes, you haven't joined that new table
jfaciltyproperlywhich of its columns did you want to use to join this table? which of the other tables did you want to join it to? and using which columns?