Domanda

I am a new user to MS Access. My table has 2 columns: a column for number of days which goes from 0 to 150+ and a column for principal paid (any number say 858576) There are over 70000 rows.

  1. Row 1 says 70 days and principal paid as 898956
  2. Row 2 says 68 days and principal paid as 13751
  3. Row 3 says 190 days and principal paid as 397159
  4. Row 4 says 11 days and principal paid as 56978
  5. Row 5 says 29 days and principal paid as 9078910

I want a query to return records from 0-30 days, 30-60 days, 60-90 days, 90-120 days, 120-150 days and 150 above and showing sum of principal against each group mentioned above. Can it be done? If so, how?

È stato utile?

Soluzione

If you know the maximum number of your days in the table and criteria for dividing into groups, you could try by using the case:

  SELECT
    SUM(principal_paid),
    days_range
  FROM
    (
      SELECT
        principal_paid,
        CASE days
          WHEN BETWEEN 0 AND 30
          THEN '0-30'
          WHEN BETWEEN 31 AND 60
          THEN '31-60'
          WHEN BETWEEN 61 AND 90
          THEN '61-90'
          WHEN BETWEEN 91 AND 120
          THEN '91-120'
          WHEN BETWEEN 121 AND 150
          THEN '121-150'
          ELSE 'over 150'
        END AS days_range
      FROM
        yourtable
    )
    as T
  GROUP BY
    days_range
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top