| 812 | |
| 813 | == CTE - Common Table Expressions == |
| 814 | |
| 815 | Common table expression are explained in details at: https://dev.mysql.com/doc/refman/8.0/en/with.html. For our query, the advantage is that the maths in is listed in the order in which we want it to be executed, not in opposite order. (Note that CTE requires MySQL 8). Here is the query using common table expressions (`WITH`). |
| 816 | |
| 817 | {{{#!sql |
| 818 | WITH Table9 AS |
| 819 | ( |
| 820 | WITH Table8 AS |
| 821 | ( |
| 822 | WITH Table7 AS |
| 823 | ( |
| 824 | WITH Table6 AS |
| 825 | ( |
| 826 | WITH Table5 AS |
| 827 | ( |
| 828 | WITH Table4 AS |
| 829 | ( |
| 830 | WITH Table3 AS |
| 831 | ( |
| 832 | WITH Table2 AS |
| 833 | ( |
| 834 | WITH Table1 AS |
| 835 | ( |
| 836 | WITH Table0 AS |
| 837 | ( |
| 838 | SELECT -- 0 |
| 839 | Weight, |
| 840 | Size, |
| 841 | NumUsedPixels, |
| 842 | NumIslands, |
| 843 | Leakage1, |
| 844 | MeanX, |
| 845 | MeanY, |
| 846 | CosDelta, |
| 847 | SinDelta, |
| 848 | M3Long, |
| 849 | SlopeLong, |
| 850 | Width/Length AS WdivL, |
| 851 | PI()*Width*Length AS Area, |
| 852 | cosa*X - sina*Y AS PX, |
| 853 | cosa*Y + sina*X AS PY |
| 854 | FROM RunInfo |
| 855 | LEFT JOIN Events USING (FileId) |
| 856 | LEFT JOIN Position USING (FileId, EvtNumber) |
| 857 | CROSS JOIN Wobble |
| 858 | WHERE |
| 859 | fSourceKey=5 |
| 860 | AND |
| 861 | fRunTypeKey=1 |
| 862 | AND |
| 863 | FileId BETWEEN 131101000 AND 131107000 |
| 864 | AND |
| 865 | fZenithDistanceMax<35 |
| 866 | AND |
| 867 | fR750Cor>0.9*fR750Ref |
| 868 | AND |
| 869 | NumUsedPixels>5.5 |
| 870 | AND |
| 871 | NumIslands<3.5 |
| 872 | AND |
| 873 | Leakage1<0.1 |
| 874 | |
| 875 | ) -- AS Table0 |
| 876 | |
| 877 | SELECT -- 1 |
| 878 | Weight, CosDelta, SinDelta, M3Long, SlopeLong, Leakage1, WdivL, |
| 879 | MeanX*1.025e0 - PX*1.0025e0 AS DX, |
| 880 | MeanY*1.025e0 - PY*1.0025e0 AS DY |
| 881 | FROM |
| 882 | Table0 |
| 883 | WHERE |
| 884 | Area < LOG10(Size)*898-1535 |
| 885 | |
| 886 | ) -- AS Table1 |
| 887 | |
| 888 | SELECT -- 2 |
| 889 | Weight, CosDelta, SinDelta, DX, DY, M3Long, SlopeLong, Leakage1, WdivL, |
| 890 | SQRT(DX*DX + DY*DY) AS Norm |
| 891 | FROM |
| 892 | Table1 |
| 893 | |
| 894 | ) -- AS Table2 |
| 895 | |
| 896 | SELECT -- 3 |
| 897 | Weight, M3Long, SlopeLong, Leakage1, WdivL, Norm, |
| 898 | TRUNCATE((CosDelta*DY - SinDelta*DX)/Norm, 6) AS LX, |
| 899 | TRUNCATE((CosDelta*DX + SinDelta*DY)/Norm, 6) AS LY |
| 900 | FROM |
| 901 | Table2 |
| 902 | |
| 903 | ) -- AS Table3 |
| 904 | |
| 905 | SELECT -- 4 |
| 906 | Weight, M3Long, SlopeLong, Leakage1, WdivL, LX, Norm, |
| 907 | ASIN(LX) AS Alpha, |
| 908 | SIGN(LY) AS Sign |
| 909 | FROM |
| 910 | Table3 |
| 911 | |
| 912 | ) -- AS Table4 |
| 913 | |
| 914 | SELECT -- 5 |
| 915 | Weight, Leakage1, WdivL, LX, |
| 916 | Norm *0.0117193246260285378e0 AS Dist, |
| 917 | M3Long *Sign*0.0117193246260285378e0 AS M3L, |
| 918 | SlopeLong*Sign/0.0117193246260285378e0 AS Slope |
| 919 | FROM |
| 920 | Table4 |
| 921 | |
| 922 | ) -- AS Table5 |
| 923 | |
| 924 | SELECT -- 6 |
| 925 | Weight, WdivL, Dist, LX, |
| 926 | 1.39252e0 + 0.154247e0*Slope + 1.67972e0*(1-1/(1+4.86232e0*Leakage1)) AS Xi, |
| 927 | M3L+0.07e0 AS Sign1, |
| 928 | (Dist-0.5e0)*7.2e0-Slope AS Sign2 |
| 929 | FROM |
| 930 | Table5 |
| 931 | |
| 932 | ) -- AS Table6 |
| 933 | |
| 934 | SELECT -- 7 |
| 935 | Weight, Dist, LX, |
| 936 | IF (SIGN(Sign1)<0 || SIGN(Sign2)<0, -Xi, Xi) * (1-WdivL) AS Disp |
| 937 | FROM |
| 938 | Table6 |
| 939 | |
| 940 | ) -- AS Table7 |
| 941 | |
| 942 | SELECT -- 8 |
| 943 | Weight, |
| 944 | (Disp*Disp + Dist*Dist - 2*Disp*Dist*SQRT(1-LX*LX)) AS ThetaSq |
| 945 | FROM |
| 946 | Table7 |
| 947 | |
| 948 | ) -- AS Table8 |
| 949 | |
| 950 | SELECT -- 9 |
| 951 | COUNT(IF(Weight>0, 1, NULL)) AS `Signal`, |
| 952 | COUNT(IF(Weight<0, 1, NULL)) AS `Background` |
| 953 | FROM |
| 954 | Table8 |
| 955 | WHERE |
| 956 | ThetaSq<0.024 |
| 957 | ) -- AS Table9 |
| 958 | |
| 959 | SELECT -- 10 |
| 960 | *, |
| 961 | `Signal` - `Background`/5 AS `Excess`, |
| 962 | LiMa(`Signal`, `Background`/5) AS `Significance` |
| 963 | FROM |
| 964 | Table9 |
| 965 | }}} |