2014-07-17 161 views
-2

tbl_claimsSQL:分割單元成行

ClaimNo|StatusDate | FromDate 
---------------------------------- 
1076| 2014-02-21 | 2014-02-10 
1029| 2014-02-22 | 2014-01-10 

tbl_settledagainst

Claimno | Amount | VerificationTeam_ClaimNo 
-------------------------------------------------- 
AC1006 | 1000 |, 1076, 1029 
AC1034 | 0.00 |, 1064, 1065, 1066 
AC1035 | 0.00 |, 1086 

第一表用於存儲A型聲稱第二表用於存儲乙類型的權利要求。 column tbl_settledagainst.VerificationTeam_ClaimNo has tbl_claims.claims ie。 索賠1076,1029已經結案agaist AC1006(B類)索賠。 現在的問題是,我想用這種方式來顯示數據:

Type_A_claim | Type_B_Claim | Amt | StatusDate 
------------------------------------------------- 
1076   | AC1006  | 1000 | 2014-02-21 
1029   | AC1006  | 1000 | 2014-02-22 

預先感謝您。

我有這個sql函數,我通常用它來分割基於分隔符的字符串。

如:SELECT * FROM dbo.split( '100,200,ABC', '')

**Create FUNCTION [dbo].[Split](@String nvarchar(4000), @Delimiter char(1))    
RETURNS @Results TABLE (Items nvarchar(4000))    
AS    
BEGIN    
    DECLARE @INDEX INT    
    DECLARE @SLICE nvarchar(4000)    
    -- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z    
    --  ERO FIRST TIME IN LOOP    
    SELECT @INDEX = 1    
    -- following line added 10/06/04 as null    
    --  values cause issues    
    IF @String IS NULL RETURN    
    WHILE @INDEX !=0    


     BEGIN    
     -- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER    
     SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)    
     -- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE    
     IF @INDEX !=0    
      SELECT @SLICE = LEFT(@STRING,@INDEX - 1)    
     ELSE    
      SELECT @SLICE = @STRING    
     -- PUT THE ITEM INTO THE RESULTS SET    
     INSERT INTO @Results(Items) VALUES(@SLICE)    
     -- CHOP THE ITEM REMOVED OFF THE MAIN STRING    
     SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)    
     -- BREAK OUT IF WE ARE DONE    
     IF LEN(@STRING) = 0 BREAK    
    END    

    RETURN    
END** 
+0

您應該妥善存放您的數據第一個地方。這個問題是找到一個不存在的問題的解決方案。 – podiluska

回答

0

你應該用APPLY

SELECT c.ClaimNo as Type_A_claim, tsa.Claimno as Type_B_Claim, 
tsa.Amount as Amt, c.StatusDate FROM 
dbo.tbl_settledagainst tsa 
CROSS APPLY [dbo].[Split](tsa.VerificationTeam_ClaimNo) fn 
INNER JOIN dbo.tbl_claims c on c.ClaimNo = fn.Items 
+0

SubhanAllah!大聲笑 我真的沒想到有人真的很容易解決這個問題。多謝兄弟! – Arbaaz

+1

不客氣的朋友。對於每個問題,有人在那裏誰知道解決方案 – Alireza

+0

並請始終標記答案爲接受,以便找到這個問題的人會知道正確的答案:) – Alireza