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

x
?
Solved

Please help with this query(s).

Posted on 2002-06-25
2
Medium Priority
?
191 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 400 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

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