0

我想计算满足此查询条件的行数:

Select DISTINCT(projects.project_id)
    , projects.project_name
    , CONVERT(char(11), projects.kickoff_date, 102) AS kickoff_date
    , status_phase.status_name
    , eal.eal_abbrv
    , tech_types.tech_name
from projects
INNER JOIN tech_assigned
    ON projects.project_id = tech_assigned.project_id
INNER JOIN tech_types
    ON tech_assigned.tech_id = tech_types.tech_id
inner join status_phase
    on projects.status_phase_id=status_phase.status_phase_id
inner join eal
    on projects.eal_id=eal.eal_id
where status_phase.status_id <= 2 
    order by eal.eal_abbrv

我想要按 eal.eal_name 列分组的计数,但是当我尝试这样做时,我得到了可怕的“列'projects.project_id'在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。” 错误信息。我可以使用Select COUNT(*)orCOUNT(1)函数对记录进行计数,但我需要在组内计数。

4

2 回答 2

0

我在 eal.eal_name 列中添加,因为您的查询中没有它。因此,您需要做的是在要为其获取#s 的列上计数(),再次添加该列,然后您需要添加 GROUP BY 子句并添加您选择的每一列。

Select DISTINCT(projects.project_id)
, projects.project_name
, count(eal.eal_name)
, eal.eal_name
, CONVERT(char(11), projects.kickoff_date, 102) AS kickoff_date
, status_phase.status_name
, eal.eal_abbrv
, tech_types.tech_name
from projects
INNER JOIN tech_assigned
ON projects.project_id = tech_assigned.project_id
INNER JOIN tech_types
ON tech_assigned.tech_id = tech_types.tech_id
inner join status_phase
on projects.status_phase_id=status_phase.status_phase_id
inner join eal
on projects.eal_id=eal.eal_id
where status_phase.status_id <= 2 
group by projects.project_id
, projects.project_name
, eal.eal_name
, CONVERT(char(11), projects.kickoff_date, 102)
, status_phase.status_name
, eal.eal_abbrv
, tech_types.tech_name
order by eal.eal_abbrv
于 2013-01-31T17:42:04.093 回答
0

尝试使用窗口函数...

像这样...

    declare @table table (a int, b int)
    insert into @table
    values (0,0),(0,1),(0,1),(1,2)

    select *
    ,COUNT(*) over () as [count all]
    ,COUNT(*) over (partition by a) as [count as grouped by a]
    ,COUNT(*) over (partition by a,b) as [count as grouped by a and b]
    from @table T

我的结果集是

    a   b   count all   count as grouped by a   count as grouped by a and b
    0   0   4           3                       1
    0   1   4           3                       2
    0   1   4           3                       2
    1   2   4           1                       1

在您的查询中应该是

    Select DISTINCT(projects.project_id)
                    , count(*) over (partition by eal.eal_name) as [count by eal_name]
        , projects.project_name
        , CONVERT(char(11), projects.kickoff_date, 102) AS kickoff_date
        , status_phase.status_name
        , eal.eal_abbrv
        , tech_types.tech_name
    from projects
    INNER JOIN tech_assigned
        ON projects.project_id = tech_assigned.project_id
    INNER JOIN tech_types
        ON tech_assigned.tech_id = tech_types.tech_id
    inner join status_phase
        on projects.status_phase_id=status_phase.status_phase_id
    inner join eal
        on projects.eal_id=eal.eal_id
    where status_phase.status_id <= 2 
        order by eal.eal_abbrv
于 2013-01-31T17:46:27.967 回答