2016-06-02 10 views
-1

가 나는 SQL 코드가 붙어 표 1에서 계산 수 (나는 충분 그것을 설명 할 수 있기를 바랍니다) :SQL 내가 데이터를 얻을 TE 필요가 복잡

Table 1 is filled with traffic accidents(VKL_NUMBER and TIME) 
Table 2 is filled with the locations of the accidents and the closest weather station (VKL_NUMBER, LOCATION and STN_NUMBER) 
Table 3 is filled with weather data and the weather station where it came from(STN_NUMBER, TIME, WEATHER_TYPE) 
: 내 데이터베이스에있는 세 개의 테이블이 있습니다

비가 올 곳의 사고를 계산해야합니다. 테이블에 키는 다음과 같습니다

From 1 to 2: VKL_NUMBER(accident number) 
From 2 to 3: STN_NUMBER (weather station number) 

어떻게 카운트 처럼의 사고에 가장 가까운 시간에 weathertype을 얻을 수 있습니다

Count accidents where it rains at the closest weatherstation. 

대한 추가 정보를 원하시면 :

The accidents table has VKL_NUMBER(FK to the locations table) TIME(HHMM format) and DATE(YYMMDD format) 
The locations table has VKL_NUMBER(FK to accidents), LOCATION(not important for this question) and STN_NUMBER(FK to the weather table) 
The weather table had STN_NUMBER(FK to locations table), WEATHERTYPE("rain","snow","hail"), TIME(HHMM format) and DATE(YYMMDD format) 
+0

"비가 오는 곳"이라고하면이 정보를 제공하는 열을 구체적으로 말할 수 있습니까? 다른 필드와 동일합니다. – Adi

+0

사고 테이블의 VKL_NUMBER (위치 테이블에 FK) TIME (HHMM 형식) 및 DATE (YYMMDD 형식) 위치 테이블의 VKL_NUMBER (사고에 FK), LOCATION (이 질문에 중요하지 않음) 및 STN_NUMBER 날씨 테이블) 날씨 테이블에 STN_NUMBER (FK to locations 테이블), WEATHERTYPE ("rain", "snow", "우박"), TIME (HHMM 형식) 및 DATE (YYMMDD 형식) –

+0

테이블 1과 테이블 2가 분리되는 이유는 무엇입니까? ? – Strawberry

답변

1
/* apparently you'll need to combine and cast the <date + time> values */ 
select count(case when weather.weathertype = 'rain' then 1 end) 
from 
(
    select 
     accidents.vkl_number, 
     min(<accidents.date + accidents.time>) as time_of_accident, 
     min(weather.stn_number) as stn_number, 
     max(timestampdiff(minute, 
       <weather.date + weather.time>, 
       <accidents.date + accidents.time> 
     )) as differential 
    from 
     t1 accidents inner join t2 accident_locations 
      on accident_locations.vkl_number = accidents.vkl_number 
     inner join t3 weather 
      on weather.stn_number = accident_locations.stn_number 
       and weather.time <= accidents.time 
    group by accidents.vkl_number 
) closest 
    inner join t3 weather 
     on weather.stn_number = closest.stn_number 
      and date_add(
        <weather.date + weather.time>, 
        interval differential second 
       ) = closest.time_of_accident 

난 당신이 사고 직전 역 시간을 원하는 있으리라 믿고있어. 관계를 깨는 것은 중대한 합병증이며 원하는 일치하는 로직에 대한 자세한 정보가 필요합니다.

0

기상 관측소 테이블에 더 많은 시간이 있다고 가정합니다. 아마 매분마다 아닙니다. 그래서 일치 항목을 놓치지 않고 시간 필드에서 직접 테이블을 조인 할 수는 없습니다.

아래의 쿼리는 테스트되지 않았으며 방금 메모장에서 작성되었습니다.
하지만 이런 식으로 보일 것이라고 생각합니다.
시간은 시간 필드의 시간 부분에서 조인됩니다.

select wc.weather_type, acc.date, count(distinct acc.vkl_number) 
from traffic_accidents acc 
inner join location_accidents loc on (acc.vkl_number = loc.vkl_number) 
inner join weather_data ws on (loc.stn_number = ws.stn_number) 
where acc.date = ws.date 
and substring(acc.time,0,2) = substring(ws.time,0,2) 
and ws.weather_type = 'rain' 
and acc.date = '160602' 
group by ws.weather_type, acc.date; 
+0

안녕하세요. 시간은이 표의 핵심이 아닙니다. 그것은 1에서 2까지 : VKL_NUMBER (사고 번호) 2에서 3까지 : STN_NUMBER (기상 관측소 번호) –

+0

그리고 stn_number와의 링크는 두 번째 조인에 있습니다. 그러나 당신은 그 기상 관측소 테이블을보아야합니다. 비가 올 때 사고를 찾고 있다면 날씨 표를 고려해야하지 않겠습니까? – LukStorms

관련 문제