unpivot

Dynamic Pivot (row to columns)

自作多情 提交于 2019-12-11 15:40:02
问题 I have a Table1 : ID Instance Name Size Tech 1 0 D1 123 ABC 1 1 D2 234 CDV 2 2 D3 234 CDV 2 3 D4 345 SDF I need the resultset using Dynamic PIVOT to look like along with the headers: ID | Instance0_Name | Instance0_Size | Instance0_Tech | Instance1_Name | Instance1_Size | Instance1_tech 1 | D1 | 123 | ABC | D2 | 234 | CDV Any help would be appreciated. using Sql Server 2008. Sorry for the earlier post. 回答1: Your desired output is not exactly clear, but you can use the both the UNPIVOT and

Error : The type of column “DOB” conflicts with the type of other columns specified in the UNPIVOT list

五迷三道 提交于 2019-12-11 15:25:43
问题 MY QUERY, simply to convert the row into column SELECT ColumnName, value FROM (SELECT id [ID], firstname [First Name], lastname [Last Name], dob [DOB], sex [Gender] FROM client WHERE id = '11') d UNPIVOT ( Value FOR ColumnName IN ([ID], [First Name], [Last Name], [DOB], [Gender]) ) unpiv; But it showing an error. The type of column "DOB" conflicts with the type of other columns specified in the UNPIVOT list. 回答1: As the result will bring back all columns in rows, building a new derived column

Query for crosstab view

放肆的年华 提交于 2019-12-11 15:03:51
问题 I have a table in PostgreSQL like below: -------------------------------------------------------------- Item1 | Item2 | Item3 | Item4 |Value1| Value2| Value3| Value4| -------------------------------------------------------------- I want a query which will show this table like below: ItemHead| ValueHead --------------- Item1 | Value1| --------------- Item2 | Value2| ---------------- Item3 | Value3| ---------------- Item4 | Value4| --------------- 回答1: Use a single SELECT with a LATERAL join to

Loop through a table using Cross apply and UNION ALL the results

a 夏天 提交于 2019-12-11 14:47:12
问题 Trying to write a query which will behave like a foreach Query : select label ,NTILE(10) over(order by label ASC) Quartile INTO #labelTempTab from dbo.ReportFieldsLookup The data will be like : label Quartile ----- -------- la1 1 la2 1 la3 1 sa1 2 sa2 2 sq3 2 ha1 3 ha2 3 ha3 3 ka1 4 ka2 4 kas3 4 Continuation of Query : DECLARE @sql nvarchar(max) SELECT * INTO #SetValuesTable FROM svo_tbl SET @sql = 'SELECT MNUM, Label , LabelValue ,[Property Type] FROM #SetValuesTable ' +' CROSS APPLY (

Calculate Days Present and absent from the table

不想你离开。 提交于 2019-12-11 12:42:16
问题 I have a table name Emp_mon_day which consists Employees Present and Absent details . What I want is I need for these 9 employees, information about days present and days absent for each employees from Emp_mon_day table merged in to below query QUERY SELECT e.comp_mkey, e.status, e.resig_date, dt_of_leave, e.emp_name, e.date_of_joining, e.emp_card_no, a.pl_days, pl_days_opening, a.month1, a.month2, a.month3, a.month4, a.month5, a.month6, a.month7, a.month8, a.month9, a.month10, a.month11, a

Rewrite code with UNPIVOT from sql to redshift

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 08:52:34
问题 I have SQL server script that needs to be converted to redshift. I converted part of it Here is part of the script where I have a problem with. SELECT a.*, b.* FROM ( SELECT u.ContactId, u.Description, CONVERT(Float,u.SeatCharge) AS SeatCharge --CAST(u.SeatCharge AS numeric (18,4)) AS SeatCharge, --CONVERT(INT, CASE WHEN IsNumeric(CONVERT(VARCHAR(12), u.SeatCharge)) = 1 then CONVERT(VARCHAR(12), u.SeatCharge) else 0 End) FROM( SELECT md.contactid, CONVERT(float, MAX(CASE WHEN md.fieldid= 7172

How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?

时光毁灭记忆、已成空白 提交于 2019-12-11 08:37:16
问题 I asked a very similar question earlier, but the requirements this time are a bit more complex. I have a table that looks like the following: | id | code_1 | code_2 | pair_1 | |----|--------|--------|--------| | 1 | a1 | a2 | b1 | | 2 | a3 | a4 | (null) | | 3 | a5 | (null) | b2 | | 4 | a6 | (null) | (null) | | 5 | (null) | a7 | b3 | | 6 | (null) | a8 | (null) | | 7 | (null) | (null) | b4 | | 8 | (null) | (null) | (null) | I want to stack code_1 and code_2 into a single column, which can be

Separate Potential Duplicates Into Different Rows

岁酱吖の 提交于 2019-12-11 06:11:29
问题 I am trying to identify potential duplicate customers in my database based on the last 4 of the SSN, last name and DOB. The stored procedure I have written does identify potential duplicates but it lists them in one row - I am trying to split into separate rows for reporting reasons. My T-SQL looks like: DECLARE @StartDate DATE = '1/1/2017', @EndDate DATE = '3/1/2017'; SELECT DENSE_RANK() OVER (ORDER BY c.socialSecurityNumber) AS [SSNRanking] , ROW_NUMBER() OVER (PARTITION BY c

Cross apply on columns on SQL server. Syntax error near )

北城以北 提交于 2019-12-11 06:06:45
问题 I am trying to unpivot several columns, but I can't find the way of solving the syntax error. It says incorrect syntax near ')'. Here is the code: SELECT dates, times, locations, events FROM mytable CROSS APPLY (VALUES ('instance1', instance1), ('instance2', instance2), ('instance3', instance3), ('instance4', instance4)) as Items(locations, events) Could it be because my SQL Server version does not support values properly and I need to store the values in a different table to refer them for

Row-wise maximum in T-SQL [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-11 04:45:51
问题 This question already has answers here : SQL MAX of multiple columns? (21 answers) Closed 4 years ago . I've got a table with a few columns, and for each row I want the maximum: -- Table: +----+----+----+----+----+ | ID | C1 | C2 | C3 | C4 | +----+----+----+----+----+ | 1 | 1 | 2 | 3 | 4 | | 2 | 11 | 10 | 11 | 9 | | 3 | 3 | 1 | 4 | 1 | | 4 | 0 | 2 | 1 | 0 | | 5 | 2 | 7 | 1 | 8 | +----+----+----+----+----+ -- Desired result: +----+---------+ | ID | row_max | +----+---------+ | 1 | 4 | | 2 | 11