Solved

Please help with this query(s).

Posted on 2002-06-25
2
186 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
ID: 7119608
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
ID: 7121527
Thanks for the insight and help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordpress email form 23 82
DNS, website, godaddy 6 90
Problem to go to Web page 2 126
Redirect to HTTPS results in Infinite LOOP 4 138
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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