Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ould
Starting Member
6 Posts |
Posted - 2008-07-31 : 13:18:42
|
Hi everyone,I am a new member here and am looking for help on a Query I am trying to code. Here's the general idea. I have a database which stores credit card transactions which come from different terminals, among many other things. Basically what I am trying to accomplish is to have a listing of the number of transactions per each terminal with $ total as well as showing me terminals that have no transactions. I have the first part working presently but am unable to get the query to bring up terminals that have no transactions. There are 4 tables involved in my query:1. CreditCardTransaction - lists credit transactions2. DevTyp - Description of the Devices3. Parking - Where the device is located4. CreditCardMerchant(not shown in my current query as I couldn't get it to work, it threw off my numbers) - lists all terminals that are able to accept credit cardsFields: CreditCardTransaction - TerminalId, Transaction_Amout, TerminalType and ParkingIdDevTyp - Joined by TerminalIdParking - Joined by ParkingIdCreditCardMerchant - Joined by TerminalIdWhat I have so far:SELECT CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.Dsc As Parking_Lot, COUNT(*) As Transaction_Count, SUM (CreditCardTransaction.Transaction_Amount) as Total_SalesFROM CreditCardTransactionJOIN Parking ON CreditCardTransaction.ParkingId = Parking.Id JOIN DevTyp ON CreditCardTransaction.TerminalType = DevTyp.DeviceIdWHERE CreditCardTransaction.Approved <> 0 ANDCreditCardTransaction.Upload_DateTime >= '07/01/2008 00:00:00' ANDCreditCardTransaction.Upload_DateTime <= '07/30/2008 23:59:59' GROUP by CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.DscORDER by Parking.Dsc ASC, DevTyp.DeviceDesc ASC, CreditCardTransaction.TerminalId ASCThis gives me output like:4 Cashier station Lot 5V 2 10.00005 Cashier station Lot 5V 284 2372.0000Which is what I am after as far as layout is concerned, but currently it doesn't show me the terminals which have no sales. I tried to use this query but it threw off my numbers and still didn't show me the terminals with no sales:SELECT CreditCardMerchant.TerminalId, DevTyp.DeviceDesc, Parking.Dsc As Parking_Lot, COUNT(*) As Transaction_Count, SUM (CreditCardTransaction.Transaction_Amount) as Total_SalesFROM CreditCardTransactionLEFT OUTER JOIN CreditCardMerchant ON CreditCardTransaction.TerminalId = CreditCardMerchant.TerminalIdJOIN Parking ON CreditCardTransaction.ParkingId = Parking.Id JOIN DevTyp ON CreditCardTransaction.TerminalType = DevTyp.DeviceIdWHERE CreditCardTransaction.Approved <> 0 ANDCreditCardTransaction.Upload_DateTime >= '07/01/2008 00:00:00' ANDCreditCardTransaction.Upload_DateTime <= '07/30/2008 23:59:59' GROUP by CreditCardMerchant.TerminalId, DevTyp.DeviceDesc, Parking.DscORDER by Parking.Dsc ASC, DevTyp.DeviceDesc ASC, CreditCardMerchant.TerminalId ASCIt gave me the same output except the numbers were skewed and it still didn't give me the terminals with no transactions.Also while I am at it how do I tell the query to give me the last week worth of data rather than editing the dates by hand everytime I run the script. So say I run it on Monday, I want the previous Monday through Sunday data.Thanks for any insight, if you need more info please let me know...Kevin |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-31 : 13:28:59
|
Which is base table for terminals?the last week worth of data can be obtained by replacing where condition asWHERE CreditCardTransaction.Approved <> 0 ANDCreditCardTransaction.Upload_DateTime >= DATEADD(d,DATEDIFF(d,0,GETDATE()-7),0) ANDCreditCardTransaction.Upload_DateTime <= DATEADD(d,DATEDIFF(d,0,GETDATE()-3),0) |
 |
|
ould
Starting Member
6 Posts |
Posted - 2008-07-31 : 13:42:47
|
quote: Originally posted by visakh16 Which is base table for terminals?the last week worth of data can be obtained by replacing where condition asWHERE CreditCardTransaction.Approved <> 0 ANDCreditCardTransaction.Upload_DateTime >= DATEADD(d,DATEDIFF(d,0,GETDATE()-7),0) ANDCreditCardTransaction.Upload_DateTime <= DATEADD(d,DATEDIFF(d,0,GETDATE()-3),0)
Thanks for the reply I will try your date strings,The "CreditCardMerchant" table lists all terminals which are able to except credit cards which is all I want returned in my query. I guess that would make it the base. "CreditCardTransaction" table just lists transactions one by one (TransactionDateTime, CC#, Expiry, Amount, terminal #, parking #, terminaltype, creditcardtype etc etc). Some terminals may have no transactions for the period listed(i.e. not being used, equipment failure etc.)Hope that helps.Kevin |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-31 : 13:50:37
|
May be this.SELECT CreditCardMerchant.TerminalId, temp.DeviceDesc, temp.Parking_Lot, temp.Transaction_Count,temp.Total_SalesFROM CreditCardMerchantLEFT OUTER JOIN ( SELECT CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.Dsc As Parking_Lot, COUNT(*) As Transaction_Count, SUM (CreditCardTransaction.Transaction_Amount) as Total_SalesFROM CreditCardTransaction JOIN Parking ON CreditCardTransaction.ParkingId = Parking.Id JOIN DevTyp ON CreditCardTransaction.TerminalType = DevTyp.DeviceIdWHERE CreditCardTransaction.Approved <> 0 ANDCreditCardTransaction.Upload_DateTime >= DATEADD(d,DATEDIFF(d,0,GETDATE()-7),0) ANDCreditCardTransaction.Upload_DateTime <=DATEADD(d,DATEDIFF(d,0,GETDATE()-3),0) GROUP by CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.Dsc)tempON temp.TerminalId = CreditCardMerchant.TerminalIdORDER by temp.Parking_Lot ASC, temp.DeviceDesc ASC, CreditCardMerchant.TerminalId ASC |
 |
|
ould
Starting Member
6 Posts |
Posted - 2008-07-31 : 16:23:06
|
Thanks a bunch!!That seemed to work! I had to change the date strings slightly and add a DISTINCT to the first SELECT statement. CreditCardMerchant has a terminalid for each Credit Card Type(Visa, MC, AMEX etc) so I had reapeating data initially.Now, is there a somewhat easy way to automatically run this every week email the results to a specified email address(es)?? Or even export to an excel file would be good. If not, no big deal I can run it manually.Thanks again,Kevin |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-02 : 06:17:32
|
quote: Originally posted by ould Thanks a bunch!!That seemed to work! I had to change the date strings slightly and add a DISTINCT to the first SELECT statement. CreditCardMerchant has a terminalid for each Credit Card Type(Visa, MC, AMEX etc) so I had reapeating data initially.Now, is there a somewhat easy way to automatically run this every week email the results to a specified email address(es)?? Or even export to an excel file would be good. If not, no big deal I can run it manually.Thanks again,Kevin
yup. you can wrap this query in a stored procedure and add a sql agent job to call the procedure and schedule it to according to your convienience. The job will automatically run procedure and get the results. you could then use xp_sendmail procedure to send results as mail to email addresses (you should have to set up mail profiles for users). |
 |
|
ould
Starting Member
6 Posts |
Posted - 2008-08-13 : 17:23:46
|
Actually after running this query a few more times I am seeing it isn't quite picking up all the terminals as I had hoped. From what I can tell if there is no transactions at all from a given terminal anywhere in the CreditCardTransaction table it won't return a NULL in my results but if there is transactions appearing from said terminal just not in my specified date range then it will return with a NULL. I want to see a NULL come up no matter what, as long as the terminal appears in the CreditCardMerchant table. I thought the "LEFT outer JOIN" would accomplish this but it doesn't appear to be working correctly. Any ideas on how I can accomplish this? Here is the query in it's current almost working state:SELECT DISTINCT CreditCardMerchant.TerminalId, temp.DeviceDesc, temp.Parking_Lot, temp.Transaction_Count,temp.Total_SalesFROM CreditCardMerchantLEFT OUTER JOIN ( SELECT CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.Dsc As Parking_Lot, COUNT(*) As Transaction_Count, SUM (CreditCardTransaction.Transaction_Amount) as Total_SalesFROM CreditCardTransaction JOIN Parking ON CreditCardTransaction.ParkingId = Parking.Id JOIN DevTyp ON CreditCardTransaction.TerminalType = DevTyp.DeviceIdWHERE CreditCardTransaction.Approved <> 0 ANDCreditCardTransaction.Upload_DateTime >= DATEADD(d,DATEDIFF(d,0,GETDATE()-7),0) ANDCreditCardTransaction.Upload_DateTime <=DATEADD(d,DATEDIFF(d,0,GETDATE()),0) GROUP by CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.Dsc)tempON temp.TerminalId = CreditCardMerchant.TerminalIdORDER by temp.Parking_Lot ASC, CreditCardMerchant.TerminalId ASC, temp.DeviceDesc ASC I had to use the DISTINCT as CreditCardMerchant has 3 records per terminal(one for each credit card type accepted). Thanks again!!Kevin |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-14 : 14:45:23
|
Not sure about what you're telling. it will return a record if it exists in CreditCardMerchant no matter whether it didnt exist in others or even didnt have matching ones. cann you explain with some data problem you're facing? |
 |
|
ould
Starting Member
6 Posts |
Posted - 2008-08-18 : 12:11:23
|
quote: Originally posted by visakh16 Not sure about what you're telling. it will return a record if it exists in CreditCardMerchant no matter whether it didnt exist in others or even didnt have matching ones. cann you explain with some data problem you're facing?
That is what I am saying, it's not returing all records from CreditCardMerchant. It's only returning when there is transactions in CreditCardTransaction. For example I have a terminal #4 from a Lot #10 which appears in CreditCardMerchant but is not presently being used hence there are no transactions currently coming from it yet it doesn't appear in my result set. It's as if the Left Join is not functioning the way it is supposed to.Here's some examples to help explain since I find it hard to explain over the net:Query:SELECT DISTINCT CreditCardMerchant.TerminalId, Parking.Dsc, DevTyp.DeviceDescFROM CreditCardMerchantJOIN Parking ON CreditCardMerchant.ParkingId = Parking.IdJOIN DevTyp ON CreditCardMerchant.TerminalType = DevTyp.DeviceIdORDER by Parking.Dsc Results:TerminalId Dsc DeviceDesc ----------- -------------------- --------------------------- 4 Lot 5V Cashier station5 Lot 5V Cashier station14 Lot 6A/Ice Gardens Cashier station1 Lot PS-II Automated pay1 Lot PS-II Cashier station1 Lot PS-II Exit2 Lot PS-II Automated pay2 Lot PS-II Exit5 PS-III Automated pay6 PS-III Automated pay7 PS-III Automated pay8 PS-III Automated pay18 PS-III Cashier station31 PS-III Exit32 PS-III Exit33 PS-III Exit34 PS-III Exit3 York Lanes Automated pay4 York Lanes Automated pay11 York Lanes Cashier station20 York Lanes Exit21 York Lanes Exit(22 row(s) affected) See there is 22 distinct terminals.However when I Run this query:SELECT DISTINCT CreditCardMerchant.TerminalId, temp.DeviceDesc, temp.Parking_Lot, temp.Transaction_Count,temp.Total_SalesFROM CreditCardMerchantLEFT OUTER JOIN ( SELECT CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.Dsc As Parking_Lot, COUNT(*) As Transaction_Count, SUM (CreditCardTransaction.Transaction_Amount) as Total_SalesFROM CreditCardTransaction JOIN Parking ON CreditCardTransaction.ParkingId = Parking.Id JOIN DevTyp ON CreditCardTransaction.TerminalType = DevTyp.DeviceIdWHERE CreditCardTransaction.Approved <> 0 ANDCreditCardTransaction.Upload_DateTime >= DATEADD(d,DATEDIFF(d,0,GETDATE()-7),0) ANDCreditCardTransaction.Upload_DateTime <=DATEADD(d,DATEDIFF(d,0,GETDATE()),0) GROUP by CreditCardTransaction.TerminalId, DevTyp.DeviceDesc, Parking.Dsc)tempON temp.TerminalId = CreditCardMerchant.TerminalIdORDER by temp.Parking_Lot ASC, CreditCardMerchant.TerminalId ASC, temp.DeviceDesc ASC here are the results:TerminalId DeviceDesc Parking_Lot Transaction_Count Total_Sales ----------- ------------------------------ ------------------------------ ----------------- --------------------- 5 Cashier station Lot 5V 54 446.000014 Cashier station Lot 6A/Ice Gardens 1 10.00001 Automated pay Lot PS-II 200 1962.00001 Cashier station Lot PS-II 169 1741.00001 Exit Lot PS-II 168 1288.00002 Automated pay Lot PS-II 56 556.00002 Exit Lot PS-II 142 1343.00005 Automated pay PS-III 5 46.00006 Automated pay PS-III 279 2463.00007 Automated pay PS-III 248 2625.00008 Automated pay PS-III 7 62.000018 Cashier station PS-III 5 40.000031 Exit PS-III 106 929.000032 Exit PS-III 115 1082.000033 Exit PS-III 51 372.000034 Exit PS-III 208 1895.00003 Automated pay York Lanes 397 3808.50004 Automated pay York Lanes 248 2173.000011 Cashier station York Lanes 96 832.000020 Exit York Lanes 99 961.000021 Exit York Lanes 66 537.0000(21 row(s) affected) It only returns 21 terminals. Terminal #4 From 5V is missing(it has no transactions as it is out of service at the moment.)Any Thoughts? I really appreciate you taking the time to look at this for me..Thanks,Kevin |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-18 : 12:51:43
|
I cant understand how it will miss out records which exists in CreditCardMerchant when you use LEFT JOIN. it can only happen if you've some where caluse attached to your query. Are you sure you're using the same query or are you merging this with something else? |
 |
|
ould
Starting Member
6 Posts |
Posted - 2008-08-18 : 13:37:11
|
I am using just what I posted and am currently only using it in Query analyzer, I copied it straight from there into my post as I ran it. I am at a loss too as to why it isn't working.Kevin |
 |
|
|
|
|
|
|