SQL Server - Update data - sql-server

I have an table with this structure (example) ORIGNTABLE:
ID IDParent
80 0
81 80
82 0
83 82
Then I make a duplicate of this data obtaining the following data:
ID IDParent
80 0
81 80
82 0
83 82
---- duplicated data ---
84 0
85 80
86 0
87 82
on the other hand, with the duplication of data also get a temporary table with the following information AUXTABLE:
OldID NewID OldParentID
80 84 0
81 85 80
82 86 0
83 87 82
So, my problem is update the new data, specifically, the IDParent. I'm failing to do this.
I pretend the final data like that:
ID IDParent
80 0
81 80
82 0
83 82
---- duplicated data ---
84 0
85 **84**
86 0
87 **86**
Anyone can help me to update the data? i'm trying many options but none work:
1st try
UPDATE ORIGINTABLE
SET IDParent = AT.NewID
FROM ORIGINTABLE OT
INNER JOIN AUXTABLE AT ON AT.NewID=OT.ID
AND AT.OldParentID=OT.IDParent
2st try
UPDATE ORIGINTABLE
SET IDParent =AT.NewID
FROM ORIGINTABLE OT
INNER JOIN AUXTABLE AT ON AT.OldParentID=OT.ID

The trick is to join AUXTABLE twice. One instance will act as a filter, so that only new rows become updated. The other instance will provide the necessary mapping between old and new ID values.
This is the query:
UPDATE ot
SET ot.IDParent = mapping.NewID
FROM ORIGINTABLE AS ot
INNER JOIN AUXTABLE AS filter ON filter.NewID = ot.ID
INNER JOIN AUXTABLE AS mapping ON mapping.OldID = ot.IDParent
;

Related

How to get multiple columns values from other table

I have two tables. One has all the values as an int and the other table has the string value that correspondents for that (int) value.
Table 01
ID
D1
D2
D3
01
12
18
9
02
66
33
14
Table 02
Column A
Column B
12
SomeText
66
Something
33
John
14
Doe
15
Mark
18
Walter
I want a way to create a view that will change the int values (d1/d2/d3) to the values in Column B from table 2. The way that I have tried is this query:
SELECT [id],
b.[Column B] AS [d1],
c.[Column B] AS [d2],
d.[Column B] AS [d3]
FROM [Table01]
LEFT JOIN [Table02] b ON d1 = b.[Column A]
LEFT JOIN [Table02] c ON d1 = c.[Column A]
LEFT JOIN [Table02] d ON d1 = d.[Column A];
This is weak solution and it takes a long time to load. Any other solutions?
ID
D1
D2
D3
01
Text
Mark
john
02
someth
Walter
doe

SQL selecting row specific data by type

After numerous joins building a query, I stuck in a table of products with 3 column identifies ID-Color-Size and the column of data barcode like
Id
Color
Size
Barcode
34
40
4
5205barcode1
34
40
4
extradata1
34
40
5
5205barcode2
34
40
5
extradata2
34
41
4
5205barcode3
34
41
4
extradata3
35
40
5
5205barcode4
35
40
5
extradata4
34
40
3
data4
35
39
5
data5
35
40
3
data6
I need to keep the unique combinations of ID-Color-Size with barcode (starting with '5205%') and remove the rows with same id-color-size (the extradata1-5 are considered duplicate).
The final table would have unique combinations of ID-Color-Size-barcode1-4 and data4-5-6
If I understand correctly you need a window function to order duplicates of id/color/size by the barcode and only select those where the barcode starts 5205:
with p as (
select *,
Row_Number() over(partition by id, color, size order by case when barcode like '5205%' then 1 end desc) rn
from t
)
select id, color, size, barcode
from p
where rn=1

Trying to Replace While loop that executes based on a value in a row from a view to a Set query

I am trying to create a query that will give me random rows from a view (view_XYZ) based on the value in the SampleSizeByRow (in view_SampleSizeByRow) then it inserts the random value from view_XYZ into tbl_Randomization.
view_SampleSizeByRow looks like
ID_x SampleSizeByRow
49 1
87 1
47 6
41 2
247 13
96 3
99 1
31 1
91 13
DECLARE #CurrSize AS INT
DECLARE #CurrID AS INT
DECLARE #SampleByValTbl AS TABLE(
Samplesize INT,
ID INT
)
INSERT INTO #SampleByValTbl (
Samplesize
, ID
)
SELECT SampleSizeByRow, ID_x
FROM view_SampleSizeByRow
ORDER BY ID_x;
WHILE(1=1) -- Infinite Loop until it is broken
BEGIN
SET #CurrID = NULL -- Reset pointer variables
SET #CurrSize = NULL
SELECT TOP(1) #CurrID = ID
FROM #SampleByValTbl
SELECT TOP(1) #CurrSize = Samplesize
FROM #SampleByValTbl
IF (#CurrID IS NULL OR #CurrSize IS NULL) -- End loop if there are no ID's to go through
BREAK
IF #CurrSize <= 0
BEGIN
DELETE TOP(1) FROM #SampleByValTbl
CONTINUE
END
ELSE
BEGIN
WHILE #CurrSize > 0
BEGIN
INSERT INTO tbl_Randomization (
ID_x
, Num_y
)
SELECT TOP(1) ID_x
, Num_y
FROM view_XYZ
ORDER BY NEWID() -- Random Selection
SET #CurrSize = #CurrSize - 1
END
DELETE TOP(1) FROM #SampleByValTbl -- To shorten the size of the table each time we parse through it
END
END
Results should be something like the table below
ID_x Num_y
41 4888084
41 4895898
46 4889509
46 4889493
47 4891612
47 4898679
47 4889485
47 4902432
49 4893435
91 4898447
91 4898892
91 4895738
91 4888350
91 4898920
91 4886422
91 4899137
91 4895741
91 4886918
91 4894146
91 4888301
91 4888383
91 4882095
91 4898402
96 4893927
96 4893738
96 4887504
99 4893713
247 4897332
247 4902343
247 4897769
247 4895529
247 4885333
247 4895512
247 4895488
247 4885279
247 4893031
247 4891872
247 4896523
247 4901417
247 4885181
247 4897541
...
I have tried to use CTE and various joins but I can't figure out how to code them so they return the same results.
If I followed you correctly, you want to select sample_size random values from view_XYZ for each id_x listed in view_SampleSizeByRow.
Here is an approach that uses a recursive query to generate the rows, then a correlated subquery to retrieve the random value:
with cte as (
select id_x, sample_size - 1 from view_SampleSizeByRow where sample_size > 0
union all
select id_x, sample_size - 1 from cte where sample_size > 0
)
insert into tbl_Randomization (id_x, num_y)
select
id_x,
(select top (1) num_y from view_XYZ v where v.id_x = c.id_x order by newid())
from cte c

How to get student maximum interm marks

i want to get maximum student interm marks from below table.
Student name Interm1 marks Interm2 marks Interm3 marks
Raj 60 75 89
raju 78 74 67
ram 67 79 65
balaji 91 89 93
My required Output is:
Balaji 93
option:
raju 78
raj 89 etc..
like this i need output.
can any body help me here for this query.
You'll need to first unpivot your data, and then get the MAX value. I prefer using VALUES to unpivot data, rather than the UNPIVOT operator:
SELECT YT.StudentName,
MAX(IM.ItermMark) AS MaxItermMark
FROM YourTable YT
CROSS APPLY(VALUES(Interm1Mark),(Interm2Mark),(Interm3Nark))IM(ItermMark)
GROUP BY StudentName;
Have a look at this. This should do what you want to do
SELECT [Student name], MAX(MaxMark)
FROM
(
SELECT [Student name],
(SELECT MAX(v) FROM (VALUES ([Interm1 marks]), ([Interm2 marks]), ([Interm3 marks])) AS value(v)
) AS [MaxMark]
) AS subquery
GROUP BY [Student name]

Querying emails that contains an individual

I want to pull a conversation for a certain order that a person has ever read a message on. For instance:
Order SentTOID ReadBy
A 111 55
A 55 55
A 111 89
B 111 89
C 111 55
C 55 55
D 111 99
D 99 99
Results
A 111 55
A 55 55
A 111 89
C 111 55
C 55 55
My code will only pull all the cases that were read by 55 but I want the whole converstaion.
Order SentTOID ReadBy
A 111 55
A 55 55
C 111 55
C 55 55
Code used.
Select *
from conversation
where readby = 55.
first get Order and apply IN clause
declare #temp table
(Orders nvarchar(7), SentTOID int, ReadBy int)
insert into #temp values ('A',111,55)
insert into #temp values ('A',55 ,55)
insert into #temp values ('A',111,89)
insert into #temp values ('B',111,89)
insert into #temp values ('C',111,55)
insert into #temp values ('C',55 ,55)
insert into #temp values ('D',111,99)
insert into #temp values ('D',99 ,99)
select * from #temp
where Orders in (select Orders from #temp where ReadBy = 55)
RESULT

Resources