0

私はSQLに変換できないという問題があります私は、転送前の状態を表す3つの列(P、L、T)のtable1のような2つのテーブルtable1とtable2があるとします

P1 L1 t1
P1 L1 t2
P1 L2 t3
P2 L5 t4
P2 L5 t5
P2 L6 t6

転送後を表す 3 列 (P、L、T) の表 2

P1 L3 t1
P1 L3 t2
P1 L4 t3 
P2 L15 t4
P2 L16 t5
P2 L16 t6

table1 と table2 の唯一の違いは列 L です。同じ P 列と T 列があります。同じ古い L に属する T が、L > 1 でグループ化された T の数が同じ新しい L に属しているように、p を選択したいと思います。

**Case** (table1)t1,t2 At L1(old L) and (Table2)t1, t2 At L3(new L). Count of T grouped by L =2  and t1, t2 belong to same L group then return P.

**Case** table1: t3 at L2 and table2: t3 at L4. Count of T grouped by L =1 then ignore P.

**Case** (table1)t4,t5 At L5(old L) and (Table2)t4, t5 At L15 and L16(new L). Count of T grouped by L =2  but t4, t5 belong to different L group then ignore P.

count(T) と Ts グループの両方を L で比較し、P を返す必要があります。

4

2 に答える 2

0

P、Tの無効な行を含むテーブル変数を作成し、table1.p、table1.Lでtable1 join table2グループから選択することで、この問題を解決しました

Drop table table1
Drop table Table2
create table table1
(
P bigint, L bigINt, T BigInt
)
create table table2
(
P bigint, L bigINt, T BigInt
)
Declare @Invalid_Ps table(P BIGINT, L BIGINT)
Insert INTO @Invalid_Ps (P, L)
Select a.Tbl1P ,a.Tbl1L
FROM
(Select tbl1.P as Tbl1P, tbl1.L as Tbl1L,Tbl2.L as Tbl2L , Tbl1.T as Tbl1T
from Table1 as tbl1 Join Table2 as tbl2
ON tbl1.P = Tbl2.P
Group By tbl1.P, tbl1.L,Tbl2.L, Tbl1.T
) a
 GROUp BY a.Tbl1P,a.Tbl1L
Having (SUM(a.Tbl2L) IS NULL OR count(distinct a.Tbl2L) <> 1)
于 2012-07-30T16:47:35.727 に答える
0
于 2012-07-31T05:35:36.460 に答える