Solved

TRANSFORM, PIVOT - implement in SQL

Posted on 2004-10-01
2
930 Views
Last Modified: 2012-06-27
Here is my access query:

TRANSFORM Sum(tblBudgetAmount.LocalAmount) AS SumOfFunctionalAmount
SELECT tblBudgetItem.CostCenterID, tblBudgetItem.GL as GL1,tblBudgetItem.ProjectID,
      Sum(tblBudgetAmount.LocalAmount) AS ColumnTotal, tblAcctCode_0.sStatus
FROM tblAcctCode_0
INNER JOIN (tblBudgetItem
INNER JOIN tblBudgetAmount ON tblBudgetItem.ID = tblBudgetAmount.BudItemID)
                  ON tblAcctCode_0.sCodeID = tblBudgetItem.GL
Where tblBudgetItem.CostCenterID ='385' and tblBudgetItem.ProjectID='1537'
      and left(tblBudgetItem.GL,2) like '62' and tblBudgetItem.ActivityID='50770'
      and tblBudgetItem.FundID='10' and tblBudgetAmount.FiscalYear ='2005'
GROUP BY tblBudgetItem.GL, tblBudgetItem.CostCenterID, tblBudgetItem.ProjectID, tblBudgetItem.GL, tblAcctCode_0.sStatus PIVOT tblBudgetItem.FundingSource


How would I go about converting this to SQL?
0
Comment
Question by:TWBit
2 Comments
 
LVL 6

Expert Comment

by:mcp111
ID: 12203837
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 125 total points
ID: 12203900
Wait for SQL Server 2005, when this type of query will be implemented? ;)

Seriously, SQL Server does not innately support these types of queries, and a fair amount has been written on how to emulate them.  Usually, it will involve some custom coding and/or dynamic SQL.

Working from what I see here, what I would probably do is:

1)  Since you PIVOT is the FundingSource, this will be key.  Dynamically create an SQL Statement to generate the resulting data, using the information from the base SELECT as the source of the CASE statements needed.  This could be something like:

Declare @sqlCase varchar(7000)

Set @sqlCase = ''

Select @sqlCase = @sqlCase + 'Sum(CASE WHEN bi.Fundingsource = ''' + Fundingsource + ''' THEN ba.LocalAmount ELSE 0 END) AS [' + Fundingsource +, '
FROM
    (Select Distinct bi.Fundingsource From tblBudgetItem bi
      INNER JOIN tblBudgerAmount ba ON bi.ID = ba.BudItemID
      INNER JOIN tblAccountCode_0 ac ON tblBudgetItem.GL = tblAccountCode_0.sCodeID
      WHERE bi.CostCenterID ='385'
            and bi.ProjectID='1537'
            and left(bi.GL,2) = '62'
            and bi.ActivityID='50770'
            and bi.FundID='10'
            and ba.FiscalYear ='2005'
    ) Tbl1

Declare @sql varchar(8000)

Set @sql = 'SELECT bi.CostCenterID, bi.GL, bi.ProjectID, ' + @sqlCase + ' ac.sStatus FROM  tblBudgetItem bi
      INNER JOIN tblBudgerAmount ba ON bi.ID = ba.BudItemID
      INNER JOIN tblAccountCode_0 ac ON tblBudgetItem.GL = tblAccountCode_0.sCodeID
      WHERE bi.CostCenterID =''385''
            and bi.ProjectID=''1537''
            and left(bi.GL,2) = ''62''
            and bi.ActivityID=''50770''
            and bi.FundID=''10''
            and ba.FiscalYear =''2005''
     GROUP BY tblBudgetItem.GL, tblBudgetItem.CostCenterID, tblBudgetItem.ProjectID, tblBudgetItem.GL, tblAcctCode_0.sStatus'

EXEC @sql
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to share SSIS Package? 6 37
T-SQL: Do I need CLUSTERED here? 13 45
convert null in sql server 12 34
Help in Bulk Insert 9 35
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question