计算车辆行程

时间:2016-03-24 05:33:08

标签: sql sql-server sql-server-2008 sql-server-2012

我想计算车辆行程但面临问题。 No Groupby没有工作。

select z.ZoneId, o.ObjectId,o.Number,o.Comment,z.Name StartTrip,zs.TimeFirst,zs.Inside,z.ZoneGroupId

from dbo.Object o join 
dbo.GroupObject gobj on o.ObjectId = gobj.ObjectId join 
dbo.[Group] g on gobj.GroupId=g.GroupId join 
dbo.[ZoneState] zs on o.ObjectId=zs.ObjectId join 
dbo.[Zone] z on zs.ZoneId=z.ZoneId
where (z.ZoneGroupId=1 OR z.ZoneGroupId=2) and o.ObjectId=3414 and (convert(date,zs.TimeFirst)>='2016/2/15') and (convert(date,zs.TimeFirst)<='2016/3/24')  

ORDER BY o.Number

当前结果

ZoneId  Objectid Number commants StartTrip TimeFirst          Inside 
 540    3414    VT-0678T VOLVO   Oil Fd  2016-03-17 17:10:31    0   
 543    3414    VT-0678T VOLVO   Khour   2016-03-19 09:38:30    1   
 540    3414    VT-0678T VOLVO   Oil Fd  2016-03-21 07:32:58    0    
 543    3414    VT-0678T VOLVO   Khour   2016-03-22 07:29:56    1  
 540    3414    VT-0678T VOLVO   Oil Fd  2016-03-22 15:22:41    0       
 543    3414    VT-0678T VOLVO   Khour   2016-03-24 07:41:27    1   

必填结果

ObjId Number   StartTrip EndTrip  TimeFirst            TimeLast 
3414  VT-0678T Oil Fd    Khour   2016-03-17 17:10:31 2016-03-19 09:38:30
3414  VT-0678T Oil Fd    Khour   2016-03-21 07:32:58 2016-03-22 07:29:56
3414  VT-0678T Oil Fd    Khour   2016-03-22 15:22:41 2016-03-24 07:41:27

1 个答案:

答案 0 :(得分:1)

  DataTable dtRequiredResult = GetRequiredResultTableDefinition();

    bool flag = true;

            DataRow rowtoAdd = dtRequiredResult.NewRow();

            for (int j = 0; j < dtQueryResult.Rows.Count; j++)
            {
                if (dtQueryResult.Rows[j]["Status"].ToString() == "0")
                {
                    if (flag)
                    {
                        rowtoAdd["Z From"] = dtQueryResult.Rows[j]["Z.Id"].ToString();
                        rowtoAdd["Veh"] = dtQueryResult.Rows[j]["Veh"].ToString();
                        rowtoAdd["Start"] = dtQueryResult.Rows[j]["Zone"].ToString();
                        rowtoAdd["S Date"] = dtQueryResult.Rows[j]["DateTime"].ToString();
                        flag = false;
                    }
                }
                if (dtQueryResult.Rows[j]["Status"].ToString() == "1")
                {
                    if (rowtoAdd["Start"].ToString() == dtQueryResult.Rows[j]["Zone"].ToString())
                    {
                        rowtoAdd["Z To"] = dtQueryResult.Rows[j-1]["Z.Id"].ToString();
                        rowtoAdd["End"] = dtQueryResult.Rows[j - 1]["Zone"].ToString();
                        rowtoAdd["E Date"] = dtQueryResult.Rows[j]["DateTime"].ToString();
                        dtRequiredResult.Rows.InsertAt(rowtoAdd, dtRequiredResult.Rows.Count);
                        rowtoAdd = dtRequiredResult.NewRow();
                        flag = true;
                    }
                }
            }

            rowtoAdd["Z To"] = dtQueryResult.Rows[dtQueryResult.Rows.Count-1]["Z.Id"].ToString();
            rowtoAdd["End"] = dtQueryResult.Rows[dtQueryResult.Rows.Count-1]["Zone"].ToString();
            dtRequiredResult.Rows.InsertAt(rowtoAdd, dtRequiredResult.Rows.Count);



 private DataTable GetRequiredResultTableDefinition()
    {
        DataTable table = new DataTable("Required");

        DataColumn col = new DataColumn("Z From", typeof(string));
        col.AllowDBNull = true;
        table.Columns.Add(col);

        col = new DataColumn("Z To", typeof(string));
        col.AllowDBNull = true;
        table.Columns.Add(col);

        col = new DataColumn("Veh", typeof(string));
        col.AllowDBNull = true;
        table.Columns.Add(col);

        col = new DataColumn("Start", typeof(string));
        col.AllowDBNull = true;
        table.Columns.Add(col);

        col = new DataColumn("End", typeof(string));
        col.AllowDBNull = true;
        table.Columns.Add(col);

        col = new DataColumn("S Date", typeof(string));
        col.AllowDBNull = true;
        table.Columns.Add(col);

        col = new DataColumn("E Date", typeof(string));
        col.AllowDBNull = true;
        table.Columns.Add(col);

        return table;
    }