sql - perhaps impossible view - so complicated -
i try implement 1 view knowledge not enough.
this query try make view
declare @cte table(documentid varchar(50),documentno varchar(50),xmlcontent xml); declare @tbl table(documentid varchar(100),documentno varchar(100),vdata varchar(100),vpublika varchar(100),vvatreshen varchar(100)); insert @cte select documentid,documentno,cast(replace(cast(content nvarchar(max)),'utf-8','utf-16') xml) xmlcontent mainsqltable doctypeid = 2; insert @tbl select documentid,documentno, replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Публикa"]/value/value')),'</value>','') ,'<value>','') vpublika, replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Дата"]/value/date')),'</date>','') ,'<date>','') vdata, replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Вътрешен №"]/value/value')),'</value>','') ,'<value>','') vvatreshen replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Датаin"]/value/date')),'</date>','') ,'<date>','') vdatain, @cte; select pri.documentno documentno, pri.vdata data, pri.vvatreshen vatreshen, sek.documentno documentno2, sek.vdatain vdata2, sek.vvatreshen vvatreshen @tbl pri left join @tbl sek on (pri.vdata = sek.vdatain) (pri.vpublika = 'Да')
have tried use ctes instead of tables?
with cte ( select documentid,documentno,cast(replace(cast(content nvarchar(max)),'utf-8','utf-16') xml) xmlcontent mainsqltable doctypeid = 2 ), tbl ( select documentid,documentno, replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Публикa"]/value/value')),'</value>','') ,'<value>','') vpublika, replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Дата"]/value/date')),'</date>','') ,'<date>','') vdata, replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Вътрешен №"]/value/value')),'</value>','') ,'<value>','') vvatreshen, replace( replace (convert(varchar(100),xmlcontent.query('/idmsdocument/documentcontent/attribute[@name="Датаin"]/value/date')),'</date>','') ,'<date>','') vdatain cte ) select pri.documentno documentno, pri.vdata data, pri.vvatreshen vatreshen, sek.documentno documentno2, sek.vdatain vdata2, sek.vvatreshen vvatreshen tbl pri left join tbl sek on (pri.vdata = sek.vdatain) (pri.vpublika = 'Да')
Comments
Post a Comment