用min来加入子查询

时间:2012-07-05 21:00:49

标签: sql-server sql-server-2005

我把头发拉过一个子查询,我正在使用它来避免大约100个重复(大约40k记录中)。重复的记录正在显示,因为它们在h2.datecreated中有2个日期,这是有正当理由的,所以我不能只清理数据。

我正试图获得最早的返回日期。第一个子查询(以“select distinct address_id”开头,以MIN开头)可以自行运行......不会返回重复项。所以看起来左连接(或者只是普通连接......我也尝试过)也不可能看到第二个h2.date创建,因为它甚至没有显示在子查询中。但是当我运行整个查询时,它会返回一些ipc.mfgid的值,一个是我想要的h2.datecreated,另一个是我不想要的。

我知道它必须是非常简单的东西,或者是不可能的东西。它真的好像应该工作!这是MSSQL。谢谢!

select distinct ipc.mfgid as IPC, h2.datecreated,
case when ad.Address is null
then ad.buildingname end as Address, cast(trace.name as varchar) 
    + '-' + cast(trace.Number       as varchar) as ONT, 
 c.ACCOUNT_Id,
case when h.datecreated is not null then h.datecreated
     else  h2.datecreated end as Install
from equipmentjoin as ipc
left join historyjoin as h on ipc.id = h.EQUIPMENT_Id 
   and h.type like 'add'
left join circuitjoin as c on ipc.ADDRESS_Id = c.ADDRESS_Id 
           and c.GRADE_Code like '%hpna%'
join (select distinct address_id, equipment_id,  
     min(datecreated) as datecreated, comment
     from history where comment like 'MAC: 5%' group by equipment_id, address_id, comment)
          as h2 on c.address_id = h2.address_id
left join (select car.id, infport.name, carport.number, car.PCIRCUITGROUP_Id
     from circuit as car (NOLOCK)  
     join port as carport (NOLOCK) on car.id = carport.CIRCUIT_Id
      and carport.name like 'lead%'
      and car.GRADE_Id = 29 
     join circuit as inf (NOLOCK) on car.CCIRCUITGROUP_Id = inf.PCIRCUITGROUP_Id
     join port as infport (NOLOCK) on inf.id = infport.CIRCUIT_Id
     and infport.name like '%olt%' )
 as trace on c.ccircuitgroup_id = trace.pcircuitgroup_id
join addressjoin as ad (NOLOCK) on ipc.address_id = ad.id

2 个答案:

答案 0 :(得分:1)

仅获得最低行的典型方法是以下之一。你没有费心去指定你正在使用的SQL Server版本,你想用关系做什么,我没有兴趣尝试将它用于你的复杂查询,所以我将向你展示一个抽象的简化不同的版本。

SQL Server 2000

SELECT x.grouping_column, x.min_column, x.other_columns ...
FROM dbo.foo AS x
INNER JOIN
(
  SELECT grouping_column, min_column = MIN(min_column)
  FROM dbo.foo GROUP BY grouping_column
) AS y  
ON x.grouping_column = y.grouping_column
AND x.min_column = y.min_column;

SQL Server 2005 +

;WITH x AS
(
   SELECT grouping_column, min_column, other_columns,
     rn = ROW_NUMBER() OVER (ORDER BY min_column)
   FROM dbo.foo
)
SELECT grouping_column, min_column, other_columns
FROM x
WHERE rn = 1;

答案 1 :(得分:0)

这个子问题:

select distinct address_id, equipment_id,  
 min(datecreated) as datecreated, comment
 from history where comment like 'MAC: 5%' group by equipment_id, address_id, comment

可能会返回多行,因为评论不能保证相同。

请改为尝试:

CROSS APPLY (
   SELECT TOP 1 H2.DateCreated, H2.Comment -- H2.Equipment_id wasn't used
   FROM History H2
   WHERE
      H2.Comment LIKE 'MAC: 5%'
      AND C.Address_ID = H2.Address_ID
   ORDER BY DateCreated
) H2

如果您想要的行没有匹配的所需历史记录条目,请将其切换为OUTER APPLY