求一条查询语句

liushiying18 2012-11-30 08:33:52
要求下面语句中表#tmp1的endtime字段有NULL时候返回NULL,没有返回max(endtime)
要怎么样做啊(sql2005)

SELECT UseNo,ctno,skno,tno,chno,sum(ErpQty) as ErpQty,min(begintime) as begintime,max(endtime) as endtime into #tmpOut1 FROM #tmp1 GROUP BY UseNo,ctno,skno,tno,chno


...全文
187 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2012-12-15
  • 打赏
  • 举报
回复
;with test as ( select 'a' name,1 as number union select 'b' name,2 as number union select 'c' name,3 as number union select 'd' name,4 as number union select 'a' name,5 as number union select 'b' name,6 as number union select 'c' name,7 as number union select 'd' name,null as number ) select name,max(number) from test a where not exists(select 1 from test where name=a.name and number is null) group by name union all select distinct name,null from test where number is null /* name ---- ----------- a 5 b 6 c 7 d NULL (4 行受影响) */
我腫了 2012-12-15
  • 打赏
  • 举报
回复
SELECT 
		UseNo
		,ctno
		,skno
		,tno
		,chno
		,sum(ErpQty)	as ErpQty
		,min(begintime) as begintime
		,Case when Exists(Select 1 from #tmp1 As x
							Where x.UseNo=a.UseNo
								And x.ctno=a.ctno
								And x.skno=a.skno
								And x.tno=a.tno
								And x.chno=a.chno
								And x.endtime Is null
						)
					Then Null 
				Else max(endtime)
			End as endtime 
		into #tmpOut1 
	FROM #tmp1 As a
	GROUP BY 
		UseNo
		,ctno
		,skno
		,tno
		,chno
yiyishuitian 2012-12-14
  • 打赏
  • 举报
回复
给你一事例看一下吧,应该是你想要的。


with test as 
(
select 'a' name,1 as number union 
select 'b' name,2 as number union 
select 'c' name,3 as number union 
select 'd' name,4 as number union 
select 'a' name,5 as number union 
select 'b' name,6 as number union 
select 'c' name,7 as number union 
select 'd' name,null as number  
)

select name
, case when MAX(isnull(number,10000))=10000 then null else MAX(number) end as number
from Test
group by name

name number a 5 b 6 c 7 d NULL 其中10000是我随便附的一个比number列中任何一个值都大的值。
以学习为目的 2012-12-14
  • 打赏
  • 举报
回复
select endtime=max(case when endtime is NULL then NULL else endtime) from  #tmp1
-Tracy-McGrady- 2012-12-13
  • 打赏
  • 举报
回复
case when then when then else end
發糞塗牆 2012-11-30
  • 打赏
  • 举报
回复
这样?
 SELECT UseNo ,
        ctno ,
        skno ,
        tno ,
        chno ,
        SUM(ErpQty) AS ErpQty ,
        MIN(begintime) AS begintime ,
        MAX(CASE WHEN endtime IS NULL THEN NULL
                 ELSE endtime
            END) AS endtime
 INTO   #tmpOut1
 FROM   #tmp1
 GROUP BY UseNo ,
        ctno ,
        skno ,
        tno ,
        chno
liushiying18 2012-11-30
  • 打赏
  • 举报
回复
不行呀,endtime应该使用聚合函数的
發糞塗牆 2012-11-30
  • 打赏
  • 举报
回复
SELECT  UseNo ,
        ctno ,
        skno ,
        tno ,
        chno ,
        SUM(ErpQty) AS ErpQty ,
        MIN(begintime) AS begintime ,
        CASE WHEN endtime IS NULL THEN NULL
             ELSE MAX(endtime)
        END AS endtime
INTO    #tmpOut1
FROM    #tmp1
GROUP BY UseNo ,
        ctno ,
        skno ,
        tno ,
        chno

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧