MySQL可以将列转换为行,动态添加行所需的列数.我认为我的问题可能与数据透视表有关,但我不确定,除了通过给出以下示例,我不知道如何构建这个问题.
给出两个表A和B,看起来像
表A.
+--+-----+----+
|id|order|data|
+--+-----+----+
|1 |1 |P |
+--+-----+----+
|2 |2 |Q |
+--+-----+----+
|2 |1 |R |
+--+-----+----+
|1 |2 |S |
+--+-----+----+
Run Code Online (Sandbox Code Playgroud)
我想编写一个如下所示的查询:
结果表
+--+-----+-----+
|id|data1|data2|
+--+-----+-----+
|1 |P |S |
+--+-----+-----+
|2 |R |Q |
+--+-----+-----+
Run Code Online (Sandbox Code Playgroud)
基本上我想将表B中的每一行转换为结果表中的一列.如果在表B中为id = 1添加了一个新条目,那么我希望结果表自动扩展一列以容纳这个额外的数据点.
我有下表但不确定是否可以转动这个并保留所有标签.
RATIO RESULT SCORE GRADE
Current Ratio 1.294 60 Good
Gearing Ratio 0.3384 70 Good
Performance Ratio 0.0427 50 Satisfactory
TOTAL NULL 180 Good
Run Code Online (Sandbox Code Playgroud)
我承认在使用枢轴方面不是很好,所以经过几次尝试导致这个输出:
SELECT 'RESULT' AS 'Ratio'
,[Current Ratio] AS 'Current Ratio'
,[Gearing Ratio] AS 'Gearing Ratio'
,[Performance Ratio] AS 'Performance Ratio'
,[TOTAL] AS 'TOTAL'
FROM
(
SELECT RATIO, RESULT
FROM GRAND_TOTALS
) AS SREC
PIVOT
(
MAX(RESULT)
FOR RATIO IN ([Current Ratio],[Gearing Ratio], [Performance Ratio], [TOTAL])
) AS PVT
Run Code Online (Sandbox Code Playgroud)
这给出了结果:
Ratio Current Ratio Gearing Ratio Performance Ratio …Run Code Online (Sandbox Code Playgroud) 我有以下数据帧.
df.head(30)
struct_id resNum score_type_name score_value
0 4294967297 1 omega 0.064840
1 4294967297 1 fa_dun 2.185618
2 4294967297 1 fa_dun_dev 0.000027
3 4294967297 1 fa_dun_semi 2.185591
4 4294967297 1 ref -1.191180
5 4294967297 2 rama -0.795161
6 4294967297 2 omega 0.222345
7 4294967297 2 fa_dun 1.378923
8 4294967297 2 fa_dun_dev 0.028560
9 4294967297 2 fa_dun_rot 1.350362
10 4294967297 2 p_aa_pp -0.442467
11 4294967297 2 ref 0.249477
12 4294967297 3 rama 0.267443
13 4294967297 3 omega 0.005106
14 4294967297 3 fa_dun …Run Code Online (Sandbox Code Playgroud) 在SQL Server 2005中使用什么更有效:PIVOT还是MULTIPLE JOIN?
例如,我使用两个连接获得此查询:
SELECT p.name, pc1.code as code1, pc2.code as code2
FROM product p
INNER JOIN product_code pc1
ON p.product_id=pc1.product_id AND pc1.type=1
INNER JOIN product_code pc2
ON p.product_id=pc2.product_id AND pc2.type=2
Run Code Online (Sandbox Code Playgroud)
我可以使用PIVOT做同样的事情:
SELECT name, [1] as code1, [2] as code2
FROM (
SELECT p.name, pc.type, pc.code
FROM product p
INNER JOIN product_code pc
ON p.product_id=pc.product_id
WHERE pc.type IN (1,2)) prods1
PIVOT(
MAX(code) FOR type IN ([1], [2])) prods2
Run Code Online (Sandbox Code Playgroud)
哪一个会更有效率?
我有一张顾客表
Customer ID Name
1 John
2 Lewis
3 Mary
Run Code Online (Sandbox Code Playgroud)
我有另一张表CustomerRewards
TypeID Description
1 Bronze
2 Silver
3 Gold
4 Platinum
5 AnotherOne
Run Code Online (Sandbox Code Playgroud)
决赛桌
RewardID TypeID CustomerID
1 1 1
2 1 1
3 2 1
4 2 2
Run Code Online (Sandbox Code Playgroud)
customerTypes表是动态的,可以添加和删除许多这些类型.基本上我想要的是动态生成的列和每个列中的计数,类似于
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
Run Code Online (Sandbox Code Playgroud)
问题就像我说的那样,类型是动态的,客户是动态的,所以我需要根据系统中的类型使列成为动态的
我已经在DataGridView中标记了c#,因为我需要它
提前致谢
我似乎反对这个问题很多,我的数据格式如下:
+----+----------------------+
| id | colors |
+----+----------------------+
| 1 | Red,Green,Blue |
| 2 | Orangered,Periwinkle |
+----+----------------------+
Run Code Online (Sandbox Code Playgroud)
但我希望它的格式如下:
+----+------------+
| id | colors |
+----+------------+
| 1 | Red |
| 1 | Green |
| 1 | Blue |
| 2 | Orangered |
| 2 | Periwinkle |
+----+------------+
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?什么是这种操作甚至叫做?
我有一个看起来像这样的表:
id feh bar
1 10 A
2 20 A
3 3 B
4 4 B
5 5 C
6 6 D
7 7 D
8 8 D
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样:
bar val1 val2 val3
A 10 20
B 3 4
C 5
D 6 7 8
Run Code Online (Sandbox Code Playgroud)
我有这个查询,它执行此操作:
SELECT bar,
MAX(CASE WHEN abc."row" = 1 THEN feh ELSE NULL END) AS "val1",
MAX(CASE WHEN abc."row" = 2 THEN feh ELSE NULL END) AS "val2",
MAX(CASE WHEN abc."row" = 3 THEN feh ELSE …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的表的某些列转换为行.我正在使用Python和Spark 1.5.0.这是我的初始表:
+-----+-----+-----+-------+
| A |col_1|col_2|col_...|
+-----+-------------------+
| 1 | 0.0| 0.6| ... |
| 2 | 0.6| 0.7| ... |
| 3 | 0.5| 0.9| ... |
| ...| ...| ...| ... |
Run Code Online (Sandbox Code Playgroud)
我想有这样的事情:
+-----+--------+-----------+
| A | col_id | col_value |
+-----+--------+-----------+
| 1 | col_1| 0.0|
| 1 | col_2| 0.6|
| ...| ...| ...|
| 2 | col_1| 0.6|
| 2 | col_2| 0.7|
| ...| ...| ...|
| 3 | col_1| 0.5|
| 3 | …Run Code Online (Sandbox Code Playgroud) 我需要设计存储文件所有元数据的表(即文件名,作者,标题,创建日期)和自定义元数据(用户已添加到文件中,例如CustUseBy,CustSendBy).无法预先设置自定义元数据字段的数量.实际上,确定在文件中添加了什么和多少自定义标记的唯一方法是检查表中存在的内容.
为了存储它,我创建了一个基表(具有文件的所有公共元数据),一个Attributes表(包含可以在文件上设置的附加,可选属性)和一个FileAttributes表(为文件的属性赋值).
CREAT TABLE FileBase (
id VARCHAR(32) PRIMARY KEY,
name VARCHAR(255) UNIQUE NOT NULL,
title VARCHAR(255),
author VARCHAR(255),
created DATETIME NOT NULL,
) Engine=InnoDB;
CREATE TABLE Attributes (
id VARCHAR(32) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL
) Engine=InnoDB;
CREATE TABLE FileAttributes (
sNo INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
fileId VARCHAR(32) NOT NULL,
attributeId VARCHAR(32) NOT NULL,
attributeValue VARCHAR(255) NOT NULL,
FOREIGN KEY fileId REFERENCES FileBase (id),
FOREIGN KEY attributeId REFERENCES Attributes …Run Code Online (Sandbox Code Playgroud) 我有两个数据表
表格1
---------------------------------------------------
| SALEID | SOLDBY | SALEPRICE | MARGIN | DATE |
| 1 | 'aa' | 10,000 | 10 | 2013-1-1 |
| 2 | 'bb' | 25,000 | 5 | 2013-5-1 |
Run Code Online (Sandbox Code Playgroud)
表2
---------------------------------------------------
| SALEITEMID | SALEID | SALEPRICE | CATEGORY |
| 1 | 1 | 6,000 | BOOKS |
| 2 | 1 | 4,000 | PRINTING |
| 3 | 2 | 5,000 | BOOKS |
| 4 | 2 | 12,000 …Run Code Online (Sandbox Code Playgroud) pivot ×10
sql ×5
mysql ×3
sql-server ×3
join ×2
python ×2
t-sql ×2
apache-spark ×1
crosstab ×1
csv ×1
format ×1
group-by ×1
group-concat ×1
pandas ×1
performance ×1
postgresql ×1
transpose ×1