[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Please help with this query(s).

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')
 (dbo.Payments.P_Payment_Date1 = CONVERT(DATETIME, '#today#'))
AND  (dbo.Payments.P_Payment_1 > 0) AND (dbo.Payments.P_Payment_Type1 = 'ck')
(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')

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,
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

1 Solution
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

jriver12Author Commented:
Thanks for the insight and help.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now