SQL Server - Select DISTINCT columns along with a NON-DISTINCT column

Asked By Techhead
07-Mar-07 11:04 AM
I am trying to select distinct columns and add a non-disticnt column
to my list.

This query works great in finding distinct records:


SELECT DISTINCT
firstname,
lastname,
middleinitial,
address1,
address2,
city,
state,
zip,
age,
gender,

FROM table
ORDER by zip

However, I have a column called DATE that I want to add to the results
of my query but I DO NOT want this to be a DISTINCT column. How do I
go by excluding a column from being DISTINCT in my select distinct
statement but adding it after the fact?
SQL Server
(1)
Distinct
(1)
Column
(1)
Table
(1)
Date
(1)
  Tom Moreau replied...
07-Mar-07 11:08 AM
Can you give us an example with data?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
.
I am trying to select distinct columns and add a non-disticnt column
to my list.

This query works great in finding distinct records:


SELECT DISTINCT
firstname,
lastname,
middleinitial,
address1,
address2,
city,
state,
zip,
age,
gender,

FROM table
ORDER by zip

However, I have a column called DATE that I want to add to the results
of my query but I DO NOT want this to be a DISTINCT column. How do I
go by excluding a column from being DISTINCT in my select distinct
statement but adding it after the fact?
  Aaron Bertrand [SQL Server MVP] replied...
07-Mar-07 11:11 AM
Which date do you want?  The earliest, the latest?

SELECT
firstname, lastname, etc.,
MIN(date_column),
MAX(date_column)
FROM
table
GROUP BY
firstname, lastname, etc.,
ORDER BY zip

--
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
  Techhead replied...
07-Mar-07 11:28 AM
I am not doing a WHERE function on my date column, I just want to add
this column to my DISTINCT results but do not want my date column to
be DISTINCT.
  Aaron Bertrand [SQL Server MVP] replied...
07-Mar-07 11:35 AM
Sorry, I do not do word problems very well.  Can you show some sample data
and desired results?  Just 10-20 rows with firstname and date should be
sufficient.

A
  Jeff replied...
13-Mar-07 08:37 AM
Bank, Account, and Note must be distinct however, I want the distinct row returned to contain the most recent plan date.

Bank	Account	Note 	Plan Date

1	123	1	5/6/2001

1	123	1	3/13/2007

1	123	1	3/3/2005

1	123	1	5/8/2006

2	555	5	8/4/2006

2	555	5	9/8/2007

3	999	2	5/8/2006

3	999	2	5/8/2001

4	888	1	3/2/2005

4	888	1	3/9/2006

4	888	1	3/9/2007
  Gilad replied...
23-Oct-07 06:46 AM
I tried to do the same thing myself and finally this is how I worked it out :



SELECT

rowCode <---- (Some unique numeral key)

firstname,lastname,address1 <---- distinct columns

plandate,plandate2 <---- non distinct columns



FROM table



WHERE rowCode IN (SELECT Min(rowCode) FROM table GROUP BY firstname,lastname,address1)



ORDER by firstname



This will return unique values for your distinct columns and will the first (min) value of your non distinct columns .

---------------------------------------------------



Actually in your case, because your 'non distinct' column has a 'date' type, you can solve it more easily by using the following code :



SELECT Bank, Account, Note, MAX(PlanDate)

FROM table

GROUP BY Bank, Account, Note
  Gilad replied...
23-Oct-07 06:47 AM
I tried to do the same thing myself and finally this is how I worked it out :



SELECT

rowCode <---- (Some unique numeral key)

firstname,lastname,address1 <---- distinct columns

plandate,plandate2 <---- non distinct columns



FROM table



WHERE rowCode IN (SELECT Min(rowCode) FROM table GROUP BY firstname,lastname,address1)



ORDER by firstname



This will return unique values for your distinct columns and will the first (min) value of your non distinct columns .

---------------------------------------------------



Actually in your case, because your 'non distinct' column has a 'date' type, you can solve it more easily by using the following code :



SELECT Bank, Account, Note, MAX(PlanDate)

FROM table

GROUP BY Bank, Account, Note
  scott replied to Gilad
08-Jan-11 11:50 AM
thank you worked well SQL SERVER 2008
Create New Account
help
Why doesn't this work? ASP -> SQL Server 2008 Select Distinct SQL Server , SQL, Server, 2008, Select, Distinct" / > This is in an ASP script, as I try to get unique classcodes (StudentClassCode column
Using DISTINCT SQL Server Is it possible to use a query to export or update using DISTINCT? SQL Server Discussions SQL Server (1) ReqNameID (1) Report (1) ReqName (1) BSc (1) PhD (1) Requestors (1) Toronto (1
Query timeout SQL Server Sql Server Experts: How do I catch a query timeout condition in my T-SQL code? I need to write some code to handle the situation. Thanks, Pingx SQL Server Programming Discussions SQL Server 2005 (1) SQL Server (1) Linkedservername.dbname.dbo.tablename (1) Glsupldbsc21.master.sys.servers
Codd's rule and SQL Server SQL Server Hi SQL Experts, Can anyone throw some light on whether SQL Server (2000 and 2005) supports all the 12 rules of CODD? If it doesn't support pls let what rules it doesn't support and why? Thanks in Advance Regards Pradeep SQL Server Programming Discussions SQL Server 2000 (1) SQL Server (1) Distributed (1) Catalog (1) Database
Intermittent "access denied" problem with SQL Server Express 2005 SQL Server We’re experiencing an intermittent connectivity problem with SQL Server Express. I can use some advice on eliminating the problem. We connect locally to a default (MSSQLSERVER) instance via SQL authentication using ADO across SQLOLEDB. This identical configuration has worked perfectly for seven years on enabled them in an unsuccessful bid to solve this problem. When the problem occurs with SQL Server Express, the COM error returned from ADO is this: Code = 0x80004005 Code meaning = Unspecified