如果你只有兩個愛好,那麼你可以使用min()
和max()
:
select email, min(preference) as preference1,
(case when min(preference) <> max(preference) then max(preference) end) as preference2
from t
group by email;
編輯:
如果您有多達七個值,然後轉動使用row_number()
:
select email,
max(case when seqnum = 1 then preference end) as preference1,
max(case when seqnum = 2 then preference end) as preference2,
max(case when seqnum = 3 then preference end) as preference3,
max(case when seqnum = 4 then preference end) as preference4,
max(case when seqnum = 5 then preference end) as preference5,
max(case when seqnum = 6 then preference end) as preference6,
max(case when seqnum = 7 then preference end) as preference7
from (select t.*, row_number() over (partition by email order by preference) as seqnum
from t
) t
group by email;
編輯二:
你其實可以與相關子查詢,而不是row_number()
做到這一點:
select email,
max(case when seqnum = 1 then preference end) as preference1,
max(case when seqnum = 2 then preference end) as preference2,
max(case when seqnum = 3 then preference end) as preference3,
max(case when seqnum = 4 then preference end) as preference4,
max(case when seqnum = 5 then preference end) as preference5,
max(case when seqnum = 6 then preference end) as preference6,
max(case when seqnum = 7 then preference end) as preference7
from (select t.*,
(select count(*)
from t t2
where t2.email = t.email and
t2.preference <= t.preference
) as seqnum
from t
) t
group by email;
你好 - 偉大的想法。我可以在75%的記錄中使用這種方法,但約25%的記錄有多達7種偏好。所以7是我的上限。 – user3126487
再次感謝 - 不幸的是我收到錯誤(156)關鍵字'over'附近的語法不正確。我曾嘗試過各種各樣的事情。我想知道row_number()是否也不被支持。感謝您的幫助....分析師對於sybase而言相對較新。 – user3126487
您應該用您正在使用的Sybase版本標記您的問題。 –