Differences

This shows you the differences between two versions of the page.

Link to this comparison view

control_sql_-_wip_and_built_integrity_check [2019/01/27 11:29]
127.0.0.1 external edit
control_sql_-_wip_and_built_integrity_check [2019/02/05 11:30]
admin
Line 24: Line 24:
  
 <code sql> <code sql>
-select   +SELECT   
-        (select DivisionName +        (SELECT DivisionName 
-                from Division +                FROM Division 
-                where coalesce(GLDivisionID, OrderDivisionID) = Division.ID) as Division,+                WHERE COALESCE(GLDivisionID, OrderDivisionID) = Division.ID) AS Division,
         OrderNumber,         OrderNumber,
-        GLStatusText as GLBalanceIn, +        GLStatusText AS GLBalanceIn, 
-        TransHeader.StatusText as CurrentOrderStatus,+        TransHeader.StatusText AS CurrentOrderStatus,
         TransHeader.SubTotalPrice,         TransHeader.SubTotalPrice,
         GLAmount,         GLAmount,
         OrdersDueAmount,                 OrdersDueAmount,        
         Account.CompanyName,         Account.CompanyName,
-        TransHeader.Description as OrderDescription, +        TransHeader.Description AS OrderDescription, 
-        (select max(EntryDateTime)  +        (SELECT MAX(EntryDateTime)  
-                from GL +                FROM GL 
-                where TransactionID = TransHeader.ID) as LastGLDate, +                WHERE TransactionID = TransHeader.ID) AS LastGLDate, 
-        TransHeader.ID as TransHeaderID +        TransHeader.ID AS TransHeaderID 
-         +  
-from+FROM
 ( (
-    select *  +    SELECT *  
-    from+    FROM
         (         (
-          Select ID as OrderTHID, +          SELECT ID AS OrderTHID, 
-                coalesce(DivisionID, 10) as OrderDivisionID, +                COALESCE(DivisionID, 10) AS OrderDivisionID, 
-                SubTotalPrice as OrderAmount, +                SubTotalPrice AS OrderAmount, 
-                'WIP' as OrderStatusText, +                'WIP' AS OrderStatusText, 
-                AccountID as OrderAccountID +                AccountID AS OrderAccountID 
-          from TransHeader +          FROM TransHeader 
-          where StatusID = 1 and SubTotalPrice  0+          WHERE StatusID = 1 AND SubTotalPrice <> 0
         ) A         ) A
-        full join+        FULL JOIN
         (         (
-          select TransactionID as GLTHID, +          SELECT TransactionID AS GLTHID, 
-                coalesce(DivisionID, 10) as GLDivisionID, +                COALESCE(DivisionID, 10) AS GLDivisionID, 
-                'WIP' as GLStatusText, +                'WIP' AS GLStatusText, 
-                Sum(Amount) as GLAmount, +                SUM(Amount) AS GLAmount, 
-                AccountID as GLsAccountID +                AccountID AS GLsAccountID 
-          from GL +          FROM GL 
-          where GLAccountID = 11 +          WHERE GLAccountID = 11 
-          group by coalesce(DivisionID, 10), TransactionID, AccountID +          GROUP BY COALESCE(DivisionID, 10), TransactionID, AccountID 
-          having Sum(Amount)  +          HAVING SUM(Amount) <> 
-        ) B on A.OrderTHID = B.GLTHID +        ) B ON A.OrderTHID = B.GLTHID 
-        left join+  
 +        LEFT JOIN
         (         (
-          select TransactionID as OrdersDueTHID, +          SELECT TransactionID AS OrdersDueTHID, 
-                -Sum(Amount) as OrdersDueAmount +                -SUM(Amount) AS OrdersDueAmount 
-          from GL +          FROM GL 
-          where GLAccountID = 21 +          WHERE GLAccountID = 21 
-          group by TransactionID +          GROUP BY TransactionID 
-          having Sum(Amount)  +          HAVING SUM(Amount) <> 
-        ) G on G.OrdersDueTHID = coalesce(A.OrderTHID, B.GLTHID) +        ) G ON G.OrdersDueTHID = COALESCE(A.OrderTHID, B.GLTHID) 
-         +  
-        Where (COALESCE (A.OrderAmount, 0) - COALESCE (B.GLAmount, 0) not between -0.01 and 0.01)        +        WHERE (COALESCE (A.OrderAmount, 0) - COALESCE (B.GLAmount, 0) NOT BETWEEN -0.01 AND 0.01)        
-        union  +  
-        select +        UNION  
-        from+  
 +        SELECT 
 +        FROM
         (         (
-          Select ID as OrderTHID, +          SELECT ID AS OrderTHID, 
-                coalesce(DivisionID, 10) as OrderDivisionID, +                COALESCE(DivisionID, 10) AS OrderDivisionID, 
-                SubTotalPrice as OrderAmount, +                SubTotalPrice AS OrderAmount, 
-                'Built' as OrderStatusText, +                'Built' AS OrderStatusText, 
-                AccountID as OrderAccountID +                AccountID AS OrderAccountID 
-          from TransHeader +          FROM TransHeader 
-          where StatusID = 2 and SubTotalPrice  0+          WHERE StatusID = 2 AND SubTotalPrice <> 0
         ) C         ) C
-        full join+        FULL JOIN
         (         (
-          select TransactionID as GLTHID, +          SELECT TransactionID AS GLTHID, 
-                coalesce(DivisionID, 10) as GLDivisionID, +                COALESCE(DivisionID, 10) AS GLDivisionID, 
-                'Built' as GLStatusText, +                'Built' AS GLStatusText, 
-                Sum(Amount) as GLAmount, +                SUM(Amount) AS GLAmount, 
-                AccountID as GLsAccountID +                AccountID AS GLsAccountID 
-          from GL +          FROM GL 
-          where GLAccountID = 12 +          WHERE GLAccountID = 12 
-          group by coalesce(DivisionID, 10), TransactionID, AccountID +          GROUP BY COALESCE(DivisionID, 10), TransactionID, AccountID 
-          having Sum(Amount)  +          HAVING SUM(Amount) <> 
-        ) D on C.OrderTHID = D.GLTHID +        ) D ON C.OrderTHID = D.GLTHID 
-         +  
-        left join+        LEFT JOIN
         (         (
-          select TransactionID as OrdersDueTHID, +          SELECT TransactionID AS OrdersDueTHID, 
-                -Sum(Amount) as OrdersDueAmount +                -SUM(Amount) AS OrdersDueAmount 
-          from GL +          FROM GL 
-          where GLAccountID = 21 +          WHERE GLAccountID = 21 
-          group by TransactionID +          GROUP BY TransactionID 
-          having Sum(Amount)  +          HAVING SUM(Amount) <> 
-        ) K on K.OrdersDueTHID = coalesce(C.OrderTHID, D.GLTHID) +        ) K ON K.OrdersDueTHID = COALESCE(C.OrderTHID, D.GLTHID) 
-         +  
-        Where (COALESCE (C.OrderAmount, 0) - COALESCE (D.GLAmount, 0) not between -0.01 and 0.01)       +        WHERE (COALESCE (C.OrderAmount, 0) - COALESCE (D.GLAmount, 0) NOT BETWEEN -0.01 AND 0.01)       
 ) E ) E
-left Join TransHeader on TransHeader.ID = coalesce(OrderTHID, GLTHID) +  
-left join Account on Account.ID = coalesce(OrderAccountID, GLsAccountID)+LEFT JOIN TransHeader ON TransHeader.ID = COALESCE(OrderTHID, GLTHID) 
 +  
 +LEFT JOIN Account ON Account.ID = COALESCE(OrderAccountID, GLsAccountID) 
 + 
 ORDER BY Division, OrderNumber ORDER BY Division, OrderNumber
 </code> </code>