COUNT 函数无法使用 DATEDIFF 正确计数
COUNT function not counting correctly with DATEDIFF
我确信这是一个简单的修复,但无法弄清楚。我有一个 COUNT 函数,需要计算出租的总数。单独使用,该功能工作得很好。我试过使用 DISTINCT、调整 GROUP BY 和 ORDER BY,但没有用。
当我添加 DATEDIFF 函数来获取租用天数时,COUNT 函数无法按预期方式工作。我应该用“2”而不是“1”得到一些结果。请注意,我需要按 Rentals.Boat_ID 订购。这是我的代码和 table 结果。谢谢
SELECT
BOATS.Boat_Brand,
COUNT(RENTALS.Boat_ID) AS NumberofRentals,
DATEDIFF(Day, RENTALS.Rental_StartDay, RENTALS.Rental_EndDay)+1) as NumberofDaysRented
FROM RENTALS
INNER JOIN BOATS
ON RENTALS.Boat_ID = BOATS.Boat_ID
GROUP BY
BOATS.Boat_Brand,
RENTALS.Rental_StartDay,
RENTALS.Rental_EndDay
ORDER BY
COUNT(RENTALS.Boat_ID) DESC;
Boat_Brand
NumberofRentals
NumberofDaysRented
Blue Martin
1
20
Blue Martin
1
35
Boston
1
52
Cherubini
1
11
Dufour
1
10
Eagle Craft
1
19
Motor Yacht
1
17
Motor Yacht
1
47
Grady-White
1
1
Horizon
1
22
Lemsteraak
1
19
Lund
1
64
Mastercraft
1
19
Mastercraft
1
1
Nauticat
1
10
Tracker
1
18
Tracker
1
1
Viking
1
20
Yamaha
1
20
预计TABLE/RESULTS:
Boat_Brand
NumberofRentals
NumberofDaysRented
Blue Martin
2
55
Motor Yacht
2
64
Mastercraft
2
20
Tracker
2
19
Boston
1
52
Cherubini
1
11
Dufour
1
10
Eagle Craft
1
19
Grady-White
1
1
Horizon
1
22
Lemsteraak
1
19
Lund
1
64
Nauticat
1
10
Viking
1
20
Yamaha
1
20
看来您只需要按品牌分组并获得租赁天数的总和:
SELECT
BOATS.Boat_Brand,
COUNT(RENTALS.Boat_ID) AS NumberofRentals,
SUM(DATEDIFF(Day,RENTALS.Rental_StartDay,RENTALS.Rental_EndDay) + 1)) as NumberofDaysRented
FROM
RENTALS
INNER JOIN BOATS ON RENTALS.Boat_ID = BOATS.Boat_ID
GROUP BY
BOATS.Boat_Brand
ORDER BY
COUNT(RENTALS.Boat_ID) DESC;
我确信这是一个简单的修复,但无法弄清楚。我有一个 COUNT 函数,需要计算出租的总数。单独使用,该功能工作得很好。我试过使用 DISTINCT、调整 GROUP BY 和 ORDER BY,但没有用。
当我添加 DATEDIFF 函数来获取租用天数时,COUNT 函数无法按预期方式工作。我应该用“2”而不是“1”得到一些结果。请注意,我需要按 Rentals.Boat_ID 订购。这是我的代码和 table 结果。谢谢
SELECT
BOATS.Boat_Brand,
COUNT(RENTALS.Boat_ID) AS NumberofRentals,
DATEDIFF(Day, RENTALS.Rental_StartDay, RENTALS.Rental_EndDay)+1) as NumberofDaysRented
FROM RENTALS
INNER JOIN BOATS
ON RENTALS.Boat_ID = BOATS.Boat_ID
GROUP BY
BOATS.Boat_Brand,
RENTALS.Rental_StartDay,
RENTALS.Rental_EndDay
ORDER BY
COUNT(RENTALS.Boat_ID) DESC;
Boat_Brand | NumberofRentals | NumberofDaysRented |
---|---|---|
Blue Martin | 1 | 20 |
Blue Martin | 1 | 35 |
Boston | 1 | 52 |
Cherubini | 1 | 11 |
Dufour | 1 | 10 |
Eagle Craft | 1 | 19 |
Motor Yacht | 1 | 17 |
Motor Yacht | 1 | 47 |
Grady-White | 1 | 1 |
Horizon | 1 | 22 |
Lemsteraak | 1 | 19 |
Lund | 1 | 64 |
Mastercraft | 1 | 19 |
Mastercraft | 1 | 1 |
Nauticat | 1 | 10 |
Tracker | 1 | 18 |
Tracker | 1 | 1 |
Viking | 1 | 20 |
Yamaha | 1 | 20 |
预计TABLE/RESULTS:
Boat_Brand | NumberofRentals | NumberofDaysRented |
---|---|---|
Blue Martin | 2 | 55 |
Motor Yacht | 2 | 64 |
Mastercraft | 2 | 20 |
Tracker | 2 | 19 |
Boston | 1 | 52 |
Cherubini | 1 | 11 |
Dufour | 1 | 10 |
Eagle Craft | 1 | 19 |
Grady-White | 1 | 1 |
Horizon | 1 | 22 |
Lemsteraak | 1 | 19 |
Lund | 1 | 64 |
Nauticat | 1 | 10 |
Viking | 1 | 20 |
Yamaha | 1 | 20 |
看来您只需要按品牌分组并获得租赁天数的总和:
SELECT
BOATS.Boat_Brand,
COUNT(RENTALS.Boat_ID) AS NumberofRentals,
SUM(DATEDIFF(Day,RENTALS.Rental_StartDay,RENTALS.Rental_EndDay) + 1)) as NumberofDaysRented
FROM
RENTALS
INNER JOIN BOATS ON RENTALS.Boat_ID = BOATS.Boat_ID
GROUP BY
BOATS.Boat_Brand
ORDER BY
COUNT(RENTALS.Boat_ID) DESC;