Tag: 数据透视表

MySQL – 根据同一个表中的行求和列值(s)

我试图根据同一个表中的ProductID在新列中获得“现金”,“支票”和“信用卡”总计。 表 – 付款 +———–+————+—————+——–+ | ProductID | SaleDate | PaymentMethod | Amount | +———–+————+—————+——–+ | 3 | 2012-02-10 | Cash | 10 | | 3 | 2012-02-10 | Cash | 10 | | 3 | 2012-02-10 | Check | 15 | | 3 | 2012-02-10 | Credit Card | 25 | | 4 | […]

MySQL数据透视表列数据作为行

我正在努力寻找解决这个MySQL问题的方法。 我似乎无法理解如何去做。 我有以下表格。 Question table +—-+————-+ | id | question | +—-+————-+ | 1 | Is it this? | | 2 | Or this? | | 3 | Or that? | +—-+————-+ Results Table +—-+———+——–+ | id | user_id | job_id | +—-+———+——–+ | 1 | 1 | 1 | | 2 | 1 | […]

转置没有聚合的行和列

我有以下数据集 Account Contact 1 324324324 1 674323234 2 833343432 2 433243443 3 787655455 4 754327545 4 455435435 5 543544355 5 432455553 5 432433242 5 432432432 我想输出如下: Account Contact1 Contact2 Contact3 Contact4 1 324324324 674323234 2 833343432 433243443 3 787655455 4 754327545 455435435 5 543544355 432455553 432433242 432432432 问题是我有一个未定额的帐户和未定的数量的联系人

SQL Server的枢轴与多重连接

在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 我可以使用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 […]

在MS Access中旋转数据

我有一个查询,我已经创build,以拉取他们已经花了一个月的学生证和餐食。 我想统计一个学生在一个月中每个项目(早餐,午餐,小吃)的数量。 看起来有太多的数据可以在数据透视表报告中处理,所以我希望有一个SQL查询可以运行。 以下是我创build的当前查询: SELECT April2013.SID, MenuItems.MealType AS Apr2013Meal FROM April2013 LEFT JOIN MenuItems ON MenuItems.Item=April2013.Item; 电stream输出: +—–+———–+ | SID | Meal | +—–+———–+ | 001 | Lunch | | 002 | Lunch | | 003 | Breakfast | | 004 | Snack | | 005 | Lunch | | 006 | Lunch | | […]

MySQL的枢轴/交叉表查询

问题1:我有一个表格,其结构和数据如下: app_id transaction_id mobile_no node_id customer_attribute entered_value 100 111 9999999999 1 Q1 2 100 111 9999999999 2 Q2 1 100 111 9999999999 3 Q3 4 100 111 9999999999 4 Q4 3 100 111 9999999999 5 Q5 2 100 222 8888888888 4 Q4 1 100 222 8888888888 3 Q3 2 100 222 8888888888 2 Q2 1 […]

在SQL 2005中的PIVOT

我需要旋转一列(数字列)。 例如需要这个数据: a 1 a 2 b 3 b 4 c 5 d 6 d 7 d 8 d 9 e 10 e 11 e 12 e 13 e 14 看起来像这样 a 1 2 b 3 4 c 5 d 6 7 8 9 e 10 11 12 13 14 任何帮助将不胜感激…

包含多个列聚合的SQL Server数据透视表

我有一张桌子: create table mytransactions(country varchar(30), totalcount int, numericmonth int, chardate char(20), totalamount money) 表中有这些logging: insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 36, 7, 'Jul-12', 699.96) Go insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 44, 8, 'Aug-12', 1368.71) Go insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 52, 9, 'Sep-12', 1161.33) Go insert into […]

T-SQLdynamic数据透视表

好吧,我有一张表,看起来像这样 ItemID | ColumnName | Value 1 | name | Peter 1 | phone | 12345678 1 | email | peter@host.com 2 | name | John 2 | phone | 87654321 2 | email | john@host.com 3 | name | Sarah 3 | phone | 55667788 3 | email | sarah@host.com 现在我需要把它变成这样: ItemID | name […]

Mysql查询将行dynamic转换为列

MySQL可以将列转换为行,dynamic地添加行所需的列数。 我想我的问题可能与数据透视表有关,但我不确定,除了给出以下示例之外,我不知道如何设置此问题。 给定两个表A和B,看起来像 表A +–+—–+—-+ |id|order|data| +–+—–+—-+ |1 |1 |P | +–+—–+—-+ |2 |2 |Q | +–+—–+—-+ |2 |1 |R | +–+—–+—-+ |1 |2 |S | +–+—–+—-+ 我喜欢写一个如下所示的查询: 结果表 +–+—–+—–+ |id|data1|data2| +–+—–+—–+ |1 |P |S | +–+—–+—–+ |2 |R |Q | +–+—–+—–+ 基本上我想把表B中的每一行变成结果表中的一列。 如果在表B中为id = 1添加了新条目,那么我希望结果表自动扩展一列以容纳这个额外的数据点。