简体   繁体   English

如何在EXCEL或SQL中转置动态矩阵数据集

[英]How to transpose dynamic matrix dataset in EXCEL or SQL

I have the following issue that I can't find the best solution for. 我遇到以下找不到最佳解决方案的问题。

I need to align hours per date per ID from a sheet with these parameters. 我需要使用这些参数来调整工作表中每个ID的每个日期的小时数。 I tried transposing in excel but I just came with a summier result that wouldn't give the rows per ID and date. 我尝试在excel中进行转置,但是我得到了一个更好的结果,该结果不会给出每个ID和日期的行数。

HOURS WORKSHEET 小时工作表

YEAR = 2015 年份= 2015

ID | MON | TUES | WED | THU | FRI | SAT | SUN | WEEKNR

15 |  6  |  8   |  9  |  -  |  -  |  -  |  -  |  14
16 |  -  |  -   |  2  |  -  |  3  |  -  |  -  |  14
17 |  -  |  3   |  5  |  -  |  -  |  5  |  -  |  14
18 |  9  |  -   |  -  |  3  |  -  |  -  |  -  |  14

I'd like to have the ID transposed on the date with the values(hours) like this 我想在日期上将ID换成这样的值(小时)

ID | DATE      | HOURS

15 | 30-3-2015 |  6
15 | 31-3-2015 |  8
15 |  1-4-2015 |  9
16 |  1-4-2015 |  2
16 |  3-4-2015 |  3
17 | 31-3-2015 |  3
17 |  1-4-2015 |  5
17 |  4-4-2015 |  5
18 | 30-3-2015 |  9
18 |  2-4-2015 |  3

Any suggestion/solution is much appreciated. 任何建议/解决方案均不胜感激。 SQL or Excel formula(VBA) SQL或Excel公式(VBA)

This would be easy in sql-server (using a table function). 这在sql-server中很容易(使用表函数)。 In MySQL however, simplest way i can think of is this: 但是在MySQL中,我能想到的最简单的方法是:

select id, date (use date_ADD() to deduce this), mon as hours from table
union all
select id, date (use date_ADD() to deduce this), tues as hours from table
union all
select id, date (use date_ADD() to deduce this), wed as hours from table
union all
select id, date (use date_ADD() to deduce this), thu as hours from table
union all
select id, date (use date_ADD() to deduce this), fri as hours from table
union all
select id, date (use date_ADD() to deduce this), sat as hours from table
union all
select id, date (use date_ADD() to deduce this), sun as hours from table

And then sort as needed. 然后根据需要进行排序。

EDIT: if you need help with the date, tell us. 编辑:如果您需要有关日期的帮助,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM