How to Build select Query split Temp Value to two column one Per Number And Another to Text when Flag Allow 1?

佐手、 提交于 2020-08-10 23:24:50

问题


I work on a query for SQL Server 2012. I have an issue: I can't build select

Query split Column Temp value to two Column When row in the temp table #nonparametric has the flag Allow = 1,

it must split column Temp value from #nonparametric to two column when the flag Allow = 1 .

suppose column Temp value has value 50.40 kg it must split to two column

First column with number so it will have 50.40 and it will be same Name as Parametric .

Second column with Text so it will have kg and it will be same Name as Parametric + 'Units'.

meaning Name will be ParametricUnit .

I need to build query that split this on two column when Flag Allow =1 .

create table #nonparametricdata
(
PART_ID nvarchar(50) ,
CompanyName  nvarchar(50),
PartNumber nvarchar(50),
DKFeatureName nvarchar(100),
Tempvalue nvarchar(50),
FlagAllow bit
)

insert into #nonparametricdata
values
('1222','Honda','silicon','package','15.50Am',0),
('1900','MERCEIS','GLASS','family','90.00Am',1),--Build select query split data because FlagAllow=1
('5000','TOYOTA','alominia','source','70.20kg',0),
('8000','MACDA','motor','parametric','50.40kg',1),----Build select query split data because FlagAllow=1
('8900','JEB','mirror','noparametric','75.35kg',0)

create table #FinalTable
(
DKFeatureName  nvarchar(50),
DisplayOrder  int
)

insert into #FinalTable (DKFeatureName,DisplayOrder) 
values 
('package',3),
('family',4),
('source',5),
('parametric',2),
('noparametric',1)

what I try is below :

   DECLARE @SelectqueryData NVARCHAR(MAX)
   SELECT
   @SelectqueryData = STUFF(
    (
        SELECT ', ' +  case when B.FlagAllow = 1 then '['+A.DKFeatureName+'],['+A.DKFeatureName+'Unit]' else quotename(A.DKFeatureName) end
        FROM #FinalTable A
        join  (Select distinct DKFeatureName,FlagAllow 
   From #nonparametricdata 
 ) B on A.DKFeatureName=B.DKFeatureName

        ORDER BY DisplayOrder
        FOR XML PATH ('')
    ),1,2,''
)
select @SelectqueryData
--select @SelectqueryData from table

Expected Result is :

[noparametric], [parametric]--QueryGetNumber,[parametricUnit]--QueryGetUnitOfMeasure
, [package], [family]--QueryGetNumber,[familyUnit]--QueryGetUnitOfMeasure, [source]

when make query above it must give me result as image(for Explain Only) :


回答1:


You're looking for a DYNAMIC PIVOT

Example

DECLARE @SelectqueryData NVARCHAR(MAX)
SELECT  @SelectqueryData = STUFF( (
           SELECT ', ' +  case when B.FlagAllow = 1 then '['+A.DKFeatureName+'],['+A.DKFeatureName+'Unit]' else quotename(A.DKFeatureName) end
           FROM #FinalTable A
           join  (Select distinct DKFeatureName,FlagAllow 
                   From #nonparametricdata 
                  ) B on A.DKFeatureName=B.DKFeatureName
           ORDER BY DisplayOrder
           FOR XML PATH ('')
         ),1,2,''
)

Declare @SQL varchar(max) = '
Select *
 From  (
        Select A.Part_ID 
              ,A.PartNumber
              ,A.CompanyName
              ,B.*
         From  #nonparametricdata A
         Cross Apply ( values ( DKFeatureName         ,case when FlagAllow=1 then left(TempValue,patindex(''%[A-Z]%'',TempValue+''A'')-1) else TempValue end ) 
                             ,( DKFeatureName+''Unit'',case when FlagAllow=1 then substring(TempValue,patindex(''%[A-Z]%'',TempValue+''A''),10) else null end ) 
                     ) B(Item,Value)
       ) src
 Pivot (max(value) for Item in ('+@SelectqueryData+') ) pvt
'
--Print @SQL
Exec(@SQL)            

Returns



来源:https://stackoverflow.com/questions/62480447/how-to-build-select-query-split-temp-value-to-two-column-one-per-number-and-anot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!