【问题标题】:How To use Where instead of Group by?如何使用 Where 而不是 Group by?
【发布时间】:2019-09-27 11:07:22
【问题描述】:

我写了一个查询,它给了我这个输出:

(这只是一个示例,显然输出表包含大约 300000 行)

这是我的查询:

  proc sql;
  create Table Output as 
  select ID_User, Division_ID, sum(conta) as Tot_Items, max(Counts) as Max_Item
  from (select c.ID_User , c.Div_ID as Division_ID, ro.code as Mat, count(*) as Counts
  from Ods.R_Ordini o
  inner join DMC.Cust_Dupl c
   on User_ID = ID_User
  inner join ods.R_Nlines ro
   on ro.Orders_Id = o.Id_Orders AND RO.SERVICE = 0
  inner join ods.R_Mat m
   on ro.Mat_Id = Id_Mat and flag = 0
  group by 
      ID_User,
      C.Division_ID,
      Ro.Code
      Having Counts > 1
     )
  group by
  Id_User,
  Division_ID
  Order by 
  Tot_Item DESC
  ;
  quit;

所以,我想要的是重写这个查询,但我想使用 Where 条件而不是 Group by,(WHERE=(DIVISION_ID=3)) 这是条件。

我尝试了几次,有些出错了,有些我确实得到了输出,但输出和原来的不一样。

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 只需在group by 之前添加where division_id = 3
  • 我已经尝试过了,但我仍然出错!!!
  • 哦,等等,我知道我的错误在哪里,解决了,谢谢。
  • 普及知识,你可以回答你自己的问题

标签: sql group-by sas where-clause proc


【解决方案1】:

SAS 数据集选项(where=(<where-expression>)) 只能与数据集名称相邻编码。因此,该选项必须应用于包含列div_id 的数据集,该列是计算列division_id 的基础。那将是表别名c

DMC.Cust_Dupl(where=(div_id=3)) as c

或者只使用普通的 SQL where 子句

…
)
where division_id=3
group by … 

【讨论】:

    【解决方案2】:

    只需在分组依据之前使用 WHERE DIVISION_ID=3。

    select ID_User, Division_ID, sum(conta) as Tot_Items, max(Counts) as Max_Item from (select c.ID_User , c.Div_ID as Division_ID, ro.code as Mat, count(*) as Counts from Ods.R_Ordini o 内连接 DMC.Cust_Dupl c on User_ID = ID_User 内连接 ods.R_Nlines ro on ro.Orders_Id = o.Id_Orders AND RO.SERVICE = 0 内连接 ods.R_Mat m on ro.Mat_Id = Id_Mat 和 flag = 0 WHERE DIVISION_ID= 3 按 ID_User 分组,C.Division_ID,Ro.Code 计数 > 1 ) 按 Id_User 分组,Division_ID 按 Tot_Item DESC 排序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多