使用ORDER BY对“年龄组”进行排序(在chartio中使用postgresql)

使用ORDER BY对“年龄组”进行排序(在chartio中使用postgresql),sql,postgresql,Sql,Postgresql,我正在显示特定年龄组的用户数量。我使用Chartio,所以我有一个图形输出。我的问题是:输出是通过列的顺序来定向的。我希望我的年龄组和他们的人数按照你的逻辑预期排列:从小到大,最终没有给出任何信息。但是我不能按字母顺序排列,那样会把桌子弄得一团糟。我的工作是添加另一列,只用于将数值von 1排序为n,在该列上使用ORDER BY,然后使该列不可见-请参见下面的代码。然而,我正在寻找一个更好的方法来做到这一点;要么我有更好的方法使列有序,要么我有更好的方法使计数更好,这样我就不必担心排序了 SE

我正在显示特定年龄组的用户数量。我使用Chartio,所以我有一个图形输出。我的问题是:输出是通过列的顺序来定向的。我希望我的年龄组和他们的人数按照你的逻辑预期排列:从小到大,最终没有给出任何信息。但是我不能按字母顺序排列,那样会把桌子弄得一团糟。我的工作是添加另一列,只用于将数值von 1排序为n,在该列上使用ORDER BY,然后使该列不可见-请参见下面的代码。然而,我正在寻找一个更好的方法来做到这一点;要么我有更好的方法使列有序,要么我有更好的方法使计数更好,这样我就不必担心排序了

 SELECT "tmp"."type" AS "Type",
        count("tmp"."type") AS "Amount",
        "tmp"."order" AS "Order"
FROM 
(SELECT "ci0"."id" AS "Id",
        "us0"."date_of_birth" AS "Date_of_Birth",
        "us0"."last_seen_at" AS "Last_seen_at",
        "us0"."username" AS "username",
        "us0"."email" AS "email",
        CASE 
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 75
    THEN '75 years or older'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 65
    THEN '65-74 years old'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 55
    THEN '55-64 years old'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 45
    THEN '45-54 years old'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 35
    THEN '35-44 years old'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 25
    THEN '25-34 years old'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 18
    THEN '18-24 years old'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 12
    THEN '12-17 years old'
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") < 12
    THEN 'under 12 years old'
  ELSE 'No Birthdate given'
    END AS "type",
        CASE 
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 75
    THEN 9
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 65
    THEN 8
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 55
    THEN 7
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 45
    THEN 6
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 35
    THEN 5
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 25
    THEN 4
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 18
    THEN 3
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") >= 12
    THEN 2
  WHEN DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth") < 12
    THEN 1
  ELSE 10
    END AS "order"

FROM "public"."users" AS "us0"
INNER JOIN "public"."chat_infos" AS "ci0"
ON "ci0"."user_id" = "us0"."id"  
  WHERE ("us0"."last_seen_at" BETWEEN NOW() - INTERVAL '28 DAY' AND NOW())
  ) AS "tmp"

GROUP BY "type", "Order"
ORDER BY "Order"
LIMIT 1000;

您可以在一个小的查找表中输入年龄作为文本信息,使用日期\部件逻辑连接到该表,并在文本字段中排序

e、 g

您的查找表将

id
age_text_description
age_from
age_to
您的加入形式如下:

...on DATE_PART('year', now()) - DATE_PART('year', "us0"."date_of_birth")
between age_from and age_to
with age_groups(ag, description) as (
    values
    (int4range(0, 11, '[]'), 'under 12 years old'),
    (int4range(12, 17, '[]'), '12-17 years old'),
    (int4range(18, 24, '[]'), '18-24 years old'),
    (int4range(25, 34, '[]'), '25-34 years old'),
    (int4range(35, 44, '[]'), '35-44 years old'),
    (int4range(45, 54, '[]'), '45-54 years old'),
    (int4range(55, 64, '[]'), '55-64 years old'),
    (int4range(65, 74, '[]'), '65-74 years old'),
    (int4range(75, 999, '[]'), '75 years or older')
)
select age_group, "Amount"
from (
    select
        ag,
        coalesce(description, 'No Birthdate given') as age_group,
        count(*) as "Amount"
    from
        (
            select (
                date_part('year', now()) - date_part('year', us0.date_of_birth)
            )::int as age
            from
                users as us0
                inner join
                chat_infos as ci0 on ci0.user_id = us0.id
            where (us0.last_seen_at between now() - interval '28 day' and now())
        ) s
        left join
        age_groups on age <@ ag
    group by 1, 2
    order by ag
) s