Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9008117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:51:56+00:00 2026-06-16T01:51:56+00:00

TRANSFORM Avg(CASE WHEN [temp].[sumUnits] > 0 THEN [temp].[SumAvgRent] / [temp].[sumUnits] ELSE 0 END) AS

  • 0
TRANSFORM Avg(CASE WHEN [temp].[sumUnits] > 0 
                   THEN [temp].[SumAvgRent] / [temp].[sumUnits] 
                   ELSE 0 
              END) AS Expr1
SELECT [temp].[Description]
  FROM [temp] 
GROUP BY [temp].[Description]
PIVOT [temp].[Period];

Need to convert this query for sql server

I have read all other posts but unable to convert this into the same

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:51:57+00:00Added an answer on June 16, 2026 at 1:51 am

    Here is the equivalent version using the PIVOT table operator:

    SELECT *
    FROM
    (
      SELECT 
        CASE 
          WHEN sumUnits > 0 
          THEN SumAvgRent / sumUnits ELSE 0 
      END AS Expr1,
      Description,
      Period
      FROM temp
    ) t
    PIVOT
    (
      AVG(Expr1)
      FOR Period IN(Period1, Period2, Period3)
    ) p;
    

    SQL Fiddle Demo

    For instance, this will give you:

    | DESCRIPTION | PERIOD1 | PERIOD2 | PERIOD3 |
    ---------------------------------------------
    |          D1 |      10 |       0 |      20 |
    |          D2 |     100 |    1000 |       0 |
    |          D3 |      50 |      10 |       2 |
    

    Note that When using the MS SQL Server PIVOT table operator, you have to enter the values for the pivoted column. However, IN MS Access, This was the work that TRANSFORM with PIVOT do, which is getting the values of the pivoted column dynamically. In this case you have to do this dynamically with the PIVOT operator, like so:

    DECLARE @cols AS NVARCHAR(MAX);
    DECLARE @query AS NVARCHAR(MAX);
    
    SELECT @cols = STUFF((SELECT distinct 
                            ',' +
                            QUOTENAME(Period)
                    FROM temp
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'');
    
    
    
    SET @query =  ' SELECT Description, ' + @cols + '
        FROM 
        (
          SELECT 
            CASE 
              WHEN sumUnits > 0 
              THEN SumAvgRent / sumUnits ELSE 0 
          END AS Expr1,
          Description,
          Period
          FROM temp
        ) t
        PIVOT
        (
          AVG(Expr1)
          FOR Period IN( ' + @cols + ') 
        ) p ';
    
    
    
    Execute(@query);
    

    Updated SQL Fiddle Demo

    This should give you the same result:

    | DESCRIPTION | PERIOD1 | PERIOD2 | PERIOD3 |
    ---------------------------------------------
    |          D1 |      10 |       0 |      20 |
    |          D2 |     100 |    1000 |       0 |
    |          D3 |      50 |      10 |       2 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting -moz-transform: translate(-283.589px, 0px) from dom by doing element.style[vendor + 'Transform'] .
TRANSFORM Count(Details.Customer_ID) AS CountOfCustomer_ID SELECT Switch( [Age] < 25, Under 25, [Age] >= 25
Hough Transform can be used to extract lines from images. It can also be
I am trying to transform data from table1 into table2 table1 UserId Date1 1
Is it possible to transform a div into this kind of shape then fill
fid=fopen('transform.txt'); alltext = textscan(fid, '%s'); d = size(alltext) for k=1:1 line = alltext{1}{k} end
Now I want to transform a Array from object_c to javascript, just like label1
Let f transform one value to another, then I'm writing a function that repeats
I want to transform photos in python to look like this: taken from doctype.com
I have a string transform(23, 45) from this string i have to extract 23

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.