group by rollup을 사용해 총계데이터를 뽑았는데 NULL값을 총계로 바꾸고싶어요.. 0 1 506

by 뮈일러 [SQL Query] [2022.01.26 13:57:33]



select 	T.materialCode, 
		b.materialName, 
		b.dimension, 
		dbo.uf_unitName(b.unitCode) unitName, 
		sum(T.materialQty) materialQty, 
		sum(T.buyingAmt) buyingAmt, 
		sum(T.buyingVat) buyingVat, 
		b.domesticText 
	from  
	(select 
		a.dealerCode, 
		a.materialCode ,
		sum(isNull(a.materialQty,0))-sum(isNull(a.rtnQty,0)) materialQty, 
		sum(isNull(a.buyingAmt,0)) buyingAmt, 
		sum(isNull(a.buyingVat,0)) buyingVat 

	from orders a 
	where a.dealerCode='1001' 
	and a.stockInDay between '20200425' and '20200425' 
	and a.storeCode='2100' 
	group by a.dealerCode,a.materialCode) T,material b 
	where T.dealercode=b.dealerCode 
	and T.materialCode=b.materialCode 
	group by rollup((T.materialCode, b.materialname, b.dimension, b.unitcode, b.domesticText))
	order by T.materialCode

를 통해 사진에 null을 총계로바꾸고싶은데 어떻게해야하나요?

by 마농 [2022.01.26 14:12:35]

1. NULL 값 대체 방법을 이미 쿼리 안 다른 곳에서 사용하고 있네요. ISNULL
- 변경전 : b.materialName
- 변경후 : ISNULL(b.materialName, '총계') materialName
2. ISNULL 사용 개선 필요
- 변경전 : SUM(ISNULL(xxx, 0))  -- 모든 값에 IsNull 처리 후에 합산
- 변경후 : ISNULL(SUM(xxx), 0)  -- 최종 합산 결과에 대해서만 IsNull 처리

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입