如何获取每列的第一行和最后一行

时间:2015-10-07 05:48:00

标签: mysql

输入

DATE    TRANSACTION    TYPE
may01      22           ATM
jun18      34           ATM
Aug14      38           NB
jul18      46           NB
Sep11      29           NB
Dec21      70           NB
jan02      46           MobileB
Jun19      20           MobileB
Sep13      81           MobileB

如何获得第一个和最后一行的每个栏目

TYPE        Start_DATE   End_DATE
ATM          may01       jun18
NB           Aug14       Dec21
MobileB      jan02       Sep13 

在此输出中必须按TYPE.PLEASE HELP ME

获取第一个日期和最后一个日期组

5 个答案:

答案 0 :(得分:1)

在你大规模修改你的问题之前,我会建议:

SELECT 'COL1' AS `COL_NAMES`,(SELECT `c1` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c1`) ORDER BY c1 ASC LIMIT 1) AS `ROW_START`,(SELECT `c1` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c1`) ORDER BY c1 DESC LIMIT 1) AS `ROW_END`
UNION ALL
SELECT 'COL2' AS `COL_NAMES`,(SELECT `c2` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c2`) ORDER BY c2 ASC LIMIT 1) AS `ROW_START`,(SELECT `c2` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c2`) ORDER BY c2 DESC LIMIT 1) AS `ROW_END`
UNION ALL
SELECT 'COL3' AS `COL_NAMES`,(SELECT `c3` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c3`) ORDER BY c3 ASC LIMIT 1) AS `ROW_START`,(SELECT `c3` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c3`) ORDER BY c3 DESC LIMIT 1) AS `ROW_END`
UNION ALL
SELECT 'COL4' AS `COL_NAMES`,(SELECT `c4` as `START_ROW` FROM `tbl` WHERE !ISNULL(`c4`) ORDER BY c4 ASC LIMIT 1) AS `ROW_START`,(SELECT `c4` as `END_ROW` FROM `tbl` WHERE !ISNULL(`c4`) ORDER BY c4 DESC LIMIT 1) AS `ROW_END`

假设一个表格如下:

CREATE TABLE IF NOT EXISTS `tbl` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) DEFAULT NULL,
  `c3` int(11) DEFAULT NULL,
  `c4` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `tbl` (`c1`, `c2`, `c3`, `c4`) VALUES
(1, 30, 89, 34),
(2, 49, 76, 44),
(NULL, 52, 90, NULL),
(NULL, NULL, 16, NULL);

现在,我现在无法使用您的新数据给您一个确切的答案,但我的答案应该会让您非常接近,您只需要修改一些内容,例如列名称,表名。如果您正在测试第一个和最后一个非!ISNULL(...)值(包括空字符串),那么我的NULL逻辑就可以了。如果您正在测试空字符串,请改为使用LENGTH(...)=0或两者兼而有之:•(!ISNULL(...) AND LENGTH(...)>0)

答案 1 :(得分:1)

您可以使用GROUP_CONCATSUBSTRING_INDEX获取第一行和最后一行,如下所示

select type,
SUBSTRING_INDEX(GROUP_CONCAT(CAST(date AS CHAR) ORDER BY date), ',', 1 ) as Start_date,
SUBSTRING_INDEX(GROUP_CONCAT(CAST(date AS CHAR) ORDER BY date DESC), ',', 1 ) as End_date
from test
group by type;

点击此处SQL Fiddle DEMO

答案 2 :(得分:0)

在SQL中,有预定义的函数,用于根据列名

获取第一行和最后一行记录
SELECT FIRST(column_name) FROM table_name;
SELECT LAST(column_name) FROM table_name; 

在MySQL中

SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;



 SELECT column_name FROM table_name
    ORDER BY column_name DESC
    LIMIT 1;

答案 3 :(得分:0)

以下代码工作正常,首先我将日期转换为日期格式 选择*,str_to_date(日期,"%m /%d /%Y")作为来自tab3的数据;

SELECT TYPE,MIN(DAT)AS SD,MAX(DAT)AS FROM FROM GROUP BY TYPE;

答案 4 :(得分:0)

select
*
from
(select * from application, (SELECT @rownums:=0) r order by  (@rownum := @rownum + 1) asc limit 1) a1

union

select
*
from
(select * from application, (SELECT @rownum:=0) r1 order by  (@rownum := @rownum + 1) desc limit 1) a2;