MyTechFinds.com

  • Increase font size
  • Default font size
  • Decrease font size
Home Articles Software Development SQL Sample of Pivot SQL

Sample of Pivot SQL

E-mail Print PDF
As a part of reporting, my friend faced a simple looking problem. He was struggling for almost half a day to solve this by a SQL query. When he showed me the problem, I just realized that it is something that we usually do in excel called pivot tables and I asked him to look over the internet if there is something called "pivot query" and to our surprise there is something called as that...we just didn't know, so I am going to document it. Here is a table we were looking at -
TestId   TestArea   TestResult
1                A           Pass
2                B           Fail
3                A           Pass
4                B           Pass
5                A           Fail

What we wanted as output was -
TestArea   Pass   Fail
A             2         1
B             1         1

So, write a query to group on testarea then create 2 colums from the testresult column and populate the values by counting passed and failed tests. Looked very simple to me and I tried to create it using inner joins but no luck.

The solution is to use Pivot Query as below -

SELECT TestArea, PASS, FAIL
FROM (
SELECT TestResult, TestArea, TestID
FROM TestResultTable1) up
PIVOT (COUNT(TestID) FOR TestResult IN (PASS, FAIL)) AS pvt

( 0 Votes )
Comments
Search
Only registered users can write comments!

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated on Monday, 19 April 2010 17:52  

Our valuable member Ajay Majgaonkar has been with us since Thursday, 23 April 2009.

Show Other Articles Of This Author

Software Development

Login

Like it? Share it!


Search

Polls

Which of the following are characteristics of testable software?
 

MyTechFinds

Advertisement

Help us
We have 3 guests online