在下面的小提琴中,我有一个带有Data的表,如下所示
http://sqlfiddle.com/#!18/c687d
尝试根据电话类型分隔电话号码列。
以下是输入内容
| LastName | PhoneNameType | PhoneNumber |
|----------|---------------|-------------|
| KRANSLER | Work | 8326244229 |
| KRANSLER | Mobile | 7239876 |
| GILBERT | Work | 2121806 |
| GILBERT | Mobile | 8406582 |
| LITZ | Work | 3462590784 |
| LITZ | Mobile | 2816284631 |
输出应如下
| LastName | WorkNumber | MobileNumber |
|----------|------------|--------------|
| GILBERT | 2121806 | 8406582 |
| KRANSLER | 8326244229 | 7239876 |
| LITZ | 3462590784 | 2816284631 |
答案 0 :(得分:3)
具有条件聚合的另一个选项。 (当每个名称的每种类型最多包含一行时,按预期工作)
SELECT LastName
,max(case when phonenametype='Work' then phonenumber end) as worknumber
,max(case when phonenametype='Mobile' then phonenumber end) as mobilenumber
FROM tempo
GROUP BY LastName