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
joaotellesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
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.
johnsoneSenior Oracle DBACommented:
If you are looking for a shell script that would generate the query for you, then this would be the start of it.
#!/bin/sh

smlmax=5
cnt=1
qry="select to_char(smwhen,'YYYYMMDDHH24') as "'"'"DateHour"'"'","
while [ $cnt -le $smlmax ]
do
  qry="${qry} count( case when smstatus = 200000042 and sml = ${cnt} then 1 else null end ) as "'"'"SM${cnt}_Delivered"'"'","
  qry="${qry} unt( case when smstatus = 200000051 and sml = ${cnt} then 1 else null end ) as "'"'"SM${cnt}_OnSMSC"'"'","
  cnt=`expr $cnt + 1`
done
qry="${qry} col2 from tab..."
echo ${qry}

Open in new window

From there you can pass the query to SQL*Plus or just use it as a query generator and past it somewhere yourself.  You can also have the value passed in.  This just gives you the beginning of the script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joaotellesAuthor Commented:
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....
slightwv (䄆 Netminder) Commented:
>>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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.