获取具有最新生效日期的行

时间:2014-10-12 17:36:19

标签: mysql

嗨我有一张桌子给出了。 我只想获得由parent_type,parent_id组成的结果,结果必须是最新生效日期。

CREATE TABLE `abc` (
              `id` char(36) NOT NULL,
              `name` varchar(150) DEFAULT NULL,
              `date_entered` datetime DEFAULT NULL,
              `date_modified` datetime DEFAULT NULL,
              `modified_user_id` char(36) DEFAULT NULL,
              `created_by` char(36) DEFAULT NULL,
              `assigned_user_id` char(36) DEFAULT NULL,
              `description` text,
              `deleted` tinyint(1) DEFAULT '0',
              `effective_date` date DEFAULT NULL,
              `parent_type` varchar(100) DEFAULT NULL,
              `parent_id` char(36) DEFAULT NULL,
              `share_type` varchar(50) DEFAULT NULL,
              `amount` double DEFAULT '0',
              PRIMARY KEY (`id`),
              KEY `idx_assigned_del` (`assigned_user_id`,`deleted`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
            /*!40101 SET character_set_client = @saved_cs_client */;

            --
            -- Dumping data for table `sharemetas`
            --

            LOCK TABLES `abc` WRITE;
            /*!40000 ALTER TABLE `sharemetas` DISABLE KEYS */;
            INSERT INTO `abc` VALUES ('bd40acfd-cc79-8bd9-85a8-542bbce80bea',NULL,'2014-10-01 08:36:11','2014-10-01 08:36:11','1','1','60beffe2-5168-a3b5-b3e0-53e32109a6b5',NULL,0,'2014-10-01','Bedcharges',NULL,'Percent',10),('be06671f-c0ae-ff9d-cae3-542bbca7ba92',NULL,'2014-10-01 08:36:11','2014-10-01 08:36:11','1','1','60beffe2-5168-a3b5-b3e0-53e32109a6b5',NULL,0,'2014-10-01','Bedcharges','6ebba940-4f7d-f5e4-fa33-53e3600c11e6','Percent',12),('be9adac8-4651-6a18-54f1-542bbc14d95c',NULL,'2014-10-01 08:36:11','2014-10-01 08:36:11','1','1','60beffe2-5168-a3b5-b3e0-53e32109a6b5',NULL,0,'2014-10-01','Bedcharges','a573477d-6756-3e62-9582-53e3619f975a','Rupees',22),('bf12ec5d-9a31-ec98-4cf0-542bec172c2e',NULL,'2014-10-01 12:00:31','2014-10-01 12:00:31','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-10-01','Billabletreatmentcharges',NULL,'Percent',10),('bfc707eb-6f5b-fdab-5c34-542becaa31b3',NULL,'2014-10-01 12:00:31','2014-10-01 12:00:31','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-10-01','Charges',NULL,'Percent',15),('c05961fa-b635-b141-0664-542bece3572e',NULL,'2014-10-01 12:00:31','2014-10-01 12:00:31','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-10-01','Labamounts',NULL,'Percent',20),('c0e78f31-d619-c029-1603-542beca7f69a',NULL,'2014-10-01 12:00:31','2014-10-01 12:00:31','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-10-01','Vaccinationamounts',NULL,'Percent',25),('c1a27b80-0d9c-0c20-da90-542bec18d38f',NULL,'2014-10-01 12:00:31','2014-10-01 12:00:31','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-10-01','Visitcharges',NULL,'Percent',30),('bf141f2b-78d4-b6d0-0f73-542bed015448',NULL,'2014-10-01 12:01:19','2014-10-01 12:01:19','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-08-01','Billabletreatmentcharges',NULL,'Percent',5),('bfdba18d-7e6c-1388-f3d1-542bedb7613d',NULL,'2014-10-01 12:01:19','2014-10-01 12:01:19','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-08-01','Charges',NULL,'Percent',10),('c0a6fc38-0615-440a-2664-542bed92ff8c',NULL,'2014-10-01 12:01:19','2014-10-01 12:01:19','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-08-01','Labamounts',NULL,'Percent',15),('c1372f58-572a-4df1-3424-542bed6e3419',NULL,'2014-10-01 12:01:19','2014-10-01 12:01:19','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-08-01','Vaccinationamounts',NULL,'Percent',20),('c1e2f1e6-cadd-f01f-d06f-542bed9cefbc',NULL,'2014-10-01 12:01:19','2014-10-01 12:01:19','1','1','136050e0-0835-6def-204d-53e335191a46',NULL,0,'2014-08-01','Visitcharges',NULL,'Percent',25),('591f912c-2e4e-0798-9965-54322d9b2b3f',NULL,'2014-10-06 05:47:48','2014-10-06 05:47:48','1','1','aae43bba-4c44-c47b-dc3f-542936f8cd05',NULL,0,'2014-10-06','Charges','6b51c07d-d0a5-44d8-03ec-53e345366677','Percent',10),('65bdb62f-abee-9ca4-64d6-54322d5ce98a',NULL,'2014-10-06 05:47:48','2014-10-06 05:47:48','1','1','aae43bba-4c44-c47b-dc3f-542936f8cd05',NULL,0,'2014-10-06','Charges','5193d15f-43c3-600f-280a-54009bc440d1','Percent',20),('3298ac05-d47b-8bef-e9ec-5432316a9f9e',NULL,'2014-10-06 06:06:17','2014-10-06 06:06:17','1','1','aae43bba-4c44-c47b-dc3f-542936f8cd05',NULL,0,'2014-10-06','Charges',NULL,'Percent',5);

我的查询如下

SELECT  `id` ,  `effective_date` ,  `parent_type` ,  `parent_id` ,  `share_type` ,  `amount` 
FROM  `abc` 
WHERE 1 
 GROUP BY  `parent_type` ,  `parent_id` 
LIMIT 0 , 30

但我没有按最新生效日期获得金额和身份

2 个答案:

答案 0 :(得分:1)

就是这样:

SELECT  `id` ,  MAX(`effective_date`) as effective_date ,  `parent_type` ,  `parent_id` ,  `share_type` ,  `amount` 
FROM  `abc` 
WHERE 1 
 GROUP BY  `parent_type` ,  `parent_id` 
LIMIT 0 , 30

玩得开心

答案 1 :(得分:1)

有不同的方法。以下是not exists方法:

SELECT  `id` ,  `effective_date` ,  `parent_type` ,  `parent_id` ,  `share_type` ,  `amount` 
FROM  `abc` 
WHERE NOT EXISTS (SELECT 1
                  FROM abc abc2
                  WHERE abc2.parent_id = abc.parent_id and abc2.parent_type = abc.parent_type and
                        abc2.effective_date > abc.effective_date
                 );
LIMIT 0 , 30;

为了提高性能,您需要abc(parent_id, parent_type, effective_date)上的索引。