0

I have 2 tables

table#1: Order

orderid  unitid  active
      1      aa      1      
      2      bb      0
      3      cc      1
      4      dd      1

table#2:Details

orderid     month
      1         6
      1         7
      1        12
      2         1
      2         6
      3         1
      3         2
      3         3
      3         4
      3         6

Output desired:

orderid   unitid     jan   feb  mar  apr  may  jun  .........  dec
  1           aa                               yes             yes
  3           cc     yes   yes  yes  yes

For all orders where ACTIVE is 1 and all unitids.

I tried using case statement, i get multiple rows for a single orderid, which is not how i want.

I see a lot of examples for pivot with one table, how to do this using 2 tables? I am using SQL Server 2012.

Seymour
  • 7,043
  • 12
  • 44
  • 51
  • Hi, does "SQL 2012" mean SQL Server 2012? Why then is it tagged Oracle? If not what does it mean? It'd be great if you could also [edit] your question to include what you've tried so far. Thanks! – Ben Dec 13 '13 at 17:53
  • To do it with two tables, just write a join and pivot the results. – Barmar Dec 13 '13 at 17:55

1 Answers1

0

Maybe a Select within a SELECT as argument

Something like this;

Select orderid, unitid, (SELECT month 
                         From Table2 
                         WHERE ...)
From table1
Where ...

I am referencing with this answer this Issue:

A select query selecting a select statement

Community
  • 1
  • 1
Diversity
  • 1,890
  • 13
  • 20