你的意思是最短的時間?誰已在upgrade_date訂單的最早時間升級?
SELECT TOP 10 s.ID,
s.Name,
su.UpgradeLevel,
su.UpgradeDate
FROM Subscribers s
INNER JOIN SubscribersUpgrades su on s.ID = su.SubscriberId
WHERE su.UpgradeLevel = 3 AND su.UpgradeDate BETWEEN '01-April-2014' AND '20-May-2016'
ORDER BY su.UpgradeDate ASC
UPDATE
如果你只需要2升級級別(級別2和3)這應該工作數據 -
SELECT TOP 10 s.Name, DATEDIFF(dd,t1.UpgradeDate,t2.UpgradeDate) UpgradePeriodInDays FROM
(SELECT su.SubscriberId, su.UpgradeDate FROM SubscribersUpgrades su
WHERE su.UpgradeLevel = 2) t1
INNER JOIN (SELECT su.SubscriberId, su.UpgradeDate FROM SubscribersUpgrades su
WHERE su.UpgradeLevel = 3) t2 on t1.SubscriberId = t2.SubscriberId
INNER JOIN Subscribers s on t1.SubscriberId = s.ID
WHERE t2.UpgradeDate BETWEEN '01-June-2016' AND '20-July-2016'
ORDER BY DATEDIFF(dd,t1.UpgradeDate,t2.UpgradeDate) ASC
您正在使用哪個數據庫管理系統? –
缺省值爲SQL –
「SQ」是一種查詢語言,而不是DBMS。每個關係數據庫都使用SQL作爲查詢語言。 –