0

I have this query

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

And its wrong as s.DateRegistered is not used in aggregate function and it is not used in group by.

How do I correct this query?

1 Answer 1

2

Add s.DateRegistered to your group-by clause

Sign up to request clarification or add additional context in comments.

5 Comments

Yea that works but is this the correct way to get the result ? Is there a better way?
Ok then thanks and one more thing, this query gives result but i want to make sure this is the correct way to do SELECT sr.ID, sr.ContentName AS service, 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
Any column that you reference in the query must be in the group-by clause unless they are inside aggregate functions
Your best bet is to run it and see
Yea it works. I just want to make sure this way is a proper way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.