2017-12-15 19 views
1

내가 설정 두 개의 테이블이 요약 :SQL 조인 및 조건은 다음과

PMmx - 출발지 - 목적지 행렬의 테이블 버전은

Origin Destination Trips 
1   1  0.2 
2   1  0.3 
3   1  0.4 
.   .   . 
.   .   . 
1   1101  0.6 
2   1101  0.7 
3   1101  0.8 
.   .   . 
.   .   .  
1101  1   0.2 
1101  2   0.3 
1101  3   0.4 

ZE - 영역이있는 테이블은

을 동등성
Precinct Zone 
1   1101 
2   1102 
3   1111 

PMmx 테이블의 행 항목을의 Zone 열과 일치시키려는 항목을 선택하고 싶습니다.테이블. 예를 들어 :

Origin Destination Trips 
1   1101  0.6 
2   1101  0.7 
3   1101  0.8 
.   .   . 
.   .   .  
1101  1   0.2 
1101  2   0.3 
1101  3   0.4 

가 나는 또한 (중 Origin 또는 Destination이있는 열을 따라하여 총 여행은 특정 영역의 수를 합산됩니다 Trips/(Total Trips)을 계산 Distribution라는 새 열을 생성하고자하는 영역 등가 일치 Zone number). 해당 행 항목이 0.6/(0.6+0.7+0.8)을 할 수 있도록 Origin 1 예를 들어

, Destination (1101), 나는 새로운 Distribution 값을합니다.

다음 코드

SELECT 
     PMmx.Origin     as Origin 
    ,PMmx.Destination    as Destination 
    ,PMmx.Trips/sum(PMmx.Trips) as 'Distribution' 
FROM PMmx 

inner join ZE on Origin=ZE.Zone or Destination=ZE.Zone 

Group by Origin, Destination, Trips 

이 GROUP BY 절없이 같은 정확한 결과를 얻을 수 있다면 확실하지 오전을 시도 나는 Column '2DVISUM_2031PMmx_unpiv.Origin' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.를 얻을 그리고 GROUP BY 절과 나는에서 Divide by zero error encountered.

를 얻을 수 inner joinsums은 0이 아니어야합니다. 따라서이 오류가 발생하는 이유는 확실하지 않습니다.

도와주세요!

편집 :

ZE :

Precinct Zone  
1   1101  
2   1102  
3   1111  
4   1211 

PMX :

Origin Destination Trips 
1   1  0.20 
2   1  0.30 
3   1  0.40 
1   1101  0.60 
2   1101  0.70 
3   1101  0.80 
1101  1  0.20 
1101  2  0.30 
1101  3  0.40 
1101  1211  0.60 
1211  1101  0.50  
나는 오류를 표시하는 쿼리

with cte as (
    select 
    origin, destination, trips 
    , SUM(Trips) over(partition by Pmx.Origin) sum_trips 
    , trips/SUM(Trips) over(partition by Pmx.Origin) trips_div 
    from Pmx 
    inner join ZE on Pmx.Origin = ZE.Zone 
) 
select 
origin, destination, trips, sum_trips, trips_div 
from cte 
union all 
select 
destination, origin, trips, sum_trips, trips_div 
from cte 

업데이트 테이블을 사용하여 현재 중복 행을 얻고있다다른 여행의 값개

출력 포함 중복 :

origin destination trips sum_trips trips_div 

1101 1 0.20 1.50 0.13333333333333333333333333 
1101 2 0.30 1.50 0.20000000000000000000000000 
1101 3 0.40 1.50 0.26666666666666666666666666 
1101 1211 0.60 1.50 0.40000000000000000000000000 
1211 1101 0.50 0.50 1.00000000000000000000000000 
1  1101 0.20 1.50 0.13333333333333333333333333 
2  1101 0.30 1.50 0.20000000000000000000000000 
3  1101 0.40 1.50 0.26666666666666666666666666 
1211 1101 0.60 1.50 0.40000000000000000000000000 
1101 1211 0.50 0.50 1.00000000000000000000000000 

편집 2 : 내가 만들 걸려에 'if 문'그래서 Pmx.origin =ZE.Zone 경우 다음 trips_div 위와 같이 trips/SUM(Trips) over(partition by Pmx.Origin)입니다. 그러나 Pmx.origin =ZE.ZonePmx.destination=ZE.Zone 인 경우 trips_div이 여전히 trips/SUM(Trips) over(partition by Pmx.Origin) 이길 원합니다. Pmx.origin does not equal ZE.ZonePmx.destination=ZE.Zone 일 때 trips/SUM(Trips) over(partition by Pmx.Destination)입니다. 나는 다양한 case when 문장을 시도했지만 제대로 작동하지 않는 것처럼 보입니다.

나는 출력이되고 싶습니다 : 나는 당신의 요구 사항을 이해한다면

origin destination trips sum_trips trips_div 

    1  1101 0.20 2.10 0.0952380952380952 
    2  1101 0.30 2.10 0.1428571428571429 
    3  1101 0.40 2.10 0.1904761904761905 
    1101 1 0.20 1.50 0.1333333333333333 
    1101 2 0.30 1.50 0.2000000000000000 
    1101 3 0.40 1.50 0.2666666666666666 
    1101 1211 0.60 1.50 0.4000000000000000 
    1211 1101 0.50 0.50 1.0000000000000000 
+0

응답이 http://sqlfiddle.com/#!6/e8020/6를 볼 경우 식의 경우시'PMX입니다 .origin <> ZE.Zone 및 Pmx.destination = ZE.Zone 다음에/SUM (Trips) 이상 (Pmx.Destination에 의한 파티션) 그 밖의 trip/SUM (Trips) 이상 (Pmx.Origin에 의한 파티션) end'하지만 결과에 아무런 영향을 미치지 않습니다. –

답변

1

난 당신이 소스 테이블의 각 행에 그 합이 사용할 수있게 합에 대한 약간 다른 접근 방식을 사용할 수 있다고 생각합니다. 이것으로 당신은 group by 절이 필요 없습니다.

SELECT 
     PMmx.Origin     as Origin 
    , PMmx.Destination    as Destination 
    , (PMmx.Trips/sum(PMmx.Trips) over(partition by Destination)) as 'Distribution' 
FROM PMmx 
inner join ZE on Origin=ZE.Zone or Destination=ZE.Zone 

SQL Fiddle

MS SQL 서버 2014 스키마 설정 : 1

CREATE TABLE Pmx 
    ([Origin] int, [Destination] int, [Trips] decimal(12,2)) 
; 

INSERT INTO Pmx 
    ([Origin], [Destination], [Trips]) 
VALUES 
    (1, 1, 0.2), 
    (2, 1, 0.3), 
    (3, 1, 0.4), 
    (1, 1101, 0.6), 
    (2, 1101, 0.7), 
    (3, 1101, 0.8), 
    (1101, 1, 0.2), 
    (1101, 2, 0.3), 
    (1101, 3, 0.4) 
; 


CREATE TABLE ZE 
    ([Precinct] int, [Zone] int) 
; 

INSERT INTO ZE 
    ([Precinct], [Zone]) 
VALUES 
    (1, 1101), 
    (2, 1102), 
    (3, 1111) 
; 

검색어 :

with cte as (
    select 
    origin, destination, trips 
    , SUM(Trips) over(partition by Pmx.Origin) sum_trips 
    , trips/SUM(Trips) over(partition by Pmx.Origin) trips_div 
    from Pmx 
    inner join ZE on Pmx.Origin = ZE.Zone 
) 
select 
origin, destination, trips, sum_trips, trips_div 
from cte 
union -- changed to union so duplication is avoided 
select 
destination, origin, trips, sum_trips, trips_div 
from cte 

,519,287,319,736,969,643,210 : 2

SQL Fiddle

MS SQL 서버 2014 스키마 설정

| origin | destination | trips | sum_trips |   trips_div | 
|--------|-------------|-------|-----------|--------------------| 
| 1101 |   1 | 0.2 |  0.9 | 0.2222222222222222 | 
| 1101 |   2 | 0.3 |  0.9 | 0.3333333333333333 | 
| 1101 |   3 | 0.4 |  0.9 | 0.4444444444444444 | 
|  1 |  1101 | 0.2 |  0.9 | 0.2222222222222222 | 
|  2 |  1101 | 0.3 |  0.9 | 0.3333333333333333 | 
|  3 |  1101 | 0.4 |  0.9 | 0.4444444444444444 | 

부분 :

CREATE TABLE Pmx 
    ([Origin] int, [Destination] int, [Trips] decimal(12,2)) 
; 

INSERT INTO Pmx 
    ([Origin], [Destination], [Trips]) 
VALUES 
    (1, 1, 0.20), 
    (2, 1, 0.30), 
    (3, 1, 0.40), 
    (1, 1101, 0.60), 
    (2, 1101, 0.70), 
    (3, 1101, 0.80), 
    (1101, 1, 0.20), 
    (1101, 2, 0.30), 
    (1101, 3, 0.40), 
    (1101, 1211, 0.60), 
    (1211, 1101, 0.50) 
; 


CREATE TABLE ZE 
    ([Precinct] int, [Zone] int) 
; 

INSERT INTO ZE 
    ([Precinct], [Zone]) 
VALUES 
    (1, 1101), 
    (2, 1102), 
    (3, 1111), 
    (4, 1211) 
; 

검색어 1 :

with cte as (
    select 
    origin, destination, trips 
    , SUM(Trips) over(partition by Pmx.Origin) sum_trips 
    , trips/SUM(Trips) over(partition by Pmx.Origin) trips_div 
    from Pmx 
    inner join ZE on Pmx.Origin = ZE.Zone 
) 
select 
origin, destination, trips, sum_trips, trips_div 
from cte 
union 
select 
destination, origin, trips, sum_trips, trips_div 
from cte 
order by 1,2,3,4 

Results : 당신의 "편집 2"에

| origin | destination | trips | sum_trips |   trips_div | 
|--------|-------------|-------|-----------|---------------------| 
|  1 |  1101 | 0.2 |  1.5 | 0.13333333333333333 | 
|  2 |  1101 | 0.3 |  1.5 |     0.2 | 
|  3 |  1101 | 0.4 |  1.5 | 0.26666666666666666 | 
| 1101 |   1 | 0.2 |  1.5 | 0.13333333333333333 | 
| 1101 |   2 | 0.3 |  1.5 |     0.2 | 
| 1101 |   3 | 0.4 |  1.5 | 0.26666666666666666 | 
| 1101 |  1211 | 0.5 |  0.5 |     1 | 
| 1101 |  1211 | 0.6 |  1.5 |     0.4 | 
| 1211 |  1101 | 0.5 |  0.5 |     1 | 
| 1211 |  1101 | 0.6 |  1.5 |     0.4 | 

+0

정말 고마워요! 나는 제대로 처리하고 어떻게 든 반복되는 행 (잘못된 결합에서 나온 것임)으로 끝나면 여러 테이블로 문제를 깨뜨리는 결과를 낳았습니다. 일단 당신의 포스트를 읽으면 나는이 방법으로 그것을 다시하고 그것 모두는 작동하는 것처럼 보인다! 고맙습니다! – Ana

+0

내가 너무 빨리 말 했어 @Used_By_ 이미! 원본 및 대상이 모두 동등성 테이블 ZE와 일치하는 항목에 대해 중복 행을 얻는 것 같습니다. 예를 들어 Pmx에서 Origin이 1101이고 대상이 1102 인 경우이 문제가 발생하는 이유를 알 수 없습니다. – Ana

+0

문제가 복제 될 수 있도록 샘플 데이터를 추가 할 수 있는지 알 수 없습니다. –