0

我有以下两张表

工商管理硕士

+--------+----------+------------+--------------+------------------+------------+
|mbaId   |  Channel | Product    |  ProgDate    |   ProgStartTime  |ProgEndTime |
+--------+----------+ -----------+--------------+------------------+------------+
|12      |    AA    |     SS     |  01/04/2011  |    16:00:00      |  20:00:00  |
|13      |    AA    |     SS     |  01/04/2011  |    16:00:00      |  20:00:00  |
|14      |    AA    |     SS     |  01/04/2011  |    16:00:00      |  20:00:00  | 
|15      |    AA    |     SS     |  01/04/2011  |    17:00:00      |  18:00:00  |
+--------+----------+------------+--------------+------------------+------------+

地图

+----------+--------+---------------+-------------------+---------+----+
|mapId     |Channel | Product       |ProgDate           |  AdvTime|    |        
+----------+--------+---------------+-------------------+---------+----+    
|8         | AA     | SS            | 01/04/2011        | 19:35:14| 30 |
|9         | AA     | SS            | 01/04/2011        | 18:40:19| 27 |
|10        | AA     | SS            | 01/04/2011        | 19:36:58| 35 |
|11        | AA     | SS            | 01/04/2011        | 17:47:13| 28 |
+----------+--------+---------------+-------------------+---------+----+

我需要以下输出

+-----------+-------+-------+-------------+---------+--------------+-----------+-----+
| mapId     |Channel|Product| ProgDate    | AdvTime |ProgStartTime |ProgEndTime|mbaid| 
+-----------+-------+-------+-------------+---------+--------------+-----------+-----+
|  8        | AA    | SS    | 01/04/2011  | 19:35:14|16:00:00      | 20:00:00  |  12 |  
|  9        | AA    | SS    | 01/04/2011  | 18:40:19|16:00:00      | 20:00:00  |  13 | 
|  10       | AA    | SS    | 01/04/2011  | 19:36:58|16:00:00      | 20:00:00  |  14 | 
|  11       | AA    | SS    | 01/04/2011  | 17:47:13|16:00:00      | 17:00:00  |  15 | 
+-----------+-------+-------+-------------+---------+--------------+-----------+-----+

我使用下面的代码

select *
from MBA2 as mba
  inner join Map2 as map on(map.Channel=mba.Channel and map.Product=mba.Product)
    where(
    (MBA.ProgStartTime < MBA.ProgEndTime
      and MBA.ProgStartTime <=
        case when datediff(mi, MBA.ProgStartTime, MBA.ProgEndTime) <= 60
          then dateadd(mi, 5, Map.AdvTime)
          else Map.AdvTime
          end
      and MBA.ProgEndTime >=
        case when datediff(mi, MBA.ProgStartTime, MBA.ProgEndTime) <= 60
          then dateadd(mi, -5, Map.AdvTime)
          else Map.AdvTime
          end) or
    (MBA.ProgStartTime > MBA.ProgEndTime
      and (MBA.ProgStartTime <=
        case when 1440 - datediff(mi, MBA.ProgEndTime, MBA.ProgStartTime) <= 60
          then dateadd(mi, 5, Map.AdvTime)
          else Map.AdvTime
          end
      or MBA.ProgEndTime >=
        case when 1440 - datediff(mi, MBA.ProgEndTime, MBA.ProgStartTime) <= 60
          then dateadd(mi, -5, Map.AdvTime)
          else Map.AdvTime
          end)))
order by advtime asc

但是我得到了重复值,即 19:35:14 的值与 12,13 和 14 的 16:00:00 到 20:00:00 的范围匹配。我需要 MAP 中的一个值匹配 MBA 中的一个值。

所以我使用下面的代码将数据添加到数据集中并在 MAPID 中查找重复项

if (repeatID.Contains(int.Parse(dr["mapID"].ToString())))
        {
            duplicateID.Add(int.Parse(dr["mapID"].ToString()));


        }

        else
        {
            DataRow dr1 = dt.NewRow();
            dr1[0] = int.Parse(dr["mapID"].ToString());

            dr1[10] = int.Parse(dr["mbaID"].ToString());

            dr1[1] = (dr["Channel"].ToString());


            dr1[2] = (dr["Product"].ToString());


            dr1[3] = (dr["ProgDate"].ToString());


            dr1[4] = (dr["AdvTime"].ToString());


            dr1[5] = (dr["Progstarttime"].ToString());


            dr1[6] = (dr["Progendtime"].ToString());


            dr1[7] = (dr["Channel"].ToString());

            dr1[8] = (dr["Product"].ToString());

            dr1[9] = (dr["ProgDate"].ToString());




            dt.Rows.Add(dr1);

        }

        repeatID.Add(int.Parse(dr["mapID"].ToString()));

       i = i + 1;
    }
    sCon.Close();
}

并删除重复的 id 行。然后使用下面的查询来显示没有重复的数据并再次显示到数据表中。它返回数据表中没有重复的 mapid,但带有 MBAID Dulicates。

using (SqlConnection sCon = new SqlConnection(connec))
        {

            foreach (int id in duplicateID)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow ddr = dt.Rows[i];
                    if (ddr["mapID"].ToString() == id.ToString())
                        ddr.Delete();
                }


                sCon.Open();
                {
                    SqlCommand cmd = new SqlCommand(@"select Distinct top 1 mp.Id as mapid, mb.Id as mbaid, mp.Channel, mp.Product, mp.ProgDate, mp.AdvTime, mb.Channel,  mb.ProgStartTime,mb.ProgEndTime,mb.progdate, convert(time, dateadd(minute, datediff(minute, mb.progStartTime, mb.progEndTime), 0)) as timeDiff 
from MBA22 as mb
  inner join Map22 as mp on(mp.Channel=mb.Channel and mp.Product=mb.Product and mb.ProgDate=mp.ProgDate )
    where(
    (mb.ProgStartTime < mb.ProgEndTime
      and mb.ProgStartTime <=
        case when datediff(mi, mb.ProgStartTime, mb.ProgEndTime) <= 60
          then dateadd(mi, 5, mp.AdvTime)
          else mp.AdvTime
          end
      and mb.ProgEndTime >=
        case when datediff(mi, mb.ProgStartTime, mb.ProgEndTime) <= 60
          then dateadd(mi, -5, mp.AdvTime)
          else mp.AdvTime
          end) or
    (mb.ProgStartTime > mb.ProgEndTime
      and (mb.ProgStartTime <=
        case when 1440 - datediff(mi, mb.ProgEndTime, mb.ProgStartTime) <= 60
          then dateadd(mi, 5, mp.AdvTime)
          else mp.AdvTime
          end
      or mb.ProgEndTime >=
        case when 1440 - datediff(mi, mb.ProgEndTime, mb.ProgStartTime) <= 60
          then dateadd(mi, -5, mp.AdvTime)
          else mp.AdvTime
          end)))
and mp.Id = '" + id + "'  order by  timeDiff asc", sCon);
                    cmd.CommandTimeout = 0;
                    SqlDataReader dr = cmd.ExecuteReader();

请帮助

4

1 回答 1

0

尝试此查询相应地工作,但有风险,因为根据您的需要 1 记录映射到另一个表,因此应正确维护序列,否则您将得到错误的记录

select 
    t2.mapId, 
    t1.Channel, 
    t1.Product, 
    t1.ProgDate, 
    t2.AdvTime,
    t1.ProgStartTime, 
    t1.ProgEndTime, 
    t1.mbaid
FROM 
    (select 
         ROW_NUMBER() OVER(order by mbaid) as rId, * 
    from 
         mba
    ) t1, 
    (select 
         ROW_NUMBER() OVER(order by mapid) as rId, * 
    from 
         map
    ) t2
WHERE 
    t1.rId = t2.rId;a

小提琴

行号

于 2013-03-19T09:51:32.293 回答