join results as one column

how do i return a result set back as one column?

select Distinct((LEFT(RIGHT(GLTran.Sub, 20), 3))) AS SubAcct , Account.Descr
from GLTran
INNER JOIN Account ON GLTran.Acct = Account.Acct
where GLTran.Acct >= '40000' AND GLTran.Acct < '50000' order by SubAcct asc

SubAccount     Descr
000                    (Gain)/Loss Extin of Debt


want 1 col instead of 2
000 - Gain/Loss Extin of Debt
fwstealerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
CAST the account number (assuming it's numeric) as a varchar, then you can concatenate the varchar number and description into one column

select CAST(Distinct((LEFT(RIGHT(GLTran.Sub, 20), 3))) AS varchar(max)) + ' - ' + Account.Descr as account
from GLTran
INNER JOIN Account ON GLTran.Acct = Account.Acct
where GLTran.Acct >= '40000' AND GLTran.Acct < '50000' order by SubAcct asc
0
fwstealerAuthor Commented:
got an error:

Incorrect syntax near the keyword 'as'.

CAST(Distinct((LEFT(RIGHT(GLTran.Sub, 20), 3))) as varchar(max))
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
If this value is always three characters than replace varchar(max) with char(3).

This may also work..

select Distinct(CAST(LEFT(RIGHT(GLTran.Sub, 20), 3) AS varchar(max))) + ' - ' + Account.Descr as account
from GLTran
INNER JOIN Account ON GLTran.Acct = Account.Acct
where GLTran.Acct >= '40000' AND GLTran.Acct < '50000' order by SubAcct asc

Since I don't have access to your data source there's no way I can validate if Distinct((LEFT(RIGHT(GLTran.Sub, 20), 3))) is correct, so you're on your own on that one.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

fwstealerAuthor Commented:
minor change:

select Distinct(CAST(LEFT(RIGHT(GLTran.Sub, 20), 3) AS varchar(max))) + ' - ' + Account.Descr as account
from GLTran
INNER JOIN Account ON GLTran.Acct = Account.Acct
where GLTran.Acct >= '40000' AND GLTran.Acct < '50000' order by GLTran.Sub asc

error:
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>ORDER BY items must appear in the select list if SELECT DISTINCT is specified

change the ORDER BY to ORDER BY 1 and see if that works.

If not, you'll have to replicate the whole SELECT formula like this (maybe this, or remove the DISTINCT, not sure on that one)

select Distinct(CAST(LEFT(RIGHT(GLTran.Sub, 20), 3) AS varchar(max))) + ' - ' + Account.Descr as account
from GLTran
INNER JOIN Account ON GLTran.Acct = Account.Acct
where GLTran.Acct >= '40000' AND GLTran.Acct < '50000'
order by Distinct(CAST(LEFT(RIGHT(GLTran.Sub, 20), 3) AS varchar(max))) + ' - ' + Account.Descr
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fwstealerAuthor Commented:
order by 1 worked!
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim

>order by 1 worked!
If you're SELECTing a lot of calculations, you can ORDER BY the position of the column (1, 2, 3, ...) instead of having to re-calculate.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.