2

我在 SQL Server 中有一个包含这些字段和值的表。

ID_Employee | ID_Company | Date       | Concept | Hours
--------------------------------------------------------
      1     |      1     | 14/03/2013 |    1    |   8
      1     |      1     | 14/03/2013 |    2    |   0
      1     |      1     | 14/03/2013 |    3    |   3
      1     |      1     | 14/03/2013 |    4    |   1
      1     |      1     | 16/03/2013 |    1    |   5
      1     |      1     | 16/03/2013 |    2    |   2
      1     |      1     | 16/03/2013 |    3    |   0
      1     |      1     | 16/03/2013 |    4    |   0

我需要的是显示值 whereID_Employee=1ID_Company=1in HTML table 按日期对行进行分组,并将他的列中的小时数排序为他们的概念值。

Date       | Concept_1 | Concept_2 | Concept_3 | Concept_4 |
------------------------------------------------------------
14/03/2013 |  8 hours  |  0 hours  |  3 hours  |  1 hour   |
16/03/2013 |  5 hours  |  2 hours  |  0 hours  |  0 hours  |

我不知道如何进行查询或在 php 中使用什么语句(while、for、foreach)<tr>为每个不同的日期创建 1 行(),每个概念和小时包含一个单元格(<td>)。

html 应如下所示:

<tr id="14/03/2013">
    <td class="concept_1">8 hours</td>
    <td class="concept_2">0 hours</td>
    <td class="concept_3">3 hours</td>
    <td class="concept_4">1 hour</td>
</tr>
<tr id="16/03/2013">
    <td class="concept_1">5 hours</td>
    <td class="concept_2">2 hours</td>
    <td class="concept_3">0 hours</td>
    <td class="concept_4">0 hour</td>
</tr>

这可能很容易,但现在我有点困惑,我找不到解决方案。

4

2 回答 2

1

您正在寻找一个PIVOT运算符,它可以让您做想要的事情。您可以使用以下查询:

select  
    pvt.date,
    [1] AS concept_1,
    [2] AS concept_2,
    [3] AS concept_3,
    [4] AS concept_4
from 
    (
        SELECT 
            date, 
            hours, 
            concept
        FROM table1
    ) p
PIVOT
(   
    AVG(hours)
    FOR concept IN
    ([1], [2], [3], [4]) 
) as pvt

也提供SqlFiddle

于 2013-03-14T12:08:38.310 回答
0

检查这个:

declare @tableHTML nvarchar(MAX);
set @tableHTML = CAST((

    select  
        1 AS Tag,
        NULL AS Parent,
        pvt.[date] AS 'tr!1!id',
        ltrim(rtrim(str([1])))+ ' hours' AS 'tr!1!td class="concept_1"!Element',
        ltrim(rtrim(str([2])))+ ' hours' AS 'tr!1!td class="concept_2"!Element',
        ltrim(rtrim(str([3])))+ ' hours' AS 'tr!1!td class="concept_3"!Element',
        ltrim(rtrim(str([4])))+ ' hours' AS 'tr!1!td class="concept_4"!Element'
    from 
        (
            SELECT 
                date, 
                hours, 
                concept
            FROM table1
        ) p
    PIVOT
    (   
        AVG(hours)
        FOR concept IN
        ([1], [2], [3], [4]) 
    ) as pvt
    FOR XML EXPLICIT) AS NVARCHAR(MAX));

PRINT REPLACE(REPLACE(REPLACE(REPLACE(@tableHTML, '</td class="concept_1">', '</td>'), '</td class="concept_2">', '</td>'), '</td class="concept_3">', '</td>'), '</td class="concept_4">', '</td>');

输出:

<tr id="2013-03-14">
    <td class="concept_1">8 hours</td>
    <td class="concept_2">0 hours</td>
    <td class="concept_3">3 hours</td>
    <td class="concept_4">1 hours</td>
</tr>
<tr id="2013-03-16">
    <td class="concept_1">5 hours</td>
    <td class="concept_2">2 hours</td>
    <td class="concept_3">0 hours</td>
    <td class="concept_4">0 hours</td>
</tr>

我使用了@Sergio提出的支点- 非常感谢 (+1)

于 2013-03-14T12:24:51.823 回答