Solved

Please help with this query(s).

Posted on 2002-06-25
2
178 Views
Last Modified: 2013-12-24
Ok,
I have a report that pulls all of its information from 2 queries and I know that it can be done with one.

the query is searching for all payments 1-6  received on #today# where the code = ex and payments >0

once the payment information is received I need to get a total of all payments received on #today#.

I am posting both queries that I have that pull this information but I know that there is a better way.
please help me out.(and yes they are ugly)

1ST Query pull the information

<cfquery name="CheckRpt" datasource="exhibition">
SELECT     TOP 100 PERCENT dbo.EXH_CONTACT_INFO.EX_Co_Name,dbo.EXH_CONTACT_INFO.EX_sort, dbo.Payments.P_ExhibitorNumber, dbo.Payments.P_booth1, dbo.Payments.Number1,
                      dbo.Payments.P_Payment_1, dbo.Payments.P_Payment_Date1, dbo.Payments.P_Name_Check1, dbo.Payments.P_Payment_Type1,
                      dbo.Payments.number2, dbo.Payments.P_Payment_2, dbo.Payments.P_Payment_Date2, dbo.Payments.P_Payment_Type2,
                      dbo.Payments.P_Name_Check2, dbo.Payments.P_Payment_3, dbo.Payments.P_Payment_Date3, dbo.Payments.P_Payment_Type3,
                      dbo.Payments.number3, dbo.Payments.P_Name_Check3, dbo.Payments.number4, dbo.Payments.P_Payment_4, dbo.Payments.P_Payment_Date4,
                      dbo.Payments.P_Payment_Type4, dbo.Payments.P_Name_Check4, dbo.Payments.number5, dbo.Payments.P_Payment_5,
                      dbo.Payments.P_Payment_Date5, dbo.Payments.P_Payment_Type5, dbo.Payments.P_Name_Check5, dbo.Payments.number6,
                      dbo.Payments.P_Payment_6, dbo.Payments.P_Payment_Date6, dbo.Payments.P_Payment_Type6, dbo.Payments.P_Name_Check6
FROM         dbo.Booth_Information INNER JOIN
                      dbo.EXH_CONTACT_INFO ON dbo.Booth_Information.BT_ExhibitorNumber = dbo.EXH_CONTACT_INFO.ExhibitorNumber INNER JOIN
                      dbo.Payments ON dbo.Booth_Information.BoothRecordID = dbo.Payments.P_BoothRecordID
WHERE     (dbo.EXH_CONTACT_INFO.EX_Exhibitor_Code = 'ex')
 AND
 (dbo.Payments.P_Payment_Date1 = CONVERT(DATETIME, '#today#'))
AND  (dbo.Payments.P_Payment_1 > 0) AND (dbo.Payments.P_Payment_Type1 = 'ck')
 OR
(dbo.Payments.P_Payment_Date2 = CONVERT(DATETIME, '#today#')) AND (dbo.Payments.P_Payment_Type2 = 'ck') AND
                      (dbo.Payments.P_Payment_2 > 0) OR
                      (dbo.Payments.P_Payment_Date3 = CONVERT(DATETIME, '#today#')) AND (dbo.Payments.P_Payment_Type3 = 'ck') OR
                      (dbo.Payments.P_Payment_4 > 0) AND (dbo.Payments.P_Payment_Date4 = CONVERT(DATETIME, '#today#')) AND
                      (dbo.Payments.P_Payment_Type4 = 'ck') OR
                      (dbo.Payments.P_Payment_5 > 0) AND (dbo.Payments.P_Payment_Date5 = CONVERT(DATETIME, '#today#')) AND
                      (dbo.Payments.P_Payment_Type5 = 'ck') OR
                      (dbo.Payments.P_Payment_6 > 0) AND (dbo.Payments.P_Payment_Date6 = CONVERT(DATETIME, '#today#')) AND
                      (dbo.Payments.P_Payment_Type6 = 'ck')
ORDER BY dbo.EXH_CONTACT_INFO.EX_sort ASC
</cfquery>


second query gets the total of all payments received on given date

<cfquery name="getpmttotals" datasource="exhibition">
SELECT     SUM(Payments.P_Payment_1 + Payments.P_Payment_2 + Payments.P_Payment_3 + Payments.P_Payment_4 + Payments.P_Payment_5 + Payments.P_Payment_6)
                       AS Total, Payments.P_Payment_Date1
FROM         Payments INNER JOIN
                      EXH_CONTACT_INFO ON Payments.P_ExhibitorNumber = EXH_CONTACT_INFO.ExhibitorNumber INNER JOIN
                      Booth_Information ON Payments.P_BoothRecordID = Booth_Information.BoothRecordID AND
                      EXH_CONTACT_INFO.ExhibitorNumber = Booth_Information.BT_ExhibitorNumber
GROUP BY EXH_CONTACT_INFO.EX_Exhibitor_Code, Payments.P_Payment_Date2, Payments.P_Payment_Date3, Payments.P_Payment_Date1,
                      Payments.P_Payment_Date4, Payments.P_Payment_Date5, Payments.P_Payment_Date6, Payments.P_Payment_Type1,
                      Payments.P_Payment_Type2, Payments.P_Payment_Type3, Payments.P_Payment_Type4, Payments.P_Payment_Type5,
                      Payments.P_Payment_Type6
HAVING      (EXH_CONTACT_INFO.EX_Exhibitor_Code = 'ex') AND (Payments.P_Payment_Date1 = '#today#') AND
                      (Payments.P_Payment_Type1 = 'ck') OR
                      (Payments.P_Payment_Date2 = '#today#') AND (Payments.P_Payment_Type2 = 'ck') OR
                      (Payments.P_Payment_Date3 = '#today#') AND (Payments.P_Payment_Type3 = 'ck') OR
                      (Payments.P_Payment_Date4 = '#today#') AND (Payments.P_Payment_Type4 = 'ck') OR
                      (Payments.P_Payment_Date5 = '#today#') AND (Payments.P_Payment_Type5 = 'ck') OR
                      (Payments.P_Payment_Date6 = '#today#') AND (Payments.P_Payment_Type6 = 'ck')



</cfquery>thanks in advance


0
Comment
Question by:jriver12
2 Comments
 

Accepted Solution

by:
jwchesley earned 100 total points
Comment Utility
well, since you wrote the 2 queries, you know that SQL group functions require a group by clause, and that you lose the detail records when you use the group by clause.   To slice and dice the data 2 ways, you need 2 queries... or another tool.  I have a few suggestions...

- if you're always using today's date in any query, use the database date rather than sending a date from CF (or at least use a cfqueryparam to let the db cache the query)

- build a stored procedure to pull the data and sum up the total in the database and deliver either 2 resultsets or, if your driver cannot handle 2 result sets, add a dummy record to the query with the total values at the end of each group and handle that in your code.

- or write a trigger to update another table with the running total for the day for each exhibitor so all you need to do for the total is pull one record for each exhibitor (but you run a slight chance of an insert updating the totals table while you are running the 1st query.. not a big deal if this is just a status report ... you've already got this issue with the 2 queries above)

- if you can't push all the processing off onto the database (where it really ought to be), run a series of steps:
1) drop or truncate a temp_table
2) run first query to load results into temp_table .. (works purely within the database)
3) run a query out of temp_table to get your first report (no where clause needed, data is pre-sorted)
4) run summary query out of temp_table for totals

it often happens that breaking up a complex operation into smaller steps can make the total processing time much shorter.  It's worth testing to see.  
- jack

0
 

Author Comment

by:jriver12
Comment Utility
Thanks for the insight and help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

9 Experts available now in Live!

Get 1:1 Help Now