4

Please help me create a select SQL statement with the results column name get from the column values in origin table (tablename is Device_Part):

enter image description here

User can input many DeviceCode which have many dynamic PartTypeName, the PartTypeName value is the PartInfo.

4
  • Maybe you're looking for dynamic MS SQL pivot : stackoverflow.com/questions/10404348/… Commented Nov 1, 2016 at 9:22
  • Thanks you very much, i try it but it may hard to understand for me, and maybe it have constant value, when my values is dynamic Commented Nov 1, 2016 at 9:43
  • If I write your code for you I'll charge you my normal hourly rate! But I'll help you for free. Have a go. Make some mistakes. Learn some stuff on the way. Then come back with what you tried, and tell us in what way it does not work. Remember to include error messages and sample data. Commented Nov 1, 2016 at 9:47
  • Thanks for your advice! I will try it ! Commented Nov 2, 2016 at 1:07

2 Answers 2

3

This may help:

CREATE Table Device (
DeviceCode NVARCHAR(100) NOT NULL,
PartTypeName NVARCHAR(100) NOT NULL,
PartInfo NVARCHAR(100) NOT NULL
)

Insert Into Device
Values('VT.SX-01','CPU','Pentium G6650'),
('VT.SX-01','Motherboard','H81M -  S2PV'),
('VT.SX-01','RAM','DDR# 4GB - bus 1866 - Nano'),
('VT.SX-01','PartType Name 01','PartInfo 01')

--QUERY
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX);
DECLARE @ColumnNamesInPivot AS NVARCHAR(MAX);

--Get distinct values of PIVOT Column 
SELECT   @ColumnNamesInPivot = ISNULL(@ColumnNamesInPivot + ',', '')
        + QUOTENAME([PartTypeName])
FROM    ( SELECT    DISTINCT
                    [PartTypeName]
          FROM      Device
        ) AS P



SELECT  @DynamicPivotQuery = N'Select DeviceCode,'
        + @ColumnNamesInPivot + ' 
            FROM    ( SELECT * 
          FROM      Device
        ) AS SourceTable PIVOT( MAX(PartInfo) FOR [PartTypeName] IN ('
        + @ColumnNamesInPivot + ') ) AS PVTTable'

EXEC sp_executesql @DynamicPivotQuery;

And the result will be:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks you very much for your work hard answer ! I run the first select success: farm6.staticflickr.com/5560/30407745360_5dc7583532_o_d.jpg , but the second is failed: farm6.staticflickr.com/5532/30707958185_4e3460c27e_o_d.jpg . Could you please help me try it, thanks you so much!
and error if i run 2 select together: farm6.staticflickr.com/5343/30708083475_535c6c2196_o_d.jpg . Could you please help me try it, thanks you so much!
I have edited the query... in order to get the desired out put, you need to select the query part from declaration of variables upto the end
Hien Nguyen, did you get what you wanted?
You are my master, the result is exactly my expected, thousand like for you, Thanks you very much for your enthusiastically help! farm6.staticflickr.com/5702/30090888554_d947cf4179_o_d.jpg . I would like to learn this technical SQL, could you please help me the technical name so i can find this on google. Thanks you!
|
1
Try this

;WITH _CTE(_DeviceCode,_PartTypeName,_PartInfo,_Id)
AS
(
 SELECT DeviceCode,PartTypeName,PartInfo ,ROW_NUMBER() OVER (PARTITION BY      PartTypeName ORDER BY Id) FROM Your_tablename
)
SELECT *
FROM
(
SELECT _DeviceCode,_PartTypeName,_PartInfo
FROM _CTE
)C
PIVOT
(
 MAX(_PartInfo) FOR _PartTypeName IN ([CPU],[MotherBoard],[RAM],[PartTypeName])
) AS PivotTable;

1 Comment

Thanks you very much for your answer, but the PartTypeName is dynamic, not only are [CPU],[MotherBoard],[RAM] , we can input more as Power, Monitor, Keyboard......

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.