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 |
|
rcp
Starting Member
32 Posts |
Posted - 2012-01-18 : 09:47:49
|
| Hi I am stuck, I am trying to get the unmatched from one table then get the unmatched from an other table with certain criteria. This is the query so farWITHORDERTABLEAS(SELECT order_id FROM CUSTORDERSWHERE CONVERT(date,created_date,101) = '2011-12-05'and [status] IN ('paid','preauth','refund'))SELECT ORDERTABLE.*FROM ORDERTABLELEFT Join (WITHT1CountsAS(SELECT * FROM [orp].[dbo].[SAMPLE]WHERE CONVERT(date,created_date,101) = '2011-12-05'),T2CountsAS(SELECT * FROM [orp].[dbo].[SAMPLE]WHERE CONVERT(date,created_date,101) = '2011-12-05'and paymentstatus like 'completed')SELECT distinct(T1.order_id) AS FailuresSAMPLEFROM T1Counts T1LEFT JOIN T2Counts T2ON T1.order_id=T2.order_idWHERE T2.order_id is null) F1on ORDERTABLE.order_id=F1.FailuresSAMPLEWHERE FailuresSAMPLE is nullRegards,Roland |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-01-18 : 09:57:14
|
your CTE placement is messed up. This is what I think you were trying to write (note that you must declare all the CTE's up front before using them).;WITH ORDERTABLE AS ( SELECT order_id FROM CUSTORDERS WHERE CONVERT(date,created_date,101) = '2011-12-05' and [status] IN ('paid','preauth','refund') ),T1Counts AS ( SELECT * FROM [orp].[dbo].[SAMPLE] WHERE CONVERT(date,created_date,101) = '2011-12-05' ),T2Counts AS ( SELECT * FROM [orp].[dbo].[SAMPLE] WHERE CONVERT(date,created_date,101) = '2011-12-05' and paymentstatus like 'completed' )SELECT ORDERTABLE.*FROM ORDERTABLE LEFT Join ( SELECT distinct(T1.order_id) AS FailuresSAMPLE FROM T1Counts T1 LEFT JOIN T2Counts T2 ON T1.order_id=T2.order_id WHERE T2.order_id is null ) AS F1 on ORDERTABLE.order_id=F1.FailuresSAMPLEWHERE FailuresSAMPLE is nullHowever, there are *many* things suspect about this code.1) Don't do stuff like this:quote: CONVERT(date,created_date,101) = '2011-12-05'
By wrapping the column in a function it is no longer sargeable (you can't use any index on the column). I guess you must be using some weird type for dates. either change the column to a date type or do the manipulation on the variable ('2011-12-05') to put it into a format directly comparable to the column.2) This:LEFT Join ( SELECT distinct(T1.order_id) AS FailuresSAMPLE FROM T1Counts T1 LEFT JOIN T2Counts T2 ON T1.order_id=T2.order_id WHERE T2.order_id is null ) AS F1 on ORDERTABLE.order_id=F1.FailuresSAMPLEWHERE FailuresSAMPLE is null What you are doing here is checking that something DOES NOT exists for a given predicate. This would be much better written as a NOT EXISTS clause. However, I can't be bothered to rewrite it. Doing the check they way you are is quite expensive compared to a NOT EXISTS alternative. Also it reads better because you are declaratively stating what you want.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
rcp
Starting Member
32 Posts |
Posted - 2012-01-18 : 11:02:27
|
| X002548, thanks for this I have written a query using your example. Charlie, I will try rewriting the query to use NOT EXIST. Thanks for you help,Roland |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-01-18 : 11:09:03
|
| My first point was actually more important.Don't manipulate columns in the where clause (or a join condition)Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
rcp
Starting Member
32 Posts |
Posted - 2012-01-18 : 12:31:36
|
| WITHT1CountsAS(SELECT * FROM [orp].[dbo].[paypal_log]WHERE CONVERT(date,created_date,101) = '2011-12-05'),T2CountsAS(SELECT * FROM [orp].[dbo].[paypal_log]WHERE CONVERT(date,created_date,101) = '2011-12-05'and paymentstatus like 'completed')SELECT distinct(T1.order_id) AS FailuresPayPalFROM T1Counts T1LEFT JOIN T2Counts T2ON T1.order_id=T2.order_idWHERE T2.order_id is nullThen the results from this need to match onto a table called customer_order with criteria where [status] IN ('Paid','preAuth') and again return the NOT EXISTS values.I hope this help.Regards,Roland |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-01-18 : 12:38:56
|
| [code] SELECT a.*, b.*, c.* FROM customer_order a LEFT JOIN (SELECT * FROM [orp].[dbo].[paypal_log] WHERE CONVERT(date,created_date,101) = '2011-12-05') AS b ON a.Order_Id = b.Order_ID LEFT JOIN (SELECT * FROM [orp].[dbo].[paypal_log] WHERE CONVERT(date,created_date,101) = '2011-12-05' AND paymentstatus like 'completed') AS c ON a.Order_Id = c.Order_Id WHERE (b.Order_Id IS NULL OR c.Order_IdIS NULL) AND a.[status] IN ('Paid','preAuth') [/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
rcp
Starting Member
32 Posts |
Posted - 2012-01-18 : 12:52:08
|
| Hi Brett, thanks agian for the response. In the end I would like to do counts on the number of orders that failed our security. WITHT1CountsAS(SELECT distinct order_idFROM [orp].[dbo].[paypal_log]WHERE CONVERT(date,created_date,101) = '2011-12-05'and (paymentstatus <> 'completed' OR paymentstatus is null)),T2CountsAS(SELECT distinct order_idFROM [orp].[dbo].[paypal_log]WHERE CONVERT(date,created_date,101) = '2011-12-05'and paymentstatus like 'completed')SELECT T1.order_idFROM T1Counts T1LEFT JOIN T2Counts T2ON T1.order_id=T2.order_idWHERE T2.order_id is nullThis tells me all orders that went through paypal and failed. They can then try a different payment method of payment. So if they get a [status] in customer_orders that is ('Paid','preAuth') with the same order_id then they have managed to make a payment. Everyone else left overe in the above query is a true fail. All I am really after is numbers of failures not really detail, but am using the deatil to do checks first.Regards,Roland |
 |
|
|
|
|
|
|
|