MySQL CASE使用如何删除重复的空列

时间:2015-01-19 02:10:58

标签: java mysql

我有MySQL问题。我创建此查询但它显示如下;我在MySQL中使用了case函数。

这是代码

SELECT DISTINCT 
    attendancehistory.CustomerFacilityId,
    (CASE '01' WHEN SUBSTRING(`Date`, 8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS '01',
(CASE '02' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS '02',
(CASE '03' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS '03',
(CASE '04' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS '04',
(CASE '05' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS '05',
(CASE '06' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS '06'
FROM 
    attendancehistory 
WHERE 
    `Employee id`='1' 
GROUP BY 
    Id;

像这样输出

CuFId | 01  |  02 |  03 |  04  |  05
1     | D   | Null| Null|  Null| Null
533   | N   | Null| Null| Null | Null
533   | Null| D   | Null| Null | Null
533   | Null| Null| N   | Null | Null
533   | Null| Null| Null| Null | D

但实际上我需要这样

CuFId | 01  | 02  | 03  | 04  | 05
1     |  D  | Null| Null| Null| Null
533   | N   |  D  | N   | Null| D

以下是示例代码:

CREATE TABLE `attendancehistory` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `LoginId` int(11) NOT NULL DEFAULT '0',
  `Employee id` int(10) NOT NULL DEFAULT '0',
  `CustomerFacilityId` int(11) NOT NULL DEFAULT '0',
  `Date` varchar(50) DEFAULT NULL,
  `Day` varchar(11) DEFAULT NULL,
  `Night` varchar(11) DEFAULT NULL,
  `EmpAttendaceTotalMonth` varchar(11) DEFAULT NULL,
  `EntryDate` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`Id`),
  KEY `FKAttendance494326` (`LoginId`),
  KEY `FKAttendance87961` (`CustomerFacilityId`),
  CONSTRAINT `FKAttendance494326` FOREIGN KEY (`LoginId`) REFERENCES `login` (`Id`),
  CONSTRAINT `FKAttendance87961` FOREIGN KEY (`CustomerFacilityId`) REFERENCES `customerfacility` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;

/*Data for the table `attendancehistory` */

insert  into `attendancehistory`
  (`Id`,`LoginId`,`Employee id`,`CustomerFacilityId`,`Date`,`Day`,`Night`,`EmpAttendaceTotalMonth`,`EntryDate`) values 
  (3,1,1,1,'2015-1-01','1','0','2015-1','2015-01-14'),
  (4,1,1,533,'2015-1-01','0','1','2015-1','2015-01-14'),
  (5,1,1,533,'2015-1-02','1','1','2015-1','2015-01-14'),
  (6,1,1,533,'2015-1-03','0','1','2015-1','2015-01-14'),
  (7,1,1,533,'2015-1-05','1','0','2015-1','2015-01-14');

1 个答案:

答案 0 :(得分:1)

据推测,你想要这个:

SELECT ah.CustomerFacilityId,
       MAX( CASE '01' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS `01`,
      MAX(CASE '02' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS `02`,
      MAX(CASE '03' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS `03`,
      MAX(CASE '04' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS `04`,
      MAX(CASE '05' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS `05`,
      MAX(CASE '06' WHEN SUBSTRING(`Date`,8) THEN IF(`Day`='1','D',IF(Night=1,'N','-'))  ELSE NULL END) AS `06`
FROM attendancehistory ah
WHERE `Employee id` = 1
GROUP BY Id;

请注意,不应对列名或整数常量使用单引号。此外,您可以通过使用case语句来简化此逻辑,但是如果没有样本数据和/或SQL小提琴,尝试更改它很容易出错。