Tricky SQL

Results 1 to 2 of 2

Thread: Tricky SQL

  1. #1
    Join Date
    Dec 1969

    Default Tricky SQL

    I need to create a query that joins a table to itself in a way that the the sum of this months values on the first table are joined to the sum of last months values for comparison purposes<BR><BR>Can I do this?<BR><BR>This is my table:<BR>tblAccounts<BR>ProductID, long integer, pk <BR>CompanyID, long integer, pk <BR>MonthYear date<BR>Balance<BR><BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default Join to yourself... easy enough.<BR><BR>Just use a different alias for each occurrence<BR><BR>SELECT P.ProductID, Sum(P.Balance) AS curBalance, SUM(S.Balance) AS lastBalance<BR>FROM tblAccounts AS P, tblAccounts AS S<BR>WHERE P.ProductID = S.ProductID<BR>AND Year(P.MonthYear) = 2002 AND Month(P.MonthYear) = 6<BR>AND Year(S.MonthYear) = 2002 AND Month(S.MonthYear) = 5<BR><BR>Untested, but something like that.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts