0

I have a table with some records

MASTER TABLE

x------x--------------------x-------x
| Id   |      PERIOD        |   QTY |
x------x--------------------x-------x
|  1   |   2014-01-13       |   10  |
|  2   |   2014-01-06       |   30  |
x------x--------------------x-------x

I have another table with parameters of this record (ID)

TABLE2

x------x--------------------x------------x
| Id   |      Parameter        |   Value |
x------x--------------------x------------x
|  1   |   Humidty             |   10    |
|  1   |   Temperature         |   30    |
|  2   |   Humidty             |   50    |
|  2   |   Temperature         |   40    |
x------x--------------------x------------x

As result I want this: (combine based on ID)

Result table

x------x--------------------x-------------------------x
| Id   |      Period        |   Humidty | Temperature |
x------x--------------------x-------------------------x
|  1   |   2014-01-13       |   10      | 30          |
|  2   |   2014-01-06       |   50      | 40          |
x------x--------------------x-------------------------x

How Can I do something like that? Inner join will not work I think.

5
  • 2
    Converting rows to columns is called PIVOT. Search around for it. Commented Apr 9, 2019 at 12:26
  • 1
    Note that SQL Server is not Oracle Commented Apr 9, 2019 at 12:26
  • Please do not tag questions with multiple databases. [oracle] and [sql-server] are different platforms with different syntaxes. All you are doing is wasting the time of people who provide solutions you cannot use. Commented Apr 9, 2019 at 12:44
  • This is a question which occurs in assorted variations very commonly on this site. There are various different solutions. Which one might be best suited to your case depends upon your answer to the following question: does your real table2 (and desired result) have a fixed number of attributes which are always present for all entries in Master table? Commented Apr 9, 2019 at 12:50
  • Hello, Yes It haves fixed attributes, always present Commented Apr 9, 2019 at 12:53

3 Answers 3

4

Join the tables and use conditional aggregation with case to extract the 2 columns:

select t1.id, t1.period,
  max(case when t2.parameter = 'Humidty' then t2.value end) Humidty,
  max(case when t2.parameter = 'Temperature' then t2.value end) Temperature
from mastertable t1 inner join table2 t2
on t2.id = t1.id
group by t1.id, t1.period
Sign up to request clarification or add additional context in comments.

Comments

0

You can pivot:

SELECT * FROM 
(
SELECT 
  t_m.Id
, t_m.Period 
, t_2.Parameter
, t_2.Value
FROM @tbl_Master t_m
INNER JOIN @tbl_2 t_2 ON t_2.Id = t_m.Id
)AS t
PIVOT 
(
    MAX(t.Value)
    FOR t.Parameter IN ([Humidity], [Temperature])
)pvt

and sample data:

DECLARE @tbl_Master TABLE
(
   Id int,
   Period Date,
   QTY int

)

DECLARE @tbl_2 TABLE
(
   Id int,
   Parameter varchar(30),
   [Value] int

)

INSERT INTO @tbl_Master
(
    Id,
    Period,
    QTY
)
VALUES
  (1, '2014-01-13', 10)
, (2, '2014-01-06', 30)


INSERT INTO @tbl_2
(
    Id ,
   Parameter ,
   [Value] 
)
VALUES
  ( 1, 'Humidity', 10)
, ( 1, 'Temperature' , 30)
, ( 2, 'Humidity', 50)
, ( 2, 'Temperature' , 40)

OUTPUT:

Id    Period    Humidity       Temperature
1   2014-01-13    10            30
2   2014-01-06    50            40

Comments

0

Try this

DECLARE @Mastertable AS TABLE(Id INT,PERIOD DATE,QTY INT)
INSERT INTO @Mastertable
SELECT 1 ,'2014-01-13', 10  UNION ALL
SELECT 2 ,'2014-01-06', 30

DECLARE @Childtable AS TABLE(Id INT,Parameter  VARCHAR(100), Value INT)
INSERT INTO @Childtable
SELECT  1  ,'Humidty'             ,   10  UNION ALL
SELECT  1  ,'Temperature'         ,   30  UNION ALL
SELECT  2  ,'Humidty'             ,   50  UNION ALL
SELECT  2 , 'Temperature'          ,   40  

SELECT  Id,Period,[Humidty],[Temperature]
FROM
(
SELECT  c.Id,
        m.PERIOD,
        Parameter,
        c.Value
 FROM @Mastertable m
INNER JOIN  @Childtable c
ON m.Id = c.Id
) AS srC
pivot 
(MAX(Value) FOR Parameter IN ([Humidty],[Temperature])
) AS PVT

Result

Id  Period      Humidty Temperature
----------------------------------
1   2014-01-13    10      30
2   2014-01-06    50      40

Comments

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.