pivot

How to get max and min of columns per row for pivoted table?

点点圈 提交于 2020-01-04 01:57:14
问题 Big thanks to @JohnCappelletti as he's shown how to pivot a table. This is a sample data: DECLARE @OperatorPrice TABLE (ID int NOT NULL, OperatorId INT NULL, Price NUMERIC(18,3) NULL, FName VARCHAR(50) NULL) INSERT INTO @OperatorPrice ( ID, OperatorId, Price, FName ) VALUES (226, 996, 22954,'Operator1') , (266, 1016, 79011.2, 'Operator3') , (112, 1029, 14869, 'Operator4') , (112, 996, 22954, 'Operator1') , (93, 1031, 10568.96, 'Operator5') DECLARE @TR TABLE ( ID varchar(25) NULL , MinPrice

Convert tables with “id,attribute,value” columns to “id,attribute1,attribute2,…”

好久不见. 提交于 2020-01-03 17:13:31
问题 I've got a table in database that looks like this: CustomerId Attribute Value ================================ 30 Name John 30 Surname Smith 30 Phone 555123456 to make some use of the data I need to make it look sth. like this: CustomerId Name Surname Phone ====================================== 30 John Smith 555123456 I know I can create stored procedure or table-valued function that will do it for me, but I'm wondering what would be the best choice here? Maybe I can do it with some clever

Convert tables with “id,attribute,value” columns to “id,attribute1,attribute2,…”

孤人 提交于 2020-01-03 17:13:27
问题 I've got a table in database that looks like this: CustomerId Attribute Value ================================ 30 Name John 30 Surname Smith 30 Phone 555123456 to make some use of the data I need to make it look sth. like this: CustomerId Name Surname Phone ====================================== 30 John Smith 555123456 I know I can create stored procedure or table-valued function that will do it for me, but I'm wondering what would be the best choice here? Maybe I can do it with some clever

Convert tables with “id,attribute,value” columns to “id,attribute1,attribute2,…”

不打扰是莪最后的温柔 提交于 2020-01-03 17:13:13
问题 I've got a table in database that looks like this: CustomerId Attribute Value ================================ 30 Name John 30 Surname Smith 30 Phone 555123456 to make some use of the data I need to make it look sth. like this: CustomerId Name Surname Phone ====================================== 30 John Smith 555123456 I know I can create stored procedure or table-valued function that will do it for me, but I'm wondering what would be the best choice here? Maybe I can do it with some clever

Using @DECLARE in MYSQL 5.1

怎甘沉沦 提交于 2020-01-03 05:46:12
问题 just playing around with some sql statements on MYSQL Server 5.1 I already asked a question how to make a specific count on two tables (see here) and I also found an answer how to transpose my result (see here) but I cant use it on my local MYSQL Server 5.1. This is table one: test id|name|test_type ------------- 1|FirstUnit|1 2|FirstWeb|2 3|SecondUnit|1 The second table: test_type id|type -------- 1|UnitTest 2|WebTest The following result will be written into "yourtable" (a temporary table)

SQL Server dynamic columns creation

感情迁移 提交于 2020-01-03 05:25:15
问题 I have a table with column and values as below How do I fetch the result as in the second tabular column with the DYNAMIC column names as -- first with "prgmg_product_id" and the rest of the column as "source ID 1","source ID 2", "source ID 3" 回答1: To achieve this using Dynamic SQL, the below will help: CREATE TABLE #Prgmg ( prgmg_product_id INT ,source_id_other INT ); INSERT #Prgmg ( prgmg_product_id ,source_id_other ) VALUES (3310,11478) ,(3337,10833) ,(3354,11466) ,(4039,4846) ,(4039,65454

Laravel 5.5 pivot join to get pivot values with main MySQL result

有些话、适合烂在心里 提交于 2020-01-03 05:13:35
问题 I'm trying to join a pivot table on a MySQL query. Basically I'm selecting the users, where one user has multiple sub categories. So in essence with my "sub_categories relationship, one user has many sub categories. But because I am using RAW select, I cannot select / use the relationships. Instead I have to use a join. This is my sub_categories table Column Type Comment id int(10) unsigned Auto Increment main_category_id int(10) unsigned [0] category_name varchar(100) created_at timestamp

SQL Server 2005 percentage in parenthesis in pivot table

依然范特西╮ 提交于 2020-01-02 08:56:09
问题 This is a follow up question from here. I have some output and was able to add a total column but now I need to use the column to add percentage like this: | LESSONID RESPONSE COUNT-> | 0 | 1 | 2 | 3 | 4 | N | --------------------------------------------------------------------------------------- |lesson1 | 1 (20%) | 1 (20%) | 1 (20%) | 1 (20%) | 1 (20%) | 5 | |lesson2 | 1 (20%) | 1 (20%) | 1 (20%) | 2 (40%) | 0 | 5 | |lesson3 | 1 (20%) | 1 (20%) | 0 | 3 (60%) | 0 | 5 | |lesson4 | 0 | 1 (20%)

SQL Server 2005 percentage in parenthesis in pivot table

独自空忆成欢 提交于 2020-01-02 08:56:08
问题 This is a follow up question from here. I have some output and was able to add a total column but now I need to use the column to add percentage like this: | LESSONID RESPONSE COUNT-> | 0 | 1 | 2 | 3 | 4 | N | --------------------------------------------------------------------------------------- |lesson1 | 1 (20%) | 1 (20%) | 1 (20%) | 1 (20%) | 1 (20%) | 5 | |lesson2 | 1 (20%) | 1 (20%) | 1 (20%) | 2 (40%) | 0 | 5 | |lesson3 | 1 (20%) | 1 (20%) | 0 | 3 (60%) | 0 | 5 | |lesson4 | 0 | 1 (20%)

python pandas pivot: How to do a proper tidyr-like spread?

旧巷老猫 提交于 2020-01-02 07:50:30
问题 I am missing spontaneous and easy conversion from long to wide and vice versa in Python. Imagine, I have a large tidy dataframe with a lot of property-columns and a single column that contains all the actual values like PropA ... PropZ Value green Saturn 400 green Venus 3 red Venus 2 . . The data itself is very nicely handled by keeping it tidy. But sometimes I have to perform some actions across certain properties (for instance it might be interesting to compare beeing red vs beeing green