开发者

table join where

开发者 https://www.devze.com 2022-12-12 08:28 出处:网络
Table1 t10 (id) id --- 1 2 table t11(a1,a2,a3) a1a2a3 ---------- 110a 110b 111b 112c 220d 221e select * from t10 a,t11 b where a.id = b.a开发者_C百科1

Table1 t10 (id)

id
---
1
2

table t11(a1,a2,a3)

a1  a2  a3
----------
1   10   a
1   10   b
1   11   b
1   12   c
2   20   d
2   21   e

select * from t10 a,t11 b where a.id = b.a开发者_C百科1 how to display

id  a1  a2  a3
--------------
1   1   10  a
1   1   10  b //(not display this row)
1   1   11  b //(not display this row)
1   1   12  c //(not display this row) 
2   2   20  d
2   2   21  e //(not display this row)

just get t11's random row

maybe display this
id  a1  a2 
---------- 
1   1   11  b
1   1   10  a //(not display this row)
1   1   10  b //(not display this row)
1   1   12  c //(not display this row) 
2   2   20 
2   2   21  //(not display this row) 


select a1 as id, a1, min(a2) as a2
from t11
group by a1

will give you:

id  a1  a2
----------
1   1   10
2   2   20


This seems almost like he wants something like FIRST/LAST from ms access.

This can ben done (very closely) in Sql Server using

DECLARE @Table TABLE(
        id INT,
        a1 INT,
        a2 INT
)

INSERT INTO @Table (id,a1,a2) SELECT 1, 1, 11
INSERT INTO @Table (id,a1,a2) SELECT 1, 1, 10
INSERT INTO @Table (id,a1,a2) SELECT 1, 1, 12
INSERT INTO @Table (id,a1,a2) SELECT 2, 2, 20
INSERT INTO @Table (id,a1,a2) SELECT 2, 2, 21

SELECT  *
FROM    @Table t
WHERE   a2 = (SELECT TOP 1 a2 FROM @Table WHERE id = t.id AND a1 = t.a1)


This is the answer:

SELECT *
  FROM t10 a, (
       SELECT * FROM (
           SELECT b.*, ROW_NUMBER() OVER(PARTITION BY a10 ORDER BY a10) as rn
           FROM t11 b
       ) WHERE rn =1) b
 WHERE a.id = b.a10(+)
0

精彩评论

暂无评论...
验证码 换一张
取 消