Advertisement
zotov-vs

Untitled

Oct 8th, 2020
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.21 KB | None | 0 0
  1. SELECT
  2.        [sil].[OrderItemID] AS [orders_products_id]
  3.      , [sih].[Ship-to Code] AS [orders_id]
  4.      , [sil].[Campaign No_] AS [kupivip_categories_external_number]
  5.      , [sil].[No_] AS [kupivip_products_code]
  6.      , [sil].[Variant Code] AS [kupivip_variants_code]
  7.      , CAST([sil].[Orig_ Quantity] AS INT) AS [is_supplied]
  8.      , CAST([sil].[Quantity] AS INT) AS [is_sold]
  9.      , CAST([sil].[Orig_ Quantity] - [sil].[Quantity] AS INT) AS [is_deleted]
  10.      , CAST([sil].[Unit Price] AS DECIMAL(15, 2)) AS [products_price]
  11.      , CAST([sil].[Amount] AS DECIMAL(15, 2)) AS [products_sum]
  12.      , CAST([sil].[Amount Including VAT] AS DECIMAL(15, 2)) AS [products_sum_with_vat]
  13.      , (select VPS.[VAT %] from [KupiVip].[dbo].[Privat Trade$VAT Posting Setup] VPS
  14.     where VPS.[VAT Prod_ Posting Group] = i.[VAT Prod_ Posting Group]
  15.       and VPS.[VAT Bus_ Posting Group] = [sil].[VAT Bus_ Posting Group])
  16.      , ISNULL(
  17.        (
  18.         SELECT
  19.                -CAST(SUM([ve].[Cost Amount (Actual)]) AS DECIMAL(15, 2)) AS [products_cost]
  20.         FROM
  21.              [dbo].[Privat Trade$Value Entry](NOLOCK) AS [ve]
  22.         WHERE [ve].[Document Line No_] = [sil].[Line No_]
  23.               AND [ve].[Document No_] = [sil].[Document No_]
  24.               AND [ve].[Document Type] = 2
  25.        ), 0) AS [products_cost]
  26.      , ISNULL(
  27.        (
  28.         SELECT
  29.                MAX([ile].[Lot No_]) AS [lot]
  30.         FROM
  31.              [dbo].[Privat Trade$Value Entry](NOLOCK) AS [ve]
  32.              JOIN [dbo].[Privat Trade$Item Ledger Entry](NOLOCK) AS [ile]
  33.                  ON [ile].[Entry No_] = [ve].[Item Ledger Entry No_]
  34.         WHERE [ve].[Document Line No_] = [sil].[Line No_]
  35.               AND [ve].[Document No_] = [sil].[Document No_]
  36.               AND [ve].[Document Type] = 2 -- (Счет-факт. прод.)
  37.        ), '') AS [lot]
  38.      , ISNULL(
  39.        (
  40.         SELECT
  41.                MAX([l].[Deal Code]) AS [incomes_id]
  42.         FROM
  43.              [dbo].[Privat Trade$Value Entry](NOLOCK) AS [ve]
  44.              JOIN [dbo].[Privat Trade$Item Ledger Entry](NOLOCK) AS [ile]
  45.                  ON [ile].[Entry No_] = [ve].[Item Ledger Entry No_]
  46.              JOIN [Privat Trade$Lot](NOLOCK) AS [l]
  47.                  ON [l].[Code] = [ile].[Lot No_]
  48.         WHERE [ve].[Document Line No_] = [sil].[Line No_]
  49.               AND [ve].[Document No_] = [sil].[Document No_]
  50.               AND [ve].[Document Type] = 2 -- (Счет-факт. прод.)
  51.        ), 0) AS [incomes_id]
  52.      , ISNULL(
  53.        (
  54.         SELECT
  55.                MAX([l].[Campaign No_]) AS [incomes_categories_id]
  56.         FROM
  57.              [dbo].[Privat Trade$Value Entry](NOLOCK) AS [ve]
  58.              JOIN [dbo].[Privat Trade$Item Ledger Entry](NOLOCK) AS [ile]
  59.                  ON [ile].[Entry No_] = [ve].[Item Ledger Entry No_]
  60.              JOIN [Privat Trade$Lot](NOLOCK) AS [l]
  61.                  ON [l].[Code] = [ile].[Lot No_]
  62.         WHERE [ve].[Document Line No_] = [sil].[Line No_]
  63.               AND [ve].[Document No_] = [sil].[Document No_]
  64.               AND [ve].[Document Type] = 2 -- (Счет-факт. прод.)
  65.        ), '') AS [incomes_kupivip_categories_external_number]
  66.      , [sil].[Posting Date] AS [date_modified]
  67. FROM
  68.      [dbo].[Privat Trade$Sales Invoice Line](NOLOCK) AS [sil]
  69.      JOIN [dbo].[Privat Trade$Sales Invoice Header](NOLOCK) AS [sih]
  70.          ON [sil].[Document No_] = [sih].[No_]
  71. WHERE [sih].[Host Code] = 'MSY'
  72.       AND [sil].[Type] = 2 -- Товар
  73. --  AND [sih].[Ship-to Code] = 4482214
  74.     AND [sih].[Posting Date] < '2020-10-01'
  75.     AND [sih].[Ship-to Code] != ''
  76. ORDER BY
  77.          [orders_id]
  78.        , [orders_products_id];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement