dynamic-pivot

Row and column total in dynamic pivot

回眸只為那壹抹淺笑 提交于 2019-11-29 10:34:27
问题 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

How to pivot dynamically with date as column

…衆ロ難τιáo~ 提交于 2019-11-27 09:26:57
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 1 | Item1 2 | Item2 Original tables - Stockdates ID | NameID | Stock | Date 1 | 1 | 8 | 2-1-2014 2 | 2

Dynamically create columns sql

狂风中的少年 提交于 2019-11-26 15:52:49
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 3 Lewis 0 1 0 0 0 1 Grand TOTAL 2 2 0 0 0 4 The problem like I said it that the types are dynamic and

MySQL dynamic-pivot

天涯浪子 提交于 2019-11-26 14:45:27
I have a table of product parts like this: Parts part_id part_type product_id -------------------------------------- 1 A 1 2 B 1 3 A 2 4 B 2 5 A 3 6 B 3 and I want a query that will return a table like this: product_id part_A_id part_B_id ---------------------------------------- 1 1 2 2 3 4 3 5 6 In its actual implementation there will be millions of product parts Unfortunately, MySQL does not have a PIVOT function but you can model it using an aggregate function and a CASE statement. For a dynamic version, you will need to use prepared statements: SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT

SQL Dynamic Pivot - how to order columns

白昼怎懂夜的黑 提交于 2019-11-26 08:33:31
问题 I\'m working on a dynamic pivot query on a table that contains: OID - OrderID Size - size of the product BucketNum - the order that the sizes should go quantity - how many ordered The size column contains different sizes depending upon the OID. So, using the code found here, I put this together: DECLARE @listCol VARCHAR(2000) DECLARE @query VARCHAR(4000) SELECT @listCol = STUFF(( SELECT distinct \'], [\' + [size] FROM #t FOR XML PATH(\'\') ), 1, 2, \'\') + \']\' SET @query = \'SELECT * FROM

SQL Server - Dynamic PIVOT Table - SQL Injection

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 07:39:17
问题 Sorry for the long question but this contains all the SQL I\'ve used to test the scenario to hopefully make it clear as to what I\'m doing. I\'m build up some dynamic SQL to produce a PIVOT table in SQL Server 2005. Below is code to do this. With various selects showing the raw data the values using GROUP BY and the values in a PIVOT as I want them. BEGIN TRAN --Create the table CREATE TABLE #PivotTest ( ColumnA nvarchar(500), ColumnB nvarchar(500), ColumnC int ) --Populate the data INSERT

MySQL dynamic-pivot

感情迁移 提交于 2019-11-26 05:56:44
问题 I have a table of product parts like this: Parts part_id part_type product_id -------------------------------------- 1 A 1 2 B 1 3 A 2 4 B 2 5 A 3 6 B 3 and I want a query that will return a table like this: product_id part_A_id part_B_id ---------------------------------------- 1 1 2 2 3 4 3 5 6 In its actual implementation there will be millions of product parts 回答1: Unfortunately, MySQL does not have a PIVOT function but you can model it using an aggregate function and a CASE statement.