-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
migrations/supabase/migrations/20241224030938_remote_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
set check_function_bodies = off; | ||
|
||
CREATE OR REPLACE FUNCTION public.analyze_user_token_usage(start_date date, end_date date) | ||
RETURNS TABLE(bot_id text, usage_date date, input_tokens bigint, output_tokens bigint, total_tokens bigint) | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT | ||
u.bot_id AS bot_id, | ||
u.date AS usage_date, -- 使用别名来避免歧义 | ||
SUM(u.input_token)::BIGINT AS input_tokens, -- 将结果转换为 BIGINT | ||
SUM(u.output_token)::BIGINT AS output_tokens, -- 将结果转换为 BIGINT | ||
SUM(u.total_token)::BIGINT AS total_tokens -- 将结果转换为 BIGINT | ||
FROM user_token_usage u | ||
WHERE | ||
u.date >= start_date AND | ||
u.date <= end_date | ||
GROUP BY u.date, u.bot_id; | ||
END; | ||
$function$ | ||
; | ||
|
||
|