Update grouped rows with ID of newly inserted grouping row - sql-server

I want to group several rows of table A and insert a new row into table B for each bunch of grouped rows.
Next to that I want to update the rows of table A with the ID of the newly inserted row.
Inserting the lines into the table with 'grouplines' is like:
INSERT INTO B(...,...,...)
SELECT col1, col2 FROM A
GROUP BY col1,col2
This will produce a list of IDs in table B. I want to update the rows of table A with the ID of the corresponding group-row of table B.
Is there a possibilty to do this?
Some sample data:
After grouping table B looks like:
And then table A should look like:

As it is, your query potentially inserts more than one row.
I think that one solution would to use two queries: first insert in table b from table a, the update table a with newly created id(s) from table b.
INSERT INTO B(col1, col2, col3)
SELECT DISTINCT col1, col2, col3 FROM A
UPDATE A
SET A.B_ID = B.B_ID
FROM A
INNER JOIN B
ON A.col1 = B.col1
AND A.col2 = B.col2
AND A.col3 = B.col3

Deme on db<>fiddle
You can achieve table B by using Row_Number like below
Select ROW_NUMBER() OVER(ORDER BY Category) as ID, Name, Category
into #B
from #A
group by Name, Category
Then Update the table A after joining with table B like
Update a
set a.ID_Of_Group_row = b.ID
from #A a
inner join #B b on a.Category = b.Category
Output

Related

SQL Server : add a new column using union

I have 2 tables which have 2 columns in common and 1 column is different in both table
Table A
Table B
I need to create a common table having the values as follows
Expected Output
I tried using join on Memid and Meas but it duplicates as the 2 field do not create unique set as shown in figure
I tried union but then I get a resultset like this
Output for Inner join with distinct condition
How do I go about achieving the desired result set?
Note: Just a note coincidentally in this case the 2 columns seems to have similar values but they can be different.
Basically I need to create this one table with the 4 columns where Payer and PPayer columns should be independent of each other.
You don't need to use UNION, you can try like following using INNER JOIN.
INSERT INTO NewTable (
UserId
,DEPT
,ROOM
,LAB
)
SELECT DISTINCT ta.UserId
,ta.DEPT
,ta.ROOM
,tb.LAB
FROM TableA ta
INNER JOIN TableB tb ON ta.UserId = tb.UserId
AND ta.DEPT = tb.DEPT
Check Working Demo
Shanawaz Khan, Try this Solution
Declare Sample Table
DECLARE #A as TABLE(
UserId INT,
DEPT VARCHAR(50),
ROOM INT)
DECLARE #B as TABLE(
UserId INT,
DEPT VARCHAR(50),
LAB VARCHAR(50))
Insert Sample Records in Created Table
INSERT INTO #A (UserId,DEPT,ROOM) VALUES(1,'A',1),(1,'B',1),(1,'A',2),(1,'B',2)
INSERT INTO #B (UserId,DEPT,LAB) VALUES(1,'A','P'),(1,'B','Q'),(1,'A','P'),(1,'B','Q')
Generate DEPT wise Row number for Both Tables and Insert into another Temptable
SELECT ROW_NUMBER() OVER(PARTITION BY A.DEPT ORDER BY A.ROOM ) AS Rno,* INTO #tbl_A FROM #A A
SELECT ROW_NUMBER() OVER(PARTITION BY B.DEPT ORDER BY B.LAB) AS Rno,* INTO #tbl_B FROM #B B
Final Query Using Inner Join
SELECT A.UserId,A.DEPT,A.ROOM,B.LAB FROM #tbl_A AS A
INNER JOIN #tbl_B AS B ON A.Rno =B.Rno AND A.DEPT =B.DEPT ORDER BY A.ROOM, B.DEPT
Drop Created Temptable
DROP TABLE #tbl_A,#tbl_B
OutPut

Snowflake- Pattern Matching with Inner join

In snowflake Can we join on a column between 2 tables based on regex/substring instead of exact equality?
For example In Table A/Column A and Table B/Column B, fetch all the records where Column A is a substring of Column B.
I referred REGEXP_SUBSTR, SUBSTR, SUBSTRING and CONTAINS functions of the snowflake, but couldn't figure out how to use it as part of Inner JOIN.
You can do it with substrings, charindex functions, and probably some regexp commands too, here are a couple overly simple examples.
CREATE TABLE table_a (column_a VARCHAR(100));
INSERT INTO table_a VALUES ('hello world'),('testing 123'),
('I like Jelly'),('this is a good question');
CREATE TABLE table_b (column_b VARCHAR(100));
INSERT INTO table_b VALUES ('this is table b'),('world'),
('jelly'),('Netflix or Hulu?'),('Goodbye');
SELECT a.*
FROM table_a a
INNER JOIN table_b b
ON SUBSTR(a.column_a, CHARINDEX(' ', a.column_a) + 1, length(a.column_a))
= b.column_b;
--1 row selected, 'hello world'
SELECT a.*, b.*
FROM table_a a,
table_b b
WHERE CHARINDEX(UPPER(b.column_b), UPPER(a.column_a)) > 0;
--2 rows selected, 'hello world'/world & 'I like Jelly'/jelly

Selecting Columns A values if no like a value in column B in SQL Server

I have Table1
And I am trying to remove every identical Column A value if one of it's row has "ZX" anywhere in Column B. So if I did it right, it will look like Table2
I did the following:
Select
Column A,
Column B
From
Table1
Where
Column B not like '%ZX%'
However, it only removes rows with ZX and not every identical Column A values and returns this Table instead
I will really appreciate any help on this! Thank you in advance :)
You can use NOT IN
SELECT
ColumnA
, ColumnB
FROM table1
WHERE ColumnA NOT IN (SELECT ColumnA FROM table1 WHERE ColumnB like '%ZX%')
I like not exists for this purpose:
Select t1.*
From Table1 T1
where not exists (select 1 from table1 tt1 where tt1.a = t1.a and tt1.b like '%ZX%');
This can take advantage of an index on table1(a, b).
Use :NOT EXISTS and that should do it:
Select
[Column A],
[Column B]
From Table1 T1
where
NOT EXISTS (
SELECT 1 FROM TABLE1 T2
WHERE T2.[Column B] like '%ZX%'
AND T2.[column a] = t1.[column a]
)

SUM from Select query then Show as another Column

I have this table that hold several products
And another table that holds the order for each product from above
How can i combine these tables and get the sum of each ord_Count per item and show it as another column, like this
To summarize it, I have a Items table that holds different products, and an Orders table that holds the orders for each product from the Items Table, then I want to have a query that combines both tables and show a my stock for each item from the Items table.
Try this:
SELECT SUM(ord_count) AS Item_stock, itm_Code
FROM YourTable
GROUP BY itm_Code
To combine both table:
SELECT SUM(B.ord_count) AS Item_stock, B.itm_Code
FROM YourTable1 AS A
INNER JOIN YourTable2 AS B
ON A.itm_Code = B.itm_Code
GROUP BY B.itm_Code
declare #t table (item varchar(20),itemcode varchar(20))
insert into #t (item,itemcode)values ('choco','ckschoc'),('chocowafer','wfrchoc'),('chocostrrae','wfrstr')
declare #tt table (ordcnt int,itemcode varchar(20),dated date)
insert into #tt (ordcnt,itemcode,dated)values (20,'ckschoc','4/24/2015'),(10,'wfrchoc','4/24/2015'),(15,'wfrstr','4/24/2015')
,(30,'ckschoc','4/24/2015'),(20,'wfrstr','4/24/2015')
we can achieve the same result using sub query and corelated join query also
Solution
select t.itemcode,p.S from #t t INNER JOIN (
select itemcode,SUM(ordcnt)S from #tt
GROUP BY itemcode)P
ON p.itemcode = t.itemcode
group by t.itemcode,P.s
select t.itemcode,
(select SUM(tt.ordcnt)Cnt from #tt tt
where tt.itemcode = t.itemcode
group by tt.itemcode )Cnt from #t t

SQL server 2000 insert rows when join does not match

I would simply like to insert rows from a table to another if rows does not exist in the target.
How should I code that? with inner join?
Below is the query which returns rows that match between source and target
select * from LOG_S1201_REFERENCE_T1 b
inner join LOG_S1201_REFERENCE_STAGING_WT5 a on b.OU_ID=a.OU_ID and
b.Plant_desc=a.Plant_desc and b.workshop=a.workshop and
b.SerieNum=a.SerieNum and b.Operation_type=a.Operation_type and
b.PC10DBName=a.PC10DBName and b.SimuDBName=a.SimuDBName and
b.ProgramName=a.ProgramName and b.Calibre=a.Calibre
Copying rows can be done through INSERT SELECT
Need an example?
INSERT INTO new_table (col1, col2, col3)
SELECT col4, col5, col6
FROM old_table
HAVING !(SELECT COUNT(*) FROM new_table WHERE col1 = old_table.col4)
Join the table that has all rows with left join with the table that has some of the rows.
Filter one of the left joined table column with IS NULL
INSERT INTO your_table
select b.*
from LOG_S1201_REFERENCE_T1 b
left join LOG_S1201_REFERENCE_STAGING_WT5 a on b.OU_ID=a.OU_ID and b.Plant_desc=a.Plant_desc and b.workshop=a.workshop and b.SerieNum=a.SerieNum and b.Operation_type=a.Operation_type and b.PC10DBName=a.PC10DBName and b.SimuDBName=a.SimuDBName and b.ProgramName=a.ProgramName and b.Calibre=a.Calibre
WHERE a.OU_ID IS NULL
In order to select rows that do NOT match you need to do a left join.
INSERT INTO LOG_S1201_REFERENCE_T1 b2
SELECT a.* FROM LOG_S1201_REFERENCE_STAGING_WT5 a
LEFT JOIN LOG_S1201_REFERENCE_T1 b ON
(b.OU_ID=a.OU_ID
AND b.Plant_desc=a.Plant_desc
AND b.workshop=a.workshop
AND b.SerieNum=a.SerieNum
AND b.Operation_type=a.Operation_type
AND b.PC10DBName=a.PC10DBName
AND b.SimuDBName=a.SimuDBName
AND b.ProgramName=a.ProgramName
AND b.Calibre=a.Calibre)
WHERE b.OU_ID IS NULL
This will select all rows from a that are not in b which will then be inserted into a.
Afterwards a and b should be identical (except for b rows that are not in a).

Resources