Advertisement
imk0tter

Untitled

Feb 18th, 2012
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 8.95 KB | None | 0 0
  1. SELECT
  2.     A.EntryDate,
  3.     COALESCE(B.AuditCount,0) AS AuditCount,
  4.     COALESCE(B.TimerCount,0) AS AuditTimerCount,
  5.     COALESCE(B.AvgTimer,0) AS AuditAvgTimer,
  6.    
  7.     COALESCE(C.AuditCount,0) AS AuditCNTCount,
  8.     COALESCE(C.TimerCount,0) AS AuditCNTTimerCount,
  9.     COALESCE(C.AvgTimer,0) AS AuditCNTAvgTimer,
  10.    
  11.     COALESCE(D.AuditCount,0) AS AuditWTOCount,
  12.     COALESCE(D.TimerCount,0) AS AuditWTOTimerCount,
  13.     COALESCE(D.AvgTimer,0) AS AuditWTOAvgTimer,
  14.    
  15.     COALESCE(E.AuditCount,0) AS AuditPassCount,
  16.     COALESCE(E.TimerCount,0) AS AuditPassTimerCount,
  17.     COALESCE(E.AvgTimer,0) AS AuditPassAvgTimer,
  18.    
  19.     COALESCE(F.AuditCount,0) AS AuditFailCount,
  20.     COALESCE(F.TimerCount,0) AS AuditFailTimerCount,
  21.     COALESCE(F.AvgTimer,0) AS AuditFailAvgTimer,
  22.    
  23.     COALESCE(G.AuditCount,0) AS QCCount,
  24.     COALESCE(G.TimerCount,0) AS QCTimerCount,
  25.     COALESCE(G.AvgTimer,0) AS QCAvgTimer,
  26.    
  27.     COALESCE(H.AuditCount,0) AS QCPassCount,
  28.     COALESCE(H.TimerCount,0) AS QCPassTimerCount,
  29.     COALESCE(H.AvgTimer,0) AS QCPassAvgTimer,
  30.    
  31.     COALESCE(I.AuditCount,0) AS QCFailCount,
  32.     COALESCE(I.TimerCount,0) AS QCFailTimerCount,
  33.     COALESCE(I.AvgTimer,0) AS QCFailAvgTimer,
  34.    
  35.     COALESCE(J.ReceiveCount,0) AS ReceiveCount,
  36.     COALESCE(J.TimerCount,0) AS ReceiveTimerCount,
  37.     COALESCE(J.AvgTimer,0) AS ReceiveAvgTimer,
  38.    
  39.     COALESCE(K.TechCount,0) AS RepairCount,
  40.     COALESCE(K.JobEntryCount,0) AS RepairJobEntryCount,
  41.     COALESCE(K.TimerCount,0) AS RepairTimerCount,
  42.     COALESCE(K.AvgTimer,0) AS RepairAvgTimer,
  43.    
  44.     COALESCE(L.TechCount,0) AS RepairPassCount,
  45.     COALESCE(L.JobEntryCount,0) AS RepairPassJobEntryCount,
  46.     COALESCE(L.TimerCount,0) AS RepairPassTimerCount,
  47.     COALESCE(L.AvgTimer,0) AS RepairPassAvgTimer,
  48.    
  49.     COALESCE(M.TechCount,0) AS RepairCBFCount,
  50.     COALESCE(M.JobEntryCount,0) AS RepairCBFJobEntryCount,
  51.     COALESCE(M.TimerCount,0) AS RepairCBFTimerCount,
  52.     COALESCE(M.AvgTimer,0) AS RepairCBFAvgTimer,
  53.    
  54.     COALESCE(N.TechCount,0) AS RefurbishCount,
  55.     COALESCE(N.JobEntryCount,0) AS RefurbishJobEntryCount,
  56.     COALESCE(N.TimerCount,0) AS RefurbishTimerCount,
  57.     COALESCE(N.AvgTimer,0) AS RefurbishAvgTimer,
  58.    
  59.     COALESCE(O.TechCount,0) AS TechCount,
  60.     COALESCE(O.JobEntryCount,0) AS TechJobEntryCount,
  61.     COALESCE(O.TimerCount,0) AS TechTimerCount,
  62.     COALESCE(O.AvgTimer,0) AS TechAvgTimer
  63. FROM
  64.     DateTable AS A
  65.     LEFT JOIN
  66.     (
  67.         SELECT
  68.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  69.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  70.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  71.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  72.         FROM
  73.             AuditTable AS A
  74.         WHERE COALESCE(A.QC,0) = 0
  75.         GROUP BY AuditDate
  76.     ) AS B
  77.     ON A.EntryDate = B.AuditDate
  78.     LEFT JOIN
  79.     (
  80.         SELECT
  81.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  82.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  83.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  84.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  85.         FROM
  86.             AuditTable AS A
  87.         WHERE COALESCE(A.CNT, 0) = 1 AND COALESCE(A.QC,0) = 0
  88.         GROUP BY AuditDate
  89.     ) AS C
  90.     ON A.EntryDate = C.AuditDate
  91.     LEFT JOIN
  92.     (
  93.         SELECT
  94.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  95.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  96.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  97.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  98.         FROM
  99.             AuditTable AS A
  100.         WHERE COALESCE(A.WTO, 0) = 1 AND COALESCE(A.QC,0) = 0
  101.         GROUP BY AuditDate
  102.     ) AS D
  103.     ON A.EntryDate = D.AuditDate
  104.     LEFT JOIN
  105.     (
  106.         SELECT
  107.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  108.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  109.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  110.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  111.         FROM
  112.             AuditTable AS A
  113.         WHERE COALESCE(A.QC,0) = 0 AND COALESCE(A.WTO,0) = 0 AND COALESCE(A.CNT,0) = 0 AND COALESCE(A.PASS,0) = 1
  114.         GROUP BY AuditDate
  115.     ) AS E
  116.     ON A.EntryDate = E.AuditDate
  117.     LEFT JOIN
  118.     (
  119.         SELECT
  120.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  121.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  122.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  123.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  124.         FROM
  125.             AuditTable AS A
  126.         WHERE COALESCE(A.QC,0) = 0 AND COALESCE(A.WTO,0) = 0 AND COALESCE(A.CNT,0) = 0 AND COALESCE(A.PASS,0) = 0
  127.         GROUP BY AuditDate
  128.     ) AS F
  129.     ON A.EntryDate = F.AuditDate
  130.     LEFT JOIN
  131.     (
  132.         SELECT
  133.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  134.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  135.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  136.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  137.         FROM
  138.             AuditTable AS A
  139.         WHERE COALESCE(A.QC,0) = 1
  140.         GROUP BY AuditDate
  141.     ) AS G
  142.     ON A.EntryDate = G.AuditDate
  143.     LEFT JOIN
  144.     (
  145.         SELECT
  146.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  147.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  148.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  149.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  150.         FROM
  151.             AuditTable AS A
  152.         WHERE COALESCE(A.QC,0) = 1 AND COALESCE(A.PASS,0) = 1
  153.         GROUP BY AuditDate
  154.     ) AS H
  155.     ON A.EntryDate = H.AuditDate
  156.     LEFT JOIN
  157.     (
  158.         SELECT
  159.             COALESCE(COUNT(A.AuditID),0) AS AuditCount,
  160.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  161.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  162.             DATEADD(dd,0,DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  163.         FROM
  164.             AuditTable AS A
  165.         WHERE COALESCE(A.QC,0) = 1 AND COALESCE(A.PASS,0) = 0
  166.         GROUP BY AuditDate
  167.     ) AS I
  168.     ON A.EntryDate = I.AuditDate
  169.     LEFT JOIN
  170.     (
  171.         SELECT
  172.             COALESCE(COUNT(A.InventoryID),0) AS ReceiveCount,
  173.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  174.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  175.             DATEADD(dd,0,DATEDIFF(dd,0,A.ReceiveDate)) AS ReceiveDate
  176.         FROM
  177.             InventoryTable AS A
  178.         GROUP BY ReceiveDate
  179.     ) AS J
  180.     ON A.EntryDate = J.ReceiveDate
  181.     LEFT JOIN
  182.     (
  183.         SELECT
  184.             COALESCE(COUNT(A.TechID),0) AS TechCount,
  185.             COALESCE(SUM(A.JobEntryCount),0) AS JobEntryCount,
  186.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  187.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  188.             DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS RepairDate
  189.         FROM
  190.             (
  191.                 SELECT
  192.                     A.TechID,
  193.                     COUNT(A.JobEntryID) AS JobEntryCount,
  194.                     COALESCE(SUM(A.Timer),0) AS Timer
  195.                 FROM
  196.                     JobEntryTable AS A
  197.                     INNER JOIN
  198.                     JobTable AS B
  199.                     ON A.JobID = B.JobID
  200.                 WHERE
  201.                     COALESCE(B.Repair,0) = 1
  202.                 GROUP BY
  203.                     A.TechID
  204.             ) AS A
  205.             INNER JOIN
  206.             TechTable AS B
  207.             ON A.TechID = B.TechID
  208.         GROUP BY RepairDate
  209.     ) AS K
  210.     ON A.EntryDate = K.RepairDate
  211.     LEFT JOIN
  212.     (
  213.         SELECT
  214.             COALESCE(COUNT(A.TechID),0) AS TechCount,
  215.             COALESCE(SUM(A.JobEntryCount),0) AS JobEntryCount,
  216.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  217.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  218.             DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS RepairDate
  219.         FROM
  220.             (
  221.                 SELECT
  222.                     A.TechID,
  223.                     COUNT(A.JobEntryID) AS JobEntryCount,
  224.                     COALESCE(SUM(A.Timer),0) AS Timer
  225.                 FROM
  226.                     JobEntryTable AS A
  227.                     INNER JOIN
  228.                     JobTable AS B
  229.                     ON A.JobID = B.JobID
  230.                 WHERE
  231.                     COALESCE(B.Repair,0) = 1 AND COALESCE(A.CBF,0) = 0
  232.                 GROUP BY
  233.                     A.TechID
  234.             ) AS A
  235.             INNER JOIN
  236.             TechTable AS B
  237.             ON A.TechID = B.TechID
  238.         GROUP BY RepairDate
  239.     ) AS L
  240.     ON A.EntryDate = L.RepairDate
  241.     LEFT JOIN
  242.     (
  243.         SELECT
  244.             COALESCE(COUNT(A.TechID),0) AS TechCount,
  245.             COALESCE(SUM(A.JobEntryCount),0) AS JobEntryCount,
  246.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  247.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  248.             DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS RepairDate
  249.         FROM
  250.             (
  251.                 SELECT
  252.                     A.TechID,
  253.                     COALESCE(COUNT(A.JobEntryID),0) AS JobEntryCount,
  254.                     COALESCE(SUM(A.Timer),0) AS Timer
  255.                 FROM
  256.                     JobEntryTable AS A
  257.                     INNER JOIN
  258.                     JobTable AS B
  259.                     ON A.JobID = B.JobID
  260.                 WHERE
  261.                     COALESCE(B.Repair,0) = 1 AND COALESCE(A.CBF,0) = 1
  262.                 GROUP BY
  263.                     A.TechID
  264.             ) AS A
  265.             INNER JOIN
  266.             TechTable AS B
  267.             ON A.TechID = B.TechID
  268.         GROUP BY RepairDate
  269.     ) AS M
  270.     ON A.EntryDate = M.RepairDate
  271.     LEFT JOIN
  272.     (
  273.         SELECT
  274.             COALESCE(COUNT(A.TechID),0) AS TechCount,
  275.             COALESCE(SUM(A.JobEntryCount),0) AS JobEntryCount,
  276.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  277.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  278.             DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS RepairDate
  279.         FROM
  280.             (
  281.                 SELECT
  282.                     A.TechID,
  283.                     COALESCE(COUNT(A.JobEntryID),0) AS JobEntryCount,
  284.                     COALESCE(SUM(A.Timer),0) AS Timer
  285.                 FROM
  286.                     JobEntryTable AS A
  287.                     INNER JOIN
  288.                     JobTable AS B
  289.                     ON A.JobID = B.JobID
  290.                 WHERE
  291.                     COALESCE(B.Repair,0) = 0
  292.                 GROUP BY
  293.                     A.TechID
  294.             ) AS A
  295.             INNER JOIN
  296.             TechTable AS B
  297.             ON A.TechID = B.TechID
  298.         GROUP BY RepairDate
  299.     ) AS N
  300.     ON A.EntryDate = N.RepairDate
  301.     LEFT JOIN
  302.     (
  303.         SELECT
  304.             COALESCE(COUNT(A.TechID),0) AS TechCount,
  305.             COALESCE(SUM(A.JobEntryCount),0) AS JobEntryCount,
  306.             COALESCE(SUM(A.Timer),0) AS TimerCount,
  307.             COALESCE(AVG(A.Timer),0) AS AvgTimer,
  308.             DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS RepairDate
  309.         FROM
  310.             (
  311.                 SELECT
  312.                     A.TechID,
  313.                     COALESCE(COUNT(A.JobEntryID),0) AS JobEntryCount,
  314.                     COALESCE(SUM(A.Timer),0) AS Timer
  315.                 FROM
  316.                     JobEntryTable AS A
  317.                     INNER JOIN
  318.                     JobTable AS B
  319.                     ON A.JobID = B.JobID
  320.                 GROUP BY
  321.                     A.TechID
  322.             ) AS A
  323.             INNER JOIN
  324.             TechTable AS B
  325.             ON A.TechID = B.TechID
  326.         GROUP BY RepairDate
  327.     ) AS O
  328.     ON A.EntryDate = O.RepairDate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement