Tag: pivot

Adding aliasing to field names in Pivot SQL query

I have a query below, and need to have field [Cmp-Goal-RF-148] (which is pivoted to be a column) – I need the column title to be something besides [Cmp-Goal-RF-148], so I suppose I need to alias it. Doing this throws up an error: ([Cmp-Goal-RF-148] AS ‘Ghost’). What am I missing? select * from ( select […]

SQL Transpose multiple columns into rows

This question already has an answer here: SQL transpose full table 1 answer

SQL pivot to transpose data

I’m having trouble wrapping my head around how to use pivot (or maybe some other solution) to just transpose data. I don’t have to worry about aggregating any information, I literally just want to transpose rows and columns. I have a table where each record stores answers to a series of questions. The structure looks […]

Pivot Table SQL server 2008

I can’t get to grips with pivot tables, I have 3 tables: AccidentType: AccidentTypeID Description 1 Type1 2 Type2 3 Type3 4 Type4 CaseAccidentType: AccidentTypeID CaseID 1 1000 2 1000 3 1001 Case: CaseID Name 1000 Case A 1001 Case B Case is the main table with caseaccidenttype as a link table to the accident […]

Pivot table multiple column values in a single column

I’ve got a table like that, id PID VID Type PriceA PriceB 41 297 2 128 70.000 80.000 42 297 3 256 90.000 100.000 43 297 4 300 110.000 120.000 44 297 5 400 130.000 140.000 45 294 2 128 10.000 50.000 46 294 3 256 20.000 60.000 47 294 4 300 30.000 70.000 48 […]

SQL Server Pivot with Date Ranges as Columns

I have a table with the following structure recording employees arrival and dispersal times: What I’m hoping to achieve in a query/stored procedure is a pivot type data return between date ranges as per bottom grid on the attached image. Could I kindly ask your expertise or sample on how to get it done? I […]

Microsoft SQL Server : converting 3 rows to 3 columns

I have a rather large report I am working on and as it turns out I need to have insurance information in 1 row rather than up to 3. I can get by on SQL coding with the basics but when it comes to pivot tables and other advanced tasks I bog down and it […]

SQL – Pivot 2 columns

I have the following sql CREATE TABLE #t ( id varchar(10) ,Rscd varchar(10) ,Accd varchar(10)) INSERT INTO #t Values (‘EHC’,’A’,’B’) INSERT INTO #t Values (‘DEN’,’C’,’D’) select EHC,DEN from ( select id as id2, value from #t cross apply ( select id,Rscd union select id,Accd ) c (id2,value) ) d pivot ( max(value) for id2 in […]

Joining 3 SQL Querys with Pivot/Unpivot in them

I have a tables named MachineSweepQuestions , MachineSweepAnswers , MachineSweetComments, I need to join all 3 of these tables using what i think are Pivots as displayed in my code so they can share an outcome via a few parameters. Below are pictures of the code with a image of the returned results and a […]

PIVOT in SQL Server with no aggregate function

I have a table like this (lets call it T1): Date | Category | Data ——————————- 2014-01-01 | 1 | 1.0 2014-01-01 | 2 | 2.0 2014-01-01 | 3 | 3.0 2014-01-02 | 1 | 4.0 2014-01-02 | 2 | 5.0 2014-01-02 | 3 | 6.0 Note that Data are floating point numbers, not sequential, […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.