MAX(Transact-SQL)

时间:2017-08-30 00:51:29

标签: sql sql-server tsql

我开始学习SQL并在下面进行查询。它提取了我需要的数据,但它没有拉出最大benmon,而是显示所有月份。有什么建议吗?

select distinct a.casenum+a.type as AG, a.casenum, d.access_number,d.action,d.dateReceived, a.type, b.region, b.admin, b.unit, b.PAS, a.received, a.due, a.timestd, a.dayspend, a.dayspend-a.timestd as BeyondStd, b.imagedate, d.localUse,e.benmon,e.yyyymm
into #temp131
FROM AMS.dbo.pendingactions a, AMS.dbo.cases_daily b, AMS.dbo.ags_daily c, sandbox.dbo.workitems17 d, datamart.dbo.fsissue17 e
where a.item='APPL' and a.casenum=b.casenum and a.casenum+a.type=c.ag and a.casenum=d.casenum and a.casenum=e.casenum
AND b.admin='88126' and b.region='SC' and d.status <> 'app dis' 
  GO
update #temp131 set BeyondStd='' where BeyondStd<1
  GO
select region, admin,unit,PAS,casenum,access_number,action,max(benmon),yyyymm, type,dateReceived,received, due, timestd, dayspend, beyondstd, imageDate, localuse 
from #temp131  group by region, admin,unit,PAS,casenum,access_number,action,benmon,yyyymm, type,dateReceived,received, due, timestd, dayspend, beyondstd, imageDate, localuse 

2 个答案:

答案 0 :(得分:0)

从GROUP BY子句中删除String assembly = "${pwd()}\\ELMS_Desktop_Application\\CARS\\BulkUpdater\\Properties\\AssemblyInfo.cs"

通常,GROUP BY中不需要您将聚合应用于(MIN,MAX,COUNT,SUM等)的任何列。

答案 1 :(得分:0)

由于您的group by子句中的所有字段,它显示每个月。 group by将基本上汇总所有字段的所有唯一组合,然后应用聚合。所以在你的小组中:

group by region, admin,unit,PAS,casenum,access_number,action,benmon,yyyymm, type

你有yyyymm和benmon作为两个领域。因此,它将为每个月创建“组”。取决于其他字段的唯一性,即该组的唯一性。