Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server中具有唯一列的聚合结果_Sql_Sql Server 2008_Tsql - Fatal编程技术网

SQL Server中具有唯一列的聚合结果

SQL Server中具有唯一列的聚合结果,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,我在表上执行聚合,下面是我正在查询的作业表的快照 我的问题与下面类似。实际上,上述派生表将有更多列作为多个联接的结果 select Attribute from Job where Description = 'Installation' and Attribute = 'NPL' group by Attribute having sum(Cost) >= 500 我想获得匹配记录的JobID,因为它是唯一的,我可以使用它进行进一步的连接。如何获取匹配记录的jobID(即

我在表上执行聚合,下面是我正在查询的作业表的快照

我的问题与下面类似。实际上,上述派生表将有更多列作为多个联接的结果

select Attribute   
from Job 
where Description = 'Installation' and Attribute = 'NPL' 
group by Attribute 
having sum(Cost) >= 500
我想获得匹配记录的
JobID
,因为它是唯一的,我可以使用它进行进一步的连接。如何获取匹配记录的jobID(即PK)

我不确定这样做是否合适

select Attribute, JobID 
from Job 
where Description = 'Installation' and Attribute = 'NPL' 
group by Attribute, JobID  
having sum(Cost) >= 500
我期望结果如下(对于上述场景)


以这种方式分组不会得到预期的结果:

Group by Attribute, JobID
这样,
JobID=34
JobID=39
将在单独的组中。有几种可能的方法之一是使用
内部联接
和子查询来实现“预期结果”图片:

select j.Attribute, j.JobID   
from Job j
    inner join
        (select Attribute   
         from Job 
         where Description = 'Installation' and j.Attribute = 'NPL' 
         group by Attribute 
         having sum(Cost) >= 500) a on a.Attribute = j.Attribute
where j.Description = 'Installation' and j.Attribute = 'NPL'

以这种方式分组不会得到预期的结果:

Group by Attribute, JobID
这样,
JobID=34
JobID=39
将在单独的组中。有几种可能的方法之一是使用
内部联接
和子查询来实现“预期结果”图片:

select j.Attribute, j.JobID   
from Job j
    inner join
        (select Attribute   
         from Job 
         where Description = 'Installation' and j.Attribute = 'NPL' 
         group by Attribute 
         having sum(Cost) >= 500) a on a.Attribute = j.Attribute
where j.Description = 'Installation' and j.Attribute = 'NPL'

可以将聚合与窗口函数一起使用,以便保留原始行,但可以对聚合值进行推理。您需要在子查询或CTE中计算聚合:

;With Totals as (
    select *,SUM(Cost) OVER (PARTITION BY Attribute) as TotalCost
    from Job
    where Description = 'Installation' and Attribute = 'NPL' 
)
select * from Totals where TotalCost > 500

可以将聚合与窗口函数一起使用,以便保留原始行,但可以对聚合值进行推理。您需要在子查询或CTE中计算聚合:

;With Totals as (
    select *,SUM(Cost) OVER (PARTITION BY Attribute) as TotalCost
    from Job
    where Description = 'Installation' and Attribute = 'NPL' 
)
select * from Totals where TotalCost > 500
试试这个

    SELECT 
        J.JobId,
        J.Description 
    FROM dbo.Jobs J
    INNER JOIN
    (
        SELECT 
            Attribute
        FROM dbo.Jobs
        GROUP BY
            Attribute
        having 
            sum(Cost) >= 500
    ) AS G
    ON J.Attribute = G.Attribute 
    where 
        J.Description = 'Installation' 
        and J.Attribute = 'NPL' 
试试这个

    SELECT 
        J.JobId,
        J.Description 
    FROM dbo.Jobs J
    INNER JOIN
    (
        SELECT 
            Attribute
        FROM dbo.Jobs
        GROUP BY
            Attribute
        having 
            sum(Cost) >= 500
    ) AS G
    ON J.Attribute = G.Attribute 
    where 
        J.Description = 'Installation' 
        and J.Attribute = 'NPL' 

你能告诉我你的输出应该是什么样子吗?嗨,Vijaykumar,我刚刚编辑了这个问题以添加我要查找的结果表。你能告诉我你的输出应该是什么样子吗?嗨,Vijaykumar,我刚刚编辑了这个问题以添加我正在寻找的结果表。但是属性不是唯一的,所以它可能会在其他场景中与不需要的记录匹配,不是吗?但是属性不是唯一的,所以它可能会在其他场景中与不需要的记录匹配,不是吗??