December 21, 2011 at 5:00 am
In my table I have names like"
sqlserver
sqlserver
sqlserver
server
server
sql
sql
sql
sql
abcde
and in my output I want all names and repeated names in only one time;
like:
sqlserver
server
sql
abcde
December 21, 2011 at 5:02 am
SELECT DISTINCT
December 21, 2011 at 5:09 am
I get this by the following query:
SELECT Column_Name FROM TableName GROUP BY Column_Name HAVING ( COUNT(Column_Name) >= 1 );
December 21, 2011 at 5:11 am
alishaik001 (12/21/2011)
I get this by the following query:SELECT Column_Name FROM TableName GROUP BY Column_Name HAVING ( COUNT(Column_Name) >= 1 );
Can you post the actual query you are using? The query above, as it stands, would eliminate dupes.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
December 21, 2011 at 5:13 am
alishaik001 (12/21/2011)
I get this by the following query:SELECT Column_Name FROM TableName GROUP BY Column_Name HAVING ( COUNT(Column_Name) >= 1 );
Inbetween SELECT and Column_Name add the keyword DISTINCT - That should eliminate duplicates from the output.
December 21, 2011 at 5:19 am
My actual query is:
here Name is the fieldName and Employee is the TableName;
SELECT Name FROM Employee GROUP BY Name HAVING ( COUNT(Name) >= 1 );
From this query I got the solution.
Is there any wrong here;
December 21, 2011 at 5:21 am
alishaik001 (12/21/2011)
My actual query is:here Name is the fieldName and Employee is the TableName;
SELECT Name FROM Employee GROUP BY Name HAVING ( COUNT(Name) >= 1 );
From this query I got the solution.
Is there any wrong here;
Try this
SELECT DISTINCT Name FROM Employee GROUP BY Name HAVING ( COUNT(Name) >= 1 );
December 21, 2011 at 5:23 am
alishaik001 (12/21/2011)
My actual query is:here Name is the fieldName and Employee is the TableName;
SELECT Name FROM Employee GROUP BY Name HAVING ( COUNT(Name) >= 1 );
From this query I got the solution.
Is there any wrong here;
Translation problem 🙂
Is this your solution or the query you use to obtain the list from your first post?
SELECT Column_Name
FROM (
SELECT Column_Name = 'sqlserver' UNION ALL
SELECT 'sqlserver' UNION ALL
SELECT 'sqlserver' UNION ALL
SELECT 'server' UNION ALL
SELECT 'server' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'abcde'
) TableName
GROUP BY Column_Name HAVING ( COUNT(Column_Name) >= 1 )
-- results:
-- abcde
-- server
-- sql
-- sqlserver
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
December 21, 2011 at 6:45 am
I'm puzzled about the inclusion of the HAVING ( COUNT(Column_Name) >= 1 ) bit. Every value that exists in Column_Name will have one or more occurrences, so this doesn't do anything except add a few extra operators to the query plan...you'll get the exact same result set if you leave the HAVING clause off altogether.
December 21, 2011 at 6:54 am
There is no significant difference in execution plans between GROUP BY and DISTINCT. Each produces a list of unique values, DISTINCT just doesn't require that aggregations be taken at the same time.
Jon is right, the HAVING clause is not needed in this instance.
Either of the following would produce a de-duped list of column names
SELECT DISTINCT Column_Name
FROM (
SELECT Column_Name = 'sqlserver' UNION ALL
SELECT 'sqlserver' UNION ALL
SELECT 'sqlserver' UNION ALL
SELECT 'server' UNION ALL
SELECT 'server' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'abcde'
) TableName
SELECT Column_Name,COUNT(*)
FROM (
SELECT Column_Name = 'sqlserver' UNION ALL
SELECT 'sqlserver' UNION ALL
SELECT 'sqlserver' UNION ALL
SELECT 'server' UNION ALL
SELECT 'server' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'sql' UNION ALL
SELECT 'abcde'
) TableName
GROUP BY Column_Name
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply