【问题标题】:SQL Group by on outer reference after 2 inner joinsSQL Group by 在 2 个内部连接后的外部引用
【发布时间】:2018-04-04 12:16:46
【问题描述】:

我的 sql 查询没有按预期工作,在当前形式下它会返回结果,但是 second 列上的一些结果是重复的,或者换句话说,一些计算机名称后面跟着盘符显示两次。

查询是这样的:

(select Distinct 'ServerDrive' as storageType, cast(assetname as 
varchar)+ '\ ' +cast(caption as varchar) as storagePath,((rqDiskdrivesHist. 
[size]-rqDiskdrivesHist.[freespace])/1024/1024) as usedMB,(rqDiskdrivesHist. 
[size]/1024/1024) as sizeMB from lansweeperdb.dbo.tblAssets 
inner join rqDiskdrivesHist
on lansweeperdb.dbo.tblAssets.AssetID=rqDiskdrivesHist.AssetID 
where drivetype=3
and lansweeperdb.dbo.tblAssets.AssetName in
(select distinct entitypath from rqcentities
inner join rqcustomer
on rqcentities.customerID=rqcustomer.customerID 
where customername like '%ak 26%' and entityType='computer'
and not (
(AssetName like 'WFP3675' and caption like 'D:')
or
(AssetName like 'WFP3675' and caption like 'E:')
or
(AssetName like 'WFP3675' and caption like 'F:')
or
(AssetName like '%00500%')
)  
)) 

如果我在最后尝试分组。与:

GROUP BY cast(assetname as varchar)+'\'+cast(caption as varchar)

我会得到“每个 GROUP BY 表达式必须至少包含一个不是外部引用的列。 " 错误。

编辑:这些是重复的结果中的 2 行。

【问题讨论】:

  • 斜杠后面的空格只是为了代码着色。 cast(assetname as varchar)+ '\ ' +cast(caption as varchar) 应被视为cast(assetname as varchar)+ '\' +cast(caption as varchar)。
  • 样本数据最好使用DDL + DML。请edit你的问题包括它和你想要的结果。

标签: sql .net sql-server sql-server-2012


【解决方案1】:

您需要对别名进行分组,因为您没有在内部查询中公开这些字段。

SELECT
    storageType,
    storagePath,
    MaxMB = MAX(usedMB),
    MinMB = MIN(usedMB),
    AvgMB = AVG(usedMB)
FROM
(
    select 'ServerDrive' as storageType, 
    cast(assetname as varchar)+ '\ ' +cast(caption as varchar) as storagePath,
    ((rqDiskdrivesHist.[size]-rqDiskdrivesHist.[freespace])/1024/1024) as usedMB,
    (rqDiskdrivesHist.[size]/1024/1024) as sizeMB 
    from 
        lansweeperdb.dbo.tblAssets 
        inner join rqDiskdrivesHist on lansweeperdb.dbo.tblAssets.AssetID=rqDiskdrivesHist.AssetID 
    where 
        drivetype=3
        and lansweeperdb.dbo.tblAssets.AssetName in
        (
            select distinct entitypath 
            from rqcentities
            inner join rqcustomer on rqcentities.customerID=rqcustomer.customerID 
            where customername like '%ak 26%' and entityType='computer'
            and not (
                        (AssetName like 'WFP3675' and caption like 'D:')
                        or
                        (AssetName like 'WFP3675' and caption like 'E:')
                        or
                        (AssetName like 'WFP3675' and caption like 'F:')
                        or
                        (AssetName like '%00500%')
                    )  
        )
) AS X
GROUP BY
    storagePath,
    storageType

【讨论】:

  • 你能展示整个查询的样子吗?
  • @lason - 添加了一种为每个路径和类型返回一条记录的方法。
  • @lason - 注意我删除了 distinct,因为它是一个组字段。此外,您实际上并不需要内部连接中的 StorageType,因为您将其限制为一个硬编码值。
猜你喜欢
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2013-03-30
相关资源
最近更新 更多