如何将一个选择与另一个选择分开

时间:2012-02-21 19:11:10

标签: sql-server sql-server-2008

我有2个表,这是第一个

select jmeno, prijmeni,
CAST(pr_o as float) cislo from(select jmeno, prijmeni, COUNT(id_objednavky) pr_o
from Objednavky o join Zamestnanci z 
on z.ID_Zamestnance=o.ID_zamestnance
where YEAR(datum_odeslani)=2010
group by jmeno, prijmeni)aa

这是第二次

select AVG(pocet) prumer_o  from(
select jmeno, prijmeni,count(id_objednavky) pocet
from Objednavky o join Zamestnanci z 
on z.ID_Zamestnance=o.ID_zamestnance
where year(datum_odeslani)=2010
group by Jmeno, prijmeni
)aaa

我想要的是一张包含3列的表:Jmeno,Prijmeni,Cislo(第一张表)除以prumer_o(第二张表)

1 个答案:

答案 0 :(得分:0)

您应该尝试仅在一个查询中执行此操作。试试这个:

SELECT jmeno, 
       prijmeni, 
       CAST(pr_o AS FLOAT)/(AVG(pr_o) OVER()) div
FROM   (SELECT jmeno, 
               prijmeni, 
               COUNT(id_objednavky) pr_o 
        FROM   objednavky o 
               JOIN zamestnanci z 
                 ON z.id_zamestnance = o.id_zamestnance 
        WHERE  YEAR(datum_odeslani) = 2010 
        GROUP  BY jmeno, 
                  prijmeni)aa