2015-10-07 64 views
3

輸入如何獲得第一和最後一排每列

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 

HOW TO GET第一個和最後一行每列LIKE

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

在此輸出必須得到第一日期和截止日期group by TYPE.PLEASE HELP ME

回答

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; 
+0

是這個函數真的在mysql中工作嗎?如果是的話,那麼在哪個版本的MySQL? –

+0

以上將在SQL中工作。如果你想要MySQL SELECT column_name FROM table_name ORDER BY column_name ASC LIMIT 1; –

+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); 

我不能給你現在正在用你的新數據得到一個確切的答案,但是我的答案在這裏會給你帶來很大的幫助,你只需要修復一些東西,比如列名,表名。如果您正在測試包含空字符串的第一個和最後一個非NULL值,則我的!ISNULL(...)邏輯正常。如果你正在爲空字符串,而不是測試,使用LENGTH(...)=0代替,或者兩者都是可能的組合:(!ISNULL(...) AND LENGTH(...)>0)

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

+0

非常感謝.... – suri5913

+1

很高興我幫助...請接受答案,如果它幫助你,這樣它會對其他:) – Crazy2crack

0

下面的代碼工作正常,首先我已將日期轉換爲日期格式 select *,str_to_date(date,「%m /%d /%Y」)as tab3;

選擇類型,MIN(DAT)作爲SD,MAX(DAT)作爲ED從GROUP BY TYPE;

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; 
相關問題