Concatenate multiple column into single in a resulted query
문제
I need a help in adding multiple column in single (DB2) after a resulted query
My resulted query looks like,
EMPI HRS MTS SDS
-------------------
sam 12 10 10
tukai 10 05 02
Now, I want this output instead:
empid Totaltimetaken
----------------------
sam 12:10:10
tukai 10:05:02
First query:
SELECT
empid
,TOTALSECONDS/3600 AS HRS
,(MOD(TOTALSECONDS, 3600) /60) AS MTS
,(MOD(TOTALSECONDS, 60)) AS SDS
FROM
(SELECT
SUM(duration) AS TOTALSECONDS
,empid
FROM table
GROUP BY empid)
From the above query result, I now want to add the columns: empid, hrs, mts, sds
.
I used this query but not getting result. Any help...
SELECT
TMP1.emp
,('0'||(TOTALSECONDS.TMP1)/3600)||':'||
('0'||(MOD(TOTALSECONDS.TMP1),3600)/60) ':'||
MOD(TOTALSECONDS.TMP1),60) AS TOTALTIMETAKEN
,TMP1.TOTALSECONDS
FROM
(SELECT
EMPID emp,
SUM(DURATION) AS TOTALSECONDS
FROM table
GROUP BY EMPID) TMP1
This is for IBM DB2.
해결책
You should be using TMP1 as a prefix-
SELECT TMP1.emp, TMP1.TOTALSECONDS,
('0' || (TMP1.TOTALSECONDS) / 3600) || ':' ||
('0' || (MOD(TMP1.TOTALSECONDS, 3600) / 60) ':' ||
MOD(TMP1.TOTALSECONDS, 60) AS TOTALTIMETAKEN
FROM (SELECT EMPID emp, SUM(DURATION) AS TOTALSECONDS
FROM table
GROUP BY EMPID) TMP1
다른 팁
This should get you on the right track....
Select One, Two
from Table
will result in 2 columns that will look like
|One| |Two|
1 2
-
Select Cast(One as varchar(30)) + '_' + Cast(Two as varchar(30)) as 'Concatenated'
from Table
result will look like
|Concatenated|
1_2
*Note this is for SQL Server
제휴하지 않습니다 StackOverflow