hỏi về câu lệnh T-SQL

Liên hệ QC

SoiBien

Thành viên tiêu biểu
Tham gia
3/9/07
Bài viết
734
Được thích
609
hi all,cho mình hỏi về cách truy vấn này với :
mình có tableA như sau :
PK, debit, Credit, account
1, 1, null, Account1
2, null, 1, Account1
3, 1, null, Account1
mình muốn select các cặp có debit = credit, account = account, nhưng chỉ select số lần xuất hiện tương ứng nhau thôi, như table A o trên thì chỉ chọn 2 record đầu, không chọn record thứ 3. Tks các bạn.

Hoặc chính xác trong trường hợp này thì mình cần select record thứ 3, các cặp tương ứng như các record 1-2 thì loại bỏ.

Câu lệnh hiện tại mình đang test :
SELECT [TableA].*
FROM [TableA] LEFT JOIN
(SELECT [TableA_3].*
FROM [TableA] AS [TableA_3] INNER JOIN
[TableA] AS [TableA_1]
ON [TableA_3].debit = [TableA_1].credit
AND [TableA_3].account = [TableA_1].account)
AS [TableA_2]
ON [TableA].pk = [TableA_2].pk
where [TableA_2].pk is NULL

Nhưng câu trên thì chẳng select được record nào (vì TableA_2 chứa cả 3 records) :=\+
 
Lần chỉnh sửa cuối:
Đây là câu lệnh mình cần (nhờ hỗ trợ của các bạn khác) mình post lên đây cho ai cần tham khảo nhé.

Mã:
[COLOR=#000088][FONT=inherit]With[/FONT][/COLOR][COLOR=#000000][FONT=inherit] cteDebit [/FONT][/COLOR][COLOR=#000088][FONT=inherit]As
[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000088][FONT=inherit]Select[/FONT][/COLOR][COLOR=#000000][FONT=inherit] PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] debit[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Credit[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] account[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Row_Number[/FONT][/COLOR][COLOR=#666600][FONT=inherit]()[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Over[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]Partition [/FONT][/COLOR][COLOR=#000088][FONT=inherit]By[/FONT][/COLOR][COLOR=#000000][FONT=inherit] account[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] debit [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Order[/FONT][/COLOR][COLOR=#000088][FONT=inherit]By[/FONT][/COLOR][COLOR=#000000][FONT=inherit] PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit]) [/FONT][/COLOR][COLOR=#000088][FONT=inherit]As[/FONT][/COLOR][COLOR=#000000][FONT=inherit] rn 
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]From[/FONT][/COLOR][COLOR=#000000][FONT=inherit] TableA
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Where[/FONT][/COLOR][COLOR=#000000][FONT=inherit] debit [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Is [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Not [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Null[/FONT][/COLOR][COLOR=#666600][FONT=inherit])
,[/FONT][/COLOR][COLOR=#000000][FONT=inherit]cteCredit [/FONT][/COLOR][COLOR=#000088][FONT=inherit]As
[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000088][FONT=inherit]Select[/FONT][/COLOR][COLOR=#000000][FONT=inherit] PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] debit[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Credit[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] account[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Row_Number[/FONT][/COLOR][COLOR=#666600][FONT=inherit]()[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Over[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]Partition [/FONT][/COLOR][COLOR=#000088][FONT=inherit]By[/FONT][/COLOR][COLOR=#000000][FONT=inherit] account[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Credit [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Order[/FONT][/COLOR][COLOR=#000088][FONT=inherit]By[/FONT][/COLOR][COLOR=#000000][FONT=inherit] PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit]) [/FONT][/COLOR][COLOR=#000088][FONT=inherit]As[/FONT][/COLOR][COLOR=#000000][FONT=inherit] rn 
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]From[/FONT][/COLOR][COLOR=#000000][FONT=inherit] TableA
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Where[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Credit [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Is [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Not [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Null[/FONT][/COLOR][COLOR=#666600][FONT=inherit])
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Select [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Coalesce[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit]) [/FONT][/COLOR][COLOR=#000088][FONT=inherit]As[/FONT][/COLOR][COLOR=#000000][FONT=inherit] PK
[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Coalesce[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]debit[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]debit[/FONT][/COLOR][COLOR=#666600][FONT=inherit]) [/FONT][/COLOR][COLOR=#000088][FONT=inherit]As[/FONT][/COLOR][COLOR=#000000][FONT=inherit] debit
[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Coalesce[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]Credit[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]Credit[/FONT][/COLOR][COLOR=#666600][FONT=inherit]) [/FONT][/COLOR][COLOR=#000088][FONT=inherit]As[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Credit
[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Coalesce[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]account[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]account [/FONT][/COLOR][COLOR=#666600][FONT=inherit])[/FONT][/COLOR][COLOR=#000088][FONT=inherit]As[/FONT][/COLOR][COLOR=#000000][FONT=inherit] account
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]From[/FONT][/COLOR][COLOR=#000000][FONT=inherit] cteDebit d [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Full[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Outer[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Join[/FONT][/COLOR][COLOR=#000000][FONT=inherit] cteCredit c 
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]On[/FONT][/COLOR][COLOR=#000000][FONT=inherit] c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]account [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]account [/FONT][/COLOR][COLOR=#000088][FONT=inherit]And[/FONT][/COLOR][COLOR=#000000][FONT=inherit] c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]Credit [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]debit [/FONT][/COLOR][COLOR=#000088][FONT=inherit]And[/FONT][/COLOR][COLOR=#000000][FONT=inherit] c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]rn [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]rn
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Where[/FONT][/COLOR][COLOR=#000000][FONT=inherit] c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]PK [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Is [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Null [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Or[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]PK [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Is [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Null 
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]Order[/FONT][/COLOR][COLOR=#000088][FONT=inherit]By [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Coalesce[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]c[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] d[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]PK[/FONT][/COLOR][COLOR=#666600][FONT=inherit]);
[/FONT][/COLOR]
 
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom