Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.81 KB | None | 0 0
  1. WITH BASE AS (
  2. SELECT
  3.         'Detail' AS columntype  ,
  4.         {JobService}.[JobId] AS JobId,
  5.         {JobService}.[Id] AS JobServiceId,
  6.         {MainService}.[GoldenRecordId] AS MainServiceId,
  7.         {MainService}.[MainServiceName] AS MainServiceName,
  8.         {Service}.[GoldenRecordId] AS ServiceId,
  9.         {Service}.[ServiceName] AS ServiceName,
  10.         {Currency}.[GoldenRecordId] AS CurrencyId,
  11.         {Currency}.[CurrencyCode] AS CurrencyCode,
  12.         {Currency}.[CurrencyCode] + ' - ' + {Currency}.[CurrencyName] AS CurrencyDisplayName  ,    
  13.         {JobService}.[ExchangeRate] AS ExchangeRate,
  14.         {Company}.[Id] AS SupplierId,
  15.         {Company}.[Name] AS SupplierName,
  16.         {JobService}.[PDA] AS PDA,
  17.         {JobService}.[ManualPayment] AS ManualPayment,
  18.         '' AS DA_No,
  19.         {JobService}.[CloseEstimate] AS CloseEstimate,
  20.         {JobService}.[ActualAmount] AS Actual,
  21.         ABS({JobService}.[CloseEstimate] - {JobService}.[ActualAmount]) AS Variance,
  22.         {JobService}.[Comments] AS Comments,
  23.         {JobService}.[StatusId] AS StatusId,
  24.         {JobService}.[CreatedBy] AS CreatedBy,
  25.         getdate() AS CreatedOn,
  26.        
  27.         CASE WHEN EXISTS
  28.         (
  29.         SELECT {JobService_Invoice}.[JobServiceId]
  30.         FROM {JobService_Invoice}
  31.         INNER JOIN {Invoice} ON
  32.         {JobService_Invoice}.[InvoiceId] = {Invoice}.[Id]
  33.         AND
  34.         {JobService_Invoice}.[JobServiceId] =  JobServiceId
  35.         AND
  36.         {Invoice}.[IsSentToU4F] = 1
  37.        
  38.         )
  39.         THEN 0
  40.         ELSE 1
  41.         END AS isEditable,
  42.        
  43.         0 AS isSelected,
  44.         1 AS isServiceValid,
  45.         CASE
  46.         WHEN LEFT({Service}.[ServiceCode], 1) = @NotMandatorySuplierCaracter
  47.         THEN 0
  48.         ELSE 1
  49.         END AS isSupplierMandatory
  50.        
  51.       FROM  {JobService}
  52. INNER JOIN     {Service} ON {JobService}.[ServiceId]        =     {Service}.[GoldenRecordId]
  53. INNER JOIN {MainService} ON    {Service}.[MainServiceId]    = {MainService}.[GoldenRecordId]
  54.  LEFT JOIN     {Company} ON {JobService}.[SupplierId]       =     {Company}.[Id]
  55.  LEFT JOIN    {Currency} ON {JobService}.[CurrencyId]       =    {Currency}.[GoldenRecordId]
  56.  
  57.      WHERE {JobService}.[JobId] = @JobId  
  58.        AND ({JobService}.[SupplierId] = @SupplierSelected OR @SupplierSelected = 0  ))
  59.         ,
  60.        
  61. TOTAL AS (    
  62.  
  63. SELECT
  64.         'Total'  AS columntype ,  
  65.          NULL AS JobId,
  66.          NULL AS JobServiceId,
  67.          NULL AS MainServiceId,
  68.          NULL AS MainServiceName,
  69.          NULL AS ServiceId,
  70.          NULL AS ServiceName,
  71.          NULL AS CurrencyId,
  72.          NULL AS CurrencyCode,
  73.          NULL AS CurrencyDisplayName  ,    
  74.          NULL AS ExchangeRate,
  75.          NULL AS SupplierId,
  76.          NULL AS SupplierName,
  77.          (SELECT SUM(PDA) FROM base)  AS PDA,
  78.          NULL AS ManualPayment,
  79.          NULL AS DA_No,
  80.          (SELECT SUM(CloseEstimate) FROM base)  AS CloseEstimate,
  81.          (SELECT SUM(Actual) FROM base)  AS Actual,
  82.          (SELECT SUM(variance) FROM base) AS Variance,    
  83.          NULL AS Comments,
  84.          NULL AS StatusId,
  85.          NULL AS CreatedBy,
  86.          NULL AS CreatedOn,
  87.         0 AS isEditable,
  88.         0 AS isSelected,
  89.         1 AS isServiceValid,
  90.         0 AS isSupplierMandatory
  91.        
  92.         FROM base  WHERE (SELECT COUNT(0) FROM base ) >= 0
  93.  
  94.       )
  95.        
  96. SELECT * FROM base
  97. UNION ALL
  98. SELECT * FROM total GROUP BY    columntype ,  
  99. JobId,
  100. JobServiceId,
  101. MainServiceId,
  102. MainServiceName,
  103. ServiceId,
  104. ServiceName,
  105. CurrencyId,
  106. CurrencyCode,
  107. CurrencyDisplayName  ,    
  108. ExchangeRate,
  109. SupplierId,
  110. SupplierName,
  111. PDA,
  112. ManualPayment,
  113. DA_No,
  114. CloseEstimate,
  115. Actual,
  116. Variance,    
  117. Comments,
  118. StatusId,
  119. CreatedBy,
  120. CreatedOn,
  121. isEditable,
  122. isSelected,
  123. isServiceValid,
  124. isSupplierMandatory
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement