总结 SQL
Summarizing in SQL
我对 SQL 很陌生,我正在尝试总结 table 在 SAS 软件上使用它。
下面是我要总结的table:
policy_number item
1234 1
1234 2
1234 3
567 1
89 1
90 1
90 2
这是我需要的结果:
policy_number item max_item
1234 1 3
1234 2 3
1234 3 3
567 1 1
89 1 1
90 1 2
90 2 2
这是我的代码:
proc sql;
create table example
as select
policy_number,
item,
max(item) as max_item
from table1
group by policy_number, item;
quit;
它给出了这个结果:
policy_number item max_item
1234 1 1
1234 2 1
1234 3 3
567 1 1
89 1 1
90 1 1
90 2 2
我做错了什么?有人可以帮我修复我的代码吗?
嗯。我希望这能做你想做的事:
proc sql;
create table example as
select policy_number, item, max(item) as max_item
from table1
group by policy_number;
quit;
这是不标准的 SQL。但在 proc SQL
中,它应该重新合并第三列的最大值。
我应该补充一点,这个版本是另一种做你想做的事情的方法:
proc sql;
create table example as
select t1.policy_number, t1.item, tt1.max_item
from table1 t1 join
(select policy_number, max(item) as max_item
from table1
group by policy_number
) tt1
on t1.policy_number = tt1.policy_number;
quit;
试着这样想。你的 table 看起来像这样。
policy_number item
1234 1
1234 2
1234 3
567 1
89 1
90 1
90 2
首先,目标是找到每个策略的最大项目,这可以像这样完成
SELECT policy_number, MAX(item) max_item
FROM table1
GROUP BY policy_number
结果如下。
policy_number max_item
1234 3
567 1
89 1
90 2
下一步是将它们合并在一起,您可以使用子查询和联接来完成。
SELECT table1.policy_number, item, max_item
FROM table1
JOIN (
SELECT policy_number, MAX(item) max_item
FROM table1
GROUP BY policy_number
) SubQ ON SubQ.policy_number = table1.policy_number
您可以通过将 table 连接到自身来实现。
下面是用于该目的的简单示例:
SELECT I.policy_number, I.item, J.mx FROM example I
LEFT JOIN
(SELECT
policy_number, max(item) AS mx
FROM example
GROUP BY policy_number) J
ON J.policy_number=I.policy_number
但这可能有效也可能无效,这取决于目的。
SELECT t.policy_number,t.item , max(item ) over(partition by t.policy_number)
作为最大值
从 dbo.table1 t
按t.policy_number
排序
我对 SQL 很陌生,我正在尝试总结 table 在 SAS 软件上使用它。
下面是我要总结的table:
policy_number item
1234 1
1234 2
1234 3
567 1
89 1
90 1
90 2
这是我需要的结果:
policy_number item max_item
1234 1 3
1234 2 3
1234 3 3
567 1 1
89 1 1
90 1 2
90 2 2
这是我的代码:
proc sql;
create table example
as select
policy_number,
item,
max(item) as max_item
from table1
group by policy_number, item;
quit;
它给出了这个结果:
policy_number item max_item
1234 1 1
1234 2 1
1234 3 3
567 1 1
89 1 1
90 1 1
90 2 2
我做错了什么?有人可以帮我修复我的代码吗?
嗯。我希望这能做你想做的事:
proc sql;
create table example as
select policy_number, item, max(item) as max_item
from table1
group by policy_number;
quit;
这是不标准的 SQL。但在 proc SQL
中,它应该重新合并第三列的最大值。
我应该补充一点,这个版本是另一种做你想做的事情的方法:
proc sql;
create table example as
select t1.policy_number, t1.item, tt1.max_item
from table1 t1 join
(select policy_number, max(item) as max_item
from table1
group by policy_number
) tt1
on t1.policy_number = tt1.policy_number;
quit;
试着这样想。你的 table 看起来像这样。
policy_number item
1234 1
1234 2
1234 3
567 1
89 1
90 1
90 2
首先,目标是找到每个策略的最大项目,这可以像这样完成
SELECT policy_number, MAX(item) max_item
FROM table1
GROUP BY policy_number
结果如下。
policy_number max_item
1234 3
567 1
89 1
90 2
下一步是将它们合并在一起,您可以使用子查询和联接来完成。
SELECT table1.policy_number, item, max_item
FROM table1
JOIN (
SELECT policy_number, MAX(item) max_item
FROM table1
GROUP BY policy_number
) SubQ ON SubQ.policy_number = table1.policy_number
您可以通过将 table 连接到自身来实现。
下面是用于该目的的简单示例:
SELECT I.policy_number, I.item, J.mx FROM example I
LEFT JOIN
(SELECT
policy_number, max(item) AS mx
FROM example
GROUP BY policy_number) J
ON J.policy_number=I.policy_number
但这可能有效也可能无效,这取决于目的。
SELECT t.policy_number,t.item , max(item ) over(partition by t.policy_number) 作为最大值
从 dbo.table1 t
按t.policy_number
排序