根据金额和日期列获得每月和每年的百分比

时间:2017-05-10 18:07:17

标签: php mysql mysqli

下面是我正在使用的表的结构表,我将解释我想要做什么。

 Table structure for table `conversion_details`
--

CREATE TABLE `conversion_details` (
  `details_ID` int(11) NOT NULL,
  `dealershipName_ID` int(11) NOT NULL,
  `cc_ID` int(11) NOT NULL,
  `source_ID` int(11) NOT NULL,
  `amount` decimal(10,0) NOT NULL,
  `date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `conversion_details`
--

INSERT INTO `conversion_details` (`details_ID`, `dealershipName_ID`, `cc_ID`, `source_ID`, `amount`, `date`) VALUES
(1, 11, 3, 2, '2', '2017-03-06 00:00:00'),
(2, 11, 2, 8, '47', '2017-02-28 00:00:00'),
(3, 11, 2, 8, '12', '2017-03-06 00:00:00'),
(4, 11, 4, 9, '94', '2017-02-28 00:00:00'),
(5, 11, 2, 9, '548', '2017-02-28 00:00:00'),
(6, 11, 4, 9, '17', '2017-03-05 00:00:00'),
(7, 11, 2, 9, '58', '2017-03-05 00:00:00'),
(8, 11, 4, 9, '57', '2017-01-31 00:00:00'),
(9, 11, 2, 9, '314', '2017-01-31 00:00:00'),
(10, 11, 4, 9, '55', '2016-12-31 00:00:00'),
(11, 11, 2, 9, '238', '2016-12-31 00:00:00'),
(12, 6, 2, 8, '36', '2017-02-28 00:00:00'),
(13, 6, 2, 8, '0', '2017-03-06 00:00:00'),
(14, 3, 2, 8, '57', '2017-02-28 00:00:00'),
(15, 3, 3, 8, '4', '2017-03-06 00:00:00'),
(16, 10, 2, 8, '1', '2017-03-06 00:00:00'),
(17, 10, 2, 10, '160', '2017-02-28 00:00:00'),
(18, 10, 4, 10, '639', '2017-02-28 00:00:00'),
(19, 10, 4, 10, '89', '2017-02-28 00:00:00'),
(20, 10, 2, 10, '19', '2017-03-05 00:00:00'),
(21, 10, 4, 10, '648', '2017-01-31 00:00:00'),
(22, 10, 2, 10, '137', '2017-01-31 00:00:00'),
(23, 10, 4, 10, '800', '2016-12-31 00:00:00'),
(24, 10, 2, 10, '125', '2016-12-31 00:00:00'),
(25, 12, 2, 8, '0', '2017-03-06 00:00:00'),
(26, 12, 4, 5, '322', '2017-02-28 00:00:00'),
(27, 12, 2, 5, '145', '2017-02-28 00:00:00'),
(28, 12, 4, 5, '60', '2017-03-05 00:00:00'),
(29, 12, 2, 5, '13', '2017-03-05 00:00:00'),
(30, 12, 4, 5, '395', '2017-01-31 00:00:00'),
(31, 12, 2, 5, '130', '2017-01-31 00:00:00'),
(32, 12, 4, 5, '501', '2016-12-31 00:00:00'),
(33, 12, 2, 5, '201', '2016-12-31 00:00:00'),
(34, 15, 2, 8, '0', '2017-03-06 00:00:00'),
(35, 15, 4, 3, '442', '2017-02-28 00:00:00'),
(36, 15, 2, 3, '94', '2017-02-28 00:00:00'),
(37, 15, 4, 3, '59', '2017-03-05 00:00:00'),
(38, 15, 2, 3, '14', '2017-03-05 00:00:00'),
(39, 15, 4, 3, '458', '2017-01-31 00:00:00'),
(40, 15, 2, 3, '87', '2017-01-31 00:00:00'),
(41, 15, 4, 3, '493', '2016-12-31 00:00:00'),
(42, 15, 2, 3, '89', '2016-12-31 00:00:00'),
(43, 14, 2, 8, '0', '2017-02-28 00:00:00'),
(44, 14, 2, 8, '2', '2017-03-06 00:00:00'),
(45, 14, 4, 5, '95', '2017-03-05 00:00:00'),
(46, 14, 2, 5, '19', '2017-03-05 00:00:00'),
(47, 14, 4, 5, '674', '2017-02-28 00:00:00'),
(48, 14, 2, 5, '131', '2017-02-28 00:00:00'),
(49, 14, 4, 5, '740', '2017-01-31 00:00:00'),
(50, 14, 2, 5, '147', '2017-01-31 00:00:00'),
(51, 14, 4, 5, '776', '2016-12-31 00:00:00'),
(52, 14, 2, 5, '145', '2016-12-31 00:00:00'),
(53, 16, 2, 8, '10', '2017-02-28 00:00:00'),
(54, 16, 2, 8, '1', '2017-03-06 00:00:00'),
(55, 16, 4, 3, '159', '2017-02-28 00:00:00'),
(56, 16, 2, 3, '61', '2017-02-28 00:00:00'),
(57, 16, 4, 3, '24', '2017-03-05 00:00:00'),
(58, 16, 2, 3, '11', '2017-03-05 00:00:00'),
(59, 16, 4, 3, '211', '2017-01-31 00:00:00'),
(60, 16, 2, 3, '49', '2017-01-31 00:00:00'),
(61, 16, 4, 3, '185', '2016-12-31 00:00:00'),
(62, 16, 2, 3, '59', '2016-12-31 00:00:00'),
(63, 11, 4, 9, '60', '2016-10-31 00:00:00'),
(64, 11, 2, 9, '255', '2016-10-31 00:00:00'),
(65, 11, 4, 9, '44', '2016-11-30 00:00:00'),
(66, 11, 6, 9, '24', '2016-10-31 00:00:00'),
(67, 11, 2, 9, '263', '2016-11-30 00:00:00'),
(68, 11, 6, 9, '45', '2016-11-30 00:00:00'),
(69, 11, 6, 9, '17', '2016-12-31 00:00:00'),
(70, 11, 6, 9, '14', '2017-01-31 00:00:00'),
(71, 11, 6, 9, '40', '2017-02-28 00:00:00'),
(72, 11, 4, 9, '94', '2017-02-28 00:00:00'),
(73, 11, 2, 8, '72', '2017-01-31 00:00:00'),
(74, 1, 2, 6, '300', '2017-04-20 00:00:00'),
(75, 1, 3, 10, '150', '2017-03-31 00:00:00'),
(76, 1, 6, 4, '900', '2016-12-14 00:00:00'),
(77, 1, 5, 15, '99', '2017-02-15 00:00:00'),
(78, 1, 4, 3, '88', '2017-04-20 00:00:00'),
(79, 1, 1, 2, '222', '2017-04-10 00:00:00'),
(80, 1, 1, 1, '100', '2017-04-12 00:00:00'),
(81, 1, 2, 1, '100', '2017-04-12 00:00:00'),
(82, 1, 3, 1, '100', '2017-04-13 00:00:00'),
(83, 1, 4, 1, '100', '2017-04-11 00:00:00'),
(84, 1, 5, 1, '100', '2017-04-12 00:00:00'),
(85, 1, 6, 1, '100', '2017-04-12 00:00:00'),
(86, 1, 6, 1, '1', '2017-04-26 00:00:00'),
(87, 1, 1, 6, '500', '2017-05-01 00:00:00'),
(88, 40, 1, 2, '4444', '2017-04-27 00:00:00');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `conversion_details`
--
ALTER TABLE `conversion_details`
  ADD PRIMARY KEY (`details_ID`),
  ADD KEY `source_ID` (`source_ID`),
  ADD KEY `cc_ID` (`cc_ID`),
  ADD KEY `dealershipName_ID` (`dealershipName_ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `conversion_details`
--
ALTER TABLE `conversion_details`
  MODIFY `details_ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=89;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `conversion_details`
--
ALTER TABLE `conversion_details`
  ADD CONSTRAINT `conversion_details_ibfk_1` FOREIGN KEY (`source_ID`) REFERENCES `lead_source` (`source_ID`),
  ADD CONSTRAINT `conversion_details_ibfk_2` FOREIGN KEY (`cc_ID`) REFERENCES `conversational_conversions` (`cc_ID`),
  ADD CONSTRAINT `conversion_details_ibfk_3` FOREIGN KEY (`dealershipName_ID`) REFERENCES `dealershipname` (`dealershipName_ID`);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

我需要做的是有一个查询,可以根据日期列中的日期进行这样的计算。 Date2 - date1 / date1 * 100 =两个日期的前提。 date1是从日期范围选择器中选取的实际结束日期。 Date2是开始日期减去1年或1个月,具体取决于月份是否超过月份或年份。 我正在考虑做类似下面的事情,从endDate或Date2开始1年。

    $startDate /*(date2)*/ = $newStartDate;
    $duration = new DateInterval('P1Y'); //1 year..
    $newStartDate -> sub($duration);

所以这将保留1年的日期,因此" sub($ duration)"部分。我会按月做同样的事情。是否有适用于此的查询。因为我一直缺少的那个不起作用。

    $y2y1 = mysqli_query($con,'(((SELECT amount FROM conversion_details WHERE date = "'.$newStartDateDate.'" AND dealershipName_ID = 1 AND cc_ID = 6  ORDER BY date DESC LIMIT 1) - ((SELECT amount FROM conversion_details  WHERE date = "'.$endDate.'" AND dealershipName_ID = 1 AND cc_ID = 6 ORDER BY date DESC LIMIT 1) / (SELECT amount FROM conversion_details WHERE date = "'.$newStartDateDate.'" AND dealershipName_ID = 1 AND cc_ID = 6  ORDER BY date DESC LIMIT 1)))*100)');  // Execute the query

任何帮助都会很棒。也许或者最有可能的是我也想过这个。 IDK的实施非常简单,这就是我要求帮助的原因。提前谢谢。

0 个答案:

没有答案