i wanna change some rows to columns.
This is a example how i have my database now
And i wanna change it by
But i'm using a ODBC connection, if you could help me with a useful example or code guide i'll appreciate it.
Thanks
You can use a MAX/CASE "manual pivot" that should work on most (all?) SQL databases, something like;
SELECT "datetime",
MAX(CASE WHEN bsc='bsccc2' THEN ineffective_attempts END) bsccc2,
MAX(CASE WHEN bsc='bsccc3' THEN ineffective_attempts END) bsccc3,
MAX(CASE WHEN bsc='bsccc4' THEN ineffective_attempts END) bsccc4,
MAX(CASE WHEN bsc='bscmb2' THEN ineffective_attempts END) bscmb2,
MAX(CASE WHEN bsc='bscmbo' THEN ineffective_attempts END) bscmbo,
MAX(CASE WHEN bsc='bscva2' THEN ineffective_attempts END) bscva2
FROM mytable
GROUP BY "datetime"
ORDER BY "datetime"
Depending on the underlying database, you may want to change the quoting of the datetime identifier (or possibly rename the field to avoid needing to quote it).
Related
I have event status table, the table structure is already defined, I would like to make several row value into one row.
current table data:
eventable:
EVENT EVENTNAME EVENTACTION STATUS TIMESTAMP
"Event1" "FootBall" "FootBall0" "started" "1554980085"
"Event1" "TableTennis" "TT0" "started" "1554980086"
"Event1" "FootBall" "FootBall0" "success" "1554980087"
"Event1" "Chess" "CHESS0" "success" "1554980095"
"Event1" "TableTennis" "TT0" "running" "1554980189"
"Event1" "FootBall" "FootBall1" "started" "1554980082"
I need to select query to display the data as follows:
EVENT EVENTNAME EVENTACTION STATUS STARTTIME RUNNINGTIME ENDTIME
"Event1" "FootBall" "FootBall0" "success" 1554980085 NULL 1554980087
"Event1" "TableTennis" "TT0" "running" 1554980086 1554980189 NULL
"Event1" "Chess" "CHESS0" "success" 1554980098 NULL 1554980100
I have to show the EVENT, EVENTNAME, EVENTACTION and latest STATUS and timestamp of started, running and success, If the time is not there then should set NULL.
I have used SELECT query with case but the timestamps doesn't place in single the relevant row.
SELECT AE.EVENT,
AE.EVENTNAME,
AE.EVENTACTION,
AE.STATUS,
(case WHEN AE.STATUS='started' THEN AE.timestamp END) as starttime,
(case WHEN AE.STATUS='running' THEN AE.timestamp END) as runningtime,
(case WHEN AE.STATUS in ('success','failed') THEN AE.timestamp END) as endtime
FROM agentEvents AE
ORDER by timestamp ASC
Any leads.
You need to aggregate by event, and then take the MAX of the CASE expressions:
SELECT
AE.EVENT,
AE.EVENTNAME,
AE.EVENTACTION,
MAX(CASE WHEN AE.STATUS = 'started' THEN AE.timestamp END) AS starttime,
MAX(CASE WHEN AE.STATUS = 'running' THEN AE.timestamp END) AS runningtime,
MAX(CASE WHEN AE.STATUS IN ('success', 'failed') THEN AE.timestamp END) AS endtime
FROM agentEvents AE
GROUP BY
AE.EVENT,
AE.EVENTNAME,
AE.EVENTACTION
ORDER BY
MAX(CASE WHEN AE.STATUS = 'started' THEN AE.timestamp END);
Note that it makes no sense to order by the timestamp column alone, because it no longer exists after aggregation. So, I order by the starting time instead. It also makes no sense to select the STATUS column, so I have removed it.
Found related type solutions, but nothing to match my need;
I want to update an existing table record (fields pu_cnt, pu_ot, pu_ltc etc.) with the result from the query (pu_cnt, pu_ot, pu_ltc etc.) etc.
Here's my query:
SELECT
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('C','D','H','F') THEN 1 END) AS
pu_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE '' THEN 1 END) AS
pu_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'C' THEN 1 END)
AS pu_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'U' THEN 1 END)
AS pu_ltu,
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('D','H','F') THEN 1 END) AS del_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE '' THEN 1 END) AS
del_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'C' THEN 1 END)
AS del_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'U' THEN 1 END)
AS del_ltu
FROM vuWO_CHC_ALL WHERE
RTRIM(vuWO_CHC_ALL.SHIPPER) LIKE #pVendor AND
RTRIM(vuWO_CHC_ALL.ORIG) LIKE #pOrigin AND
RTRIM(vuWO_CHC_ALL.Dest) LIKE #pDestin
..
Figured it out using #variables.
Need your expertise in writing an SQL for the below scenario
I have a single row in a table "range_num" as follows.
start_num end_num
10 14
Is there any way we can write a query to find all the gaps between 10 and 14 i ..e 11,12,13 in the below format
col1 col2 col3 col4 col5
10 11 12 13 14
I am using Teradata databases , so any compliant SQL query will be of great help!!
Here is solution for your problem...
select
max(case when rownum=1 then gap end) as col1,max(case when rownum=2 then gap end) as col2,
max(case when rownum=3 then gap end) as col3,max(case when rownum=4 then gap end) as col4,
max(case when rownum=5 then gap end) as col5,max(case when rownum=6 then gap end) as col6,
max(case when rownum=7 then gap end) as col7,max(case when rownum=8 then gap end) as col8,
max(case when rownum=9 then gap end) as col9,max(case when rownum=10 then gap end) as col10,
max(case when rownum=11 then gap end) as col11,max(case when rownum=12 then gap end) as col12,
max(case when rownum=13 then gap end) as col13,max(case when rownum=14 then gap end) as col14,
max(case when rownum=15 then gap end) as col15,max(case when rownum=16 then gap end) as col16,
max(case when rownum=17 then gap end) as col17,max(case when rownum=18 then gap end) as col18,
max(case when rownum=19 then gap end) as col19,max(case when rownum=20 then gap end) as col20,
max(case when rownum=21 then gap end) as col21,max(case when rownum=22 then gap end) as col22,
max(case when rownum=23 then gap end) as col23
from
(
select a.id_start,
b.day_of_calendar as gap,
csum(1,1) as rownum
from db_sok.testt a,
sys_calendar.calendar b
where b.day_of_calendar between a.id_start and a.id_end
)X
do a little tweaking in case you need something else...:)
I'm trying to export the result of a query in db2 to file, but the result like:
+000000869836.|+00000000000001401.
+000000978137.|+00000000000000511.
What can I do to obtain the following result:
869836.|1401.
978137.|511.
My current query:
EXPORT TO /tmp/tmp.tmp OF del modified by NOCHARDEL coldel|
select
iv.bpd_instance_id,
max(case when alias = 'abc' then INT_VALUE else null end) as A,
max(case when alias = 'xyz' then STRING_VALUE else null end) as X,
from lsw_bpd_instance_variables iv
where bpd_instance_id in ($instancesDB)
group by iv.bpd_instance_id
with ur
The default behavior of DB2's EXPORT utility is to format any DECIMAL value with a leading positive/negative sign and pad it with leading zeros. To override this behavior, specify the DECPLUSBLANK and STRIPLZEROS options in your MODIFIED BY clause, or CAST the DECIMAL values to some other type in your SELECT statement.
I have a table value in sqlite like..
i'm getting value in sqlite query like this..
select * from tablename where id='101' and id='102' and id='1' and id='18'
101 Local Local Local Local Local Local
102 9 12 9 12 9
1 3:55 4:20 4:40 5:00 5:20 5:40
18 4:50 5:15 5:35 5:55 6:15 6:35
Above value I need to convert in sqlite query like below..
Local 9 3:55 4:50
Local 12 4:20 5:15
Local 9 4:40 5:35
Local 12 5:00 5:55
Local 9 5:20 6:15
Local 9 5:40 6:35
Oh, so you're stuck with a terrible DB design which you aren't allowed to change... that sucks. It ought to be divided into 4 tables (at least). Anyway, here is one way to do it in this particular case.
I assume the first query is equivalent to this:
SELECT id, F1, F2, F3, F4, F5, F6
FROM tablename
WHERE id IN (101,102,1,18)
So, you can do transposing/pivoting/whatever it's actually called like this:
SELECT
MAX(CASE WHEN id=101 THEN F1 END) R1,
MAX(CASE WHEN id=102 THEN F1 END) R2,
MAX(CASE WHEN id=1 THEN F1 END) R3,
MAX(CASE WHEN id=18 THEN F1 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F2 END) R1,
MAX(CASE WHEN id=102 THEN F2 END) R2,
MAX(CASE WHEN id=1 THEN F2 END) R3,
MAX(CASE WHEN id=18 THEN F2 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F3 END) R1,
MAX(CASE WHEN id=102 THEN F3 END) R2,
MAX(CASE WHEN id=1 THEN F3 END) R3,
MAX(CASE WHEN id=18 THEN F3 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F4 END) R1,
MAX(CASE WHEN id=102 THEN F4 END) R2,
MAX(CASE WHEN id=1 THEN F4 END) R3,
MAX(CASE WHEN id=18 THEN F4 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F5 END) R1,
MAX(CASE WHEN id=102 THEN F5 END) R2,
MAX(CASE WHEN id=1 THEN F5 END) R3,
MAX(CASE WHEN id=18 THEN F5 END) R4
FROM tablename
UNION
SELECT
MAX(CASE WHEN id=101 THEN F6 END) R1,
MAX(CASE WHEN id=102 THEN F6 END) R2,
MAX(CASE WHEN id=1 THEN F6 END) R3,
MAX(CASE WHEN id=18 THEN F6 END) R4
FROM tablename
Unfortunately, it's sorted in a strange way.
And I really advise you to persuade someone in charge to re-design the DB. Some designers think that having tables with such structure is "flexible" and "improves performance", but it's not so: doing actual work with such tables requires highly non-trivial queries, which most of DBMSs cannot optimize because they were never expected to deal with such atrocities.
Such tables also break the very idea of the relational model. Each table must have one-statement "template", for example, CREATE TABLE S (id INTEGER, name TEXT, address TEXT) can have template "A supplier with id ID is called NAME and is located at ADDRESS". Now, each row in this table yields you a true statement about the world when you put its contents into the template: if S would contain a row (1, "Bob & Co.", "Lime St., 125"), then it would tell you that "A supplier with id 1 is called Bob & Co. and is located at Lime St., 125".
All relational operations: selection, projection, joins, filtering, etc. don't just combine tables in a particular way, no! They also combine "templates", so you can say what exactly information a query gives you. And vice versa, if you can express what information you want through combining those "templates", it would almost automatically give you a corresponding query.
And the table you have to work with can be associated with no sane "template". That's why you have to write nonsensical queries in order to get something useful. Note that I can basically guess the "template" of the resulting set,
R1 R2 R3 R4
============================
Local 9 3:55 4:50
...
It's something like "An event of type R2 was happening at place R1, starting at time R3 and ending at time R4". Am I correct?