num | price |
1 | 6000 |
2 | 8000 |
3 | 10000 |
4 | 15000 |
5 | 0 |
데이터 구조는 위와 같습니다.
num 은 고정값, price 는 계속 변하는 값입니다.
여기서 넘겨지는 조건 금액이 6000 이하이면 1
6000 ~ 8000 사이이면 2
8000 ~ 10000 사이이면 3
10000 ~ 150000 사이이면 4
현재 5번 값이 없으므로,
15000 이상이면 4
이런 쿼리를 구하려고 합니다..
참고로,
1번 price값이 0인 경우는 무조껀 1
1번 price는 값이 있지만, 2번 price가 0인 경우는 무조껀 1
1번, 2번 price는 값이 있지만, 3번 price가 0인 경우는 2 가 나와야 합니다..
검색조건에 해당하는 컬럼이 두개여서 두개만 뽑았구요, 다른 컬럼들이 많아서 옆으로 돌리기는 어려울꺼 같아요.. ;;;;
얼핏보면 쉬워 보이는데, 오랫만에 쿼리를 짜서 그런지.. 영 안나오네요..
고수님들 도움 부탁드립니다.. ㅜ.ㅜ
Comment 4
-
비폭력무저항
2016.09.07 22:05
로우 갯수는 많지 않고, 딱 5개 행이 정해졌다고 보면 됩니다.속도도 크게 상관없구요,..;;; -
비폭력무저항
2016.09.07 22:26
declare @price int
set @price = 7000select distinct case
when @price < (select basicprice from tablename where num = 1) then 0
when @price between (select basicprice from tablename where num = 1) and (select basicprice from tablename where num = 2) then 1
when @price between (select basicprice from tablename where num = 2) and (select basicprice from tablename where num = 3) then 2
when @price between (select basicprice from tablename where num = 3) and (select basicprice from tablename where num = 4) then 3
when @price between (select basicprice from tablename where num = 4) and (select basicprice from tablename where num = 5) then 4
when @price > (select basicprice from tablename where num = 5) then 5
end as num
from tablename이렇게 하면 원하는 값은 나옵니다.. ;;;;
그런데, 너무 비 효율적인거 같네요.. ㅠ.ㅠ -
아스날
2016.09.08 11:30
DECLARE @temp TABLE (num INT, price INT)DECLARE @price INT = 19000INSERT @tempVALUES (1, 6000),(2, 8000),(3, 10000),(4, 15000),(5, 0)SELECT ISNULL(MAX(num), 1) AS 'num'FROM @tempWHERE num < (SELECT ISNULL(MIN(num), MAX(num)) AS 'maxnum' FROM @temp WHERE price = 0)AND price <= @price -
비폭력무저항
2016.09.08 13:04
감사합니다.. 바로 해결되었네요.. ^^