导致A TOP N值的SQL查询可能不是负面错误

时间:2014-04-28 15:06:33

标签: vb.net sql-server-2008

我正在使用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;

1 个答案:

答案 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;