2015-07-06 119 views
0

多個表獲取數據我有這個疑問從我的SQL Server 2012

SELECT 
    sa.MSISDN, 
    MIN(sa.DateRegistered) AS firstReg, 
    s.DateRegistered AS currentReg, 
    MAX(sa.DateRegistered) AS previousReg 
FROM 
    sms.dbo.SubscriptionsArchive sa 
INNER JOIN 
    sms.dbo.Subscriptions s ON sa.MSISDN = s.MSISDN 
GROUP BY 
    sa.MSISDN 

及其作爲s.DateRegistered錯並不在聚合函數使用,它不是在GROUP BY使用。

如何更正此查詢?

回答

2

將s.Date註冊到您的group-by子句

+0

是的,但這是正確的方式來獲得結果嗎?有沒有更好的辦法? – suman

+0

這是正確的方式 –

+0

好吧然後感謝和一件事情,這個查詢給出了結果,但我想確保這是做正確的方式'SELECT sr.ID,sr.ContentName AS服務,sa.MSISDN,MIN( sa.DateRegistered)AS firstReg,s.DateRegistered AS currentReg,MAX(sa.DateRegistered)AS previousReg FROM sms.dbo.SubscriptionsArchive sa INNER JOIN(SELECT DateRegistered,MSISDN,ServiceID from sms.dbo.Subscriptions)s ON sa.MSISDN = s.MSISDN INNER JOIN(SELECT * FROM sms.dbo.Services)sr ON sr.Id = s.ServiceID GROUP BY sa.MSISDN,s.DateRegistered,sr.ID,sr.ContentName' – suman