-3

ホテルごとの各部屋の合計金額を計算するのに少し問題があります。

例えば:

ROOM_1_TOTAL は、Hotel_ID が 1 である各行で 170 になります。ROOM_2_TOTAL は、Hotel_ID が 1 である各行で 170 になります。

ROOM_1_TOTAL は、Hotel_ID が 2 である各行で 10 になります (確かに、機能していません) ROOM_2_TOTAL は、Hotel_ID が 2 である各行で 10 になります (確かに、機能していません)。

等々...

ここに私のコードと出力があります..

http://sqlfiddle.com/#!2/575d3/2

MYSQL クエリ:

DB構造とダミーデータ...

CREATE TABLE IF NOT EXISTS `omc_hotel` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
)ENGINE=InnoDB  DEFAULT CHARSET=latin1;

INSERT INTO `omc_hotel` (`id`, `name`) VALUES
(1, 'Hotel ABC'),
(2, 'Hotel CSB'),
(3, 'Hotel CSD'),
(4, 'Hotel NDG');

CREATE TABLE IF NOT EXISTS `omc_hotel_room` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
   `pax_min` int(11) DEFAULT NULL,
  `pax_max` int(11) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
   `hotel_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ;
INSERT INTO `omc_hotel_room` (`id`, `name`, `pax_min`,  `pax_max`, `quantity`, `hotel_id`) VALUES
(1,'Single',1,1,150,1),
(2,'Single',1,1,250,2),
(3,'Double',2,2,200,1),
(4,'Double',2,2,405,2),
(5,'Double',1,1,405,3),
(6,'Double Delax',2,2,50,3),
(7,'Double',1,1,100,4);


CREATE TABLE IF NOT EXISTS `omc_hotel_room_extra_quota` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `hotel_id` int(11) NOT NULL,
  `room_type_id` int(11) NOT NULL,
  `quantity` int(11) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO `omc_hotel_room_extra_quota` (`id`, `hotel_id`, `room_type_id`, `quantity`, `date`) VALUES
(1, 1, 1, -10, '2013-08-13'),
(2, 1, 3, -10, '2013-08-25'),
(3, 2, 2, 0, '2013-09-26'),
(4, 2, 4, 0, '2013-09-27'),
(5, 3, 5, 0, '2013-10-28'),
(6, 3, 5, 0, '2013-10-09');


CREATE TABLE IF NOT EXISTS `omc_hotel_room_pricelist` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `hotel_id` int(11) NOT NULL,
  `room_type_id` int(11) NOT NULL,
  `season_id` int(11) NOT NULL,
  `price_normal` decimal(5,2) DEFAULT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;

INSERT INTO `omc_hotel_room_pricelist` (`id`, `hotel_id`, `room_type_id`, `season_id`, `price_normal`) VALUES
(1, 1, 1, 1, 50.00),
(2, 2, 2, 2, 10.00),
(3, 1, 1, 1, 35.00),
(4, 1, 1, 2, 70.00),
(5, 1, 1, 1, 80.00),
(6, 2, 4, 1, 80.00),
(7, 4, 7, 2, 80.00),
(8, 3, 6, 1, 80.00),
(9, 3, 5, 1, 30.00);

CREATE TABLE IF NOT EXISTS `omc_hotel_season_period` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `hotel_id` int(11) NOT NULL,
  `season_id` int(11) NOT NULL,
  `date` date NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;


INSERT INTO `omc_hotel_season_period` (`id`, `hotel_id`, `season_id`, `date`) VALUES
(1, 1, 1, '2013-08-13'),
(2, 1, 1, '2013-08-14'),
(3, 1, 2, '2013-08-15'),
(4, 2, 1, '2013-08-13'),
(5, 2, 1, '2013-08-14'),
(6, 2, 2, '2013-08-15'),
(7, 3, 2, '2013-08-13'),
(8, 3, 2, '2013-08-14'),
(9, 3, 1, '2013-08-15'),
(10, 1, 1, '2013-08-13'),
(11, 1, 1, '2013-08-14'),
(12, 1, 1, '2013-08-14');

SELECT candidates.hotel_id, omc_hotel_season_period.season_id,
       room_1_id, r1_price.price_normal AS room_1_price, SUM(r1_price.price_normal) as room_1_total,
       room_2_id,r2_price.price_normal AS room_2_price,SUM(r2_price.price_normal) as room_2_total,
       omc_hotel_season_period.date
FROM (SELECT r1.hotel_id, r1.id AS room_1_id, r2.id AS room_2_id FROM omc_hotel_room r1
      INNER JOIN omc_hotel_room r2 ON r1.hotel_id = r2.hotel_id
      WHERE r1.quantity >= 3 AND r2.quantity >= 4 AND r1.pax_max = 1 AND r2.pax_max = 2) AS candidates
LEFT JOIN omc_hotel_season_period
          ON candidates.hotel_id = omc_hotel_season_period.hotel_id
             AND date BETWEEN '2013-08-13' AND '2013-08-15'
             AND omc_hotel_season_period.date <> 0
LEFT JOIN omc_hotel_room_pricelist r1_price
          ON (candidates.hotel_id = r1_price.hotel_id
              AND room_1_id= r1_price.room_type_id)
LEFT JOIN omc_hotel_room_pricelist r2_price
          ON (candidates.hotel_id = r2_price.hotel_id
              AND room_1_id = r2_price.room_type_id)
WHERE
    r1_price.season_id =  omc_hotel_season_period.season_id
    AND
    r2_price.season_id =  omc_hotel_season_period.season_id
GROUP BY candidates.hotel_id, omc_hotel_season_period.date 
4

1 に答える 1

0

これを試して、これがあなたが望むものかどうかを確認してください。

SELECT candidates.hotel_id, omc_hotel_season_period.season_id,
   room_1_id, r1_price.price_normal AS room_1_price, SUM(r1_price.price_normal) as room_1_total,
   room_2_id,r2_price.price_normal AS room_2_price,SUM(r2_price.price_normal) as room_2_total,
   omc_hotel_season_period.date,
   (SELECT SUM(room1_price.price_normal)
    FROM omc_hotel_room_pricelist room1_price
    INNER JOIN omc_hotel_room room1 ON room1_price.hotel_id = room1.hotel_id
    INNER JOIN omc_hotel_season_period so1 ON room1_price.hotel_id = so1.hotel_id AND room1_price.season_id = so1.season_id
    INNER JOIN omc_hotel hotel1 ON room1_price.hotel_id = hotel1.id
    WHERE
       so1.date BETWEEN '2013-08-13' AND '2013-08-15' AND
       room1.quantity >= 3 AND room1.pax_max = 1 AND
       hotel1.id = candidates.hotel_id 
    GROUP BY candidates.hotel_id
   ) AS room_1_sum
FROM (SELECT r1.hotel_id, r1.id AS room_1_id, r2.id AS room_2_id FROM omc_hotel_room r1
  INNER JOIN omc_hotel_room r2 ON r1.hotel_id = r2.hotel_id
  WHERE r1.quantity >= 3 AND r2.quantity >= 4 AND r1.pax_max = 1 AND r2.pax_max = 2) AS    candidates
LEFT JOIN omc_hotel_season_period
      ON candidates.hotel_id = omc_hotel_season_period.hotel_id
         AND date BETWEEN '2013-08-13' AND '2013-08-15'
         AND omc_hotel_season_period.date <> 0
LEFT JOIN omc_hotel_room_pricelist r1_price
      ON (candidates.hotel_id = r1_price.hotel_id
          AND room_1_id= r1_price.room_type_id)
LEFT JOIN omc_hotel_room_pricelist r2_price
      ON (candidates.hotel_id = r2_price.hotel_id
          AND room_1_id = r2_price.room_type_id)
WHERE
r1_price.season_id =  omc_hotel_season_period.season_id
AND
r2_price.season_id =  omc_hotel_season_period.season_id
GROUP BY candidates.hotel_id
于 2013-08-12T23:19:55.707 に答える