Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 89
  • Last Modified:

SQL Get distinct list from any query

Hi

I want to get a distinct list from any query such as the one below

Select * From Customers Where [Company] >= 'Company A' And [Company] <= 'Company G'

I tried the following but get the error Incorrect syntax near ')'.

Select Distinct Company From (Select * From Customers Where [Company] >= 'Company A' And [Company] <= 'Company G')
0
Murray Brown
Asked:
Murray Brown
2 Solutions
 
FarWestCommented:
Select distinct works on result set row level so it will get only one row of any repeated row
Specifying column name only works whith aggregation function like Count
0
 
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
Try this:

Select Distinct t.Company From (Select * From Customers Where [Company] >= 'Company A' And [Company] <= 'Company G') AS t

(I don't have a SQL Server instance readily accessible - I'll share a working example as soon as I have one).

Bascially, SQL Server is looking for a table alias that is can use for the outer query.
0
 
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
Here's an example:
USE tempdb;
GO
DECLARE @Customers TABLE (CustomerId INT IDENTITY(1,1) NOT NULL,
                          CustomerName NVARCHAR(50) NOT NULL,
                          Company NVARCHAR(50) NOT NULL
                        );

INSERT INTO @Customers (CustomerName, Company)
VALUES ('Customer1',1),
       ('Customer2',1),
       ('Customer3',1),
       ('Customer4',2),
       ('Customer5',3),
       ('Customer6',4),
       ('Customer7',3)

Select DISTINCT t.Company 
FROM (SELECT * 
      FROM @Customers 
      WHERE [Company] >= 1 
        AND [Company] <= 3
     ) AS t;
GO

Open in new window


NOTE:
Company Names are alphanumeric. So, the greater than/less than operators will not work if you only want companies whose names are Company A, B, C, D, E, F and G. To do this, you will have to either use an IN clause or use Id values (like what I have done in my quick example)
Also, you don't really need a sub-query. With the same example code as above, the following query will also return the same results

SELECT DISTINCT c.Company
FROM @Customers AS c
WHERE c.Company BETWEEN 1 AND 3;
GO

Open in new window

0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
PortletPaulCommented:
You can use >= or <= with strings (or between can be used, it is the same). The following are functionally the same:

SELECT DISTINCT
       Company 
FROM Customers
WHERE [Company] >= 'Company A' And [Company] <= 'Company G'
;

SELECT DISTINCT
       Company 
FROM Customers
WHERE [Company] BETWEEN 'Company A' AND 'Company G'
;

Open in new window


for some sample data:
CREATE TABLE Customers
	([Company] varchar(60))
;
	
INSERT INTO Customers
	([Company])
VALUES
	('Company A'),	('Company B'),	('Company C'),
	('Company D'),	('Company E'),	('Company F'),	('Company G'),

	('Company H'),	('Company I'),	('Company Z')
;

Open in new window


The results of both queries are:
|   COMPANY |
|-----------|
| Company A |
| Company B |
| Company C |
| Company D |
| Company E |
| Company F |
| Company G |

Open in new window


But note that a [Company] of 'Aardvark Inc' is not be BETWEEN 'Company A' AND 'Company G'
(i.e. the use of >= and <= on strings may not meet your expectations)
0
 
PortletPaulCommented:
>>"I want to get a distinct list from any query ..."

Please be careful with the notion that any query is suited to use of "SELECT DISTINCT"
"SELECT DISTINCT" is the enemy of performance, it should be used sparingly.
IF used, do it with as few columns as possible.

see "Select Distinct is returning duplicates" in particular the “Hail Mary Distinct”
There are 2 references at the end of that article I recommend too.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now