October 2, 2024
How can I get just one sql result per ID (pk)
Comments
(1)
October 2, 2024
How can I get just one sql result per ID (pk)
Newbie 1 posts
Followers: 0 people
(1)

I am using ColdFusion on a website hosting account and mssql (late version). I’m looking for all instances of the StoryID but I want to display only one record for each StoryID.  The storyid is a uniqueidentifier.

Below is my query. It works but the problem is that  it shows multiple instances of the same storyid. Each unique storyid represents a news story where readers comment about that story. So I want to display only the first story comment but offer a link to a page showing all comments to a particular storyid. There may be dozens of comments to the same storyid. But my stab at this does not work the way I want. Currently, all comments are shown on the same page and it’s not attractive, so cumbersome.

I’m not a programmer. Not really, just a basic beginner, so I came here hoping someone would show me how the cfquery should be written to achieve what I want.

Here is my feeble attempt. And yeah, I also tried using ‘select top 1’ but that just gives me one record. I want all comment records on the first page but only one each per storyid.  Show only the first storyid record found  (Debate table ID (pk) ordered by id asc).

<cfquery name=”AllInfo” datasource=”Publishers”>
select distinct (select top 1 storyid) id, pubdate,title,comment, storyid
from DEBATE
order by id desc
</cfquery>

Please help. I am so tired of reading and trying so many different possibilities.

 

1 Comment
2024-10-09 13:40:01
2024-10-09 13:40:01

Since it’s been a week with no reply (curiously, I only got notified of this today), I’ll suggest other places you could/should ask. See my list at cf411.com/cfsocialhelp.

Each are more active than this portal. And you’ll get many ideas from many people, I’m sure–or they’ll ask leading questions to prompt you or others to improve on things. Then you can pick which suits you best.

Like
Add Comment