如何在SQL中用日期替换这些空值?

时间:2016-03-22 20:12:16

标签: mysql sql

2003-04-01和2003-04-07之间的所有日子都可以找到罗斯托夫镇的旅行次数。结果集:日期,行程次数。

以下是数据库架构:http://sql-ex.ru/help/select13.php#db_4

查询和结果集:

SELECT MyDates.d as Dt, 
       count(distinct trip.trip_no) as Qty
FROM   trip 

JOIN pass_in_trip 
         ON trip.trip_no = pass_in_trip.trip_no 
FULL OUTER JOIN
(select cast(cast(x.yr as varchar)+'-'+cast(y.mo as varchar)+'-'+cast(z.dy as varchar) as datetime) d from (select 2003 yr) x CROSS JOIN (SELECT 4 mo) y CROSS JOIN (select 1 dy UNION ALL select 2 UNION ALL select 3 UNION ALL select 4 UNION ALL select 5 UNION ALL select 6 UNION ALL select 7) z where dy<=7) MyDates
ON Mydates.d = pass_in_trip.date
WHERE  trip.town_from = 'Rostov' 
GROUP BY MyDates.d, pass_in_trip.date, trip.trip_no

+-------------------------+-----+
|           Dt            | Qty |
+-------------------------+-----+
| NULL                    |   1 |
| NULL                    |   1 |
| NULL                    |   1 |
| NULL                    |   1 |
| 2003-04-01 00:00:00.000 |   1 |
| 2003-04-05 00:00:00.000 |   1 |
+-------------------------+-----+

以下是我想要的结果集

+-------------------------+-----+
|           Dt            | Qty |
+-------------------------+-----+
| 2003-04-01 00:00:00.000 |   1 |
| 2003-04-02 00:00:00.000 |   0 |
| 2003-04-03 00:00:00.000 |   0 |
| 2003-04-04 00:00:00.000 |   0 |
| 2003-04-05 00:00:00.000 |   1 |
| 2003-04-06 00:00:00.000 |   0 |
| 2003-04-07 00:00:00.000 |   0 |
+-------------------------+-----+

仅供参考 - 我认为这是使用MySQL或SQL服务器,对我来说有点不清楚。

0 个答案:

没有答案