質問

I will try to explain this problem, and i simplified the two needed tables that is important in this procedure:

**Table 1: book_sales**
identifiers
sales_price
sales_date
store
quantity

**Table2: discount**
identifiers
sales_price (the changed / discounted price)
from_date
to_date
  • If the to_date in Discount table is '2999-01-01' it should overwrite the sales price in book_sales (In this example it is: 99,-)
  • If the from_date and to_date in Discount table is for just a period, lets say From_Date: 2014-02-03 and To_Date 2014-02-05 it should overwrite the other prices for exactly that period. (In this example it is 69,-)

My stored procedure is supposed to return data like this:

+------------+-----------+---------+----+-----+
| 2014-02-01 |  ItemName |  Item01 |  3 |  99 |
| 2014-02-02 |  ItemName |  Item01 |  2 |  99 |
| 2014-02-03 |  ItemName |  Item01 |  2 |  69 |
| 2014-02-04 |  ItemName |  Item01 |  2 |  69 |
| 2014-02-05 |  ItemName |  Item01 |  2 |  69 |
| 2014-02-06 |  ItemName |  Item01 |  2 |  99 |
+------------+-----------+---------+----+-----+

But it is showing both and returning data like this:

+------------+-----------+---------+----+-----+
| 2014-02-01 |  ItemName |  Item01 |  3 |  99 |
| 2014-02-01 |  ItemName |  Item01 |  3 |  69 |
| 2014-02-02 |  ItemName |  Item01 |  2 |  99 |
| 2014-02-02 |  ItemName |  Item01 |  2 |  69 |
| 2014-02-03 |  ItemName |  Item01 |  2 |  99 |
| 2014-02-03 |  ItemName |  Item01 |  2 |  69 |
+------------+-----------+---------+----+-----+

etc... So this is the actual procedure, can you guys see anything wrong?

ALTER PROCEDURE [dbo].[Loid] @month         INT, 
                             @year          INT, 
                             @report_source NVARCHAR(255), 
                             @is_primary    INT 
AS 
    SELECT Cast(isa.sales_date AS DATE)                                   AS 
           DATE, 
           BV.name, 
           isa.identifiers, 
           isa.quantity, 
           Isnull(id.sales_price, Isnull(u.sales_price, isa.sales_price)) AS 
           SALES_PRICE 
    FROM   book_sales AS isa 
           INNER JOIN store AS BV 
                   ON bv.store_id = isa.store_id 
           LEFT OUTER JOIN discount AS id 
                        ON id.identifiers = isa.identifiers 
                           AND id.from_date <= isa.sales_date 
                           AND id.to_date >= isa.sales_date 
                           AND id.to_date < '2999-01-01' 
                           AND BV.name = id.store 
           LEFT OUTER JOIN discount AS u 
                        ON u.identifiers = isa.identifiers 
                           AND u.to_date = '2999-01-01' 
           LEFT OUTER JOIN book_contributor AS BC 
                        ON BC.book_id = isa.book_id 
    WHERE  Month(isa.sales_date) = @month 
           AND Year(isa.sales_date) = @year 
           AND isa.report_source = @report_source 
           AND bc.is_primary = @is_primary 
役に立ちましたか?

解決

By looking at the result i assume you have set a from_date value to discount records which has to_date '2999-01-01'

LEFT OUTER JOIN discount AS id 
ON id.identifiers = isa.identifiers 
AND id.from_date <= isa.sales_date 
AND id.to_date >= isa.sales_date 

If so in this join statement those discount records also joins which creates duplicate records.

Either you can set from_date to a higher value like '2999-01-01' for the discount records which has to_date '2999-01-01' or change the join statemnt as below.

LEFT OUTER JOIN discount AS id 
ON id.identifiers = isa.identifiers 
AND id.from_date <= isa.sales_date 
AND id.to_date >= isa.sales_date 
AND id.to_date < '2999-01-01'

EDIT

According to chat following should work. A new condition BV.name = u.store is added to second join with discount table.

ALTER PROCEDURE [dbo].[Loid] @month         INT, 
                             @year          INT, 
                             @report_source NVARCHAR(255), 
                             @is_primary    INT 
AS 
    SELECT Cast(isa.sales_date AS DATE)                                   AS 
           DATE, 
           BV.name, 
           isa.identifiers, 
           isa.quantity, 
           Isnull(id.sales_price, Isnull(u.sales_price, isa.sales_price)) AS 
           SALES_PRICE 
    FROM   book_sales AS isa 
           INNER JOIN store AS BV 
                   ON bv.store_id = isa.store_id 
           LEFT OUTER JOIN discount AS id 
                        ON id.identifiers = isa.identifiers 
                           AND id.from_date <= isa.sales_date 
                           AND id.to_date >= isa.sales_date 
                           AND id.to_date < '2999-01-01' 
                           AND BV.name = id.store 
           LEFT OUTER JOIN discount AS u 
                        ON u.identifiers = isa.identifiers 
                           AND u.to_date = '2999-01-01'
                           AND BV.name = u.store  
           LEFT OUTER JOIN book_contributor AS BC 
                        ON BC.book_id = isa.book_id 
    WHERE  Month(isa.sales_date) = @month 
           AND Year(isa.sales_date) = @year 
           AND isa.report_source = @report_source 
           AND bc.is_primary = @is_primary 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top