0

I have 2 tables with common column id

Table A:
ID   value1    value2    value3
------------------------------------        
 1     0         0        0

Table B:
ID    value_type      value   
--------------------------------------          
1     value1          0.01  
2     value2          0.02
3     value3          0.03

I want to update table A value using table b value,

Final Output :

ID   value1    value2    value3
-----------------------------------------
1     0.01       0.02      0.03

Can anyone show me how to write a query and fetch the result as above, Thanks very much! I have tried join, pivot. all result nothing.

4
  • 2
    What happens if Table A has more than one ID value? Commented Oct 8, 2018 at 6:16
  • 1
    Might be ID column having value 1 for all records of Table B in given example. Please confirm. Commented Oct 8, 2018 at 6:17
  • yes there is multiple value in table A as well as table B with common column ID Commented Oct 8, 2018 at 6:19
  • 1
    Not according to the sample data you've posted. In table A you only posted one record and it table B every record have a different value in the ID column. Please edit your question to resolve the conflict between the text and the sample data. While you're at it, you should read the excellent post over on meta.dba.stackexchange called Help me write this query in SQL. and convert the sample data to DDL+DML statements. Commented Oct 8, 2018 at 6:27

2 Answers 2

1

A quick (but not scalable) solution would be to use an update statement with a join to a derived table with conditional aggrigation:

UPDATE T
SET value1 = v1,
    value2 = v2,
    value3 = v3
FROM TableA T
JOIN 
(
    SELECT 1 As ID
           MAX(CASE WHEN value_type = 'value1' THEN value END) As v1,
           MAX(CASE WHEN value_type = 'value2' THEN value END) As v2,
           MAX(CASE WHEN value_type = 'value3' THEN value END) As v3
    FROM TableB
) V
    ON T.ID = V.ID
Sign up to request clarification or add additional context in comments.

3 Comments

there are no fixed column numbers in table it is possible to fetch dynamic?
Please edit your question to specify your needs exactly. Also, be sure that the sample data matches the text. Before you do that, anything I'll write would be a guess.
@Thebeginner . . . This answers the question that you have asked, which is specifically about updating three columns in the first table. If you have a different question, then ask it as a new question. You should be aware that tables do not have dynamic columns, so be careful about how you phrase your next question.
0

using pivot we can do this

Since iam storing data into one table and after that only iam doinn updation

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.value_type)
            FROM #b c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT rn as id , '  + (@cols) + ' into dynamictable  from 
            (
                select *,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID ) AS RN
                from #b
           ) x
            pivot 
            (
                MAX(VALUEE)
                for VALUE_TYPE in (' + @cols + ')
            ) p 
          '


exec(@query)
UPDATE T
SET T.value1 = V.value1,
    T.value2 = V.value2,
    T.value3 = V.value3
FROM #a T
JOIN 
(SELECT   id ,MAX(VALUE1)VALUE1 , MAX(VALUE2)VALUE2, MAX(VALUE3)VALUE3 
FROM  dynamictable
GROUP BY id
) V
    ON T.ID = V.ID

enter image description here

4 Comments

there are no fixed column numbers in table it is possible to fetch dynamic?
@Thebeginner then you have to use dynamic pivot
can u please help me?
@Thebeginner this will not be possible even dynamic query also because we can do upto pivot multiple columns but the thing is we cant update the required output into table1 since table is having only 3 columns so i can give code upto dynamic pivot only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.