Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends,
I have a table which contains 4 columns like below.

GRP_KIT_NO	COMPONENT	COMP_TYPE	QTY
PLATE-1_4123	99S31653	PRT	        1
PLATE-1_4123	33125112	PRT	        1
PLATE-1_4123	49DE0006	PRT	        1


Now I insert same rows in same table with different Grp_KIt_NO like

GRP_KIT_NO	COMPONENT	COMP_TYPE	QTY
PLATE-2_4123	        99S31653	       PRT	   1
PLATE-2_4123	        33125112	       PRT	    1
PLATE-2_4123	        49DE0006	       PRT	    1


SO I have created mulitple copy of same date with different Grp_KIt_NO.

Now our client doesnt want to bill dupliacte groups which contains same data.

I am not able to find such group.

NOw I give an example

supppose I have input

GRP_KIT_NO	COMPONENT	COMP_TYPE	QTY
PLATE-1_4123	22              PRT	        1
PLATE-1_4123	33              PRT	        1

PLATE-2_4123	22	        PRT	        1
PLATE-2_4123	33	        PRT	        1

PLATE-3_4123	11	        PRT	        1
PLATE-3_4123	33      	PRT	        1
PLATE-3_4123	22      	PRT	        1

AAA-3_4123	33      	PRT	        1


So I want output like

GRP_KIT_NO	COMPONENT	COMP_TYPE	QTY
PLATE-1_4123	22       	PRT	        1
PLATE-1_4123	33              PRT	        1

PLATE-2_4123	22      	PRT	        1
PLATE-2_4123	33       	PRT	        1


Becuase both grp_kit_no matches exact data.

Please help me guys.

What I have tried:

I tried below query but not as expected


WITH A   -- Get a list of unique combinations of Alias and [Record ID]
AS  (
   SELECT Distinct   GRP_KIT_NO, COMPONENT, COMP_TYPE,QTY--,INDEX_NO
    from GROUP_KIT_BOM
)
,   B  -- Get a list of all those Alias values that have more than one [Record ID] associated
AS  (
    SELECT COMPONENT, COMP_TYPE,QTY--,INDEX_NO
    FROM   A  
    GROUP BY
           COMPONENT, COMP_TYPE,QTY--,INDEX_NO
    HAVING COUNT(*) > 1
)
--select * from b order by COMPONENT, COMP_TYPE,QTY
SELECT A.GRP_KIT_NO, A.COMPONENT, A.COMP_TYPE,A.QTY
FROM    A
    JOIN B
        ON  A.COMPONENT = B.COMPONENT AND A.COMP_TYPE=B.COMP_TYPE ANd A.QTY=B.QTY-- AND A.INDEX_NO=B.INDEX_NO
              order by GRP_KIT_NO,COMPONENT, COMP_TYPE,QTY
Posted
Updated 22-Jan-17 23:59pm
v7
Comments
Tomas Takac 20-Jan-17 5:05am    
You should consider storing data in a different way that would allow you to catch the reuse of components easily.
Rahul_Pandit 21-Jan-17 1:48am    
Tomas,
Thanks for reply. I cant change the approach of storing value as its our client requirement .
Please guide me how can I write query for same if my question is clear to you.
Tomas Takac 21-Jan-17 9:37am    
What you want to do is not trivial. Basically you need to compare each group to all the others to find out if they match. I will give it a thought and maybe post an answer later. This might require some advanced features so I recommend you update tags of your question and specify the version of SQL Server you use.
Rahul_Pandit 23-Jan-17 3:37am    
Dear Tomas,
Please post your answer. Its very important task for me and need to complete as soon as possible.
Rahul_Pandit 23-Jan-17 0:13am    
UPdated.

1 solution

I have done this by using below code..

SELECT DISTINCT
  SUBSTRING(Grp_kit_NO, 0, CHARINDEX('_', Grp_kit_NO)) GRP_KIT_NO,  0 rownum INTO #temp
FROM group_kit_detail
DECLARE @i int = 0
UPDATE #temp
SET @i = rownum = @i + 1

SELECT
  GRP_KIT_NO,
  CONVERT(varchar(max), '') comp,
  0 RW INTO #T1
FROM GROUP_KIT_BOM
WHERE 1 = 2
SELECT
  CONVERT(varchar(max), '') GRP_KIT_NO,
  CONVERT(varchar(max), '') Match_GRP_KIT_NO INTO #Final
FROM GROUP_KIT_BOM
WHERE 1 = 2


DECLARE @RowCounter int = 1
WHILE @RowCounter <= (SELECT
    COUNT(1)
  FROM #temp)
BEGIN
  DECLARE @MasterGroup varchar(50) = ''
  SELECT
    @MasterGroup = GRP_KIT_NO
  FROM #temp
  WHERE rownum = @RowCounter

  SELECT
    GRP_KIT_NO,
    COMPONENT + COMP_TYPE + QTY comp,
    0 Row1 INTO #TInput
  FROM GROUP_KIT_BOM
  WHERE SUBSTRING(Grp_kit_NO, 0, CHARINDEX('_', Grp_kit_NO)) = @MasterGroup
  ORDER BY GRP_KIT_NO, comp
  DECLARE @j int = 0
  UPDATE #TInput
  SET @j = Row1 = @j + 1

  INSERT INTO #T1
    SELECT
      GRP_KIT_NO,
      COMP = STUFF((SELECT
        ',' + COMP
      FROM #TInput A
      WHERE A.GRP_KIT_NO = B.GRP_KIT_NO     
      FOR xml PATH ('')), 1, 1, ''),
      0
    FROM #TInput B
    GROUP BY B.GRP_KIT_NO
    ORDER BY COMP

  DROP TABLE #TInput
  SET @RowCounter = @RowCounter + 1

END

DECLARE @k int = 0
UPDATE #T1
SET @k = RW = @k + 1


DECLARE @Counter int = 1-----------------matching comp

WHILE @Counter <= (SELECT
    COUNT(1)
  FROM #T1)
BEGIN

  DECLARE @Group varchar(50) = ''
  SELECT
    @Group = GRP_KIT_NO
  FROM #T1
  WHERE RW = @Counter

  INSERT INTO #Final
    SELECT DISTINCT
      A.GRP_KIT_NO,
      B.GRP_KIT_NO
    FROM #T1 A,
         #T1 B
    WHERE B.GRP_KIT_NO <> @Group
    AND A.comp = B.comp
    AND A.GRP_KIT_NO <> B.GRP_KIT_NO
    ORDER BY A.GRP_KIT_NO

  SET @Counter = @Counter + 1
END
 
SELECT
  *
FROM #Final
		
DROP TABLE #Final
DROP TABLE #T1
DROP TABLE #TEMP
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900