如何一起使用 INTO 和 GROUP BY 子句
How to use INTO and GROUP BY clause together
SELECT cast ( SUBSTRING ( CAST ("ProcessingDate" AS text), 5, 2 ) as integer),
COUNT(*) INTO resultValue1,resultValue2
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y' AND "ProcessingDate" >= 20160110
GROUP BY 1
ORDER BY 1;
在我的数据库中,ProcessingDate
存储为 YYYYMMDD。所以,我从中提取月份。
如果我们删除 INTO
子句,此查询工作正常,但是,我想存储结果以进一步使用它。
那么变量的数据类型应该是什么resultValue1
和resultValue2
如何存储数据(因为数据将是多个)。
由于我是 PostgreSQL 的新手,我不知道该怎么做,谁能帮帮我。
试试这个:
SELECT cast ( SUBSTRING ( cast ("ProcessingDate" as text),5 , 2 ) as integer)resultValue1,
COUNT(*)resultValue2
INTO <Table Name>
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y'
AND "ProcessingDate" >= '20160110'
Group By 1
Order By 1;
此处 resultValue1
& resultValue2
将是表格而不是变量。您可以使用列名进行分组。
使用它们为列和组提供一些列别名。
你可能想要这个。
SELECT cast ( SUBSTRING ( cast ("ProcessingDate" as text),5 , 2 ) as
integer) AS resultValue1, COUNT(*) AS resultValue2
INTO <NewTable> --NewTable will be created with those two columns
FROM "DemoLogs"."Project"
-- conditions
Group By 1
-- other contitions/clauses
;
请参考此INTO Documentation。
希望这对您有所帮助。
将上面的 query
存储在变量中并从中删除 INTO
子句。
所以,查询现在是:
query = SELECT cast ( SUBSTRING ( CAST ("ProcessingDate" AS text), 5, 2 ) as
integer),
COUNT(*)
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y' AND "ProcessingDate" >= 20160110
GROUP BY 1
ORDER BY 1;
现在声明 result_data 类型 record
并按以下方式使用:
我们正在循环 result_data
因为它给出了行数作为输出
并且我已经声明 resultset
类型 text
来存储结果(因为我需要进一步)
FOR result_data IN EXECUTE query
LOOP
RAISE NOTICE 'result : %',to_json(result_data);
resultset = resultset || to_json(result_data);
END LOOP;
SELECT cast ( SUBSTRING ( CAST ("ProcessingDate" AS text), 5, 2 ) as integer),
COUNT(*) INTO resultValue1,resultValue2
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y' AND "ProcessingDate" >= 20160110
GROUP BY 1
ORDER BY 1;
在我的数据库中,ProcessingDate
存储为 YYYYMMDD。所以,我从中提取月份。
如果我们删除 INTO
子句,此查询工作正常,但是,我想存储结果以进一步使用它。
那么变量的数据类型应该是什么resultValue1
和resultValue2
如何存储数据(因为数据将是多个)。
由于我是 PostgreSQL 的新手,我不知道该怎么做,谁能帮帮我。
试试这个:
SELECT cast ( SUBSTRING ( cast ("ProcessingDate" as text),5 , 2 ) as integer)resultValue1,
COUNT(*)resultValue2
INTO <Table Name>
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y'
AND "ProcessingDate" >= '20160110'
Group By 1
Order By 1;
此处 resultValue1
& resultValue2
将是表格而不是变量。您可以使用列名进行分组。
使用它们为列和组提供一些列别名。
你可能想要这个。
SELECT cast ( SUBSTRING ( cast ("ProcessingDate" as text),5 , 2 ) as
integer) AS resultValue1, COUNT(*) AS resultValue2
INTO <NewTable> --NewTable will be created with those two columns
FROM "DemoLogs"."Project"
-- conditions
Group By 1
-- other contitions/clauses
;
请参考此INTO Documentation。
希望这对您有所帮助。
将上面的 query
存储在变量中并从中删除 INTO
子句。
所以,查询现在是:
query = SELECT cast ( SUBSTRING ( CAST ("ProcessingDate" AS text), 5, 2 ) as
integer),
COUNT(*)
FROM "DemoLogs"."Project"
WHERE "Addr" = 'Y' AND "ProcessingDate" >= 20160110
GROUP BY 1
ORDER BY 1;
现在声明 result_data 类型 record
并按以下方式使用:
我们正在循环 result_data
因为它给出了行数作为输出
并且我已经声明 resultset
类型 text
来存储结果(因为我需要进一步)
FOR result_data IN EXECUTE query
LOOP
RAISE NOTICE 'result : %',to_json(result_data);
resultset = resultset || to_json(result_data);
END LOOP;