dynamic-pivot

How to use PIVOT in SQL Server 2005 Stored Procedure Joining Two Views

十年热恋 提交于 2019-12-29 09:07:07
问题 Good Morning, I have 2 views: ICCUDays which contains one record per account with fields ACCOUNT and ICCUDays, ICCUEnctrSelectedRevCatsDirCost which contains multiple records per account with fields ACCOUNT, UBCATEGORY, and DirectCost. My Goal: To create a stored procedure that outputs one record per ACCOUNT with ICCUDays and DirectCost by UBCATEGORY. This will be a crosstab or pivot and has to allow for the possibility of nulls in one or more direct cost ubcategory bucket. Finally, this

How to pivot dynamically with date as column

本小妞迷上赌 提交于 2019-12-28 03:08:24
问题 I have a table with product id's and names, and another table with the stock of these products on certain dates. Such as Item1 had 6 stock on 1-1-2014 and 8 stock on 2-1-2014 . I'm trying to show these in a stored procedure so that it looks like a calendar, showing all the dates in a month and the stock available in the cells. What is the best way to show this? For example: Name | 1-1-2014 | 2-1-2014 | 3-1-2014 | 4-1-2014 Item1 | 6 | 8 | | 6 Item2 | | 2 | 1 | Original tables - Names ID | Name

c# adding a variable in a SQL dynamic pivot string

北城余情 提交于 2019-12-25 02:08:08
问题 I am trying to show the results of a dynamic pivot in a c# datagridview. So far I have got the following code but I am stumped as how to incorporate the @Date variable in the @query string. What am I missing here? The code works fine with hard coded dates and as it is returns Additional information: Incorrect syntax near '@Date'. Please help, Thanks, A da2.SelectCommand = new SqlCommand(@"DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); SET @cols = STUFF((SELECT distinct ',' +

Dynamically create columns sql

走远了吗. 提交于 2019-12-17 03:26:13
问题 I have a table of Customers Customer ID Name 1 John 2 Lewis 3 Mary I have another table CustomerRewards TypeID Description 1 Bronze 2 Silver 3 Gold 4 Platinum 5 AnotherOne And the final table RewardID TypeID CustomerID 1 1 1 2 1 1 3 2 1 4 2 2 The customerTypes table is dynamic, many of these types can be added and removed. Basically all I want is the columns to be generated dynamically and a count in each, something like CustomerName Bronze Silver Gold Platinum AnotherOne total John 2 1 0 0 0

Dynamically create columns sql

匆匆过客 提交于 2019-12-17 03:25:24
问题 I have a table of Customers Customer ID Name 1 John 2 Lewis 3 Mary I have another table CustomerRewards TypeID Description 1 Bronze 2 Silver 3 Gold 4 Platinum 5 AnotherOne And the final table RewardID TypeID CustomerID 1 1 1 2 1 1 3 2 1 4 2 2 The customerTypes table is dynamic, many of these types can be added and removed. Basically all I want is the columns to be generated dynamically and a count in each, something like CustomerName Bronze Silver Gold Platinum AnotherOne total John 2 1 0 0 0

Help needed with Dynamic Pivoting in SQL2005

妖精的绣舞 提交于 2019-12-11 06:34:45
问题 I have a table of name value pairs where I am storing tags: TAGID | NAME | VALUE I have a table of 'Things' this tags apply to THINGID | TAGID I need a query to generate a resultSet were the columns/fields are all possible TAG-NAMES (NAME field in the TAG table)for a given THINGID and the values are the correspondent tag values. THINGID | TAGNAME1 | TAGNAME2 | ... |etc. I can find examples with fixed columns but nothing like this. 回答1: Something similar with slightly different schema (thing

SQL Server 2008 R2 - Dynamic Pivot/Unpivot with (moving) Dates

无人久伴 提交于 2019-12-11 03:05:38
问题 I have posted a pivot question before and it was answered. However, this particular case requires dates as the headers and the dates will change (and be unknown) each time the pivot is generated. I believe I need dynamic pivot/unpivot based on a few samples but I cannot figure the syntax out. Here is the table: CREATE TABLE [dbo].[PhaseFlowChart]( [pfckey] [int] NULL, [hourlykey] [bigint] NULL, [daykey] [bigint] NULL, [weekkey] [int] NULL, [monthkey] [int] NULL, [bbkey] [int] NULL, [Day]

Arithmetic operators against dynamic column in SQL Server 2008

强颜欢笑 提交于 2019-12-11 00:49:18
问题 I have query to create table and result below: -- tblPart PartCode[NVARCHAR(50)],UnitPrice[Decimal(18,2)] SELECT * INTO #tblPart FROM( SELECT 'A' PartCode, '10' UnitPrice UNION All SELECT 'B','11' UNION All SELECT 'C','38' UNION All SELECT 'D','20' UNION All SELECT 'E','12')part; -- tblPartCondition ConditionCode[NVARCHAR(50)],PercentagePrice[Decimal(18,2)] SELECT * INTO #tblPriceCondition FROM( SELECT 'Weekly' ConditionCode, '3' PercentagePrice UNION All SELECT 'Urgent','-5' UNION All SELECT

Row and column total in dynamic pivot

送分小仙女□ 提交于 2019-11-30 07:30:12
In SQL Server 2008, I have a table (tblStock) with 3 columns: PartCode (NVARCHAR (50)) StockQty (INT) Location (NVARCHAR(50)) some example data below: PartCode StockQty Location ......... ......... ......... A 10 WHs-A B 22 WHs-A A 1 WHs-B C 20 WHs-A D 39 WHs-F E 3 WHs-D F 7 WHs-A A 9 WHs-C D 2 WHs-A F 54 WHs-E How to create procedure to get the result as below? PartCode WHs-A WHs-B WHs-C WHs-D WHs-E WHs-F Total ........ ..... ..... ..... ...... ..... ..... ..... A 10 1 9 0 0 0 20 B 22 0 0 0 0 0 22 C 20 0 0 0 0 0 20 D 2 0 0 0 0 39 41 E 0 0 0 3 0 0 3 F 7 0 0 0 54 0 61 Total 61 1 9 3 54 39 167

How to use PIVOT in SQL Server 2005 Stored Procedure Joining Two Views

喜你入骨 提交于 2019-11-29 16:16:02
Good Morning, I have 2 views: ICCUDays which contains one record per account with fields ACCOUNT and ICCUDays, ICCUEnctrSelectedRevCatsDirCost which contains multiple records per account with fields ACCOUNT, UBCATEGORY, and DirectCost. My Goal: To create a stored procedure that outputs one record per ACCOUNT with ICCUDays and DirectCost by UBCATEGORY. This will be a crosstab or pivot and has to allow for the possibility of nulls in one or more direct cost ubcategory bucket. Finally, this crosstab or pivot needs to be sent to a new table EnctrUBCatPivot. Questions: What is the correct PIVOT