ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old March 19th, 2004, 11:56 AM
tech-writer tech-writer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 tech-writer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 9 sec
Reputation Power: 5
showing distinct information for individual records in a grouped output

Got another one that is giving us a headache over here:

I have a number of queries which utilize "group by." This works great except when grabbing data from multiple queries.

For example, I have a query "details" that grabs a bunch of related data across 5 tables and has an "order by."

Then to get the most recent entry date:

<CFQUERY NAME= "lastDate" DATASOURCE= "QMB_DB">select max(DATE_CREATED) date_created from qmb where qmb.pid = '#details.pid#' or qmb.qid = '#details.pid#'</CFQUERY>


In the output:

<CFOUTPUT QUERY= "details" group="NAME">
<CFOUTPUT>
<tr>
#name#
#lastDate.date_created#
</tr>
</CFOUTPUT>
</CFOUTPUT>

This should give me rows representing indivual query results and the most recent "date created" for the records associated with that particular row.

Unfortunately, what it does is grab the first "created_by" date and applies it to every row.

Any ideas?

Reply With Quote
  #2  
Old March 19th, 2004, 01:15 PM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,480 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 17 h 33 m 17 sec
Reputation Power: 42
You can try it without the second set of <cfoutput> tags. But regardless, won't every record displayed have the same value for lastdate.date_created because that query only returns 1 record (the max date)? You only have one value for lastdate.date_created, so you will naturally have the same value for that variable every time you output it.

Reply With Quote
  #3  
Old March 19th, 2004, 01:58 PM
tech-writer tech-writer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 tech-writer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 9 sec
Reputation Power: 5
On the forum page here it lists various threads. For each thread it not only shows when the last entry was made in that thread, but who made that entry.

How can you do that in ColdFusion with Oracle?

Reply With Quote
  #4  
Old March 19th, 2004, 03:46 PM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,480 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 17 h 33 m 17 sec
Reputation Power: 42
You would return a query with each row containing the thread information (title, thread starter, etc.) for each thread, along with the most recent time each thread was posted to. If you use the max() function, you must remember that if you mix column data and aggregate functions, you must use a GROUP BY clause in your SQL statement.

Reply With Quote
  #5  
Old March 22nd, 2004, 09:08 AM
tech-writer tech-writer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 tech-writer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 9 sec
Reputation Power: 5
I am using GROUP BY statements.

Are you saying that each "post" record would need a data field for information on the thread? I don't have that. I thought I could run a query which compared the associated posts and then provide a seperate count for each output row, and display the most recent "date_created" for the associated records represented in each output row.

Do I need to change the DB structure to make this work?

At this point, the "count" displayed in each row is the count calculation for the first row only, and the "last post date" in each row is also based only on the first row calculation.

Reply With Quote
  #6  
Old March 22nd, 2004, 10:04 AM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,480 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 17 h 33 m 17 sec
Reputation Power: 42
Yes, each record that you are displaying would have a field for the date created, something like this:

select max(p.DATE_CREATED) lastPostDate, t.threadName
from threads t, posts p
where t.threadID = p.threadID
group by t.threadName

Now if you output each thread name, you also have the date of the most recent post for that thread. You should not need to change your database at all.

Last edited by kiteless : March 22nd, 2004 at 10:06 AM.

Reply With Quote
  #7  
Old March 22nd, 2004, 12:13 PM
tech-writer tech-writer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 tech-writer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 9 sec
Reputation Power: 5
No threads table.

Currently there is a post table with a post_id. Associations are established by a question_id field which is populated only in responses with the value of the post_id for the first post of the thread. So the question_id field for the second and all subsequent posts would be equal to the value of the post_id for the first post.

So, one query identifies the questions (question_id field is null).

Another query extracts the datafields for the records identified in the first query.

The count query that should count the number of answers to each question just grabs for the first question only.

The lastdate query that should grab the most recent date_created for answers to a question also only grabs the data for the first record.

There is something we are missign here when we have a grouped output and are using multiple queries.

If/when I find the answer I'll post it here for you. I know it can be done because I've seen it done in CF based message boards. One place even provided a demo. Unfortunately, the code was encrypted so I couldn't see how they structured the queries.

Reply With Quote
  #8  
Old March 22nd, 2004, 01:57 PM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,480 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 17 h 33 m 17 sec
Reputation Power: 42
Quote:
Originally Posted by tech-writer
No threads table.
I was just following your example scenario about this forum and the way it displays threads and the date of the last post in each thread.

I whipped up a simple access table which had a post_id, question_id, date_created, and questionText. question_id is null for new questions, and has the post_id of the original post if it is an answer to a post. This query gets all the posts, the count of replies, and the date of the most recent reply. It could probably be cleaned up (or collapsed into one query through the use of a LEFT OUTER JOIN) but I only had a few minutes:

select q1.post_ID, q1.questionText, count(*) as postInThread, max(q2.date_created) as mostRecentPost
from qmb q1,
qmb q2
where q1.question_ID IS NULL
and q1.post_id = q2.question_id
group by q1.post_ID, q1.questionText
order by q1.post_ID
UNION
select q1.post_ID, q1.questionText, 0, date_created
from qmb q1
where q1.question_ID IS NULL and not exists (select 1 from qmb q2 where q1.post_id = q2.question_id)

Reply With Quote
  #9  
Old March 23rd, 2004, 08:09 AM
tech-writer tech-writer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 tech-writer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 9 sec
Reputation Power: 5
I sent you a message ... did you get it?

Reply With Quote
  #10  
Old March 23rd, 2004, 08:53 AM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,480 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 17 h 33 m 17 sec
Reputation Power: 42
I did, and sent you an email with the database attached, it's a very simple setup but it should demonstrate the use of the query I posted.

Reply With Quote
  #11  
Old March 26th, 2004, 10:38 AM
tech-writer tech-writer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 tech-writer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 9 sec
Reputation Power: 5
It was a bit tricky, but we got it implemented. Here is the complete final query:

select qmb_user.first_name,qmb_user.last_name,qmb_user.email,
qmb_topic_post.topic_id, qmb_post.post_id, qmb_post.post_name,
qmb_post.post_desc, qmb_post.user_id,
TO_CHAR(qmb_post.date_created,'MM/DD/YYYY HH24:MI') date_created,
(select TO_CHAR( max( DATE_CREATED ) ,'MM/DD/YYYY HH24:MI' ) from QMB_POST a where a.QUESTION_id= qmb_post.post_id group by qmb_post.post_id) lastdate,
QMB_TOPIC.topic_name, QMB_TOPIC.topic_id,
(select count(*) from qmb_post b
where b.question_id is not null
and b.question_id=qmb_post.post_id group by b.question_id) anscount
from qmb_post,
qmb_forum, qmb_topic_post, qmb_topic, qmb_user
where qmb_post.post_id = qmb_topic_post.post_id
and qmb_topic_post.topic_id = qmb_topic.topic_id
and qmb_topic.forum_id = qmb_forum.forum_id
and qmb_forum.forum_id = '#FORUM_ID#'
and qmb_post.question_id IS NULL
and qmb_user.user_id = qmb_post.user_id
order by qmb_topic_post.topic_id,qmb_post.post_id

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > showing distinct information for individual records in a grouped output


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |