7

I have table with the following information:

CREATE TABLE [dbo].[ApprovedPalmsInFarm](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [FarmID] [nvarchar](10) NOT NULL,
    [kalas] [int] NULL,
    [khnizi] [int] NULL

The default result of select query is:

id  FarmID      kalas   khnizi
4   12100462    5        0
5   11520344    3        0
6   12104277    150      15

I wanted to generate report using Unpivot query where the columns become row values

My report query is as the following:

SELECT FarmID, PalmsName, Quantity
FROM [ApprovedPalmsInFarm]
unpivot
(
Quantity
  FOR PalmsName in ([kalas] , [khnizi] )
) u;

The result of my Unpivot query is the following:

FarmID      PalmsName   Quantity
12100462    kalas        5
12100462    khnizi       0
11520344    kalas        3
11520344    khnizi       0
12104277    kalas        150
12104277    khnizi       15

Now my question is, how to change the name of the values of PalmsName while they are not understandable and they are the names of the columns so I would like to change the names for example to Arabic Language as the following:

خلاص instead of kalas

خنيزي instead of khnizi

Is it possible to change the name of the columns after they became row values?

3
  • You want to change the name of the values in your unpivot query? If so then this can be easily done by storing that query into a temp table then changing the values of the names in PalmsName. But I'm guessing you want to do it within the same query?
    – John Odom
    Commented May 6, 2015 at 22:12
  • yes in the same query, is it possible?
    – amal50
    Commented May 6, 2015 at 22:22
  • 2
    Yeah, you can use a CASE statement (CASE PalmsName WHEN 'kalas' THEN 'خلاص ' ... END AS 'PalmsName' etc). It'll be a bit clunky, but it'll work.
    – Jim
    Commented May 6, 2015 at 22:27

2 Answers 2

14

I made a comment, but I'll put an answer down here so it formats better:

SELECT 
    FarmID, 
    CASE PalmsName 
        WHEN 'kalas' THEN 'خلاص'
        WHEN 'khnizi' THEN 'خنيزي'
        ELSE PalmsName END AS 'PalmsName',
    Quantity
FROM [ApprovedPalmsInFarm]
unpivot
(
Quantity
  FOR PalmsName in ([kalas] , [khnizi] )
) u;
5
  • Didn't know that was possible with pivot/unpivot lol. +1 when I get my upvote counts replenished.
    – John Odom
    Commented May 6, 2015 at 22:33
  • it gives me error Incorrect syntax near the keyword 'AS'.
    – amal50
    Commented May 6, 2015 at 22:37
  • I removed the else statment and I makde the query as the following SELECT FarmID, CASE PalmsName WHEN 'kalas' THEN N'خلاص' WHEN 'khnizi' THEN N'خنيزي' END Quantity FROM [ApprovedPalmsInFarm] unpivot ( Quantity FOR PalmsName in ([kalas] , [khnizi] ) ) u; it worked perfectly, I really appreciate your help
    – amal50
    Commented May 6, 2015 at 22:42
  • It's because @Jim forgot the to END his CASE :P.
    – John Odom
    Commented May 6, 2015 at 22:42
  • Yep, missed the END. Fixed it.
    – Jim
    Commented May 6, 2015 at 22:45
0

You can also alias by creating a self contained subquery like so:

SELECT FarmID, PalmsName, Quantity
FROM 
(
SELECT FarmID, [kalas] AS [خلاص], [khnizi] AS [خنيزي] FROM
[ApprovedPalmsInFarm]
) AS apif
unpivot
(
Quantity FOR PalmsName in (apif.[خلاص] , apif.[خنيزي] )
) u;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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