【问题标题】:Populating database with price range values使用价格范围值填充数据库
【发布时间】:2016-12-11 13:56:37
【问题描述】:

我一直在尝试填充具有价格范围的给定表格。我试过十进制数据类型、数字、浮点数,但对我没有任何帮助。 更具体地说,我不太明白如何为“订单数量”列编写脚本,因为它具有价格范围内的值。

表:

以及我尝试的脚本..

  DROP TABLE MAIL_ORDERS;
CREATE TABLE MAIL_ORDERS (
amountofOrder MONEY not null PRIMARY KEY,
orderID INT not null);

DROP TABLE DELIVERY_DETAILS;
CREATE TABLE DELIVERY_DETAILS (
deliveryID INT not null PRIMARY KEY,
regular MONEY not null,
rush MONEY not null,
express MONEY not null,
amountofOrder MONEY not null,
CONSTRAINT fk_deliverydetails_mailorders FOREIGN KEY (amountofOrder)
REFERENCES MAIL_ORDERS (amountofOrder));

INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($0.00 - $15.00, 1);
INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($15.01 - $30.00, 2);
INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($30.01 - $45.00, 3);
INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($45.01 - $65.00, 4);
INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($65.01 - $90.00, 5);
INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($90.01 - $125.00, 6);
INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($125.01 - $200.00, 7);
INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($200.01 - null, 8);

INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (1, $4.95, $9.95, $17.45, $0.00 - $15.00);
INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (2, $5.95, $10.95, $18.45, $15.01 - $30.00);
INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (3, $7.95, $12.95, $20.45, $30.01 - $45.00);
INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (4, $9.95, $14.95, $22.45, $45.01 - $65.00);
INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (5, $11.95, $16.95, $24.45, $65.01 - $90.00);
INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (6, $13.95, $18.95, $26.45, $90.01 - $125.00);
INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (7, $14.95, $19.95, $27.45, $125.01 - $200.00);
INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, amountofOrder)
VALUES (8, $16.95, $21.95, $29.45, $200.01 - null);

SELECT * FROM MAIL_ORDER
SELECT * FROM DELIVERY_DETAILS

【问题讨论】:

    标签: sql-server data-modeling


    【解决方案1】:

    好吧……好吧

    INSERT INTO MAIL_ORDERS (amountofOrder, orderID) VALUES ($15.01 - $30.00, 2); 
    --Cristina:  Actually you are substracting $15.01 - $30.00 = -14.99
    

    你不能那样做,你需要两个字段,而且关系不太好。

    通常我们将主键设置为int

    查看示例:

    DROP TABLE MAIL_ORDERS;
    create TABLE MAIL_ORDERS (
    amountofOrderInit MONEY not null,
    amountofOrderEnd MONEY null,
    orderID INT not null primary key);
    
          go
    
    DROP TABLE DELIVERY_DETAILS;
    CREATE TABLE DELIVERY_DETAILS (
    deliveryID INT not null PRIMARY KEY,
    regular MONEY not null,
    rush MONEY not null,
    express MONEY not null,
    orderID int not null,
    CONSTRAINT fk_deliverydetails_mailorders FOREIGN KEY (orderID)
    REFERENCES MAIL_ORDERS (orderID));
    
    go
    
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($0.00 , $15.00, 1);
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($15.01 , $30.00, 2); --Cristina:  Actually you are substracting $15.01 - $30.00 = -14.99
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($30.01 , $45.00, 3); 
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($45.01 , $65.00, 4);
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($65.01 , $90.00, 5);
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($90.01 , $125.00, 6);
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($125.01 , $200.00, 7);
    INSERT INTO MAIL_ORDERS (amountofOrderInit, amountofOrderEnd, orderID) VALUES ($200.01 , null, 8);
    
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (1, $4.95, $9.95, $17.45, 1);
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (2, $5.95, $10.95, $18.45, 2);
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (3, $7.95, $12.95, $20.45, 3);
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (4, $9.95, $14.95, $22.45,4);
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (5, $11.95, $16.95, $24.45, 5);
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (6, $13.95, $18.95, $26.45, 6);
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (7, $14.95, $19.95, $27.45, 7);
    INSERT INTO DELIVERY_DETAILS (deliveryID, regular, rush, express, orderID)
    VALUES (8, $16.95, $21.95, $29.45, 8);
    
    
    SELECT 
        mo.*
        ,cONCAT('$ ', mo.amountofOrderInit, ' - ' ,'$ ',mo.amountofOrderEnd) as amountofOrder --add column only for the query
    FROM MAIL_ORDERS mo
    
    SELECT 
        dd.* 
    ,CONCAT('$ ', mo.amountofOrderInit, ' - ' ,'$ ', mo.amountofOrderEnd) as amountofOrder --add column only for the query, in this case the info comes from the MAIL_ORDERS table
    FROM DELIVERY_DETAILS dd     
        inner join MAIL_ORDERS mo     --Add a relationship between the tables
        on  dd.orderID = mo.orderID
    

    那么...你想用这些数据做什么?

    我将只使用一张表,比如这个例子:

    declare @priceByAmount table(
        id int identity not null primary key ,
        initialAmount money not null,
        finalAmount money,
        reguarDelivery money not null,
        rushDelivery money not null,
        expressDelivery money not null
    );
    
    insert into @priceByAmount values (0,15,4.95,9.95,17.45)
    insert into @priceByAmount values (15.01,30,5.95, 10.95, 18.45)
    insert into @priceByAmount values (30.01,45, 7.95, 12.95, 20.45)
    insert into @priceByAmount values (45.01,65,9.95, 14.95, 22.45)
    insert into @priceByAmount values (65.01,90,11.95, 16.95, 24.45)
    insert into @priceByAmount values (90.01,125,13.95, 18.95, 26.45)
    insert into @priceByAmount values (125.01,200,14.95, 19.95, 27.45)
    insert into @priceByAmount values (200.01,null,16.95, 21.95, 29.45)
    
    
    select 
        --*
        CONCAT('$ ', initialAmount,  isnull(' to $' + convert(nvarchar(100),finalAmount),' + ')) as [Amount of Order]
        ,reguarDelivery [Regular Delivery 7-10 Days]
        ,rushDelivery [Rush Delivery 4-5 Business Days]
        ,expressDelivery [Express Delivery 1-2 Business Days]
    from @priceByAmount
    

    【讨论】:

      【解决方案2】:

      我绝对会推荐 3 张桌子来做这样的事情,这样如果您需要更改运输选项、日期范围、个别价格,扩展为依赖于另一张桌子,它可以灵活地更改,而无需做大量的工作代码重构。

      无论如何,我会将其标准化为:

      CREATE TABLE AmountRanges (
          AmountRangeId INT NOT NULL AUTO_INCREMENT PRIMARY KEY
          ,StartRange NUMERIC(15,4) NOT NULL
          ,EndRange NUMERIC(15,4) NOT NULL
      );
      
      CREATE TABLE ShippingOptions (
          ShippingOptionId INT NOT NULL AUTO_INCREMENT PRIMARY KEY
          ,StartRange INT NOT NULL
          ,EndRange INT NOT NULL
          ,Description VARCHAR(50)
      ); 
      
      CREATE TABLE ShippingPrices (
          ShippingPriceId INT NOT NULL AUTO_INCREMENT PRIMARY KEY
          ,AmountRangeId INT NOT NULL
          ,ShippingOptionId INT NOT NULL
          ,Price NUMERIC(15,4) NOT NULL
          ,FOREIGN KEY (AmountRangeId) REFERENCES AmountRanges (AmountRangeId)
          ,FOREIGN KEY (ShippingOptionId) REFERENCES ShippingOptions (ShippingOptionId)
      );
      
      
      INSERT INTO AmountRanges (StartRange, EndRange) VALUES (0,15),(15.01,30),(30.01,45),(45.01,65),(65.01,90);
      INSERT INTO ShippingOptions (StartRange, EndRange,Description) VALUES (7,10,'Regular Delivery 7-10 days')
      ,(4,5,'Rush Delivery 4-5 Busienss Days'),(1,2,'Express Delivery 1-2 Business Days');
      INSERT INTO ShippingPrices (AmountRangeId,ShippingOptionId,Price) VALUES (1,1,4.95),(1,2,9.95),(1,3,17.45)
      ,(2,1,5.95),(2,2,10.95),(2,3,18.45),(3,1,7.95),(3,2,12.95),(3,3,20.45)
      ,(4,1,9.95),(4,2,14.95),(4,3,22.45),(5,1,11.95),(5,2,16.95),(5,3,24.45);
      
      
      SELECT
          p.ShippingPriceId
          ,a.StartRange as AmountStartRange
          ,a.EndRange as AmountEndRange
          ,o.StartRange as DaysStartRange
          ,o.EndRange as DaysEndRange
          ,o.Description
          ,p.Price
      FROM
         ShippingPrices p
         INNER JOIN AmountRanges a
         ON p.AmountRangeId = a.AmountRangeId
         INNER JOIN ShippingOptions o
         ON p.ShippingOptionId = o.ShippingOptionId
      ;   
      

      这是一个显示其工作原理的链接http://rextester.com/l/mysql_online_compiler

      如果您真的希望表格采用完全相同的格式,您所要做的就是选择运输选项。在这种情况下,我建议使用条件聚合来进行数据透视。

      【讨论】:

        【解决方案3】:

        如果数据值是一个范围,我认为你将它们设计成两列会更好地处理,例如,你可以像下面这样更改表 MAIL_ORDERS,当处理数据如查询时使用 start dan end value ,新的 cloumn AmountofOrder 是用于显示和主键的计算 cloumn。

        CREATE TABLE MAIL_ORDERS (
            AmountofOrderStart MONEY,
            AmountofOrderEnd MONEY,
            AmountofOrder AS ISNULL('$'+LTRIM(AmountofOrderStart),'Lesst than ')+ISNULL(' - $'+LTRIM(AmountofOrderEnd), ' +'),
            orderID INT not NULL,
            CONSTRAINT [PK_MAIL_ORDERS] PRIMARY KEY CLUSTERED (AmountofOrder)
        )
        INSERT INTO MAIL_ORDERS(AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($0.00, $15.00, 1);
        INSERT INTO MAIL_ORDERS (AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($15.01 , $30.00, 2);
        INSERT INTO MAIL_ORDERS (AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($30.01 , $45.00, 3);
        INSERT INTO MAIL_ORDERS (AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($45.01 , $65.00, 4);
        INSERT INTO MAIL_ORDERS (AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($65.01 , $90.00, 5);
        INSERT INTO MAIL_ORDERS (AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($90.01 , $125.00, 6);
        INSERT INTO MAIL_ORDERS (AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($125.01 , $200.00, 7);
        INSERT INTO MAIL_ORDERS (AmountofOrderStart,AmountofOrderEnd, orderID) VALUES ($200.01 , null, 8);
        
        SELECT * FROM MAIL_ORDERS ORDER BY AmountofOrderStart
        
        AmountofOrderStart AmountofOrderEnd AmountofOrder orderID --------------------- --------- -------- -------------------------------------------------- --------------- ------------ 0.00 15.00 $0.00 - $15.00 1 15.01 30.00 $15.01 - $30.00 2 30.01 45.00 $30.01 - $45.00 3 45.01 65.00 $45.01 - $65.00 4 65.01 90.00 美元 65.01 - 90.00 美元 5 90.01 125.00 $90.01 - $125.00 6 125.01 200.00 $125.01 - $200.00 7 200.01 空 $200.01+ 8

        【讨论】:

          猜你喜欢
          • 2021-09-26
          • 2011-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多