Solved

SQL syntax with Distinct and All

Posted on 2001-06-11
7
353 Views
Last Modified: 2008-02-01
I am presently using the below code to return a recordset for a DAO 3.6/VB6 project. It truncates the Abstract field since it is a memo field and I have found that we can't use DISTINCT with a memo field. Could someone please tell me how to alter the code to use SELECT DISTINCT for all the other fields and include the Abstract field without DISTINCT?

strSQL = "SELECT DISTINCT AuthorPub.MedlineAuthorName, Pub.Journal, Pub.Abstract, Pub.Citation , Pub.PubYear, Pub.Title, Pub.MedlineID From Pub, AuthorPub " _
& "WHERE Pub.medlineid = AuthorPub.MedlineID " _
& "AND (AuthorPub.MedlineAuthorName = '" & strAuthor & "') " _
& "ORDER BY Pub.PubYear DESC"
0
Comment
Question by:TurnerP
7 Comments
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6178262
If you are reducing the number of rows for the other fields by using distinct you will also have to reduce the number of values for the memo field (you can only have one value per row).
You could use a subquery to get back the data for the memo field and a group by for all the others.

SELECT AuthorPub.MedlineAuthorName, Pub.Journal, Pub.Citation , Pub.PubYear,
Pub.Title, Pub.MedlineID ,
Abstract = (select P2.Abstract from pub p2 where p2.id = pub.id)
0
 

Author Comment

by:TurnerP
ID: 6178554
OK, I understand the logic here, but I am rather new at this and don't understand subqueries and the proper syntax. I'll give you the 200 if you would please rewrite my above code incorporating the subquery so it could work. (I have a deadline and we are moving our home on Wednesday. I have packed my SQL book!)I hope you'll do this. Thanks.
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6179049
try something like (this is for sql server don't know what you have)

"SELECT DISTINCT AuthorPub.MedlineAuthorName, Pub.Journal, Pub.Citation , Pub.PubYear,
Pub.Title, Pub.MedlineID, " _
& "Abstract = (select top 1 P2.Abstract from pub p2 where p2.id = pub.id), " _
& " From Pub, AuthorPub " _
& "WHERE Pub.medlineid = AuthorPub.MedlineID " _
& "AND (AuthorPub.MedlineAuthorName = '" & strAuthor & "') " _
& "ORDER BY Pub.PubYear DESC"
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:TurnerP
ID: 6179366
Great! Thanks for your help. We're getting there...

It returns the following SQL statement and a runtime error 3342 (Invalid memo, OLE or Hyperlink Object in subquery 'P2.Abstract'

SELECT DISTINCT AuthorPub.MedlineAuthorName, Pub.Journal, Pub.Citation , Pub.PubYear, Pub.Title , Pub.MedlineID, Abstract = (select top 1 P2.Abstract from pub p2 where p2.id = pub.id)  From Pub, AuthorPub WHERE Pub.medlineid = AuthorPub.MedlineID AND (AuthorPub.MedlineAuthorName = 'Amstey MS') ORDER BY Pub.PubYear DESC



0
 

Author Comment

by:TurnerP
ID: 6181394
According to the documentation, you can't use a memo field in a subquery. Is there a way around this?
0
 
LVL 54

Expert Comment

by:nico5038
ID: 7257253

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - PAQ'd and pts refunded
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
0
 

Accepted Solution

by:
Jgould earned 0 total points
ID: 7282207
Question has been closed as per recommendation

JGould-EE Moderator
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now