我有一张桌子。
l <- list(ab = 1, b = 2, c = 3)
l$a
#[1] 1
l[["a"]]
#NULL
l[["a", exact = FALSE]]
#[1] 1
简单的选择查询无法解决此问题
ID Contact_Details Pref
1 1234567890 A
1 test@test.com A
2 1234567891 A
2 test1@test.com B
我想要这样的结果:
select contact_details as mobile,contact_details as email
from table
where pref in (A,B);
请注意,我对电子邮件和移动设备的偏好不同,我希望在输出中使用它。
答案 0 :(得分:2)
您可以写:
select t1.ID, t1.Contact_Details as Mobile, t1.Pref as PrefMobile,
t2.Contact_Details as Email, t2.Pref as PrefEmail
from Table t1
inner join Table t2
on t1.ID = t2.ID
where t1.Contact_Details not like '%@%'
and t2.Contact_Details like '%@%'
答案 1 :(得分:2)
类似的事情会起作用:
SELECT Id,
MAX(CASE WHEN Contact_Details NOT LIKE '%@%' THEN Contact_Details END) as Mobile,
MAX(CASE WHEN Contact_Details NOT LIKE '%@%' THEN Pref END) as MobilePref,
MAX(CASE WHEN Contact_Details LIKE '%@%' THEN Contact_Details END) as EMAil,
MAX(CASE WHEN Contact_Details LIKE '%@%' THEN Pref END) as EMailPref
FROM T
GROUP BY ID
答案 2 :(得分:1)
DECLARE @Table TABLE (ID int, [Contact_Details] nvarchar(200), Pref char(1));
INSERT INTO @Table (ID,[Contact_Details], Pref)
VALUES
(1,'1234567890', 'A')
,(1,'test@test.com', 'A')
,(2,'1234567891','A')
,(2,'test1@test.com','B');
WITH Emails
AS (
SELECT ID, [Contact_Details], Pref
FROM
@Table
WHERE CHARINDEX('@',[Contact_Details])>0
)
, Mobils
AS (
SELECT ID, [Contact_Details], Pref
FROM
@Table
WHERE CHARINDEX('@',[Contact_Details])<1
)
SELECT ISNULL(Mobils.ID,Emails.ID) AS ID, Mobils.[Contact_Details] AS Mobile,Mobils.Pref AS PrefMobile, Emails.[Contact_Details] AS Email, Emails.Pref AS PrefEmail
FROM Mobils
FULL OUTER JOIN Emails
ON Mobils.ID=Emails.Id
此查询的结果:
ID Mobile PrefMobile Email PrefEmail
1 1234567890 A test@test.com A
2 1234567891 A test1@test.com B