简体   繁体   中英

CTE with multiple rows and tables

I have a problem with CTE, below my tables:

Table: Purchase

PID      szProductID     curQty        curPrice
1        001             10            100
2        001             30            200
3        001             50            300

and

Table: Sales

SID      szProductID     curQty        curSalesPrice
1        001             15            500
2        001             5             400
3        001             5             400
4        001             5             400
5        001             40            400

And I want to show those tables like below:

Result

SID      szProductID     curQty        curSalesPrice   curPurchasePrice
1        001             10            500             100
1        001             5             500             200
2        001             5             400             200
3        001             5             400             200
4        001             5             400             200
5        001             5             400             200
5        001             35            400             300

Basically, its just like a FIFO method..

I've found many CTE samples but they only compare many rows to one row, and sometimes i found an error : 'The statement terminated. The maximum recursion 100 has been exhausted before statement completion.' when i tried those samples...

Any help will be appreciated.

SQL will default cap out at 100 recursions unless you specify otherwise. You can get past 100 by using this at the end of your query.

OPTION (MAXRECURSION *NUMBER UP TO 32767 HERE*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM