select a.dealerCode,@A_StockInDay as stockInDay, b.materialCode,b.cnt,a.materialName,a.dimension, dbo.UF_UnitName(a.unitCode) unitName,a.isVat,b.totalQty, isNull((select x.refmaterialCode from stockMaterial x where x.dealerCode=@A_DealerCode and x.materialcode=a.materialCode),'xxxxxx') refMaterialCode, b.orderCnt-b.isCompleted as isCompleted from material a, ( select dealerCode,materialCode,count(storeCode) cnt,sum(materialQty-rtnQty) totalQty, sum(case when (validationNo is null or len(validationNo)=0) and materialQty-rtnQty>0 then 0 else 1 end) isCompleted, count(materialCode) orderCnt from orders where dealerCode=@A_DealerCode and stockInDay=@A_StockinDay and storeCode!='9999' and isWarehouse='W' group by dealerCode,materialCode ) b where a.dealerCode=@A_DealerCode and a.materialName like '%'+@A_SearchMaterialName+'%' and a.dealerCode=b.dealerCode and a.materialCode=b.materialCode order by a.materialName
현재 쿼리는 하루만 조회하여 결과를 가져오는건데
제가 바꾸고싶은건 하루만 조회하는걸 기간으로 바꿔서 결과를 가져오고싶습니다.
서브쿼리쪽에서 between을 쓰고싶은데 가능한방법이 있을까요?
and stockInDay=@A_StockinDay 이 부분을 between and 를 쓰면 될거같은데
하위 쿼리에서 값을 둘 이상 반환했습니다. 하위 쿼리 앞에 =, !=, <, <=, >, >= 등이 오거나 하위 쿼리가 하나의 식으로 사용된 경우에는 여러 값을 반환할 수 없습니다.
이 오류가 떠서 막혔습니다..
서브쿼리에서 결과값이 여러개가 나오는 모양입니다.
max 나 min 으로 결과 확인해보시는게 어떨까요?
올려주신 쿼리에서 stockInDay 조건이 들어가는 서브쿼리는 인라인뷰입니다. (해당 오류와 무관)
해당 오류는 스칼라서브쿼리에서 나는 오류입니다.
스칼라서브쿼리가 Select 절에 하나 있긴 하네요.
근데 이쿼리를 그대로 실행하면 결과가 잘 나옵니다
stockinday를 between을 넣는순간 저 오류가 나와요 ㅠㅠ
Select 절의 스칼라 서브쿼리의 조건이 a.materialCode 인데
예를 들면. 메인쿼리의 하루 검색시 코드가 a, b, c 가 나오는데
기간 검색으로 바꾸면 a,b,c,d,e 가 나온다고 가정해 보면
a,b,c 의 경우 서브쿼리 결과가 1건이어서 에러가 안나다가
d 의 자료가 2건이 나오게 된다면 에러가 나는 것이죠.
스칼라서브쿼리 주석처리하고 실행해 보세요.
주석처리하니 실행이되네요
그러면 저 서브쿼리를 쓸수가없는거죠?
다중행 서브쿼리를 스칼라서브쿼리로 쓸수는 없습니다.
테이블 관계나, 자료의 특성, 업무 요건등을 분석하여 하나의 행만 나오도록 개선해야 합니다.