DECLARE calcDocType int maxDate datetime matID int vMatID int izSostav

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
DECLARE
@calcDocType int,
@maxDate datetime,
@matID int, @vMatID int,
@izSostava int
SET @maxDate = '2009/1/29'
set @calcDocType = 1
set @matID = 3
set @vMatID = 4
set @izSostava = -2
select distinct
[req].[r1] as [ReqID],
[nmk].[NOTE] + ' ' + [nmk].[NAME] as [REQ_NAME],
[VMAT_REQ_QUANT].[s2]as [VMat],
[IzS_REQ_QUANT].[s3] as [iz_Sostava],
[MatQuant].[s4] as [MatQuant],
[MatQuant2].[s6]as [MatQuant2],
[NmkQuant].[nmkSum] as [PlanNmkQuant],
isnull([VMAT_REQ_QUANT].[s2], isnull([IzS_REQ_QUANT].[s3], 0)) as [Result],
[V_MEASURES].[NOTE] as [Requirements_MesurNote],
[reg_c].[U_COST] as [U_COST],
[reg_c].[dc],
[cdd].[maxdocID],
[cdSpec].[REGISTRATION_PRICE],
[contragents].[NAME] as [contragent_name],
[req].[PRODUCTION_BOM_ID],
[req].[PRODUCTION_BOM_LIST_ID],
[prodBoms].[Note] as [bomNote] ,
[prodBoms].[Name] as [bomName]
from
((((((((((((((
(select distinct
[V_PRODUCTION_BOMS_REQUIREMENTS].[REQUIREMENTS] as [r1] ,
[V_PRODUCTION_BOMS_REQUIREMENTS].[PRODUCTION_BOM_ID],
[V_PRODUCTION_BOMS_REQUIREMENTS].[PRODUCTION_BOM_LIST_ID],
[V_PRODUCTION_BOMS_REQUIREMENTS].[RESOURCE_TYPE] as [resType],
[V_PRODUCTION_BOMS_REQUIREMENTS].[MAN_PROCESS_REQUIREMENTS_ID]
from
[V_PRODUCTION_BOMS_REQUIREMENTS]
where ([RESOURCE_TYPE] = @matID or [RESOURCE_TYPE] = @vMatID )
) as [req]
LEFT JOIN
(select
SUM([r2].[REQUIREMENTS_TOTAL_QUANTITY]) OVER (PARTITION BY [r2].[REQUIREMENTS], [r2].[PRODUCTION_BOM_ID]) as [s2],
[r2].[requirements] as [r2r],
[r2].[PRODUCTION_BOM_ID] as [pbomID]
from [V_PRODUCTION_BOMS_REQUIREMENTS] as [r2]
where [r2].[PRODUCTION_BOM_ID] in (356) and [RESOURCE_TYPE] = @vMatID
) as [VMAT_REQ_QUANT]
on ([req].[r1] = [VMAT_REQ_QUANT].[r2r] and [req].[PRODUCTION_BOM_ID] = [VMAT_REQ_QUANT].[pbomID]))
left JOIN
(select
SUM([r3].[REQUIREMENTS_TOTAL_QUANTITY]) OVER (PARTITION BY [r3].[REQUIREMENTS], [r3].[PRODUCTION_BOM_ID]) as [s3],
[r3].[requirements] as [r3r] ,
[r3].[PRODUCTION_BOM_ID] as [pbomID]
from [V_PRODUCTION_BOMS_REQUIREMENTS] as [r3]
where [r3].[PRODUCTION_BOM_ID] in (356) and [RESOURCE_TYPE] = @izSostava
) as [IzS_REQ_QUANT]
on ([req].[r1] = [IzS_REQ_QUANT].[r3r] and [req].[PRODUCTION_BOM_ID] = [IzS_REQ_QUANT].[pbomID]))
LEFT JOIN
(select
SUM([r4].[REQUIREMENTS_TOTAL_QUANTITY]) OVER (PARTITION BY [r4].[REQUIREMENTS], [r4].[PRODUCTION_BOM_ID]) as [s4],
[r4].[requirements] as [r4r],
[r4].[PRODUCTION_BOM_ID] as [pbomID]
from [V_PRODUCTION_BOMS_REQUIREMENTS] as [r4]
where [r4].[PRODUCTION_BOM_ID] in (356) and [RESOURCE_TYPE] = @matID and [r4].[requirements] <> [NOMENCLATURE_ITEM_ID] ) as [MatQuant]
on ([req].[r1] = [MatQuant].[r4r] and [req].[PRODUCTION_BOM_ID] = [MatQuant].[pbomID]))
LEFT JOIN
(select
SUM([r6].[REQUIREMENTS_TOTAL_QUANTITY]) OVER (PARTITION BY [r6].[REQUIREMENTS], [r6].[PRODUCTION_BOM_ID]) as [s6],
[r6].[requirements] as [r6r] ,
[r6].[PRODUCTION_BOM_ID] as [pbomID]
from [V_PRODUCTION_BOMS_REQUIREMENTS] as [r6]
where [r6].[PRODUCTION_BOM_ID] in (356) and [RESOURCE_TYPE] = @matID and [r6].[requirements] = [NOMENCLATURE_ITEM_ID] ) as [MatQuant2]
on ([req].[r1] = [MatQuant2].[r6r] and [req].[PRODUCTION_BOM_ID] = [MatQuant2].[pbomID]))
LEFT JOIN
(select distinct
SUM([r5].[QUANTITY]) OVER (PARTITION BY [r5].[NOMENCLATURE_ITEM_ID], [r5].[PRODUCTION_BOM_ID]) as [nmkSum] ,
[r5].[NOMENCLATURE_ITEM_ID] as [nmkID]
from
(select distinct
[r].[PRODUCTION_BOM_ITEM_ID],
[r].[PRODUCTION_BOM_LIST_ID],
[r].[PRODUCTION_BOM_ID] ,
[r].[NOMENCLATURE_ITEM_ID] ,
[r].[QUANTITY]
from [V_PRODUCTION_BOMS_REQUIREMENTS] as [r]
where [r].[PRODUCTION_BOM_ID] in (356)
) as [r5]
) as [nmkQuant]
on [nmkQuant].[nmkID] = [req].[r1])
LEFT JOIN
[V_NOMENCLATURE] as [nmk]
on [nmk].[id] = [req].[r1])
LEFT JOIN
(select
ROW_NUMBER() OVER (PARTITION BY [reg_cards].[NOMENCLATURE_ITEM_ID] ORDER BY [reg_cards].[DATE_CREATED] DESC, [reg_cards].[NUMBER] DESC, [reg_cards].[ID] DESC) AS [RN],
[reg_cards].[cost] as [U_COST],
[reg_cards].[NOMENCLATURE_ITEM_ID] as [rc_nmk_id],
[reg_cards].[SHOP_ID] as [shop_id],
max([reg_cards].[DATE_CREATED]) OVER (PARTITION BY [reg_cards].[NOMENCLATURE_ITEM_ID]) as [dc]
from [V_REGISTRATION_CARDS] as [reg_cards]
WHERE [shop_id] in (118)
) as [reg_c]
on [req].[r1] = [reg_c].[rc_nmk_id] ))
LEFT JOIN
(select distinct
[cad].[Document_ID] as [DocID],
[cad].[NOMENCLATURE_ITEM_ID] as [specNMK],
max([calcDocs].[DOCUMENT_DATE]) over (PARTITION BY [cad].[NOMENCLATURE_ITEM_ID]) as [max_dd],
[calcDocs].[DOCUMENT_DATE] as [dd],
max([cad].[Document_ID]) over (PARTITION BY [cad].[NOMENCLATURE_ITEM_ID],[calcDocs].[DOCUMENT_DATE]) as [maxDocID]
from
(select
[doc].[DOCUMENT_DATE],
[doc].[ID]
from [V_CALCULATED_DOCUMENTS] as [doc]
where [doc].[DOCUMENT_DATE] < @maxDate and [doc].[TYPE_ID] = @calcDocType
) as [calcDocs]
LEFT JOIN
[V_CALC_DOC_SPECIFICATIONS] as [cad]
on [calcDocs].[ID] = [cad].[Document_ID]
) as [cdd]
on [req].[r1] = [cdd].[specNMK] and [cdd].[dd] = [cdd].[max_dd])
LEFT JOIN
[V_CALC_DOC_SPECIFICATIONS] as [cdSpec]
on ([cdSpec].[Document_ID] = [cdd].[maxDocID] and [cdSpec].[NOMENCLATURE_ITEM_ID] = [req].[r1]))
LEFT JOIN
[V_MEASURES]
on [nmk].[BASE_MEASURE_ID] = [V_MEASURES].[ID] )
LEFT JOIN
[V_CALCULATED_DOCUMENTS] as [calculated_docs]
on [calculated_Docs].[ID] = [cdd].[maxDocID] )
LEFT JOIN
[V_COUNTERAGENTS] as [contragents]
on [contragents].[ID] = [calculated_docs].[CONTRAGENT_ID] )
LEFT JOIN
[V_PRODUCTION_BOMS] as [prodBoms]
on [prodBoms].[ID] = [req].[PRODUCTION_BOM_ID] )
LEFT JOIN
(SELECT
[V_RESOURCES_PARAMETERS].[MAN_PROCESS_REQUIREMENTS_ID] AS [REQ_ID],
[V_RESOURCES_PARAMETERS].[PARAMETER_VALUE] AS [PAR_VALUE] ,
[PARS].[UNIQUE_CODE] AS [CODE]
FROM
[V_RESOURCES_PARAMETERS]
INNER JOIN
(SELECT
[V_PARAMETERS].*
FROM [V_PARAMETERS]
WHERE [V_PARAMETERS].[UNIQUE_CODE] = 'FROM_COMPL'
) AS [PARS]
ON [V_RESOURCES_PARAMETERS].[PARAMETERS_ID] = [PARS].[ID]
) AS [RES_PAR]
ON [req].[MAN_PROCESS_REQUIREMENTS_ID] = [RES_PAR].[REQ_ID]
where [req].[PRODUCTION_BOM_ID] in (356) and ([RN]=1 or [rn] is null) and (RES_PAR.CODE is null)
order by [req].[r1]