Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a table as shown below.My question is:How can I convert columns into rows?BTW,I'm using Microsoft SQL Server 2005.

my input Data

  ONAY_ID  sip_RECno    KULLANICI       ONAY
   1           1         user1          true
   2           1         user2          true
   3           1         user3          false
   4           1         user4          true

Output would be

 sip_RECno  user1     user2    user3     user4       

  1          true     true    false    true          
share|improve this question
1  
What database/version are you working with? Based on previous questions, I'd assume SQL Server but you've had titles with 2000 and 2005 so version will matter –  billinkc Sep 12 '11 at 14:09
    
I've updated the tags to include sql-server - can you please get in the habit of being specific (including version) so that folks using Oracle, DB2 etc. don't try to solve your problem with inappropriate solutions? –  Aaron Bertrand Sep 13 '11 at 13:34
    
@Aaron Bertrand-I've updated my question. –  Cell-o Sep 13 '11 at 14:26

1 Answer 1

up vote 1 down vote accepted

EDIT for new data type revelation...

SELECT sip_RECno, 
    user1 = MAX(CASE WHEN ONAY_ID = 1 THEN CONVERT(TINYINT, ONAY) END),
    user2 = MAX(CASE WHEN ONAY_ID = 2 THEN CONVERT(TINYINT, ONAY) END),
    user3 = MAX(CASE WHEN ONAY_ID = 3 THEN CONVERT(TINYINT, ONAY) END),
    user4 = MAX(CASE WHEN ONAY_ID = 4 THEN CONVERT(TINYINT, ONAY) END)
FROM dbo.tablename
WHERE sup_RECno = 1
GROUP BY sip_RECno;
share|improve this answer
    
Shouldn't the WHERE clause be before the GROUP clause, or is that valid for the DB you're using (assume sql-server). Might add that to your answer. –  Derek Downey Sep 12 '11 at 14:19
    
You're right, I added the where clause as an afterthought. –  Aaron Bertrand Sep 12 '11 at 14:23
2  
No, just spaced-outedness. –  Aaron Bertrand Sep 12 '11 at 14:26
1  
If ONAY is a bit, why does the question list strings "true" and "false"? bit <> boolean. –  Aaron Bertrand Sep 13 '11 at 13:13
1  
It's an alias, equivalent to SELECT foo AS bar; - I prefer the alias = syntax over ` AS alias` syntax so that my column names line up nicely on the left. P.S. I'm making an assumption the user is using SQL Server based on the comment to the original question and the OP's error message above. Would be great if people posted more specifics. –  Aaron Bertrand Sep 13 '11 at 13:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.