SQL相同值但不同的辅助值

时间:2016-10-28 13:20:30

标签: sql sql-server join count

我已经尝试了几个小时和不同的论坛,但我无法理解这一点。我尝试了加入和各种各样的但是我经常得到错误的结果。

我的示例表如下所示

Item Branch  Type
A    Main    Box
A    London  Single
A    Paris   Single
A    Tokyo   Box
B    Main    Single
B    London  Single
B    Paris   Single
C    Main    Single
C    London  Box
C    Paris   Single
C    Tokyo   Single
D    Main    Box
D    London  Single
D    Paris   Box
D    Tokyo   Box
D    Sydney  Single

我想得到的是因为我的结果集是所有与主要'分支'具有不同'类型'的项目

例如,如果Main Type为Box,则显示Main分支和具有不同类型的其他分支

结果表应如下所示。

Item Branch  Type
**A    Main    Box**
A    London  Single
A    Paris   Single
**C    Main    Single**
C    London  Box
**D    Main    Box**
D    London  Single
D    Sydney  Single

非常感谢任何帮助。

感谢

3 个答案:

答案 0 :(得分:1)

这是获取任何分支上的类型与主分支不同的行的一种方法。

select * 
from t t1 
where (branch='Main' or (branch <> 'Main' 
                         and exists (select 1 from t 
                                     where item=t1.item and branch = 'Main' 
                                     and type <> t1.type)
                         )
       )
and item in (select item from t group by item having count(distinct type) > 1)

如果上面的查询看起来令人困惑,您可以使用cte来获取其类型与项目的主分支不同的所有非主分支。然后使用union all获取这些项的主分支行。

with y as (
select * from t t1
where branch <> 'Main' and exists (select 1 from t 
                                   where item=t1.item and branch = 'Main' 
                                   and type <> t1.type)
    )
select * from t where branch='Main' and item in (select item from y)
union all
select * from y

答案 1 :(得分:0)

select A.item, A.branch, A.type 
from ibt A, ibt B 
on A.item = B.item and B.branch = "Main" and A.type != B.type;

结果如下:

A|London|Single
A|Paris|Single
C|London|Box
D|London|Single
D|Sydney|Single

答案 2 :(得分:0)

我不确定我是否明白你需要什么。 但是,这是一个可能的部分解决方案。 您可以根据自己的要求改进这一过程。

postgre中的样本。

CREATE TEMPORARY  TABLE items (
  item varchar(10),
  branch varchar(10),
  type varchar(10)
);


INSERT INTO items VALUES ('A', 'Main', 'Box');
INSERT INTO items VALUES ('A', 'London', 'Single');
INSERT INTO items VALUES ('A', 'Paris', 'Single');
INSERT INTO items VALUES ('A', 'Tokyo', 'Box');
INSERT INTO items VALUES ('B', 'Main', 'Single');
INSERT INTO items VALUES ('B', 'London', 'Single');
INSERT INTO items VALUES ('B', 'Paris', 'Single');
INSERT INTO items VALUES ('C', 'Main', 'Single');
INSERT INTO items VALUES ('C', 'London', 'Box');
INSERT INTO items VALUES ('C', 'Paris', 'Single');
INSERT INTO items VALUES ('C', 'Tokyo', 'Single');
INSERT INTO items VALUES ('D', 'Main', 'Box');
INSERT INTO items VALUES ('D', 'London', 'Single');
INSERT INTO items VALUES ('D', 'Paris', 'Box');
INSERT INTO items VALUES ('D', 'Tokyo', 'Box');
INSERT INTO items VALUES ('D', 'Sydney', 'Single');

SELECT
CASE WHEN T.branch = 'Main' THEN rpad(T.item, length(T.item) + T.children::INT, '*') ELSE T.item END item,
T.branch,
T.type
FROM (
SELECT
  I.item,
  I.branch,
  I.type,
  MAIN_ITEMS.item                              mitem,
  MAIN_ITEMS.branch                            mbranch,
  MAIN_ITEMS.type                              mtype,
  (SELECT count(j.item)
   FROM items j
   WHERE I.ITEM = j.item AND I.type <> j.type) children    FROM items I
  JOIN (
         SELECT
           item,
           branch,
           type
         FROM items
         WHERE branch = 'Main'

       ) MAIN_ITEMS
    ON ((I.ITEM = MAIN_ITEMS.item AND I.type <> MAIN_ITEMS.type) OR (
    I.ITEM = MAIN_ITEMS.item AND I.branch = MAIN_ITEMS.branch AND I.type = MAIN_ITEMS.type
  )
    )
) T
where T.children > 0;

输出:

A** Main    Box
A   London  Single
A   Paris   Single
C*  Main    Single
C   London  Box
D** Main    Box
D   London  Single
D   Sydney  Single

我希望这个示例可以帮助您。