Solved

Please help with this query(s).

Posted on 2002-06-25
2
184 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 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

18 Experts available now in Live!

Get 1:1 Help Now