Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2004
    Posts
    5

    Unanswered: Combing queries - grouping by date ranges

    Masters,
    The below queries return the data that I seek, but I have no idea how to combine them into a single query.


    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [0 to 30]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) between '0' and '30'

    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [31 to 60]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) between '31' and '60'

    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [61 to 90]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) between '61' and '90'

    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [90+]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) > '90'



    Any assistance that can be provided will be greatly appreciated.
    Grasshopper

  2. #2
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    select [0 to 30], [31 to 60], [61 to 90], [90+]
    from (
    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [0 to 30]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) between '0' and '30' ) x1,
    (
    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [31 to 60]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) between '31' and '60' ) x2,
    (
    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [61 to 90]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) between '61' and '90' ) x3,
    (
    SELECT SUM(TOTALSVCAMT) - SUM(TOTALPAYMENTAMT) - SUM(TOTALADJAMT) as [90+]
    FROM MDM2
    WHERE DATEDIFF(day, SERVICEDATE, getdate()) > '90' ) x4
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

Posting Permissions

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