Advertisement
Pashanze

Untitled

Aug 7th, 2024 (edited)
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 18.07 KB | None | 0 0
  1. SELECT  imns_code,
  2.     dictGetString('tables_fields_dictionaries_ids', 'dictionary_ids',
  3.                 tuple('imns_code')) AS imns_code_dictionary_id,
  4.         unp,
  5.         short_name,
  6.         date_registration,
  7.         state_code,
  8.         dictGetString('tables_fields_dictionaries_ids', 'dictionary_ids',
  9.                 tuple('payer_state_code')) AS state_code_dictionary_id,
  10.         date_begin_state,
  11.         dictGetUInt8('addresses_info', 'is_mass_registration',
  12.                 tuple('address_code')) AS is_mass_registration,
  13.         address,
  14.  
  15.         -- БП 81
  16.         date_from,
  17.         date_to,
  18.         reasons,
  19.  
  20.         -- БП 135
  21.         period_halfyear,
  22.  
  23.         -- БП 201
  24.         indicator,
  25.  
  26.         payer_unp_is_quarter,
  27.  
  28.         -- БП 8
  29.         oked_code_1,
  30.         oked_code_dictionary_id AS oked_code_1_dictionary_id,
  31.         oked_code_2,
  32.         oked_code_dictionary_id AS oked_code_2_dictionary_id,
  33.         oked_codes_declarations_codes,
  34.         declarations_codes_dictionary_id AS oked_codes_declarations_codes_dictionary_id,
  35.         oked_codes_period_codes,
  36.         period_codes_dictionary_id AS oked_codes_period_codes_dictionary_id,
  37.  
  38.         -- БП 5
  39.         sum_revenue_declarations_codes,
  40.         declarations_codes_dictionary_id AS sum_revenue_declarations_codes_dictionary_id,
  41.         sum_revenue_period_codes,
  42.         period_codes_dictionary_id AS sum_revenue_period_codes_dictionary_id,
  43.         sum_revenue,
  44.  
  45.         -- БП 6
  46.         count_employees_declarations_codes,
  47.         declarations_codes_dictionary_id AS count_employees_declarations_codes_dictionary_id,
  48.         count_employees_period_codes,
  49.         period_codes_dictionary_id AS count_employees_period_codes_dictionary_id,
  50.         count_employees,
  51.  
  52.         -- БП 9
  53.         provider_tax_burden,
  54.  
  55.         sum_total_cost_vat,
  56.         sum_total_vat,
  57.         sum_signed_total_vat,
  58.  
  59.         sum_signed_by_monthly_counterparty_total_vat,
  60.         sum_signed_by_monthly_counterparty_after_declaration_period_end_total_vat,
  61.         sum_signed_by_quarter_counterparty_after_declaration_period_end_total_vat,
  62.         sum_accepted_by_monthly_counterparty_after_declaration_period_end_vat_amount,
  63.         sum_accepted_by_monthly_counterparty_vat_amount,
  64.         sum_not_signed_total_vat,
  65.         sum_not_signed_by_monthly_counterparty_total_vat,
  66.  
  67.         --БП 134(БП 200(3 поля), БП 284 (1 поле), БП 134(1 поле), БП5, БП6, БП125)
  68.         sale_turnover,
  69.         sale_turnover_period_code,
  70.         sale_turnover_period_number,
  71.         accrued_nds,
  72.         tax_deduction,
  73.         nds_period_number,
  74.         nds_period_code,
  75.             period_codes_dictionary_id AS nds_period_code_dictionary_id,
  76.         nds,
  77.  
  78.         --БП 116
  79.         credited_from_1001_nds_amount,
  80.         sum_total_vat - accrued_nds AS deviation_vat_amount
  81.         deviation_vat_amount - sum_not_signed_total_vat - sum_accepted_by_monthly_counterparty_after_declaration_period_end_vat_amount + credited_from_1001_nds_amount AS tax_gap_vat_amount, --46A
  82.         tax_gap_amount,
  83.  
  84.         risk_group_code,
  85.         dictGetString('tables_fields_dictionaries_ids', 'dictionary_ids',
  86.                      tuple('risk_group_code')) AS risk_group_dictionary_id,
  87.         total_score,
  88.         DATE
  89.  
  90. FROM (
  91.     SELECT *,
  92.          dictGetString('tables_fields_dictionaries_ids', 'dictionary_ids',
  93.                 tuple('oked_code')) AS oked_code_dictionary_id,
  94.          dictGetString('tables_fields_dictionaries_ids', 'dictionary_ids',
  95.                 tuple('rn_declaration_type_code')) AS declarations_codes_dictionary_id,
  96.          dictGetString('tables_fields_dictionaries_ids', 'dictionary_ids',
  97.                 tuple('rn_declaration_period_type_code')) AS period_codes_dictionary_id
  98.     FROM(
  99.         SELECT*
  100.         FROM(
  101.             SELECT *
  102.             FROM(
  103.                 SELECT *
  104.                 FROM(
  105.                     SELECT *
  106.                     FROM(
  107.                         SELECT *
  108.                         FROM(
  109.                             SELECT *
  110.                             FROM(
  111.                                 SELECT *
  112.                                 FROM (
  113.                                     SELECT *
  114.                                     FROM(
  115.  
  116.                                         SELECT tax.*
  117. FROM (SELECT declaration_month_number,
  118.              payer_unp,
  119.              argMax(payer_unp_is_quarter, date_calculated)               AS payer_unp_is_quarter,
  120.              argMax(sum_total_cost_vat, date_calculated)                 AS sum_total_cost_vat,
  121.              argMax(sum_total_vat, date_calculated)                      AS sum_total_vat,
  122.              argMax(tuple(sum_signed_total_vat), date_calculated).1      AS sum_signed_total_vat,
  123.              argMax(tuple(sum_signed_by_monthly_counterparty_total_vat),
  124.                     date_calculated).1                                   AS sum_signed_by_monthly_counterparty_total_vat,
  125.              argMax(tuple(sum_signed_by_monthly_counterparty_after_declaration_period_end_total_vat),
  126.                     date_calculated).1                                   AS sum_signed_by_monthly_counterparty_after_declaration_period_end_total_vat,
  127.              argMax(tuple(sum_accepted_by_monthly_counterparty_after_declaration_period_end_vat_amount),
  128.                     date_calculated).1                                   AS sum_accepted_by_monthly_counterparty_after_declaration_period_end_vat_amount,
  129.              argMax(tuple(sum_signed_by_quarter_counterparty_after_declaration_period_end_total_vat),
  130.                     date_calculated).1                                   AS sum_signed_by_quarter_counterparty_after_declaration_period_end_total_vat,
  131.              argMax(tuple(sum_accepted_by_monthly_counterparty_vat_amount),
  132.                     date_calculated).1                                   AS sum_accepted_by_monthly_counterparty_vat_amount,
  133.              argMax(tuple(sum_not_signed_total_vat), date_calculated).1  AS sum_not_signed_total_vat,
  134.              argMax(tuple(sum_not_signed_by_monthly_counterparty_total_vat),
  135.                     date_calculated).1                                   AS sum_not_signed_by_monthly_counterparty_total_vat
  136.       FROM distributed_usf_exhibited_taxes
  137.           prewhere YEAR = 2023 AND last_month = 9
  138.       GROUP BY YEAR, last_month, declaration_month_number, payer_unp
  139.       HAVING argMax(sign, date_calculated) = 1) AS tax
  140.          LEFT OUTER JOIN (SELECT unp,
  141.                                  MAX(month_number)                                                           AS max_month_number,
  142.                                  IF(max_month_number % 3 = 0 AND max_month_number = 9, 0,
  143.                                     max_month_number)                                                        AS declaration_month_number
  144.                           FROM distributed_rn_actual_declarations
  145.                               prewhere YEAR = 2023 AND month_number <= 9
  146.                           WHERE type_code = 'NDS'
  147.                             AND period_type_code = '2'
  148.                             AND have_part_1 = 1
  149.                           GROUP BY unp) AS decl ON tax.payer_unp = decl.unp AND tax.payer_unp_is_quarter = 1 AND
  150.                                                    tax.declaration_month_number = decl.declaration_month_number;
  151.                                     )   ALL INNER JOIN (
  152.  
  153.                                                     -- GetGRPPayers with Typology1 filters
  154. SELECT  unp,
  155.         argMax(tuple(imns_code), date_load).1         AS imns_code,
  156.         argMax(tuple(short_name), date_load).1        AS short_name,
  157.         argMax(tuple(address), date_load).1           AS address,
  158.         argMax(tuple(date_registration), date_load).1 AS date_registration,
  159.         argMax(tuple(state_code), date_load).1        AS state_code,
  160.         argMax(tuple(date_begin_state), date_load).1  AS date_begin_state,
  161.         argMax(tuple(address_code), date_load).1      AS address_code,
  162.         argMax(tuple(sign), date_load).1              AS sign
  163. FROM distributed_grp_actual_payers
  164.  
  165.     -- UnpCodes filter
  166. --
  167.  
  168. GROUP BY unp
  169. HAVING sign = 1 -- постоянный фильтр, без него будут грязные данные!!!
  170. -- ImnsCodes filter
  171.         AND imns_code IN ('000', '001', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '111', '112', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '211', '213', '214', '215',
  172. '216', '217', '218', '232', '233', '241', '251', '271', '291', '292', '293', '294', '295', '300', '301', '302', '303', '304', '305', '306', '307', '308', '309', '311', '312', '313', '314', '315', '316', '317', '318', '319'
  173. , '321', '322', '323', '341', '351', '362', '363', '364', '371', '381', '391', '392', '393', '394', '395', '397', '399', '400', '401', '402', '403', '404', '405', '406', '407', '408', '409', '411', '412', '413', '414', '41
  174. 5', '416', '417', '418', '419', '421', '422', '423', '424', '432', '433', '434', '435', '491', '492', '493', '494', '495', '496', '497', '498', '499', '500', '501', '502', '503', '504', '505', '506', '507', '508', '509', '
  175. 511', '512', '513', '514', '515', '516', '518', '519', '542', '543', '571', '591', '592', '593', '594', '595', '596', '600', '601', '602', '603', '604', '605', '606', '607', '608', '609', '611', '612', '613', '614', '615',
  176.  '616', '617', '618', '619', '621', '622', '623', '624', '625', '661', '671', '691', '692', '693', '694', '695', '696', '697', '700', '701', '702', '703', '704', '705', '707', '708', '709', '711', '712', '713', '715', '716', '717', '718', '719', '721', '722', '723', '724', '741', '751', '761', '772', '773', '774', '791', '792', '793', '794', '795', '796', '797', '800', '801', '000')
  177. --
  178.  
  179.     -- RegistrationDate filters
  180. --
  181.  
  182. --
  183.                                             ) USING unp
  184.                                 )   ALL LEFT JOIN  (
  185.  
  186.                                                                 -- GetRiskGroups
  187. SELECT  unp,
  188.                 argMax(tuple(risk_group_code), date_calculated).1 AS risk_group_code,
  189.                 argMax(tuple(total_score), date_calculated).1 AS total_score,
  190.                 argMax(tuple(DATE), date_calculated).1 AS DATE
  191. FROM (
  192.         SELECT  unp,
  193.                         risk_group_code,
  194.                         total_score,
  195.                         dictGetInt64('finished_risk_calculation_history', 'date', tuple(calculation_code)) AS DATE,
  196.                         date_calculated
  197.         FROM distributed_risk_groups
  198.  
  199.                         prewhere YEAR * 12 + MONTH IN (24277, 24278, 24279, 24280, 24281, 24282, 24283, 24284, 24285)
  200.  
  201.  
  202.                         -- UnpCodes filter
  203. --
  204.  
  205.  
  206.         WHERE dictGetUInt8('finished_risk_calculation_history', 'is_finished', tuple(calculation_code)) --обязательный фильтр
  207.  
  208.  
  209.                 AND DATE BETWEEN 1672520400 AND 1696107599
  210.  
  211.  
  212. )
  213. GROUP BY unp
  214.  
  215. --
  216.                                                         ) USING unp
  217.                             ) ALL LEFT JOIN (
  218.  
  219.                                                         WITH 100 AS coins_factor
  220. SELECT unp,
  221.         YEAR,
  222.         last_month,
  223.         --БП125
  224.         argMax(tuple(nds), date_calculated).1                                                    AS nds,
  225.         argMax(tuple(nds_period_number), date_calculated).1                                      AS nds_period_number,
  226.         argMax(nds_period_code, date_calculated)                                                 AS nds_period_code,
  227.         --БП5
  228.         argMax(tuple(sum_revenue), date_calculated).1                                            AS sum_revenue,
  229.         argMax(sum_revenue_declarations_codes, date_calculated)                                  AS sum_revenue_declarations_codes,
  230.         argMax(sum_revenue_period_codes, date_calculated)                                        AS sum_revenue_period_codes,
  231.         --БП6
  232.         argMax(tuple(count_employees), date_calculated).1                                        AS count_employees,
  233.         argMax(count_employees_declarations_codes, date_calculated)                              AS count_employees_declarations_codes,
  234.         argMax(count_employees_period_codes, date_calculated)                                    AS count_employees_period_codes,
  235.  
  236.         --БП134
  237.         argMax(tuple(tax_deduction), date_calculated).1                                          AS tax_deduction,
  238.         --БП284
  239.         argMax(tuple(accrued_nds), date_calculated).1                                            AS accrued_nds,
  240.         --БП200
  241.         argMax(tuple(sale_turnover), date_calculated).1                                          AS sale_turnover,
  242.         argMax(sale_turnover_period_code, date_calculated)                                       AS sale_turnover_period_code,
  243.         argMax(tuple(sale_turnover_period_number), date_calculated).1                            AS sale_turnover_period_number,
  244. FROM distributed_rn_actual_declarations_to_br_info
  245. WHERE YEAR = 2023
  246.   AND last_month = 9
  247. GROUP BY YEAR, last_month, unp;
  248.                                                 ) USING unp
  249.                         ) ALL LEFT JOIN (
  250.  
  251.                                                 -- GetLastVOPCriteria_5
  252. SELECT  unp,
  253.         argMax(tuple(group_id), KEY).1                 AS group_id,
  254.         argMax(tuple(hrc), KEY).1                      AS hrc,
  255.         argMax(tuple(criterion_weight), KEY).1         AS criterion_weight,
  256.         argMax(tuple(criterion_list), KEY).1           AS criterion_list,
  257.         argMax(tuple(YEAR), KEY).1                     AS YEAR,
  258.         argMax(tuple(half_year), KEY).1                AS half_year,
  259.         argMax(criterion_weight, KEY) > argMax(hrc, KEY) AS indicator
  260. FROM distributed_kr_vop_criteria
  261.  
  262.     -- UnpCodes filter
  263. --
  264.  
  265. AND YEAR = 2023
  266. AND halfyear = 1
  267.     OR halfyear = 2
  268.  
  269. GROUP BY unp
  270. --
  271.                                         ) USING unp
  272.                     ) ALL LEFT JOIN (
  273.                                         -- GetOKEDCodesInfo Head 1
  274. SELECT  YEAR,
  275.         last_month,
  276.         unp,
  277.         oked_code_1,
  278.         oked_code_2,
  279.         oked_codes_declarations_codes,
  280.         oked_codes_period_codes
  281. FROM (
  282.     SELECT  YEAR,
  283.             unp,
  284.             last_month,
  285.             groupArray(oked_code)                   AS oked_codes,
  286.             oked_codes[1]                           AS oked_code_1,
  287.             oked_codes[2]                           AS oked_code_2,
  288.             groupArrayArray(oked_code_declarations_code) AS oked_codes_declarations_codes,
  289.             groupArrayArray(oked_code_period_code)       AS oked_codes_period_codes
  290.     FROM (
  291.         SELECT  YEAR,
  292.                 unp,
  293.                 last_month,
  294.                 info.1            AS oked_code,
  295.                 info.2            AS oked_code_declarations_code,
  296.                 info.3            AS oked_code_period_code
  297.         FROM (
  298.             SELECT  YEAR,
  299.                     last_month,
  300.                     unp,
  301.                     arrayJoin(arraySlice(groupArray(info), 1, 2)) AS info
  302.             FROM (
  303.                 SELECT  YEAR,
  304.                         last_month,
  305.                         unp,
  306.                         (oked_code,
  307.                          groupArray(type_code)          AS oked_code_type_codes,
  308.                          groupArray(period_type_code)   AS oked_code_period_type_codes) AS info
  309.                 FROM (
  310.                     SELECT  YEAR,
  311.                             last_month,
  312.                             unp,
  313.                             type_code,
  314.                             argMax(tuple(oked_code), date_calculated).1        AS oked_code,
  315.                             argMax(tuple(period_type_code), date_calculated).1 AS period_type_code
  316.                     FROM distributed_rn_actual_declarations_by_periods_info
  317. --
  318.  
  319.                                 ) ON unp = counterparty_unp
  320.                 ) ALL LEFT JOIN (
  321.  
  322.                                 --GetKrFalseCompanies
  323. SELECT unp,
  324.        argMax(tuple(date_from), date_load).1 AS date_from,
  325.        argMax(tuple(date_to), date_load).1   AS date_to,
  326.        argMax(tuple(reasons), date_load).1   AS reasons
  327. FROM distributed_kr_false_companies
  328. GROUP BY unp
  329. --
  330.                         ) ON fc.unp = counterparty_unp
  331.             ) ALL LEFT JOIN (
  332.  
  333.                         -- GetTaxBurden
  334. SELECT unp, argMax(tuple(tax_burden), period).1 AS tax_burden
  335. FROM (
  336.         SELECT  period,
  337.                 unp,
  338.                 argMax(tuple(tax_burden), date_calculated).1 AS tax_burden
  339.         FROM distributed_tax_burdens
  340.         WHERE  YEAR = 2023
  341.         AND  period <= round((9-1)/3, 0)
  342.  
  343.         -- UnpCodes filter
  344. --
  345.  
  346.         GROUP BY period, unp
  347.         HAVING isNotNull(tax_burden) = 1
  348. )
  349. GROUP BY unp
  350.  
  351. --
  352.                     ) USING unp
  353.         ) ALL LEFT JOIN (
  354.  
  355.                     SELECT payer_unp,
  356.        argMax(full_exhibited_total_val, date_load)              AS full_exhibited_total_val,
  357.        argMax(full_signed_total_vat, date_load)                 AS full_signed_total_vat,
  358.        counterparty_unp,
  359.        argMax(exhibited_total_val, date_load)                   AS exhibited_total_val,
  360.        argMax(signed_total_vat, date_load)                      AS signed_total_vat,
  361.        argMax(signed_total_vat_rate, date_load)                 AS signed_total_vat_rate,
  362.        argMax(tuple(kls_balance), date_load).1                  AS kls_balance,
  363.        argMax(tuple(kls_balance_per_counterparty), date_load).1 AS kls_balance_per_counterparty,
  364.        argMax(tax_gap_amount, date_load)                        AS tax_gap_amount
  365. FROM distributed_tax_gaps
  366. WHERE YEAR = 2023
  367.   AND last_month = 9
  368. GROUP BY payer_unp, counterparty_unp
  369. HAVING argMax(sign, date_load) = 1;
  370.                 ) USING unp
  371.     ) ALL LEFT JOIN (
  372.  
  373.                 --GetKlsNds55
  374. SELECT unp,
  375.        budget_code,
  376.        argMax(tuple(credited_from_1001_nds_amount), date_calculated).1     AS credited_from_1001_nds_amount,
  377. FROM distributed_rn_kls_nds
  378. WHERE budget_code = '1001'
  379.     AND YEAR = 2023
  380.     AND operation_month = 9
  381. GROUP BY unp, budget_code
  382. --
  383.             ) USING unp
  384. )
  385.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement