Link to home
Start Free TrialLog in
Avatar of joaotelles
joaotellesFlag for United States of America

asked on

Shell/SQL - Iterations for a query

Hi,

I have the following query:

select to_char(smwhen,'YYYYMMDDHH24') 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,'YYYYMMDDHH24')
      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,'YYYYMMDDHH24') 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
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

If you are asking about dynamically generating the counts pivoted based on the number of rows, then no, you cannot do this in SQL

You cannot dynamically create columns at runtime.

If you have a maximum of 10, you'll need to create 10 entries in the query.
ASKER CERTIFIED SOLUTION
Avatar of johnsone
johnsone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joaotelles

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,'YYYYMMDDHH24') 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,'YYYYMMDDHH24') 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

Nothing to move.  It is already in the Shell Scripting Topic Area.