MohitPandit

asked on

# SQL Server 2008: Query logic using CTE

Hello,

I've a staging table in which I've many rows with respect to booking number.

So, can I do in a single query using CTE to find out below concern? FYI, one way I can use cursor or some iterative loop. But I would like to use in single query using CTE.

--> as per below tables records, booking # 101, A needs to pay $3000 to B; So, first A paid to E = 1000 and A paid to C = 2000 (so that E & C now responsible to pay B).

So, E, C, D, F, X, Y, and Z all our mediator.

Below thing I need to achieve from this table:

1. How many mediator used in this booking and the amount they received from source point; actually they paid all amount to other mediator like A to E 1000 so, from E, it should be paid to other like D & F 500. It is same for C, X, Y & Z?

2. The amount should be delivered completely for each mediator i.e. it should not D = 500 and F = 490.

For e.g., (also, please find attached .png file here)

Note: Booking #, Source & Destination will be duplicate.

Booking#	Source	Destination	Mid Source	Mid Destination	Payment ($)

101	A	B	A	E	1000

101	A	B	A	C	2000

101	A	B	E	D	500

101	A	B	E	F	500

101	A	B	C	X	1000

101	A	B	C	Y	700

101	A	B	C	Z	300

101	A	B	X	B	1000

101	A	B	Y	B	700

101	A	B	Z	B	300

Best Regards,

Mohit Pandit

001-Snap.PNG

I've a staging table in which I've many rows with respect to booking number.

So, can I do in a single query using CTE to find out below concern? FYI, one way I can use cursor or some iterative loop. But I would like to use in single query using CTE.

--> as per below tables records, booking # 101, A needs to pay $3000 to B; So, first A paid to E = 1000 and A paid to C = 2000 (so that E & C now responsible to pay B).

So, E, C, D, F, X, Y, and Z all our mediator.

Below thing I need to achieve from this table:

1. How many mediator used in this booking and the amount they received from source point; actually they paid all amount to other mediator like A to E 1000 so, from E, it should be paid to other like D & F 500. It is same for C, X, Y & Z?

2. The amount should be delivered completely for each mediator i.e. it should not D = 500 and F = 490.

For e.g., (also, please find attached .png file here)

Note: Booking #, Source & Destination will be duplicate.

Booking#	Source	Dest

101	A	B	A	E	

101	A	B	A	C	

101	A	B	E	D	

101	A	B	E	F	

101	A	B	C	X	

101	A	B	C	Y	

101	A	B	C	Z	

101	A	B	X	B	

101	A	B	Y	B	

101	A	B	Z	B	

Best Regards,

Mohit Pandit

001-Snap.PNG

Should there not also be these two rows?

101 A B D B 500

101 A B F B 500

101 A B D B 500

101 A B F B 500

Need more explanation...

ASKER

I need to put validations either respective booking is valid or not i.e. A needs to pay $3000 to B and the amount mediator receives then the same amount should travel to other mediator and at last the destination i.e. B.

Let's take A (source) paid E (mediator) 1000 then E paid to D & F (mediator) 500 each.

--> in that case E receives 1000 and same amount he paid to E & D i.e. 500 each.

I apologize, two more records should be in table; added in below table in last (marked with *).

Booking# Source Destination Mid Source Mid Destination Payment ($)

101 A B A E 1000

101 A B A C 2000

101 A B E D 500

101 A B E F 500

101 A B C X 1000

101 A B C Y 700

101 A B C Z 300

101 A B X B 1000

101 A B Y B 700

101 A B Z B 300

101 A B D B 500 *

101 A B F B 500 *

Similar A paid to C and C paid to X,Y, & Z and X,Y and Z paid to B. So, I need to put validations the amount given by source/mid source then it should be same travel at destination/mid destination otherwise I need to put an alert to end user.

I hope, it should clear now. Kindly let me know in case still not clear.

Let's take A (source) paid E (mediator) 1000 then E paid to D & F (mediator) 500 each.

--> in that case E receives 1000 and same amount he paid to E & D i.e. 500 each.

I apologize, two more records should be in table; added in below table in last (marked with *).

Booking# Source Destination Mid Source Mid Destination Payment ($)

101 A B A E 1000

101 A B A C 2000

101 A B E D 500

101 A B E F 500

101 A B C X 1000

101 A B C Y 700

101 A B C Z 300

101 A B X B 1000

101 A B Y B 700

101 A B Z B 300

101 A B D B 500 *

101 A B F B 500 *

Similar A paid to C and C paid to X,Y, & Z and X,Y and Z paid to B. So, I need to put validations the amount given by source/mid source then it should be same travel at destination/mid destination otherwise I need to put an alert to end user.

I hope, it should clear now. Kindly let me know in case still not clear.

Check below code... below query can solve your problem.. you can write it in your way...

even you can use CTE if you are working with multiple sources and destinations...

even you can use CTE if you are working with multiple sources and destinations...

```
Declare @ta table (Booking int, Source char(2) , Destination char(2) , [Mid Source] char(2) , [Mid Destination] char(2) , Payment int)
insert into @ta
select 101 , 'A' ,'B', 'A' , 'E' ,1000
union all select 101 , 'A' ,'B', 'A' ,'C' , 2000
union all select 101 , 'A' ,'B', 'E' ,'D' , 500
union all select 101 , 'A' ,'B', 'E' ,'F' , 500
union all select 101 , 'A' ,'B', 'C' ,'X' , 1000
union all select 101 , 'A' ,'B', 'C' ,'Y' , 700
union all select 101 , 'A' ,'B', 'C' ,'Z', 300
union all select 101 , 'A' ,'B', 'X' ,'B', 1000
union all select 101 , 'A' ,'B', 'Y' ,'B', 700
union all select 101 , 'A' ,'B', 'Z' ,'B', 300
union all select 101 , 'A' ,'B', 'D' ,'B', 500
union all select 101 , 'A' ,'B', 'F' ,'B', 50
select * from @ta
select t.*, case when x.payment<>t.Payment then 'Payment Mistmatch ' else '' end as [status] from @ta t join
(select booking,Source,Destination,[Mid Source] , SUM(Payment) as payment from @ta
group by booking,Source,Destination,[Mid Source]) x
on t.Booking=x.booking and t.Source=x.Source and t.Destination=x.Destination and t.[Mid Destination]=x.[Mid Source]
```

Sauv,

I had prepared something similar. It might suffice, but for one point - if a transaction is completely missing, it does not appear. E.g. if E made no transfer, A -> E with 1000 will not appear. Using a Left Join (and applying isnull to x.payment) should help.

Additionally I would also output the missing amount.

I had prepared something similar. It might suffice, but for one point - if a transaction is completely missing, it does not appear. E.g. if E made no transfer, A -> E with 1000 will not appear. Using a Left Join (and applying isnull to x.payment) should help.

Additionally I would also output the missing amount.

ASKER

Hi Saurv,

I checked the code and looks good. Actually, I can have 30k records in a file so it would good if we will use CTE. Could you please share your thoughts?

I also noted Qlemo point for using LEFT OUTER JOIN.

Thanks

Best Regards,

Mohit Pandit

I checked the code and looks good. Actually, I can have 30k records in a file so it would good if we will use CTE. Could you please share your thoughts?

I also noted Qlemo point for using LEFT OUTER JOIN.

Thanks

Best Regards,

Mohit Pandit

Your original request is still not clear. If you have 30k records, what do you expect as output from them? Are interested in just in something like

```
Booking Source Dest Total Remaining NoMediators
101 A B 3000 0 7
```

and then need a query to drill-down to the mediators not having distributed the received money?
SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Hi Saurv,

I'll take a look over it and let you update after that.

Thanks

Best Regards,

Mohit Sharma

I'll take a look over it and let you update after that.

Thanks

Best Regards,

Mohit Sharma

ASKER CERTIFIED SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Thank you very much mark_wills for sharing your great thoughts. I'll try to dig it and let you update after that.

Thanks again!

Thanks again!

ASKER

Excellent!

I mean a sample output dataset as per above data.... and also some more explanation....