Unix Technical Forum

Trying to add a NON-DISTINCT field to a DISTINCT record set in a query.

This is a discussion on Trying to add a NON-DISTINCT field to a DISTINCT record set in a query. within the SQL Server forums, part of the Microsoft SQL Server category; --> I need to run a SELECT DISTINCT query across multiple fields, but I need to add another field that ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:40 PM
Techhead
 
Posts: n/a
Default Trying to add a NON-DISTINCT field to a DISTINCT record set in a query.

I need to run a SELECT DISTINCT query across
multiple fields, but I need to add another field that is NON-DISTINCT
to my record set.

Here is my query:


SELECT DISTINCT lastname, firstname, middleinitial, address1,
address2, city, state, zip, age, gender
FROM gpresults
WHERE age>='18' and serviceline not in ('4TH','4E','4W')
and financialclass not in ('Z','X') and age not in
('1','2','3','4','5','6','7','8','9','0')
and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
ORDER BY zip


This query runs perfect. No problems whatsoever. However, I need to
also include another field called "admitdate" that should be treated
as NON-DISTINCT. How do I add this in to the query?


I've tried this but doesn't work:


SELECT admitdate
FROM (SELECT DISTINCT lastname, firstname, middleinitial, address1,
address2, city, state, zip, age, gender from gpresults)
WHERE age>='18' and serviceline not in ('4TH','4E','4W')
and financialclass not in ('Z','X') and age not in
('1','2','3','4','5','6','7','8','9','0')
and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
ORDER BY zip


This has to be simple but I do not know the syntax to accomplish
this.


Thanks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:40 PM
Sylwester Lewandowski
 
Posts: n/a
Default Re: Trying to add a NON-DISTINCT field to a DISTINCT record set ina query.

Check something like that:

SELECT lastname, firstname, middleinitial, address1,address2,city,
state, zip, age, gender, admitdate
FROM gpresults
WHERE age>='18' and serviceline not in ('4TH','4E','4W')
and financialclass not in ('Z','X') and age not in
('1','2','3','4','5','6','7','8','9','0')
and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
GROUP BY lastname, firstname, middleinitial, address1,address2,city,
state, zip, age, gender
ORDER BY zip

I wonder if it solves the problem.





Techhead napisaƂ(a):
> I need to run a SELECT DISTINCT query across
> multiple fields, but I need to add another field that is NON-DISTINCT
> to my record set.
>
> Here is my query:
>
>
> SELECT DISTINCT lastname, firstname, middleinitial, address1,
> address2, city, state, zip, age, gender
> FROM gpresults
> WHERE age>='18' and serviceline not in ('4TH','4E','4W')
> and financialclass not in ('Z','X') and age not in
> ('1','2','3','4','5','6','7','8','9','0')
> and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
> ORDER BY zip
>
>
> This query runs perfect. No problems whatsoever. However, I need to
> also include another field called "admitdate" that should be treated
> as NON-DISTINCT. How do I add this in to the query?
>
>
> I've tried this but doesn't work:
>
>
> SELECT admitdate
> FROM (SELECT DISTINCT lastname, firstname, middleinitial, address1,
> address2, city, state, zip, age, gender from gpresults)
> WHERE age>='18' and serviceline not in ('4TH','4E','4W')
> and financialclass not in ('Z','X') and age not in
> ('1','2','3','4','5','6','7','8','9','0')
> and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
> ORDER BY zip
>
>
> This has to be simple but I do not know the syntax to accomplish
> this.
>
>
> Thanks
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:41 PM
M A Srinivas
 
Posts: n/a
Default Re: Trying to add a NON-DISTINCT field to a DISTINCT record set in a query.

On Mar 12, 9:21 pm, "Techhead" <jorgenso...@gmail.com> wrote:
> I need to run a SELECT DISTINCT query across
> multiple fields, but I need to add another field that is NON-DISTINCT
> to my record set.
>
> Here is my query:
>
> SELECT DISTINCT lastname, firstname, middleinitial, address1,
> address2, city, state, zip, age, gender
> FROM gpresults
> WHERE age>='18' and serviceline not in ('4TH','4E','4W')
> and financialclass not in ('Z','X') and age not in
> ('1','2','3','4','5','6','7','8','9','0')
> and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
> ORDER BY zip
>
> This query runs perfect. No problems whatsoever. However, I need to
> also include another field called "admitdate" that should be treated
> as NON-DISTINCT. How do I add this in to the query?
>
> I've tried this but doesn't work:
>
> SELECT admitdate
> FROM (SELECT DISTINCT lastname, firstname, middleinitial, address1,
> address2, city, state, zip, age, gender from gpresults)
> WHERE age>='18' and serviceline not in ('4TH','4E','4W')
> and financialclass not in ('Z','X') and age not in
> ('1','2','3','4','5','6','7','8','9','0')
> and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
> ORDER BY zip
>
> This has to be simple but I do not know the syntax to accomplish
> this.
>
> Thanks


Why can't you add admitdate in distinct . If admitdates are different
they will show
as two entries . I hope this is what you want

SELECT DISTINCT lastname, firstname, middleinitial, address1,
address2, city, state, zip, age, gender,admitdate
FROM gpresults
WHERE age>='18' and serviceline not in ('4TH','4E','4W')
and financialclass not in ('Z','X') and age not in
('1','2','3','4','5','6','7','8','9','0')
and (CAST (ADMITDATE AS DATETIME) >= DATEDIFF(day, 60, GETDATE()))
ORDER BY zip



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:20 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com