选择不同的列值并添加另一列的值的相关值

时间:2014-07-15 17:02:18

标签: sql-server distinct

我正在使用时间卡系统,用户可以在一个单位或“跑步”中输入时间。单位。

我已经创建了时间输入部分,用户可以根据'运行时间输入时间。并且它将为指定为“运行”的一部分的每个单元创建单独的行。它还划分了他们输入的时间(即,run15有8个小时的时间,因此每个单位将获得4个小时) 请注意,如果针对单个单元输入时间,则RunNum保留为空白。

UnitNum    RunNum       Hours               Date
---        ----         ------            --------
1          12            5                08-01-2013
2          12            5                08-01-2013
3          12            5                08-01-2013
4          -            10                08-02-2013
5          -             8                08-02-2013
6          -             4                08-03-2013
9          15            4                08-03-2013
9          15            4                08-03-2013
1          12            1.5              08-04-2013
2          12            1.5              08-04-2013
3          12            1.5              08-04-2013

现在我正在进入系统的编辑部分;事情有点复杂。 由于我不希望用户必须在运行中编辑单个单元,我希望它再次显示为单次运行。

我需要结合不同的运行;添加相应的小时数,日期相同;并隐藏unitnum:

UnitNum    RunNum       Hours               Date
---        ----         ------            --------
-          12           15                08-01-2013
4          -            10                08-02-2013
5          -             8                08-02-2013
6          -             4                08-03-2013
-          15            8                08-03-2013
-          12            4.5              08-04-2013

到目前为止,我的代码抓住了所有内容,看起来像这样:

<?PHP
$EditPickSelect = SQLSRV_QUERY($conn1,"SELECT runnum, unitnum, date, hours FROM dbo.TimecardHours ORDER BY date");
ECHO "<br>";
ECHO "<table border='1'>";

ECHO "<tr>";

ECHO "<td>";
    ECHO "Run";
ECHO "</td>";

ECHO "<td>";
    ECHO "Unit";
ECHO "</td>";

ECHO "<td>";
    ECHO "Hours";
ECHO "</td>";

ECHO "<td>";
    ECHO "Date";
ECHO "</td>";

ECHO "</tr>";


WHILE ($EditPickFetch=SQLSRV_FETCH_ARRAY($EditPickSelect, SQLSRV_FETCH_ASSOC))
{

ECHO "<tr>";

    ECHO "<td>";
    ECHO "<center>";
        if ($EditPickFetch['runnum'] == '') {
        ECHO "-";
        } else {
        ECHO $EditPickFetch['runnum'];
        }
    ECHO "</center>";
    ECHO "</td>";

    ECHO "<td>";
    ECHO "<center>";
        if ($EditPickFetch['unitnum'] == '') {
        ECHO "-";
        } else {
        ECHO $EditPickFetch['unitnum'];
        }
    ECHO "</center>";
    ECHO "</td>";

    ECHO "<td>";
    ECHO "<center>";
        ECHO $EditPickFetch['hours'];
    ECHO "</center>";
    ECHO "</td>";

    ECHO "<td>";
    ECHO "<center>";
        ECHO $EditPickFetch['date'];
    ECHO "</center>";
    ECHO "</td>";

ECHO "</tr>";

}

ECHO "</table>";
?>

2 个答案:

答案 0 :(得分:0)

听起来你只想做一个聚合:

Select runNum, date, sum(hours) as hours 
from dbo.TimeCardHours 
Group by runNum, date 
order by Date

答案 1 :(得分:0)

这应该适合你。

SELECT NULL AS UnitNum, [Runnum], SUM([Hours]), [Date]
FROM [Dbo].Timecardhours
WHERE Runnum IS NOT NULL
GROUP BY Date, 
  Runnum
UNION ALL
SELECT UnitNum, [Runnum], SUM([Hours]), [Date]
 FROM [Dbo].Timecardhours
WHERE Runnum IS NULL
GROUP BY Date, 
  Runnum, 
  Unitnum;