SQL 如何计算每个辅助条目的条目数

问题描述 投票:0回答:1

我正在使用 postgres,而且对它很陌生。假设我有一张表,其中包含公司完成的每笔销售的 sales_id 编号,另一个表包含商店位置。我如何选择每个商店位置的销售数量? 具体来说,我将如何以一种可以将其输入到函数中而不是简单地显示它的方式来做到这一点?

即我想它看起来像这样:

CREATE FUNCTION get_dollars(cost numeric, COUNT(thePartImConfusedAbout) smallint)
sql postgresql
1个回答
0
投票
-- Create a function to get the number of sales per store location
CREATE OR REPLACE FUNCTION get_sales_per_store() RETURNS TABLE (store_location text, sales_count integer) AS $$
BEGIN
    RETURN QUERY
    SELECT store_location, COUNT(sales_id) AS sales_count
    FROM sales
    GROUP BY store_location;
END;
$$ LANGUAGE plpgsql;

-- Example usage of the function
SELECT * FROM get_sales_per_store();
© www.soinside.com 2019 - 2024. All rights reserved.