Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on 

SQL Query - Distinct with Concatenated Columns

Good Day Experts!

I am in need of assistance with a SQL query for use in my VB.net application.  I am getting first name and last name from seperate columns in a table. Then I had to concatenate the two seperated by a comma to show Last, First in my combo box.  

The trouble lies in the SQL table...there are at times 2 records with the same first and last name as a result of DataEntry duplication.  So, I am seeing 2 rows of Smith, Bob in my combobox.  

How can I get only one to be returned back in my query results? I tried looking at Distinct but could not get it to work.  

Here is my query that works but shows duplicates:

Select lname + ',' + fname as name from Login where corporation like '%Contech%'
                                        order by name asc

Thanks,
jimbo99999
Microsoft SQL Server

Avatar of undefined
Last Comment
Jimbo99999
ASKER CERTIFIED SOLUTION
Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Pra4444
Pra4444
Flag of United States of America image

have you tried this

Select distinct(lname + ',' + fname) as name from Login where corporation like '%Contech%'  order by  distinct(lname + ',' + fname)
Avatar of Jimbo99999
Jimbo99999
Flag of United States of America image

ASKER

It has an issue with the order by 1,2 but I was able to get the following version to work.  Surprisingly it was ordered properly.

SELECT     lname + ',' + fname AS name
FROM         (SELECT DISTINCT lname, fname
                       FROM          Login
                       WHERE      corporation LIKE '%Contech%') a

Thanks,
jimbo99999
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo