gpt4 book ai didi

sql - 获取字段首次更改为其当前值的日期

转载 作者:行者123 更新时间:2023-11-29 05:44:01 25 4
gpt4 key购买 nike

用语言表达是一件相当尴尬的事情,但我想要在项目首次切换到关税/客户时的行。如果一个项目转换了客户,那么转换的日期应该被返回,不管关税有什么不同。如果一个项目切换项目,返回的日期不得改变,除非新项目的关税与旧项目不同。

不太确定如何让它更清楚,但我愿意接受建议。

我的查询是这样的:

SET @id = 1;
SELECT DISTINCT
ip.ItemID,
ip.ProjectID,
p.TariffID,
p.CustomerID,
cs.Date
FROM item_project ip
LEFT JOIN item_project ip1
ON ip.ItemID = ip1.ItemID
AND ip.Date < ip1.Date
LEFT JOIN project p
ON ip.ProjectID = p.ProjectID
LEFT JOIN (
SELECT
ip.ItemID,
ip.Date
FROM item_project ip
LEFT JOIN item_project ip1
ON ip.ProjectID = ip1.ProjectID
AND ip.Date > ip1.Date
LEFT JOIN project p
ON ip.ProjectID = p.ProjectID
WHERE ip.ItemID = @id
AND ip1.ItemID IS NULL
AND p.CustomerID = (
SELECT p.CustomerID
FROM project p
LEFT JOIN item_project ip
ON p.ProjectID = ip.ProjectID
LEFT JOIN item_project ip1
ON ip.ItemID = ip1.ItemID
AND ip.Date < ip1.Date
WHERE ip.ItemID = @id
AND ip1.ItemID IS NULL
)
AND p.TariffID = (
SELECT p.TariffID
FROM project p
LEFT JOIN item_project ip
ON p.ProjectID = ip.ProjectID
LEFT JOIN item_project ip1
ON ip.ItemID = ip1.ItemID
AND ip.Date < ip1.Date
WHERE ip.ItemID = @id
AND ip1.ItemID IS NULL
)
) AS cs
ON ip.ItemID = cs.ItemID
WHERE ip.ItemID = @id
AND ip1.ItemID IS NULL

这给了我

"ItemID","ProjectID","TariffID","CustomerID","Date"
"1","2","1","1","2010-11-10 00:00:00"

日期不对

SET @id=2 给我:

"2","2","1","1",NULL

哪个是正确的,除了日期

SET @id=3 给我:

"3","2","1","1",NULL

这也是正确的,除了日期。

这是数据库

CREATE TABLE IF NOT EXISTS `item_project` (
`ID` int(10) unsigned NOT NULL auto_increment,
`ItemID` varchar(10) NOT NULL,
`ProjectID` int(10) unsigned NOT NULL,
`Date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

INSERT INTO `item_project` (`ID`, `ItemID`, `ProjectID`, `Date`) VALUES
(1, '1', 1, '2010-11-05 00:00:00'),
(2, '1', 2, '2010-11-10 00:00:00'),
(3, '1', 3, '2010-11-20 00:00:00'),
(4, '2', 2, '2010-11-21 00:00:00'),
(5, '3', 4, '2010-11-21 00:00:00'),
(6, '3', 2, '2010-11-22 00:00:00'),
(7, '1', 2, '2010-11-23 00:00:00'),

CREATE TABLE IF NOT EXISTS `project` (
`ProjectID` int(10) unsigned NOT NULL auto_increment,
`Name` varchar(45) NOT NULL,
`TariffID` varchar(45) NOT NULL,
`CustomerID` varchar(45) NOT NULL,
PRIMARY KEY (`ProjectID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

INSERT INTO `project` (`ProjectID`, `Name`, `TariffID`, `CustomerID`) VALUES
(1, 'Test', '2', '1'),
(2, 'Another test', '1', '1'),
(3, 'Project1', '1', '1'),
(4, 'Main project', '2', '2');

CREATE TABLE IF NOT EXISTS `tariff` (
`TariffID` int(10) unsigned NOT NULL auto_increment,
`Tariff` varchar(45) NOT NULL,
PRIMARY KEY (`TariffID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

INSERT INTO `tariff` (`TariffID`, `Tariff`) VALUES
(1, 'Tariff 1'),
(2, 'Tariff 2');

编辑:项目可以返回到旧关税或客户。在这种情况下,显示的日期应该是发生这种情况的日期。

最佳答案

有趣的情况。这是我想出的

SELECT i_general.ItemID, ProjectID, TariffID, CustomerID, the_date
FROM
(
SELECT
ip.ItemID,
p.ProjectID,
p.TariffID,
p.CustomerID
FROM item_project ip
INNER JOIN project p ON ip.ProjectID = p.ProjectID
INNER JOIN tariff t ON p.TariffID = t.TariffID
INNER JOIN (
SELECT
ip.ItemID, MAX(Date) AS max_date
FROM item_project ip
GROUP BY ip.ItemID
) ip_max ON ip_max.ItemID=ip.ItemID AND ip_max.max_date=ip.Date
) i_general
INNER JOIN (
SELECT ip1.ItemID, IF(MIN(ip2.Date) IS NULL,MIN(ip1.Date),MIN(ip2.Date)) AS the_date
FROM item_project ip1
INNER JOIN project p1 ON ip1.ProjectID = p1.ProjectID
LEFT JOIN item_project ip2 ON ip1.ItemID=ip2.ItemID AND ip1.Date < ip2.Date
LEFT JOIN project p2 ON ip2.ProjectID = p2.ProjectID AND (p2.TariffID!=p1.TariffID OR p2.CustomerID!=p1.CustomerID)
GROUP BY ip1.ItemID
) i_date_info ON i_date_info.ItemID = i_general.ItemID

当然,您可以根据需要插入一些WHERE ItemID = @id。内部查询越多越好。

无论如何,它会导致

+--------+-----------+----------+------------+---------------------+
| ItemID | ProjectID | TariffID | CustomerID | the_date |
+--------+-----------+----------+------------+---------------------+
| 1 | 3 | 1 | 1 | 2010-11-10 00:00:00 |
| 2 | 2 | 1 | 1 | 2010-11-21 00:00:00 |
| 3 | 2 | 1 | 1 | 2010-11-22 00:00:00 |
+--------+-----------+----------+------------+---------------------+

因此,它似乎适用于现有数据集。让我知道您是否可以提供它不起作用的测试数据。

关于sql - 获取字段首次更改为其当前值的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4284693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com