Oracle SQL - 根据group by从表中获取名称

时间:2017-10-02 17:52:11

标签: sql oracle

我有一个名为con_server = pyodbc.connect("Driver={SQL Server}; Server=link") df1 =pd.read_sql_query("SELECT * FROM OPENQUERY(view1, 'SELECT * \ FROM summary1 WHERE code like (''%300%'') AND\ type = ''FUNNEL'' AND \ count > 100 AND\ date > TO_DATE(''01-SEP-2017'')'\ )",con_server) df1['SFC'] = df1['SAMPLE'].apply(lambda x: 'L'+x[2:]+'0000') df2=pd.read_sql_query("SELECT * FROM OPENQUERY(view1, 'SELECT SFC,\ DATA_PARAM, DATA \ FROM summary2 WHERE DEPT like \ (''%PIN%'') AND CODE like (''%300%'') \ AND DT > TO_DATE(''20-JUL-2017'') \ ORDER BY DT')",con_server) df2 = df2.pivot_table(index='SFC', columns='DATA_PARAM', values='DATA', aggfunc='first').reset_index() df2 = df2.astype(float,raise_on_error=False) df = df1.merge(df2, how='inner', on='SFC') df.corr() 的表,其列如下

employee

我希望将输出作为该部门中员工的任何一个名称和每个部门的员工数量。我尝试了以下,但没有成功

emp_id number emp_name varchar(30) salary float dept_id number

预期产出:

SELECT emp_name, count(*) FROM emp GROUP BY dept_id, emp_name;

有人可以建议吗?

1 个答案:

答案 0 :(得分:1)

如果您只想要一名基本的"随机员工,那么您可以尝试这样做。显示每个部门。

  select emp_name, emp_count
    from (
        select emp_name, dept_id,
                 count(*) over (partition by dept_id) emp_count,
                 row_number() over (partition by dept_id 
                                   order by dbms_random.value )  rnum
          from employee
          )
  where rnum = 1
  /

这使用分析函数计算计数,然后选择1个随机行进行显示。