Numpy gives the wrong eigenvectors? - numpy

Okay, so this is really weird. I have this hermitian matrix and for some reason, the numpy eigenvectors are not actually eigenvectors! What!
matrix = np. array([[ 5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 61.14109509+774.06193172j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 3.11819585 -39.47715852j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 5. +0.j , -9.52578079 -11.43230285j, -9.52578079 -11.43230285j, -9.52578079 +11.43230285j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -244.56443004+597.56276449j, -8.63584441 -7.34967968j, 10.68293151 -3.8040208j , -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -12.47278593 -30.47570099j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , -9.52578079 +11.43230285j, 5. +0.j , 0. +0.j , -9.52578079 -11.43230285j, -9.52578079 +11.43230285j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 10.68293151 +3.8040208j , -550.26995517+421.06359727j, 0. +0.j , 10.68293151 -3.8040208j , -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -28.06376771 -21.47424346j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , -9.52578079 +11.43230285j, 0. +0.j , 5. +0.j , -9.52578079 -11.43230285j, 0. +0.j , -9.52578079 +11.43230285j, 0. +0.j , 0. +0.j , 0. +0.j , -8.63584441 +7.34967968j, 0. +0.j , 61.14109509+421.06359727j, -8.63584441 -7.34967968j, 0. +0.j , -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 3.11819585 -21.47424346j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , -9.52578079 -11.43230285j, -9.52578079 +11.43230285j, -9.52578079 +11.43230285j, 5. +0.j , -9.52578079 -11.43230285j, -9.52578079 -11.43230285j, -9.52578079 +11.43230285j, 0. +0.j , 0. +0.j , -2.04708711 +11.15370048j, -8.63584441 +7.34967968j, 10.68293151 +3.8040208j , -244.56443004+244.56443004j, -8.63584441 -7.34967968j, 10.68293151 -3.8040208j , -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -12.47278593 -12.47278593j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , -9.52578079 -11.43230285j, 0. +0.j , -9.52578079 +11.43230285j, 5. +0.j , 0. +0.j , -9.52578079 -11.43230285j, 0. +0.j , 0. +0.j , 0. +0.j , -2.04708711 +11.15370048j, 0. +0.j , 10.68293151 +3.8040208j , -550.26995517 +68.06526281j, 0. +0.j , 10.68293151 -3.8040208j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -28.06376771 -3.4713284j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , -9.52578079 -11.43230285j, -9.52578079 +11.43230285j, 0. +0.j , 5. +0.j , -9.52578079 -11.43230285j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -2.04708711 +11.15370048j, -8.63584441 +7.34967968j, 0. +0.j , 61.14109509 +68.06526281j, -8.63584441 -7.34967968j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 3.11819585 -3.4713284j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -9.52578079 -11.43230285j, -9.52578079 +11.43230285j, -9.52578079 +11.43230285j, 5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -2.04708711 +11.15370048j, -8.63584441 +7.34967968j, 10.68293151 +3.8040208j , -244.56443004-108.43390441j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -12.47278593 +5.53012912j, 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517-284.93307164j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -28.06376771 +14.53158665j], [ 61.14109509-774.06193172j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , -244.56443004-597.56276449j, 10.68293151 -3.8040208j , -8.63584441 -7.34967968j, -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 5. +0.j , 8.29952587 -8.790553j , 8.29952587 -8.790553j , 8.29952587 +8.790553j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , -8.63584441 +7.34967968j, -550.26995517-421.06359727j, 0. +0.j , -8.63584441 -7.34967968j, -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 8.29952587 +8.790553j , 5. +0.j , 0. +0.j , 8.29952587 -8.790553j , 8.29952587 +8.790553j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 10.68293151 +3.8040208j , 0. +0.j , 61.14109509-421.06359727j, 10.68293151 -3.8040208j , 0. +0.j , -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 0. +0.j , 8.29952587 +8.790553j , 0. +0.j , 5. +0.j , 8.29952587 -8.790553j , 0. +0.j , 8.29952587 +8.790553j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , -2.04708711 +11.15370048j, 10.68293151 +3.8040208j , -8.63584441 +7.34967968j, -244.56443004-244.56443004j, 10.68293151 -3.8040208j , -8.63584441 -7.34967968j, -2.04708711 -11.15370048j, 0. +0.j , 0. +0.j , 8.29952587 -8.790553j , 8.29952587 +8.790553j , 8.29952587 +8.790553j , 5. +0.j , 8.29952587 -8.790553j , 8.29952587 -8.790553j , 8.29952587 +8.790553j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , -2.04708711 +11.15370048j, 0. +0.j , -8.63584441 +7.34967968j, -550.26995517 -68.06526281j, 0. +0.j , -8.63584441 -7.34967968j, 0. +0.j , 0. +0.j , 0. +0.j , 8.29952587 -8.790553j , 0. +0.j , 8.29952587 +8.790553j , 5. +0.j , 0. +0.j , 8.29952587 -8.790553j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , -2.04708711 +11.15370048j, 10.68293151 +3.8040208j , 0. +0.j , 61.14109509 -68.06526281j, 10.68293151 -3.8040208j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 8.29952587 -8.790553j , 8.29952587 +8.790553j , 0. +0.j , 5. +0.j , 8.29952587 -8.790553j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -2.04708711 +11.15370048j, 10.68293151 +3.8040208j , -8.63584441 +7.34967968j, -244.56443004+108.43390441j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 8.29952587 -8.790553j , 8.29952587 +8.790553j , 8.29952587 +8.790553j , 5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517+284.93307164j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 61.14109509+774.06193172j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -244.56443004+597.56276449j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517+421.06359727j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 61.14109509+421.06359727j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -244.56443004+244.56443004j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517 +68.06526281j, 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 61.14109509 +68.06526281j, 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -244.56443004-108.43390441j, 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 340. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517-284.93307164j], [ 3.11819585 +39.47715852j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 61.14109509-774.06193172j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , -12.47278593 +30.47570099j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -244.56443004-597.56276449j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , -28.06376771 +21.47424346j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517-421.06359727j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 3.11819585 +21.47424346j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 61.14109509-421.06359727j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -12.47278593 +12.47278593j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -244.56443004-244.56443004j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -28.06376771 +3.4713284j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517 -68.06526281j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 3.11819585 +3.4713284j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 61.14109509 -68.06526281j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -12.47278593 -5.53012912j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -244.56443004+108.43390441j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j , 0. +0.j ], [ 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -28.06376771 -14.53158665j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -550.26995517+284.93307164j, 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , -5. +0.j ]])
testvec = LA.eigh(matrix)[1][:, 18]
testval = LA.eigh(matrix)[0][18]
print(testval)
print(np.dot(matrix, testvec)/testvec)
The output there is
28.081038605585686
[ -183.43245272+1.74213886e+01j 28.08103861+7.25481867e-11j
28.08103861+1.27643875e-11j 28.08103861-1.41589018e-12j
28.08103861-6.60184127e-13j 28.08103861+6.75330988e-12j
28.08103861+1.16790389e-14j 28.08103861-3.70296960e-12j
236.26728922+3.14873632e+02j -1079.23472835-4.74317731e+01j
28.08103861-9.92409189e-12j 28.08103861-3.35359500e-11j
28.08103861-1.04000538e-13j 28.08103861+2.78132206e-13j
28.08103861+4.88732504e-12j 28.08103861+3.98266910e-14j
28.08103861-1.37676972e-12j 586.99433236-4.65807994e+02j
-14.23315879+4.40331948e+03j 28.08103861+1.56567705e-09j
28.08103861-4.39930895e-10j 28.08103861-7.51553075e-12j
28.08103861+1.92682082e-11j 28.08103861+9.93263280e-11j
28.08103861+3.95907444e-13j 28.08103861-6.75700975e-12j
-118.22531925+5.61652861e+01j -2.79796252-1.38935399e+02j
28.08103861+3.03407400e-11j 28.08103861+5.30616379e-10j
28.08103861+3.10058409e-12j 28.08103861-5.18881898e-13j
28.08103861+3.52468153e-12j 28.08103861-4.98108172e-14j
28.08103861+3.96217802e-13j -238.25990447-8.91950754e+02j]
How does this make any sense? Some components give it right but others don't? What is the issue here?

Your problem happens because of precision of numbers close to 0, testvec would be:
array([-3.59900540e-19+0.00000000e+00j, -5.17637105e-04-2.76467148e-04j,
-2.15232299e-04+2.40919102e-04j, 8.68903778e-03+1.58504512e-02j,
1.31080038e-02+2.40391043e-02j, -7.77501222e-04-1.52668092e-03j,
-2.73682736e-01-6.93085398e-01j, 7.51273340e-03+1.59428750e-02j,
2.46330734e-16-7.52436308e-17j, 5.52080470e-20+2.96032521e-19j,
-7.05446819e-04-1.48378619e-04j, -6.84977730e-05-4.20362420e-04j,
-1.49592643e-02+2.24449546e-02j, -3.65064949e-02+3.22827890e-03j,
-4.67427537e-04-1.08629775e-03j, -1.42698210e-01-3.57459915e-02j,
3.32809901e-02-6.92791416e-03j, -4.41354873e-17-2.80018652e-17j,
5.42101086e-20-2.16840434e-19j, 1.01201785e-05-2.46242545e-05j,
-7.40223595e-06-3.96792638e-06j, 1.59380630e-03-7.50085850e-04j,
2.22688637e-03+3.68340275e-04j, 7.47964828e-05+1.05972530e-04j,
1.78419605e-01+6.68711013e-02j, -4.96084176e-03+2.62909133e-04j,
3.14618038e-17-1.33145850e-16j, 1.19770459e-18+4.40457133e-18j,
-6.97579774e-05-3.73394056e-04j, 9.88225468e-05-1.83875899e-04j,
-1.60717176e-02-1.45382244e-02j, -2.77458699e-02-2.33076540e-02j,
-2.06341303e-04-7.03094158e-04j, 5.14279493e-01-3.37559644e-01j,
3.56457556e-02-2.54713924e-02j, 7.10394311e-17+5.07212415e-17j])
If you look close the values that differs are in the entries close to 0, where usually there are precision problems in division.
When you do print(np.dot(matrix, testvec)/testvec) you are assuming none of the entries of testvec are 0 (or close). If you try this code:
np.isclose(np.dot(matrix, testvec), testval*testvec)
You will see that they are indeed the eigenvalues and eigenvectors.

Related

Outer reference to restrict inner group by

I have a series of Receive and Issue transactions. Received inventory can be at different costs per job material item, possibly received in multiple transactions. And there are two possible methods of receipt: Mfg Receipt and Receipt Lines. Where neither exist, the cost defaults to average inventory cost, which is reliably stored in Cost Type. Received cost is not reliably stored in an Issue transaction's Cost Type, so I have to search backward in the receipt transactions. This precedence is handled by COALESCE.
The aim is to return (reverse) issue to inventory at the same cost at which it was received. Subsequent receipts are possible, at a different cost, and returns should be at that cost, and not at costs from receipts prior or subsequent within the same job.
Transaction (prefix imt) joined to Cost Type (prefix int) look like this for Material ID 21:
Each MaterialIssue transactions can pair to each Receipt Lines, so I have to restrict the inner result to a single receive transaction to prevent Cartesian product, without which, I could get multiple results, as in material item 21:
I have tried to accomplish with this CTE, using ROW() ranking, commented with the imaginary desired method, but it's mind-bending and I'm not sure how to go about it:
DECLARE #bShowNonInventory BIT = 1
DECLARE #nCostingMethod INT = 1
DECLARE #vJobID VARCHAR (11) = '23532-02-01'
; WITH
MaterialIssues as (
SELECT
MI.imtJobID JobID
, MI.imtJobAssemblyID AssemblyID
, MI.imtJobMaterialID MaterialID
, SUM(
(
COALESCE(
MR.intActualUnitMaterialCost
, RL.intActualUnitMaterialCost
, intUnitMaterialCost
)
)
* - (intQuantity)
) NetCost
FROM PartTransactions MI -- Material Issue
INNER JOIN PartTransactionCosts
ON imtPartTransactionID = intPartTransactionID
LEFT JOIN /* get single, most recent received cost */
( SELECT * FROM
(
SELECT
imtJobID, imtJobAssemblyID, imtJobMaterialID
, imtPartTransactionID
, intActualUnitMaterialCost
, ROW_Number()
OVER (Partition by imtJobID, imtJobAssemblyID, imtJobMaterialID
Order by imtPartTransactionID DESC) as rn
--^ set of results from ranking too broad
FROM PartTransactionCosts
JOIN PartTransactions
on imtPartTransactionID=intPartTransactionID
WHERE imtTableName='MfgReceipts'
AND intCostType = 4
--< need outer reference here somehow: imtPartTransactionID < (earlier) MI.imtPartTransactionID
) as d
WHERE rn=1
) MR -- Receipt by Mfg Receipt
ON MI.imtJobID=MR.imtJobID
AND MI.imtJobAssemblyID=MR.imtJobAssemblyID
AND MI.imtJobMaterialID=MR.imtJobMaterialID
AND MR.imtPartTransactionID < MI.imtPartTransactionID --< dubious restriction on inner result
LEFT JOIN
( SELECT * FROM
(
SELECT imtJobID, imtJobAssemblyID, imtJobMaterialID
, intActualUnitMaterialCost, imtPartTransactionID
, ROW_Number()
OVER (Partition by imtJobID, imtJobAssemblyID, imtJobMaterialID
Order by imtPartTransactionID DESC) as rn
FROM PartTransactionCosts
JOIN PartTransactions
on imtPartTransactionID=intPartTransactionID
WHERE imtTableName='ReceiptLines'
AND intCostType = 4
--< need outer reference here somehow: imtPartTransactionID < (earlier) MI.imtPartTransactionID
) as d
WHERE rn=1
) RL -- Receipt by ReceiptLine
ON MI.imtJobID=RL.imtJobID
AND MI.imtJobAssemblyID=RL.imtJobAssemblyID
AND MI.imtJobMaterialID=RL.imtJobMaterialID
AND RL.imtPartTransactionID < MI.imtPartTransactionID --< dubious restriction on inner result
WHERE
imtSource = 3
AND imtNonInventoryTransaction <= #bShowNonInventory
AND intCostType = #nCostingMethod
GROUP BY MI.imtJobID
, MI.imtJobAssemblyID
, MI.imtJobMaterialID
)
-- test query:
Select * from
MaterialIssues
WHERE JobID = #vJobID
Note that this problem is very similar to Restricting inner query with outer query atttribute - but I don't see a requirement to restrict the inner set to one, most recent, but not subsequent result.
In summary, the net result of quantities received and (return) issued for item 21 is zero, but the net cost should also be zero, but is not correct.
Thank you in advance for your suggestions.
EDIT: DDL and sample data:
USE [master]
GO
CREATE DATABASE [test]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'test', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\test.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'test_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\test_log.ldf' , SIZE = 199680KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
USE test
CREATE TABLE [test].dbo.[PartTransactions](
[imtPartTransactionID] [int] NOT NULL,
[imtTableName] [nvarchar](30) NOT NULL,
[imtTransactionType] [tinyint] NOT NULL,
[imtSource] [tinyint] NOT NULL,
[imtNonInventoryTransaction] [bit] NOT NULL,
[imtJobID] [nvarchar](20) NOT NULL,
[imtJobAssemblyID] [int] NOT NULL,
[imtJobMaterialID] [int] NOT NULL,
[imtPartID] [nvarchar](30) NOT NULL,
) ON [PRIMARY]
GO
CREATE TABLE [test].[dbo].[PartTransactionCosts](
[intPartTransactionID] [int] NOT NULL,
[intPartTransactionCostID] [int] NOT NULL,
[intCostType] [tinyint] NOT NULL,
[intQuantity] [numeric](15, 5) NOT NULL,
[intUnitMaterialCost] [numeric](15, 5) NOT NULL,
[intActualUnitMaterialCost] [numeric](15, 5) NOT NULL,
) ON [PRIMARY]
GO
-- USE [test]
GO
INSERT [dbo].[PartTransactions] ([imtPartTransactionID], [imtTableName], [imtTransactionType], [imtSource], [imtNonInventoryTransaction], [imtJobID], [imtJobAssemblyID], [imtJobMaterialID], [imtPartID]) VALUES (117718, N'MaterialIssueLines', 2, 3, 0, N'23532-02-01', 0, 21, N'26271/2GC')
GO
INSERT [dbo].[PartTransactions] ([imtPartTransactionID], [imtTableName], [imtTransactionType], [imtSource], [imtNonInventoryTransaction], [imtJobID], [imtJobAssemblyID], [imtJobMaterialID], [imtPartID]) VALUES (117719, N'MfgReceipts', 1, 2, 1, N'23532-02-01', 0, 21, N'26271/2GC')
GO
INSERT [dbo].[PartTransactions] ([imtPartTransactionID], [imtTableName], [imtTransactionType], [imtSource], [imtNonInventoryTransaction], [imtJobID], [imtJobAssemblyID], [imtJobMaterialID], [imtPartID]) VALUES (117726, N'MaterialIssueLines', 2, 3, 0, N'23532-02-01', 0, 21, N'26271/2GC')
GO
INSERT [dbo].[PartTransactions] ([imtPartTransactionID], [imtTableName], [imtTransactionType], [imtSource], [imtNonInventoryTransaction], [imtJobID], [imtJobAssemblyID], [imtJobMaterialID], [imtPartID]) VALUES (117725, N'MfgReceipts', 1, 2, 1, N'23532-02-01', 0, 21, N'26271/2GC')
GO
INSERT [dbo].[PartTransactions] ([imtPartTransactionID], [imtTableName], [imtTransactionType], [imtSource], [imtNonInventoryTransaction], [imtJobID], [imtJobAssemblyID], [imtJobMaterialID], [imtPartID]) VALUES (117727, N'MaterialIssueLines', 2, 3, 0, N'23532-02-01', 0, 21, N'26271/2GC')
GO
INSERT [dbo].[PartTransactions] ([imtPartTransactionID], [imtTableName], [imtTransactionType], [imtSource], [imtNonInventoryTransaction], [imtJobID], [imtJobAssemblyID], [imtJobMaterialID], [imtPartID]) VALUES (117513, N'MfgReceipts', 1, 2, 1, N'23532-02-01', 0, 21, N'26271/2GC')
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117718, 1, 1, CAST(555.00000 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117718, 2, 2, CAST(555.00000 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117718, 3, 3, CAST(555.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117718, 4, 4, CAST(555.00000 AS Numeric(15, 5)), CAST(1.24865 AS Numeric(15, 5)), CAST(1.24865 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117719, 1, 1, CAST(555.00000 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117719, 2, 2, CAST(555.00000 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117719, 3, 3, CAST(555.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117719, 4, 4, CAST(555.00000 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117726, 1, 1, CAST(1100.00000 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117726, 2, 2, CAST(1100.00000 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117726, 3, 3, CAST(1100.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117726, 4, 4, CAST(1100.00000 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117725, 1, 1, CAST(555.00000 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117725, 2, 2, CAST(555.00000 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117725, 3, 3, CAST(555.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117725, 4, 4, CAST(555.00000 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117727, 1, 1, CAST(10.00000 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117727, 2, 2, CAST(10.00000 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117727, 3, 3, CAST(10.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117727, 4, 4, CAST(10.00000 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)), CAST(1.16410 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117513, 1, 1, CAST(555.00000 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)), CAST(1.61513 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117513, 2, 2, CAST(555.00000 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)), CAST(1.53503 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117513, 3, 3, CAST(555.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)), CAST(0.00000 AS Numeric(15, 5)))
GO
INSERT [dbo].[PartTransactionCosts] ([intPartTransactionID], [intPartTransactionCostID], [intCostType], [intQuantity], [intUnitMaterialCost], [intActualUnitMaterialCost]) VALUES (117513, 4, 4, CAST(555.00000 AS Numeric(15, 5)), CAST(1.24865 AS Numeric(15, 5)), CAST(1.24865 AS Numeric(15, 5)))
GO
UPDATE: What this does.
First of all, this is from a DB backend for an ERP system called M1. Its table structures are immutable.
Receiving material to inventory or directly to a job is done through "part transactions." A part transaction joins to one of 4 possible part transaction cost types: 1) average cost 2) Last cost (regardless of the job in question) 3) Standard cost 4) Actual cost (as received directly to a job). Which is to be used is fixed by company-wide accounting policy settings.
Average cost is used for transactions that issue to a job from inventory, or return to inventory (unused) from a job. This is calculated in application code (VB) upon each transaction.
Material issues and returns NOT involving inventory are received directly to a job through various kinds of receipts, such as from a Purchase Order or Manufacturing Receipt (tables: ReceiptLines, MfgReceipt) - and a number of other possible methods (e.g. DMR, RMI, Inspection, Scrap, Shipment, Adjustment). These entities do not have the same footprint and are stored in different tables.
Unfortunately, the application code does not reliably store the "received" cost to a job via Receipt (PO) or Manufacturing receipt, so the data in PartTransactionCost (type 4, Actual) cannot be used as intended. Usually it does calculate correctly, but mysteriously sometimes it doesn't. It's a bug.
Therefore the only reliable way to return materials from a job at the same cost at which they were recieved by any kind of receipt, is to get the actual cost from the applicable receipt record. The COALESCE function selects the non-null join, in order of precedence, and where no receipt is found (i.e. all null results), the cost of last resort is the Average cost.
In the sample data given, There was one receipt of 555 quantity of item 21 at a unit cost of 1.25, then all 555 were returned (i.e. unused). This is done through a MaterialIssue transaction (whose sign is positive, though MaterialIssues have a negative Qty), resulting in a Net Quantity 0, Net Cost 0.00.
Then two more receipts were done for a total quantity of 1100, but at a different recieved unit cost than that of the first receipt. Then two more return transactions occured resulting in an net issue quantity again of 0. However, the code I inherited did not result in a net cost of zero, because the simple join produced a cross product of 3 x 3 receive and return transactions, for a spurious result of 9 components in the sum. So the net returned cost was wrong by multiples. This error was not noticed for a long time because that sequence of transactions is rare.
It must be noted that in this example the Last costs happen to be correct, but since they cannot be relied upon, I needed to get the actual received cost from the receipt record (table: MfgReceipts), and not from the receipt transaction. And the same technique is needed for other reciept types, such as in the table ReceiptLines. But only one receipt record can be used. In the solution provided, the last receipt's actual cost is returned only once, and used in the return transaction in lieu of the stored "last cost" - this results in a net issue quantity of zero and a net cost of zero.
The essence of the solution:
-- solved by example of answer 1-1) at
-- https://stackoverflow.com/questions/9275132/real-life-example-when-to-use-outer-cross-apply-in-sql
SELECT *
FROM
(SELECT
MI.imtJobID JobID
, MI.imtJobAssemblyID AssemblyID
, MI.imtJobMaterialID MaterialID
, MI.imtPartTransactionID
, intQuantity
FROM PartTransactions MI -- Material Issue
INNER JOIN PartTransactionCosts
ON imtPartTransactionID = intPartTransactionID
WHERE imtSource=3
AND intCostType = 4 -- Average cost, last resort
) MI
OUTER APPLY
(SELECT TOP 1
imtJobID JobID
, imtJobAssemblyID AssemblyID
, imtJobMaterialID MaterialID
, intActualUnitMaterialCost, imtPartTransactionID
, imtTableName, intCostType
FROM PartTransactions PTI JOIN PartTransactionCosts PTCI
on PTI.imtPartTransactionID=PTCI.intPartTransactionID
WHERE imtTableName='MfgReceipts'
AND intCostType = 4
AND PTI.imtJobID=MI.JobID AND PTI.imtJobAssemblyID=MI.AssemblyID AND PTI.imtJobMaterialID=MI.MaterialID
AND PTI.imtPartTransactionID < MI.imtPartTransactionID
-- most recent recieved cost prior to this issuance
ORDER BY PTI.imtPartTransactionID DESC
) MR
order by MI.MaterialID, MI.imtPartTransactionID
and whose result is more easily visualized thus:
Full implementation looks like this:
; WITH
MaterialIssues as (
SELECT
MI.imtJobID JobID
, MI.imtJobAssemblyID AssemblyID
, MI.imtJobMaterialID MaterialID
,
/* abstraction for different job types & sources*/
(
COALESCE(
MR.intActualUnitMaterialCost
, RL.intActualUnitMaterialCost
, intUnitMaterialCost
)
+ intUnitLaborCost
+ intUnitOverheadCost
+ intUnitSubcontractCost
+ intUnitDutyCost
+ intUnitFreightCost
+ intUnitMiscCost
) UnitCost
, intQuantity
Quantity
, imtSource
, imtJobType
, imtNonInventoryTransaction
, imtReceiptID
, imtTableName
FROM PartTransactions MI
INNER JOIN PartTransactionCosts
ON imtPartTransactionID = intPartTransactionID
LEFT JOIN Warehouses
ON imtPartWarehouseLocationID = imwWarehouseID
OUTER APPLY /* get single, most recent receieved cost */
(
SELECT TOP 1
imtJobID JobID, imtJobAssemblyID AssemblyID, imtJobMaterialID MaterialID
, imtPartTransactionID
, intActualUnitMaterialCost
FROM PartTransactionCosts PTCI
JOIN PartTransactions PTI
on imtPartTransactionID=intPartTransactionID
WHERE imtTableName='MfgReceipts'
AND intCostType = 4
AND PTI.imtJobID=MI.imtJobID
AND PTI.imtJobAssemblyID=MI.imtJobAssemblyID
AND PTI.imtJobMaterialID=MI.imtJobMaterialID
AND PTI.imtPartTransactionID < MI.imtPartTransactionID
ORDER BY PTI.imtPartTransactionID DESC
) MR
OUTER APPLY
(
SELECT TOP 1
imtJobID JobID, imtJobAssemblyID AssemblyID, imtJobMaterialID MaterialID
, imtPartTransactionID
, intActualUnitMaterialCost
FROM PartTransactionCosts PTCI
JOIN PartTransactions PTI
on imtPartTransactionID=intPartTransactionID
WHERE imtTableName='ReceiptLines'
AND intCostType = 4
AND PTI.imtJobID=MI.imtJobID
AND PTI.imtJobAssemblyID=MI.imtJobAssemblyID
AND PTI.imtJobMaterialID=MI.imtJobMaterialID
AND PTI.imtPartTransactionID < MI.imtPartTransactionID
ORDER BY PTI.imtPartTransactionID DESC
) RL
WHERE
imtTransactionDate < #dEndDate
AND imtTransactionDate >= #dStartDate
AND (
imtNonNettable = 0
OR (
imtNonNettable <> 0
AND ISNULL(imwDoNotIncludeInJobCosts, 0) = 0
)
)
AND intCostType = #nCostingMethod
/* always 1 */
)
, PartTransactionMaterialCosts as (
SELECT JobID, AssemblyID
, SUM(
UnitCost
*
(
(
CASE
WHEN imtSource = 3
/*MaterialIssueLines*/
THEN - 1
ELSE 1
END
) * Quantity
)
)
NetCost /*NOTE: negated conditionally on Source 3*/
FROM MaterialIssues
WHERE
imtNonInventoryTransaction <= #bShowNonInventory
AND imtSource IN(2, 3)
AND imtJobType IN(1, 3)
AND imtReceiptID = ''
AND Upper(imtTableName) NOT IN ('RECEIPTLINES', 'MFGRECEIPTS', 'MFGRECEIPTCOMPONENTS', 'RECEIPTCOMPONENTS')
GROUP BY JobID, AssemblyID
)
-- test:
Select * from PartTransactionMaterialCosts

SQL calculations based on results previous rows - recursive

I'm trying to do a calculation within SQL which I'm struggling with.
Below is code to create the example that I am working with
CREATE TABLE #temptable ( [AccountNo] bigint, [Month] int,
[PaymentCycle] int, [OpeningBal] decimal(2,2), [Issue] decimal(13,2),
[Collections] decimal(38,2), [Rebates] decimal(38,2), [ClosingBal]
decimal(38,2) )
INSERT INTO #temptable
VALUES
( 1, 201703, 0, 0.00, 100.00, 0.00, 0.00, 100.00 ),
( 1, 201704, 1, NULL, 0.00, -31.60, 0.00, NULL ),
( 1, 201705, 2, NULL, 0.00, 0.00, 0.00, NULL ),
( 1, 201706, 3, NULL, 0.00, 0.00, 0.00, NULL ),
( 1, 201707, 4, NULL, 0.00, -60.00, 0.00, NULL ),
( 1, 201708, 5, NULL, 0.00, -78.00, 0.00, NULL ),
( 1, 201709, 6, NULL, 0.00, 0.00, 0.00, NULL ),
( 1, 201710, 7, NULL, 0.00, 0.00, 0.00, NULL ),
( 1, 201711, 8, NULL, 0.00, 0.00, 0.00, NULL )
In this example I know what the values will be for the first row (Payment Cycle 0) and therefore can manually feed these in, for all of the subsequent rows I need to do 3 calculations.
1) OpeningBal - This is the LAG of the previous rows closing figure
2) Revenue - Calculated as the OpeningBal x 20%
3) ClosingBal - This value is the sum of all components within the row so
OpeningBal + Revenue + Issue + Collections + Rebates
I am able to achieve this for Payment Cycle 1 using the LAG function however I want to this to cycle through and store the previous closing balances in memory to use in the next cycle - I've read up on recursive CTE's but am struggling to implement.
Any help would be greatly appreciated.
Regards,
Nathan
Edit: Added desired result below

Error while Updating SQL table - conversion of the varchar value overflowed an int column

I have to update trade number for all account where it is missing.
to update a trade number rules are :-
when Postcode for two or more account match then check whether any of the phone number is same or address match with wild card. if any of the crieteria got match than Trade number got updated.
DDL:-
DECLARE #MASTER TABLE
(
[Account Number] VARCHAR (15),
[Trade Number] VARCHAR (25),
[ADDRESS 1] VARCHAR (35),
[BRNAME] VARCHAR (35),
[POSTCODE] VARCHAR (25),
[Phone Number 1] VARCHAR (8000),
[Phone Number 2] VARCHAR (8000),
[NEW Phone Number 1]VARCHAR (8000),
[NEW Phone Number 2]VARCHAR (8000)
)
INSERT #MASTER
SELECT 'a0001205','', 'UNIT 39', 'SOUTH', 'RM15 4YG', '07967967404', '222222222','9988779988','' UNION ALL
SELECT 'a0001205','', 'UNIT 39', 'SOUTH', 'RM15 4YG', '222222222', '','','' UNION ALL
SELECT 'a1005338','', 'Unit 3', 'SOUTH', 'RM15 4YG', '07967967404', '','','' UNION ALL
SELECT 'a1005338','', 'Unit 3', 'SOUTH', 'RM15 4YG', '01708865140', '','','' UNION ALL
SELECT 'C5108924','119734','UNIT 39', 'BASILDON', 'RM15 4YG', '07967967404', '','','' UNION ALL
SELECT 'C5108924','119734','UNIT 39', 'BASILDON', 'RM15 4YG', '01708865140', '','','' UNION ALL
SELECT 'C5108965','', 'UNIT 39', 'BASILDON', 'RM15 4YG', '865140' , '','','' UNION ALL
SELECT 'C5108965','', 'UNIT 39', 'BASILDON', 'RM15 4YG', '865140' , '','','' UNION ALL
SELECT 'I0010050','', 'UNIT 39', 'WEST', 'RM15 4YG', '01708865140', '01708865140','','55555555' UNION ALL
SELECT 'I0010050','', 'UNIT 39', 'WEST', 'RM15 4YG', '', '01708865140','','' UNION ALL
SELECT 'I7000438','', 'Unit 3', 'EAST', 'RM15 4YG', '', '01708865140','','' UNION ALL
SELECT 'I7000438','', 'Unit 3', 'EAST', 'RM15 4YG', '55555555', '01708865140','',''
Code:
;with cte as (
select o.*, x.FoundTradeNumber
from #MASTER as o
cross apply (
select top 1 FoundTradeNumber = i.[Trade Number]
from #MASTER as i
where REPLACE(i.[POSTCODE], ' ', '')=REPLACE(o.[POSTCODE], ' ', '')
and nullif(i.[Trade Number],'') is not null
and (
nullif(i.[Phone Number 1],0) in (o.[Phone Number 1], o.[Phone Number 2],o.[NEW Phone Number 1], o.[NEW Phone Number 2])
or nullif(i.[Phone Number 2],0) in (o.[Phone Number 1], o.[Phone Number 2],o.[NEW Phone Number 1], o.[NEW Phone Number 2])
or nullif(i.[NEW Phone Number 1],0) in (o.[Phone Number 1], o.[Phone Number 2],o.[NEW Phone Number 1], o.[NEW Phone Number 2])
or nullif(i.[NEW Phone Number 2],0) in (o.[Phone Number 1], o.[Phone Number 2],o.[NEW Phone Number 1], o.[NEW Phone Number 2])
or i.[ADDRESS 1] LIKE o.[ADDRESS 1]+'%'
)
) as x
where nullif(o.[Trade Number],'') is null
and (o.[Phone Number 1] > 0 or o.[Phone Number 2] > 0 OR o.[NEW Phone Number 1] >0 OR o.[NEW Phone Number 2]>0 )
)
--select * from cte
update cte set [Trade Number] = FoundTradeNumber
Error:-
Msg 248, Level 16, State 1, Line 32
The conversion of the varchar value '07967967404' overflowed an int column.
i am not authorized to change Datatype of column.
Is there any other way to do this.
Thanks
This should works, You just need 0 within single quotes ('0')
nullif(i.[Phone Number 1],'0')
o.[Phone Number 1] > '0'
Result:

Conditional Unpivot SQL table

I have a table which looks like this <SubCodeReport3> which has a column called Rank. For each row I need to the what is the Rank and based on that value I need to unpivot SubCode columns (SubCode1,SubCode2 & Subcode3 etc) and convert them into rows.
As seen above for a
Rank 2 Subcode1 & SubCode2 have been unpivoted
Rank 1 SubCode1 has been unpivoted
Rank 3 Subcode1, Subcode2 & SubCode3 have been unpivoted.
there will not be case where the Rank is higher than the no. of available SubCode columns. Any Ideas?
Cursor though rows ?
Here is some SQL to create this sample table
USE TESTDB
GO
/****** Object: Table [dbo].[SubCodeReport3] Script Date: 10/6/2015 2:27:49 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[SubCodeReport3](
[ S-ID] [varchar](50) NULL,
[Rank] [smallint] NULL,
[AGE] [varchar](50) NULL,
[SchoolCode] [varchar](50) NULL,
[SubCode1] [varchar](50) NULL,
[SubCode2] [varchar](50) NULL,
[SubCode3] [varchar](50) NULL,
[SubCode4] [varchar](50) NULL,
[SubCode5] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[SubCodeReport3] ([ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'25', 1, N'23', N'KEN-009', N'ENG', N'MAT', N'ZOO', N'', N'')
INSERT [dbo].[SubCodeReport3] ([ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'26', 1, N'21', N'DLK-009', N'ENG', N'', N'', N'', N'')
INSERT [dbo].[SubCodeReport3] ([ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'27', 2, N'25', N'DLK-006', N'MAT', N'ENG', N'STAT', N'', N'')
INSERT [dbo].[SubCodeReport3] ([ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'28', 1, N'21', N'HLI-005', N'ENG', N'', N'', N'', N'')
INSERT [dbo].[SubCodeReport3] ([ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]) VALUES (N'30', 3, N'22', N'INN-009', N'ENG', N'MAT', N'ZOO', N'GEO', N'')
Start with a CTE or derived table that does a full unpivot of the table, and adds a partitioned row_number so that for each row in the original table, SubCode1 will be on row_number 1, SubCode2 on row_number 2, etc.
Then select from that CTE where row_number is less than or equal to [Rank].
You need to CROSS JOIN to a table with sequential numbers:
with cte as
(
select 1 as n
union all select 2
union all select 3
union all select 4
union all select 5
)
select Rank, Age, SchoolCode,
case n
when 1 then SubCode1
when 2 then SubCode2
when 3 then SubCode3
when 4 then SubCode4
when 5 then SubCode5
end as SubCode
from SubCodeReport3
join cte
on n <= rank
See Fiddle
Since you know the number of subcodes you can always do this and your question asks how to UNPIVOT:
select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode]
from (select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1]
from SubCodeReport3 SCR3
where SCR3.[Rank] = 1) R1
unpivot (SubCode FOR Subject IN (SubCode1)) as unpvt1
UNION ALL
select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode]
from (select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2]
from SubCodeReport3 SCR3
where SCR3.[Rank] = 2) R1
unpivot (SubCode FOR Subject IN (SubCode1, SubCode2)) as unpvt2
UNION ALL
select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode]
from (select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3]
from SubCodeReport3 SCR3
where SCR3.[Rank] = 3) R1
unpivot (SubCode FOR Subject IN (SubCode1, SubCode2, SubCode3)) as unpvt3
UNION ALL
select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode]
from (select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4]
from SubCodeReport3 SCR3
where SCR3.[Rank] = 4) R1
unpivot (SubCode FOR Subject IN (SubCode1, SubCode2, SubCode3, SubCode4)) as unpvt4
UNION ALL
select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode]
from (select [ S-ID], [Rank], [AGE], [SchoolCode], [SubCode1], [SubCode2], [SubCode3], [SubCode4], [SubCode5]
from SubCodeReport3 SCR3
where SCR3.[Rank] = 5) R1
unpivot (SubCode FOR Subject IN (SubCode1, SubCode2, SubCode3, SubCode4, SubCode5)) as unpvt5
See fiddle
This is what worked for me.
WITH CodesReportCTE
(
[Number]
,[ S-ID]
,[Rank]
,[AGE]
,[SchoolCode]
,[Code]
)
AS
(
SELECT
ROW_NUMBER() over (PARTITION BY [ S-ID],[SchoolCode] ORDER BY [ S-ID],[SchoolCode]) AS Number
,[ S-ID]
,[Rank]
,[AGE]
,[SchoolCode]
,up.Code [Code]
FROM [dbo].[SubCodeReport3]
UNPIVOT
(
Code
for x in (SubCode1,SubCode2,SubCode3,SubCode4,SubCode5) ) up
WHERE up.Code <> ' '
)
SELECT
--[Number]
--,
[ S-ID]
,[Rank]
,[AGE]
,[SchoolCode]
,[Code]
FROM CodesReportCTE
WHERE Number <= [Rank]

Average problems with different tables

I have a database with the following structure with the following registers
CREATE TABLE aircraft (
number_aircraft int(11) ,
type_aircraft VARCHAR(10),
PRIMARY KEY ( number_aircraft )
);
CREATE TABLE passenger (
number_flight int(11) ,
ID int(11),
Name char(10) DEFAULT NULL,
PRIMARY KEY ( number_flight , ID ),
FOREIGN KEY ( number_flight ) REFERENCES flight ( number_flight )
);
CREATE TABLE flight (
number_flight int(11) ,
since char(2) ,
until char(2) ,
Date_flight date ,
number_aircraft int(11),
PRIMARY KEY ( number_flight,number_aircraft ),
FOREIGN KEY ( number_aircraft ) REFERENCES aircraft ( number_aircraft )
);
INSERT INTO aircraft ( number_aircraft , type_aircraft ) VALUES (1,'B-777');
INSERT INTO aircraft ( number_aircraft , type_aircraft ) VALUES (2,'B-755');
INSERT INTO aircraft ( number_aircraft , type_aircraft ) VALUES (3,'B-775');
INSERT INTO aircraft ( number_aircraft , type_aircraft ) VALUES (4,'B-505');
INSERT INTO aircraft ( number_aircraft , type_aircraft ) VALUES (5,'Airbus A3');
INSERT INTO aircraft ( number_aircraft , type_aircraft ) VALUES (6,'B-707');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (1,34889876,'German');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (1,44859776,'Ariel');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (2,34587880,'Andres');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (2,34886463,'David');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (3,24757385,'Juan');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (3,34885768,'Marcos');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (4,10000001,'Bárbara');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (4,20000002,'Daniela');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (5,20000003,'Alejandra');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (5,20000004,'Melina');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (6,30000001,'Daniela');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (6,30000002,'Valeria');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (7,40000001,'Silvana');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (7,50000001,'Cecilia');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (8,50000002,'Noelia');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (8,50000003,'Cintia');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (9,60000001,'Mariela');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (9,60000002,'Ornella');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (10,60000003,'Cecilia');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (10,60000004,'Valeria');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (11,60000005,'Agustina');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (11,60000006,'Aldana');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (12,60000007,'Carolina');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (12,60000008,'Cecilia');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (13,70000001,'Bárbara');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (13,70000002,'Gisele');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (14,70000003,'Cecilia');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (14,70000004,'Vicuntilria');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (15,60000009,'Sabrina');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (15,60000010,'Agostina');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (16,80000001,'Andrea');
INSERT INTO passenger ( number_flight , ID , Name ) VALUES (16,80000002,'Tamara');
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (1,'A','A','2001-02-14',1);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (2,'A','F','2010-05-15',1);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (3,'A','D','2012-08-09',4);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (4,'B','H','2001-02-14',3);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (5,'D','H','2005-06-17',4);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (6,'A','D','2007-03-17',1);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (7,'A','F','2001-02-19',2);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (8,'C','F','2011-03-14',2);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (9,'A','B','2013-03-02',4);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (10,'A','B','2010-03-12',3);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (11,'A','D','2011-04-28',3);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (12,'C','B','2013-01-01',4);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (13,'B','D','2013-03-21',5);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (14,'A','B','2012-03-02',5);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (15,'D','H','2013-06-06',6);
INSERT INTO flight ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (16,'A','H','2013-12-13',6);
I have to list the average number of passengers transported by aircraft
company, by aircraft type, as I do? attempt to use the avg () but
I do not use it when I have a join.
If I understand you correctly, this should do it, simpler done using two COUNT DISTINCT instead of AVG; (sorry, with the original question's names)
SELECT a.Tipo_avion, COUNT(DISTINCT p.Documento)/COUNT(DISTINCT v.Nro_vuelo) avg_passengers
FROM vuelo v
LEFT JOIN pasajero p
ON v.Nro_Vuelo = p.Nro_Vuelo
LEFT JOIN avion a
ON v.Nro_Avion = a.Nro_Avion
GROUP BY a.Tipo_avion
An SQLfiddle to test with.