我正在使用SQL Server 2008创建一个asp.net游戏租赁网站,并且有一个传递页面,当我运行此代码时会导致TOP N
值错误。
我在我的页面上发现了一个查询,该页面在从sqldatasource加载页面时运行,但它不包含任何顶部减号。
SELECT TOP (
SELECT MIN([Rentability].[RemainingRentable])
FROM (
/* Remaining rentable at a time */
SELECT
[Subscriptions].[Game_at_a_time] - (
/* Number of games currently rented by the customer */
SELECT COUNT(*) AS [CurraentlyRented]
FROM [Rentals]
WHERE [Rentals].[Date_returned] IS NULL
AND [Users].[User_ID] = [Rentals].[User_ID]
) AS RemainingRentable
FROM [Users]
JOIN [Subscriptions]
ON [Users].[Subscription_ID] = [Subscriptions].[Subscription_ID]
WHERE [Users].[User_ID] = 1 /* CHANGE THIS to your Customer ID */
UNION
SELECT
[Subscriptions].[Max_games] - (
/* Number of total games rented by the customer this month */
SELECT COUNT(*) AS [RentedThisMonth]
FROM [Rentals]
WHERE MONTH([Rentals].[Date_rented]) = MONTH(GETDATE())
AND YEAR([Rentals].[Date_rented]) = MONTH(GETDATE())
AND [Users].[User_ID] = [Rentals].[User_ID]
) AS RemainingRentable
FROM [Users]
JOIN [Subscriptions]
ON [Users].[Subscription_ID] = [Subscriptions].[Subscription_ID]
WHERE [Users].[User_ID] = 1 /* CHANGE THIS to your Customer ID */
) Rentability) [Games].[Game_barcode]FROM [Games]JOIN [Favourites]ON [Favourites].[Game_name] = [Games].[Name] JOIN [Users] ON [Users].[User_ID] = [Favourites].[User_ID] WHERE [Users].[User_ID] = 1 /* CHANGE THIS to your Customer ID */
AND [Games].[Quantity] > (
/* Number of currently rented copies */
SELECT COUNT(*) FROM [Rentals]
WHERE [Games].[Game_barcode] = [Rentals].[Game_barcode]
AND [Rentals].[Date_returned] IS NULL
)
ORDER BY Favourites.Priority, Favourites.DatePicked asc;
答案 0 :(得分:0)
如何使用CASE语句确保永远不会得到负数:
SELECT TOP (
SELECT CASE WHEN MIN([Rentability].[RemainingRentable]) < 0 THEN 0
ELSE MIN([Rentability].[RemainingRentable]) END AS RemainingRentable
FROM (
/* Remaining rentable at a time */
SELECT
[Subscriptions].[Game_at_a_time] - (
/* Number of games currently rented by the customer */
SELECT COUNT(*) AS [CurraentlyRented]
FROM [Rentals]
WHERE [Rentals].[Date_returned] IS NULL
AND [Users].[User_ID] = [Rentals].[User_ID]
) AS RemainingRentable
FROM [Users]
JOIN [Subscriptions]
ON [Users].[Subscription_ID] = [Subscriptions].[Subscription_ID]
WHERE [Users].[User_ID] = 1 /* CHANGE THIS to your Customer ID */
UNION
SELECT
[Subscriptions].[Max_games] - (
/* Number of total games rented by the customer this month */
SELECT COUNT(*) AS [RentedThisMonth]
FROM [Rentals]
WHERE MONTH([Rentals].[Date_rented]) = MONTH(GETDATE())
AND YEAR([Rentals].[Date_rented]) = MONTH(GETDATE())
AND [Users].[User_ID] = [Rentals].[User_ID]
) AS RemainingRentable
FROM [Users]
JOIN [Subscriptions]
ON [Users].[Subscription_ID] = [Subscriptions].[Subscription_ID]
WHERE [Users].[User_ID] = 1 /* CHANGE THIS to your Customer ID */
) Rentability
) [Games].[Game_barcode]
FROM [Games]
JOIN [Favourites] ON [Favourites].[Game_name] = [Games].[Name]
JOIN [Users] ON [Users].[User_ID] = [Favourites].[User_ID]
WHERE [Users].[User_ID] = 1 /* CHANGE THIS to your Customer ID */
AND [Games].[Quantity] > (
/* Number of currently rented copies */
SELECT COUNT(*) FROM [Rentals]
WHERE [Games].[Game_barcode] = [Rentals].[Game_barcode]
AND [Rentals].[Date_returned] IS NULL
)
ORDER BY Favourites.Priority, Favourites.DatePicked asc;