Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

cfoutput group function

Posted on 2011-03-16
12
Medium Priority
?
361 Views
Last Modified: 2012-05-11
One check pays for multiple invoices.

I want this output to display one check number with one or more invoice numbers in the same line.

It works fine if 1 check paid 1 invoice, otherwise it lists the check each time it was used to pay an invoice. When I use the "group" function, it will list the check once, but will only list the first invoice it paid for.

Any ideas?
SELECT *
FROM invoice
LEFT OUTER JOIN Banking ON invoice.Check_Number=banking.Check_Number

<cfoutput query="vchecks" maxrows="500" group="check_number">

Open in new window

0
Comment
Question by:aka_FATCAT
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35153964
use the <cfoutput query="qchecks"> check @ main and inside use another question with cfoutput group attribute
0
 

Author Comment

by:aka_FATCAT
ID: 35153985
<cfoutput query="vchecks" maxrows="500">
<cfoutput group="check_number">#invoice_number#</cfoutput>
</cfoutput>

This returned every invoice number, not just the ones associated with that check.
0
 

Accepted Solution

by:
aka_FATCAT earned 0 total points
ID: 35154216
Got it.
<cfoutput query="vchecks" maxrows="500">
<cfloop query="invoicenumbers" >#invoice_number# - </cfloop>

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35154410
@ good u found answer!
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35156568
Assuming that your check number and your invoice numbers are coming from that one query, your answer is not optimal, all you need is the group by with a 2nd set of cfoutput tags within it.

Also, to ensure the grouping works, you have to order your query by check number so the check numbers are all grouped together

SELECT *
 FROM invoice
 LEFT OUTER JOIN Banking ON invoice.Check_Number=banking.Check_Number
order by check_number, invoice_date

<cfoutput query="vchecks" maxrows="500" group="check_number">
  #check_number# - <cfoutput>#invoice_number#</cfoutput>
</cfoutput>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35159407
I agree with gdemaria's answer. It's the more optimal approach.  

<cfoutput query="vchecks" maxrows="500"

... another performance tip, limit the number of rows in the db.  The current code actually pulls every record in the 2 tables, but only displays the first X records.  If you only need 500 records, specify that in your sql.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35159857
Got it.
<cfoutput query="vchecks" maxrows="500">
<cfloop query="invoicenumbers" >#invoice_number# - </cfloop>


Hm... on second glance, ... I don't believe that would work. At least not if there's more than 1 checknumber as your query suggests.
0
 

Author Comment

by:aka_FATCAT
ID: 35161380
Here is the complete working code.
<cfquery name="vchecks" datasource="#request.dataSource#">
SELECT *
FROM banking
WHERE 0=0
<!---LEFT OUTER JOIN Banking ON invoice.Check_Number=banking.Check_Number--->
<cfif isDefined ("URL.check_status")>
AND cstatid = '#URL.check_status#'
</cfif>
<cfif isDefined("FORM.check_date")>
AND check_date = '#FORM.check_date#'
<cfelseif isDefined("URL.check_date")>
AND check_date = '#URL.check_date#'
<cfelse>
AND check_date > '#DateFormat(startdate, "mm-dd-yyyy")#'
</cfif>
<cfif isDefined ("URL.date")>
ORDER BY check_date
<cfelse>
ORDER BY banking.check_number desc
</cfif>
</cfquery>


<cfoutput query="vchecks" maxrows="500">
<cfquery name="cstat" datasource="#request.dataSource#">
SELECT cstatid, check_status
FROM cstat
WHERE cstatid = '#vchecks.cstatid#'
</cfquery>
<cfquery name="invoicenumbers" datasource="#request.dataSource#">
SELECT invoice_number
FROM invoice
WHERE check_number = '#vchecks.check_number#'
</cfquery>

<tr>
    <td>#check_number#</td>
    <td>#DateFormat(check_date, "ddd, mm-dd-yyyy")#</td>
    <td>#pay_to#</td>
    <td><cfloop query="invoicenumbers" >#invoice_number# - </cfloop></td>
    <td>#DollarFormat(amount)#</td>
    <td>#notes#</td>
    <td>
    <cfform name="cstatus" method="post" action="/_admin/view_checks.cfm">
    <cfselect name="cstatus_select" query="cstatf" value="cstatid" selected="#cstat.cstatid#" display="check_status"/>
    <cfinput type="submit" name="submit" value="Submit">
    <cfinput type="hidden" value="#vchecks.cid#" name="cid">
    </cfform>
    
    </td>
</tr>
</cfoutput>
</table>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35161700

That is one way to do it, yes.  Did you post it so we could help you do it better or just to show the "answer?"

Right now you have one main query, plus two queries that run for EVERY check number, so if there are 50 checks, you will run 101 queries.   The way agx and I suggested, you would have one query no matter what.

You will also have 50 forms instead of one form.

If you want more help, let us know, if you were just posting because you wanted to show it working, that's fine too.
0
 

Author Comment

by:aka_FATCAT
ID: 35161793
Right now, I'm just happy it's working.

If you can tell me how to do it better as in more efficiently, I would be very grateful.

Thanks.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35161895
Something like this..
<cfquery name="vchecks" datasource="#request.dataSource#">
 SELECT TOP 500
        b.*
      , inv.invoice_number
      , cs.check_status
 FROM banking b
   left join invoice inv on inv.check_number = b.check_number
   left join cstat cs on cs.cstatId = b.cstatid
 WHERE 0=0
<!---LEFT OUTER JOIN Banking ON invoice.Check_Number=banking.Check_Number--->
 <cfif isDefined ("URL.check_status")>
  AND b.cstatid = '#URL.check_status#'
 </cfif>
 <cfif isDefined("FORM.check_date")>
  AND b.check_date = '#FORM.check_date#'
 <cfelseif isDefined("URL.check_date")>
  AND b.check_date = '#URL.check_date#'
 <cfelse>
  AND b.check_date > '#DateFormat(startdate, "mm-dd-yyyy")#'
 </cfif>
 <cfif isDefined ("URL.date")>
  ORDER BY b.check_date, b.check_number desc
 <cfelse>
  ORDER BY b.check_number desc
 </cfif>
</cfquery>

<cfform name="cstatus" method="post" action="/_admin/view_checks.cfm">


<cfoutput query="vchecks" group="check_number">
<tr>
    <td>#check_number#</td>
    <td>#DateFormat(check_date, "ddd, mm-dd-yyyy")#</td>
    <td>#pay_to#</td>
    <td><cfoutput>#invoice_number# - </cfoutput></td>
    <td>#DollarFormat(amount)#</td>
    <td>#notes#</td>
    <td>
      <cfselect name="cstatus_select#vchecks.currentRow#" query="cstatf" value="cstatid" selected="#vchecks.cstatid#" display="check_status"/>
      <cfinput  name="cid#vchecks.currentRow#" type="hidden" value="#vchecks.cid#" >
    </td>
</tr>
</cfoutput>
</table>

  <cfinput type="submit" name="submit" value="Submit">
</cfform>

Open in new window

0
 

Author Closing Comment

by:aka_FATCAT
ID: 35178889
Found the answer myself.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

877 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