简体   繁体   English

从另一个表中获取“分组依据”参数列名/别名

[英]Get "group by" parameter column name/ alias from another table

I have a table podcast .我有一个桌面podcast Each podcast has category and subcategory associated to it in the form of ids.每个播客都有以 id 形式与之关联的类别和子类别。

在此处输入图片说明

This category_id and subcategory_id have their name values in their respective tables -这个 category_id 和 subcategory_id 在它们各自的表中有它们的名称值 -

在此处输入图片说明 在此处输入图片说明

Now, I want to get the count of podcasts under each category, subcategory combination -现在,我想获取每个类别、子类别组合下的播客数量 -

SELECT podcast_category_id, podcast_subcategory_id, count(*)
FROM podcasts
where podcast_owner = 14 AND podcast_upload_time_stamp >= timestamp '2020-10-22 00:00:00'
GROUP BY podcast_category_id, podcast_subcategory_id

Output -输出 -

在此处输入图片说明

The output shows podcast_category_id and podcast_subcategory_id columns as expected.输出按预期显示podcast_category_idpodcast_subcategory_id列。 However, I want to replace podcast_category_id with category_name and podcast_subcategory_id with sub_category_name .不过,我想替换podcast_category_idcategory_namepodcast_subcategory_idsub_category_name How do I do that?我怎么做?

Join to those tables.加入这些表。

    SELECT c.category_name, sc.sub_category_name, count(p.*)
    FROM podcasts p
    join categories c
      on c.category_id = p.podcast_category_id
    join sub_categories sc
      on sc.sub_category_id = p.podcast_subcategory_id
    where p.podcast_owner = 14 
      AND p.podcast_upload_time_stamp >= timestamp '2020-10-22 00:00:00'
    GROUP BY 1,2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM