以下の2つのテーブルがあります
MBA
+--------+----------+------------+--------------+------------------+------------+
|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 は、ID 12、13、および 14 の範囲 16:00:00 から 20:00:00 に一致します。MAP の 1 つの値が MBA の 1 つの値に一致する必要があります。
SO 私は以下のコードを使用してデータセットにデータを追加し、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 が重複しています。
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();
助けてくださいPPPP