joaotelles
asked on
Shell/SQL - Iterations for a query
Hi,
I have the following query:
select to_char(smwhen,'YYYYMMDDHH 24') as "DateHour",
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM1_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM1_OnSMSC"
from ts_shoge
group by to_char(smwhen,'YYYYMMDDHH 24')
order by 1;
But I can have variable numbers of sml 1 to 10 for example (never skipping a number)
So I would like for this query to run for as many sml I have... and changing the "as" part as well to reflect the sml number.. like if I have sml 1 and 2:
select to_char(smwhen,'YYYYMMDDHH 24') as "DateHour",
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM2_OnSMSC"
count( case when smstatus = 200000042 and sml = 2 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 2 then 1 else null end ) as "SM2_OnSMSC"
.
.
Is this possible with only a query change or it needs to be a shell script loop somehow?
Tks,
Joao
I have the following query:
select to_char(smwhen,'YYYYMMDDHH
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM1_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM1_OnSMSC"
from ts_shoge
group by to_char(smwhen,'YYYYMMDDHH
order by 1;
But I can have variable numbers of sml 1 to 10 for example (never skipping a number)
So I would like for this query to run for as many sml I have... and changing the "as" part as well to reflect the sml number.. like if I have sml 1 and 2:
select to_char(smwhen,'YYYYMMDDHH
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM2_OnSMSC"
count( case when smstatus = 200000042 and sml = 2 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 2 then 1 else null end ) as "SM2_OnSMSC"
.
.
Is this possible with only a query change or it needs to be a shell script loop somehow?
Tks,
Joao
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tks slightwv...
I think then I will move this question only to the shell scripting area, because this number can vary, so I need something that will dynamically go through all the sml values I have and run the query for each of them..
Like run on time for this:
select to_char(smwhen,'YYYYMMDDHH 24') as "DateHour",
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM2_OnSMSC"
.
.
Other for this:
select to_char(smwhen,'YYYYMMDDHH 24') as "DateHour",
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM2_OnSMSC"
.
.
In a loop....
I think then I will move this question only to the shell scripting area, because this number can vary, so I need something that will dynamically go through all the sml values I have and run the query for each of them..
Like run on time for this:
select to_char(smwhen,'YYYYMMDDHH
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM2_OnSMSC"
.
.
Other for this:
select to_char(smwhen,'YYYYMMDDHH
count( case when smstatus = 200000042 and sml = 1 then 1 else null end ) as "SM2_Delivered",
count( case when smstatus = 200000051 and sml = 1 then 1 else null end ) as "SM2_OnSMSC"
.
.
In a loop....
>>I think then I will move this question only to the shell scripting
Nothing to move. It is already in the Shell Scripting Topic Area.
Nothing to move. It is already in the Shell Scripting Topic Area.
You cannot dynamically create columns at runtime.
If you have a maximum of 10, you'll need to create 10 entries in the query.