Sql 基于列中标志的视图

Sql view based on a flag in column

我在 SQL 非常忙碌的情况下感到震惊。 我有一个 table 和列 (name,is_right),我想获得如下视图:

Wrong Name|| Right Name 
Jass      ||  Jasu  

为 table

Name  || is_right
Jass  || 0  
Jasu  || 1

我正在尝试为此找到解决方案,但没有任何效果。 请帮助我或指导我完成任何示例,以便我可以选择它。

我假设您有用户的 memberID。 请假设@table 是您当前的 table 用户信息。我假设每个memberID只有一个值0和一个值1

declare @table table
(
  memberID nvarchar(10),
  name nvarchar(10),
  is_right integer
 )

 insert into @table values ('A','Jass',0);
 insert into @table values ('A','Jasu',1);
 insert into @table values ('B','Pata',0);
 insert into @table values ('B','Peter',1);

 select distinct 
 (select B.name from @table B where b.is_right = 0 and B.memberID = A.memberID) as 'wrong name',
 (select C.name from @table C where c.is_right = 1 and C.memberID = A.memberID) as 'right name'
 from @table A

这是什么return

名字错误|正确的名字

贾斯 |贾苏

巴塔 |彼得

使用 2 个子查询。这些将需要与 ID 链接才能将它们链接到主查询,但您尚未提供该数据。

SELECT 
(SELECT Name FROM yourtable WHERE is_right = 0) AS Wrong_Name,
(SELECT Name FROM yourtable WHERE is_right = 1) AS RIGHT_NAME
FROM yourtable