我有两个名为零售和活动的表格,数据如下所示:
零售表
活动表
我主要关注的是零售表格的确定和错误列,因为您可以看到它包含 ActivityId的逗号分隔值。
我想要的是,如果确定列有 ActivityId ,相应的列将是,如果错误列有 ActivityId ,那么它应标记为否
注意我只有四列已修复,这意味着我必须检查其中四列的值是否为确定或错误,如果是,则只有我必须打印是或否,否则为空。
期望的结果应该是: 如果值为Ok,那么是其他明智的否。
答案 0 :(得分:0)
我猜你想在某些栏目中存储'是'或'否'。以下是更新该列的查询:
UPDATE RetailTable
SET <Result_Column>=
CASE
WHEN Ok IS NOT NULL THEN 'Yes'
WHEN Fault IS NOT NULL THEN 'No'
END
答案 1 :(得分:0)
您可以使用以下代码作为起点:
DECLARE @Retail TABLE
(
PhoneAuditID INT,
HandsetQuoteID INT,
Ok VARCHAR(50)
)
INSERT INTO @Retail VALUES (1, 1009228, '4,22,5')
INSERT INTO @Retail VALUES (2, 1009229, '1')
DECLARE @Activity TABLE
(
ID INT,
Activity VARCHAR(50)
)
INSERT INTO @Activity VALUES (1, 'BatteryOK?'), (4, 'PhonePowersUp?'), (22,'SomeOtherQuestion?'), (5,'LCD works OK?')
SELECT R.[PhoneAuditID], R.[HandsetQuoteID], A.[Activity], [Ok] = CASE WHEN A.[ID] IS NOT NULL THEN 'Yes' END
FROM @Retail R
CROSS APPLY dbo.Split(R.Ok, ',') S
LEFT JOIN @Activity A ON S.[items] = A.[ID]
我使用过这里提供的Split功能: separate comma separated values and store in table in sql server
答案 2 :(得分:0)
尝试以下查询。我使用pivot将行显示为列。我还使用了分割功能来分割你可以在网上轻松找到的id值:
CREATE TABLE PhoneAudit
(
PhoneAuditRetailID INT,
HandsetQuoteID INT,
Ok VARCHAR(50),
Fault VARCHAR(50)
)
INSERT INTO PhoneAudit VALUES (1,10090,'1,2','3')
CREATE TABLE ActivityT
(
ID INT,
Activity VARCHAR(100)
)
INSERT INTO ActivityT VALUES (1,'Battery')
INSERT INTO ActivityT VALUES (2,'HasCharger')
INSERT INTO ActivityT VALUES (3,'HasMemoryCard')
INSERT INTO ActivityT VALUES (4,'Test')
DECLARE @SQL AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(Activity) FROM (SELECT DISTINCT Activity FROM ActivityT) AS Activities
SET @SQL = 'SELECT PhoneAuditRetailID, HandsetQuoteID,
' + @ColumnName + '
FROM
(SELECT
t1.PhoneAuditRetailID,
t1.HandsetQuoteID,
TEMPOK.*
FROM
PhoneAudit t1
CROSS APPLY
(
SELECT
Activity,
(CASE WHEN ID IN (SELECT * FROM dbo.SplitIDs(t1.Ok,'',''))
THEN ''YES''
ELSE ''NO''
END) AS VALUE
FROM
ActivityT t2
) AS TEMPOK) AS t3
PIVOT
(
MIN(VALUE)
FOR Activity IN ('+ @ColumnName + ')
) AS PivotTable;'
EXEC sp_executesql @SQL
DROP TABLE PhoneAudit
DROP TABLE ActivityT
答案 3 :(得分:0)
有几种方法可以做到这一点。如果您正在寻找一种纯粹的声明式方法,您可以使用递归CTE。下面的示例作为通用解决方案提供,其中包含的测试数据应该能够满足您的需求:
Declare @Delimiter As Varchar(2)
Set @Delimiter = ','
Declare @Strings As Table
(
String Varchar(50)
)
Insert Into @Strings
Values
('12,345,6,78,9'),
(Null),
(''),
('123')
;With String_Columns As
(
Select
String,
Case
When String Is Null Then ''
When CharIndex(@Delimiter,String,0) = 0 Then ''
When Len(String) = 0 Then ''
Else Left(String,CharIndex(@Delimiter,String,0)-1)
End As String_Column,
Case
When String Is Null Then ''
When CharIndex(@Delimiter,String,0) = 0 Then ''
When Len(String) = 0 Then ''
When Len(Left(String,CharIndex(@Delimiter,String,0)-1)) = 0 Then ''
Else Right(String,Len(String)-Len(Left(String,CharIndex(@Delimiter,String,0)-1))-1)
End As Remainder,
1 As String_Column_Number
From
@Strings
Union All
Select
String,
Case
When CharIndex(@Delimiter,Remainder,0) = 0 Then Remainder
Else Left(Remainder,CharIndex(@Delimiter,Remainder,0)-1)
End As Remainder,
Case
When CharIndex(@Delimiter,Remainder,0) = 0 Then ''
When Len(Left(Remainder,CharIndex(@Delimiter,Remainder,0)-1)) = 0 Then ''
Else Right(Remainder,Len(Remainder)-Len(Left(Remainder,CharIndex(@Delimiter,Remainder,0)-1))-1)
End As Remainder,
String_Column_Number + 1
From
String_Columns
Where
(Remainder Is Not Null And Len(Remainder) > 1)
)
Select
String,
String_Column,
String_Column_Number
From
String_Columns