안녕 하세요
많은 도움을 받고 있는 메아리(쿼리쫌) 입니다.
감사 드리고요
한번더 부탁좀 드립니다.
대략 짜집기로 해서 select 문은 만들어 냈지만,
날짜와 시간에서 문제가 생깁니다
=============================================================
1 => 000 번의 id 에 대해서, 하루 하나의 레코드만 추출
2 => 255 번의 id 에 대해서, 하루 하나의 레코드만 추출
3 => 위의 1번의 결과와, 2 번의 결과를 합친다
이때 아래 2번 그림에서 보듯이 아랫쪽에는 날짜만 나오고 시간이 안나옵니다.
=============================================================
집계함수 때문인것 같은데 아시는 분께 부탁좀 드립니다.
글로써 설명하기가 너무 복잡할거 같아서, 캡쳐화면을 첨부하여 드립니다.
부탁좀 드립니다.
감사 합니다.
WITH surveydb ([site_code], [timestamp], [sensor_id], [sensor_type], sensor_value, calc_value, sensor_value2, calc_value2, etc, alert_flag, site_addr)
AS
(
SELECT '001007003001', '2015-08-29 11:51:41', '000', '3', 21403, 0.02, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-30 11:51:42', '000', '3', 21403, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-31 11:51:43', '000', '3', 21403, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-01 11:51:44', '000', '3', 21403, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-02 11:51:45', '000', '3', 21467, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-03 11:51:46', '000', '3', 21467, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-04 11:51:47', '000', '3', 21467, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-05 11:51:58', '000', '3', 21467, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-05 11:11:48', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-29 11:21:41', '255', '3', 12345, 0.02, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-30 11:31:42', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-31 11:41:43', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-01 11:51:44', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-02 11:61:45', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-03 11:52:46', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-04 11:53:47', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-29 11:54:51', '255', '3', 12345, 0.02, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-30 11:55:52', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-08-31 11:56:53', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-01 11:57:54', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-02 11:58:55', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-03 11:59:56', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-04 11:31:57', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL UNION ALL
SELECT '001007003001', '2015-09-05 11:32:58', '255', '3', 23456, 0.03, NULL, NULL, 53, 0, NULL
),
masterdb ([site_code], [sensor_id], [begin_date], [end_date], value, [offset_eq] )
AS
(
SELECT '001007003001', '000', '2015-08-29 00:39:00', '2015-08-29 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '255', '2015-08-30 11:51:41', '2015-08-30 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-08-31 11:51:41', '2015-08-31 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '255', '2015-09-01 11:51:41', '2015-09-01 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-02 11:51:41', '2015-09-02 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-03 11:51:41', '2015-09-03 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-04 00:14:00', '2015-09-04 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '255', '2015-09-04 11:51:41', '2015-09-04 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-05 11:51:41', '2015-09-05 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-29 11:51:41', '2015-08-29 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-08-29 11:51:41', '2015-08-30 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-08-30 11:51:41', '2015-08-31 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-08-31 11:51:41', '2015-09-01 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-01 11:51:41', '2015-09-02 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-02 11:51:41', '2015-09-03 11:51:41', 393.74, '({v} / 100.0) - {o}' UNION ALL
SELECT '001007003001', '000', '2015-09-05 00:15:00', '2015-09-05 11:51:41', 393.74, '({v} / 100.0) - {o}'
)
SELECT a.*
FROM
(
SELECT CONVERT(NVARCHAR, s.timestamp, 120) as timestamp, s.sensor_id as sensor, s.sensor_value as raw_value,
s.calc_value as calc_value, o.value as offset, o.offset_eq as equation, s.etc as etc
FROM surveydb s
inner join
(
SELECT site_code, MIN(timestamp) timestamp , sensor_id
FROM surveydb
WHERE site_code = '001007003001'
AND CONVERT(VARCHAR, timestamp, 23) between '2015-08-29' AND '2015-09-05'
AND sensor_id IN ('000')
GROUP BY site_code, CONVERT(VARCHAR, timestamp, 23) , sensor_id
) ss
on s.site_code = ss.site_code
and s.timestamp = ss.timestamp
and s.sensor_id = ss.sensor_id
LEFT OUTER JOIN masterdb o
ON s.site_code = o.site_code
AND s.sensor_id = o.sensor_id
AND o.begin_date <= s.timestamp AND o.end_date >= s.timestamp
) a
union all
select b.*
FROM
(
SELECT CONVERT(VARCHAR, ss.timestamp, 23) as timestamp, ss.sensor_id , sum(s.sensor_value) raw_value ,
sum( s.calc_value) as calc_value, sum(o.value) as offset ,max(o.offset_eq) as equation, max(s.etc) as etc
FROM surveydb s
inner join surveydb ss
on s.site_code = ss.site_code and s.timestamp = ss.timestamp
and s.sensor_id = ss.sensor_id
LEFT OUTER JOIN masterdb o
ON s.site_code = o.site_code
AND s.sensor_id = o.sensor_id
AND o.begin_date <= s.timestamp
AND o.end_date >= s.timestamp
WHERE ss.site_code = '001007003001'
AND CONVERT(VARCHAR, ss.timestamp, 23) between '2015-08-29' AND '2015-09-05'
AND ss.sensor_id IN ('255')
GROUP BY CONVERT(VARCHAR, ss.timestamp, 23), ss.sensor_id
) b
Comment 6
-
이리
2016.11.23 09:36
-
쿼리쫌 ^^
2016.11.23 10:11
이리님 안녕 하십니까 감사 드리고요
23 => 120 으로 하게 되면은 1일 하나의 레코드만 추출하는 기능이 안 먹힙니다.ㅋ
-
이리
2016.11.23 10:48
SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY SUBSTRING(a.timestamp, 1, 10) ORDER BY a.timestamp) AS ROW_NUM, a.*FROM(SELECT s.timestamp, s.sensor_id as sensor, s.sensor_value as raw_value,s.calc_value as calc_value, o.value as offset, o.offset_eq as equation, s.etc as etcFROM surveydb sLEFT OUTER JOIN masterdb oON s.site_code = o.site_codeAND s.sensor_id = o.sensor_idAND s.timestamp >= CONVERT(CHAR(10), o.begin_date, 120) + ' 00:00:00'AND s.timestamp <= CONVERT(CHAR(10), o.begin_date, 120) + ' 23:59:59'WHERE s.site_code = '001007003001'AND s.timestamp between '2015-08-29 00:00:00' AND '2015-09-05 23:59:59'AND s.sensor_id IN ('000')) aunion allselect ROW_NUMBER() OVER(PARTITION BY SUBSTRING(b.timestamp, 1, 10) ORDER BY b.timestamp) AS ROW_NUM, b.*FROM(SELECT s.timestamp, s.sensor_id , sum(s.sensor_value) raw_value ,sum(s.calc_value) as calc_value, sum(o.value) as offset ,max(o.offset_eq) as equation, max(s.etc) as etcFROM surveydb sLEFT OUTER JOIN masterdb oON s.site_code = o.site_codeAND s.sensor_id = o.sensor_idAND s.timestamp >= CONVERT(CHAR(10), o.begin_date, 120) + ' 00:00:00'AND s.timestamp <= CONVERT(CHAR(10), o.begin_date, 120) + ' 23:59:59'WHERE s.site_code = '001007003001'AND s.timestamp between '2015-08-29 00:00:00' AND '2015-09-05 23:59:59'AND s.sensor_id IN ('255')GROUP BY s.timestamp, s.sensor_id) b) AS aWHERE a.ROW_NUM = 1이런식의 결과면 되는것인지요?그리고 예제로 주신 data에 61분인거 있네요.. -
쿼리쫌 ^^
2016.11.23 12:13
이리님 감사 합니다.
하나만 더 부탁 드리겠습니다.
raw_value 하고 calc_value 가 하루의 합계가 나오는 것만 부탁좀 드리겠습니다.
그래서 sum 하려고 convert (nvarchar, timestamp, 23) 식으로 23 을 했던 것입니다.
올려주신 쿼리는 합계가 나오지 않고, 0 이 나옵니다
필요한 부분은 아래와 같습니다.
한번만 더 부탁 드리겠습니다.
정말 감사 합니다 꾸~~벅~~~~~~~~~
-
이리
2016.11.23 12:57
SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY SUBSTRING(a.timestamp, 1, 10) ORDER BY a.timestamp) AS ROW_NUM, a.*FROM(SELECT s.timestamp, s.sensor_id as sensor, s.sensor_value as raw_value,s.calc_value as calc_value, o.value as offset, o.offset_eq as equation, s.etc as etcFROM surveydb sLEFT OUTER JOIN masterdb oON s.site_code = o.site_codeAND s.sensor_id = o.sensor_idAND s.timestamp >= CONVERT(CHAR(10), o.begin_date, 120) + ' 00:00:00'AND s.timestamp <= CONVERT(CHAR(10), o.begin_date, 120) + ' 23:59:59'WHERE s.site_code = '001007003001'AND s.timestamp between '2015-08-29 00:00:00' AND '2015-09-05 23:59:59'AND s.sensor_id IN ('000')) aunion allselect ROW_NUMBER() OVER(PARTITION BY SUBSTRING(b.timestamp, 1, 10) ORDER BY b.timestamp) AS ROW_NUM, b.*FROM(SELECT s.timestamp, s.sensor_id as sensor, ss.raw_value as raw_value,ss.calc_value as calc_value, ss.offset as offset, ss.equation as equation, s.etc as etcFROM surveydb sINNER JOIN(SELECT s.site_code, s.sensor_id, CONVERT(CHAR(10), s.timestamp, 120) AS dt, sum(s.sensor_value) raw_value, sum(s.calc_value) as calc_value, sum(o.value) as offset, max(o.offset_eq) as equation, max(s.etc) as etcFROM surveydb sLEFT OUTER JOIN masterdb oON s.site_code = o.site_codeAND s.sensor_id = o.sensor_idAND s.timestamp >= CONVERT(CHAR(10), o.begin_date, 120) + ' 00:00:00'AND s.timestamp <= CONVERT(CHAR(10), o.begin_date, 120) + ' 23:59:59'WHERE s.site_code = '001007003001'AND s.timestamp between '2015-08-29 00:00:00' AND '2015-09-05 23:59:59'AND s.sensor_id IN ('255')GROUP BY s.site_code, s.sensor_id, CONVERT(CHAR(10), s.timestamp, 120)) ss ON s.site_code = ss.site_code AND s.sensor_id = ss.sensor_id) b) AS aWHERE a.ROW_NUM = 1이렇게인가요? -
쿼리쫌 ^^
2016.11.23 13:54
마지막에 조건하나 추가 했습니다.
AND convert (nvarchar, s.timestamp, 23) = convert(nvarchar, ss.dt, 23)
감사합니다 ^^대단히 감사 합니다~~~~~~~~~~~~~~~~~
꾸~~~~~~~~~~~~~~~벅~~~~~~~~~~~~~~~~~
며칠간 삽질 했는데. 감사 감사 ^^
이리님은 어디 사십니까 ?? 저는 지방입니다만,,,,
쿼리 보면 위 SELECT 에는 120을 쓰시고 밑에는 23을 쓰시는게 이유가 있는지요?
SELECT CONVERT(VARCHAR, GETDATE(), 23)
SELECT CONVERT(VARCHAR, GETDATE(), 120)
결과 비교해보세요..