Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am counting the records for a specific project Value (cprj) from the table [MatchingRowsOutput-CI-BOM-ProjectCumulative] and cross-referencing the number to a [DWRowCount] value in another table, [OrderEntry], for the same project 'cprj'.

If these values match, I would like to take all the records for that project (cprj) from the [MatchingRowsOutput-CI-BOM-ProjectCumulative] table and output them into another table called [CI-BM-CPRJ-Transfer].

I have tried many different methods, trying to use CASE clause, WHERE clause, HAVING clause but none of these have worked.

The code pasted below is the most successful that I can achieve, returning 0 rows affected. I know for a fact there should be a return of 26 rows for the TEK#7000 cprj.

Also, after I have pulled the records from the [MatchingRowsOutput-CI-BOM-ProjectCumulative] table and placed into the [CI-BM-CPRJ-Transfer] table I would like to delete the records that was used for the reference

out of the [OrderEntry] table. I have not tried to code that part yet in the code below.

Any help is much appreciated.


Here are samples of the 2 tables with the information.

[OrderEntry] Table
ID	cprj		TotalRowCount		DWRowCount
1	TEK#7000	80			26
2	TEK#4444	50			30
3	TEK#88888	120			24
4	TEK#4700	37			13
5	TEK#99999	53			42


[MatchingRowsOutput-CI-BOM-ProjectCumulative] table
ID	cprj		item		PartNumber		SWITEM	QTY	rutm	
196	TEK#7000	A23-3171-X	A23-3171\2583		1.2	2	NULL	
197	TEK#5555	N02-0846-X	N02-0846A5\59\2583	2	1	NULL	
198	TEK#7000	N02-0846-X	N02-0846A5\59\2583	2	1	NULL	
199	TEK#5555	A23-1354-X	A23-1354\2583		2.3	2	NULL	
200	TEK#7000	A23-1354-X	A23-1354\2583		2.3	2	NULL	
201	TEK#7000	B11-0304-X	B11-0304\25		2.4	2	NULL	
202	TEK#7000	B11-0304-X	B11-0304\25		1.4	2	NULL	
203	TEK#5555	B11-0304-X	B11-0304\25		2.4	2	NULL	
204	TEK#5555	B11-0304-X	B11-0304\25		1.4	2	NULL	
205	TEK#5555	B11-0305-X	B11-0305\59		1.5	2	NULL	
206	TEK#5555	B11-0305-X	B11-0305\59		2.5	2	NULL	
207	TEK#7000	B11-0305-X	B11-0305\59		1.5	2	NULL


I could not show all the columns for this 2nd table.

[CI-BM-CPRJ-Transfer] is a new table that the results would be written into.

What I have tried:

SQL
Insertinto SSIS.dbo.[CI-BM-CPRJ-Transfer](cprj, item, PartNumber, SWITEM, QTY, rutm, trid, mitm, pono, sitm, opol, qana, scpf, cwar, opno, cpha, exin, itlu, ssta, dsca, dscb, dscd, suno, ctyo, eitm)

SELECT 
    PartList.[cprj],
    XMLLines.[item],
    XMLLines.[PartNumber],
    XMLLines.[SWITEM],
    XMLLines.[QTY],
    XMLLines.[rutm],
    XMLLines.[trid],
    XMLLines.[mitm],
    XMLLines.[pono],
    XMLLines.[sitm],
    XMLLines.[opol],
    XMLLines.[qana],
    XMLLines.[scpf],
    XMLLines.[cwar],
    XMLLines.[opno],
    XMLLines.[cpha],
    XMLLines.[exin],
    XMLLines.[itlu],
    XMLLines.[ssta],
    XMLLines.[dsca],
    XMLLines.[dscb],
    XMLLines.[dscd],
    XMLLines.[suno],
    XMLLines.[ctyo],
    XMLLines.[eitm]
FROM 
    OrderEntry.dbo.OrderEntry as PartList

INNERJOIN
SSIS.dbo.[MatchingRowsOutput-CI-BOM-ProjectCumulative]AS XMLLines ON 
XMLLines.cprj = PartList.cprj

    (SELECT cprj FROM SSIS.dbo.[MatchingRowsOutput-CI-BOM-ProjectCumulative])GroupBy XMLLines.cprj, PartList.DWRowCount, XMLLines.[item], XMLLines.[PartNumber], XMLLines.[SWITEM], XMLLines.[QTY], XMLLines.[rutm], XMLLines.[trid], XMLLines.[mitm], XMLLines.[pono], XMLLines.[sitm],
XMLLines.[opol], XMLLines.[qana], XMLLines.[scpf], XMLLines.[cwar], XMLLines.[opno], XMLLines.[cpha], XMLLines.[exin], XMLLines.[itlu], XMLLines.[ssta], XMLLines.[dsca], XMLLines.[dscb], XMLLines.[dscd],
XMLLines.[suno], XMLLines.[ctyo], XMLLines.[eitm], PartList.[cprj]


Having count(XMLLines.cprj)= PartList.DWRowCount
Posted
Updated 23-Mar-20 4:38am
v2

Rough Guide of my solution

I would create a table variable as shown in solution 1 that is representative of the columns in the transfer table that is populated by join to order entry and the cumulative.

Enter the data into temp

insert into @temp (columns needed)
select (columns needed)
from order_enter
inner join cumulative


Populate Transfer

insert into transfer (columns needed )
select (columns needed )
from @temp

Deletion

delete from oe

orderentry oe
inner join @temp t on t.cprj = oe.cprj

Deletion with a join Example

How to Delete using INNER JOIN with SQL Server? - Stack Overflow[^]
 
Share this answer
 
Comments
Member 14778402 23-Mar-20 10:52am    
Hi Marcus, Thanks for your help. The one condition that is really pertinent is that the count of a particular cprj from the cumulative table must equal the value in the column DWRowCount in the order entry table for the same cprj. This determines the cprj records to pull from the cumulative table to the transfer table. Not sure how to code that condition. The count is returning a zero value as mentioned above.
You see, if the count from cumulative table for cprj and the value of DWRowCount are not equal, i do not want the records at all.
MarcusCole6833 23-Mar-20 11:07am    
use the table variable to create a record of where you have a condition. Then you use this temp table as a reference for the remainder of steps. The reason I did not add any extra code or conditional logic was I could not work out what was meant to match in each tables.
Member 14778402 23-Mar-20 12:55pm    
Hi Marcus for the life of me I cannot figure out how to create the condition.
Here is what I have so far, but I can't seem to get the condition to work:


declare @COUNTER INT
select @COUNTER = count(cprj)
from SSIS.dbo.[MatchingRowsOutput-CI-BOM-ProjectCumulative]
--where
--XMLLines.cprj=OrderEntry.DBO.OrderEntry.cprj


DECLARE @tmp TABLE (
ID INT,
[DWRowCount] [int] NULL,
[CumuCount] [int] NULL,
[cprj] [varchar](500) NULL,
[item] [varchar](500) NULL,
[PartNumber] [varchar](500) NULL,
[SWITEM] [varchar](500) NULL,
[QTY] [varchar](500) NULL,
[rutm] [varchar](500) NULL,
[trid] [varchar](500) NULL,
[mitm] [varchar](500) NULL,
[pono] [varchar](500) NULL,
[sitm] [varchar](500) NULL,
[opol] [varchar](500) NULL,
[qana] [varchar](500) NULL,
[scpf] [varchar](500) NULL,
[cwar] [varchar](500) NULL,
[opno] [varchar](500) NULL,
[cpha] [varchar](500) NULL,
[exin] [varchar](500) NULL,
[itlu] [varchar](500) NULL,
[ssta] [varchar](500) NULL,
[dsca] [varchar](500) NULL,
[dscb] [varchar](500) NULL,
[dscd] [varchar](500) NULL,
[suno] [varchar](500) NULL,
[ctyo] [varchar](500) NULL,
[eitm] [varchar](500) NULL)

Insert into @tmp

(DWRowCount, CumuCount, cprj, item, PartNumber, SWITEM, QTY, rutm, trid, mitm, pono, sitm, opol, qana, scpf, cwar, opno, cpha, exin, itlu, ssta, dsca, dscb, dscd, suno, ctyo, eitm)


SELECT
PartList.DWRowCount,
CumuCount = @COUNTER,
XMLLines.[cprj],
XMLLines.[item],
XMLLines.[PartNumber],
XMLLines.[SWITEM],
XMLLines.[QTY],
XMLLines.[rutm],
XMLLines.[trid],
XMLLines.[mitm],
XMLLines.[pono],
XMLLines.[sitm],
XMLLines.[opol],
XMLLines.[qana],
XMLLines.[scpf],
XMLLines.[cwar],
XMLLines.[opno],
XMLLines.[cpha],
XMLLines.[exin],
XMLLines.[itlu],
XMLLines.[ssta],
XMLLines.[dsca],
XMLLines.[dscb],
XMLLines.[dscd],
XMLLines.[suno],
XMLLines.[ctyo],
XMLLines.[eitm]

FROM
SSIS.dbo.[MatchingRowsOutput-CI-BOM-ProjectCumulative] AS XMLLines

inner JOIN
OrderEntry.dbo.OrderEntry as PartList ON
(XMLLines.cprj) = PartList.cprj

with the @Counter variable declared it only returns 156 count so its counting all the records under the cprj column. I need it to count only those total records in the cumulative table column for a specific project i.e TEK#7000 has only 26 entries, TEK#4700 has 13 entries and so forth but the return value is 156 which represents all the records for the entire table.

In the new @tmp table there are 2 columns created DWRowCount (from the Order Entry Table) and CumuCount that was just calculated with the condition.

I can then finally create the final table, deleting everything in the exception where the DWRowCount and CumuCount are equal.

Once this table is created I would like to got to the OrderEntry table and delete out the records that were retained in the final transfer table.

Thanks for your help!
MarcusCole6833 23-Mar-20 14:57pm    
is the qnty from cumulative you wish to count as a group, then compare to the order list?
Member 14778402 23-Mar-20 15:08pm    
No its all based on the number of rows and the cprj #. If you look at the tables I provided at the top QTY is not in the orderentry table. This order entry table is just a reference we use on how many rows for a particualr project is being sent thru our backend process and I am just comparing the number of lines on a per project basis from our cumulative table of the things sitting in a qeue. If they are equal I am getting the qeue to flow on a per project basis sending the transfer table thru
It's quite easy to get count of items by using COUNT (Transact-SQL)[^]

SQL
DECLARE @tmp TABLE (ID	INT, cprj VARCHAR(30), item VARCHAR(30), PartNumber VARCHAR(30), SWITEM VARCHAR(10), QTY INT)

INSERT INTO @tmp (ID, cprj, item, PartNumber, SWITEM, QTY)
VALUES(196, 'TEK#7000', 'A23-3171-X', 'A23-3171\2583', '1.2', 2),
(197, 'TEK#5555', 'N02-0846-X', 'N02-0846A5\59\2583', '2', 1),
(198, 'TEK#7000', 'N02-0846-X', 'N02-0846A5\59\2583', '2', 1),
(199, 'TEK#5555', 'A23-1354-X', 'A23-1354\2583', '2.3', 2),
(200, 'TEK#7000', 'A23-1354-X', 'A23-1354\2583', '2.3', 2),
(201, 'TEK#7000', 'B11-0304-X', 'B11-0304\25', '2.4', 2),
(202, 'TEK#7000', 'B11-0304-X', 'B11-0304\25', '1.4', 2),
(203, 'TEK#5555', 'B11-0304-X', 'B11-0304\25', '2.4', 2),
(204, 'TEK#5555', 'B11-0304-X', 'B11-0304\25', '1.4', 2),
(205, 'TEK#5555', 'B11-0305-X', 'B11-0305\59', '1.5', 2),
(206, 'TEK#5555', 'B11-0305-X', 'B11-0305\59', '2.5', 2),
(207, 'TEK#7000', 'B11-0305-X', 'B11-0305\59', '1.5', 2)

SELECT cprj, COUNT(cprj) CountOfCprj
FROM @tmp
GROUP BY cprj


Based on data have been posted, the result is:
cprj	CountOfCprj
TEK#5555	6
TEK#7000	6
 
Share this answer
 
Comments
Member 14778402 23-Mar-20 9:16am    
Thanks Maciej. My big problem I have is using this counter as a condition to extract records from the [MatchingRowsOutput-CI-BOM-ProjectCumulative] table with the condition that the counter matches a value in the column DWRowCount of the [OrderEntry] table and that the cprj value in both tables match. The records are then extracted to a new table [CI-BM-CPRJ-Transfer].
Lastly once the extraction occurs, I have to delete the records from the [OrderEntry] table based on the same conditions above, where the count is equal and the cprj are equal.
I keep getting a '0 rows returned' from my code.
My latest code is as below:

declare @COUNTER INT
select @COUNTER = count(cprj)
from SSIS.dbo.[MatchingRowsOutput-CI-BOM-ProjectCumulative]

Insert into SSIS.dbo.[CI-BM-CPRJ-Transfer]

(cprj, item, PartNumber, SWITEM, QTY, rutm, trid, mitm, pono, sitm, opol, qana, scpf, cwar, opno, cpha, exin, itlu, ssta, dsca, dscb, dscd, suno, ctyo, eitm)


SELECT
XMLLines.[cprj],
XMLLines.[item],
XMLLines.[PartNumber],
XMLLines.[SWITEM],
XMLLines.[QTY],
XMLLines.[rutm],
XMLLines.[trid],
XMLLines.[mitm],
XMLLines.[pono],
XMLLines.[sitm],
XMLLines.[opol],
XMLLines.[qana],
XMLLines.[scpf],
XMLLines.[cwar],
XMLLines.[opno],
XMLLines.[cpha],
XMLLines.[exin],
XMLLines.[itlu],
XMLLines.[ssta],
XMLLines.[dsca],
XMLLines.[dscb],
XMLLines.[dscd],
XMLLines.[suno],
XMLLines.[ctyo],
XMLLines.[eitm]


FROM
SSIS.dbo.[MatchingRowsOutput-CI-BOM-ProjectCumulative] AS XMLLines

inner JOIN
OrderEntry.dbo.OrderEntry as PartList ON
(XMLLines.cprj) = PartList.cprj

WHERE @COUNTER = PartList.DWRowCount

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