Solved

SQL Query Pattern in DB2

Posted on 2009-07-02
16
490 Views
Last Modified: 2013-12-07
Hello
I need a SQL Output as below

Company        Name          Age
A                     abc              31
                       bcd              33
                       xyz              34
B                     123              44
                       234             55
                       345              33

Please help
0
Comment
Question by:Senz79
  • 9
  • 7
16 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24769740
what do you mean by this output?
which version of db2 are you using?
you can try this:

select company1 as company, name, age
from (
  select *, case when rown=1 then company else '' end as company1 from (
  select company, name, age, rownumber() over(partition by company) as rown
  from yourTable ) t1
  ) t2
order by company,rown
0
 
LVL 4

Author Comment

by:Senz79
ID: 24769772
can you help me with the fields below

DATE ('20' || substr("Timestamp",2,2) || '-' || substr("Timestamp",4,2) || '-' || substr("Timestamp",6,2)) as "Date",
TIME (substr("Timestamp",8,2) || '.' || substr("Timestamp",10,2) || '.' || substr ("Timestamp",12,2)) as "Time",
"Server_Name"  from "NT_System_H"

based on the server_name i want to group the result.

0
 
LVL 4

Author Comment

by:Senz79
ID: 24769774
i am using DB2 version 9
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24769800
when you want to group by server_name you need to decide which  function to apply to every other column such as date - the min / max / avg function?
0
 
LVL 4

Author Comment

by:Senz79
ID: 24769815
I didnt get that ...... In case i want a output pattern i discussed.... how can that help
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24769822
can you post here the output you got?
0
 
LVL 4

Author Comment

by:Senz79
ID: 24770143
I wanted a output like

Company        Name          Age
A                     abc              31
                       bcd              33
                       xyz              34
B                     123              44
                       234             55
                       345              33
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24770209
i know that
but what is the output you get from my statement?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Author Comment

by:Senz79
ID: 24770378
db2 => select "Server_Name" , "LAT_User_Name" from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name" from (se
lect "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name") as "rown" from "NT_System_H" ) t1) t2 order by "Serv
er_Name","rown"

SQL0206N  "LAT_User_Name" is not valid in the context where it is used.
SQLSTATE=42703
db2 =>

0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24770477
try

select "Server_Name" , "LAT_User_Name"
from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name", *
 from (select "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name" order by "LAT_User_Name") as "rown" from "NT_System_H" ) t1) t2
order by "Server_Name","rown"
0
 
LVL 4

Author Comment

by:Senz79
ID: 24770500
db2 => select "Server_Name" , "LAT_User_Name" from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name", * from
(select "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name" order by "LAT_User_Name") as "rown" from "NT_Syste
m_H" ) t1) t2 order by "Server_Name","rown"

SQL0104N  An unexpected token "*" was found following "nd as "Server_Name",".
Expected tokens may include:  "<select_sublist>".  SQLSTATE=42601
db2 =>
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24770515
select "Server_Name1" , "LAT_User_Name"
from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name1", "Server_Name" , "LAT_User_Name", rown
from
(select "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name" order by "LAT_User_Name") as "rown" from "NT_Syste
m_H" ) t1) t2 order by "Server_Name","rown"
0
 
LVL 4

Author Comment

by:Senz79
ID: 24770530
db2 => select "Server_Name1" , "LAT_User_Name" from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name1", "Serv
er_Name" , "LAT_User_Name", rown from (select "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name" order by "LA
T_User_Name") as "rown" from "NT_System_H" ) t1) t2 order by "Server_Name","rown"

SQL0206N  "ROWN" is not valid in the context where it is used.  SQLSTATE=42703
db2 =>
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 250 total points
ID: 24770539
select "Server_Name1" , "LAT_User_Name","rown" from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name1", "Serv
er_Name" , "LAT_User_Name", rown from (select "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name" order by "LA
T_User_Name") as "rown" from "NT_System_H" ) t1) t2 order by "Server_Name","rown"

0
 
LVL 4

Author Comment

by:Senz79
ID: 24770547
still the same error
db2 => select "Server_Name1" , "LAT_User_Name","rown" from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name1"
, "Server_Name" , "LAT_User_Name", rown from (select "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name" order
 by "LAT_User_Name") as "rown" from "NT_System_H" ) t1) t2 order by "Server_Name","rown"
SQL0206N  "ROWN" is not valid in the context where it is used.  SQLSTATE=42703
db2 =>

0
 
LVL 4

Author Comment

by:Senz79
ID: 24770556
select "Server_Name1" , "LAT_User_Name","rown" from ( select case when "rown"=1 then "Server_Name" else '' end as "Server_Name1", "Server_Name" , "LAT_User_Name", "rown" from (select "Server_Name" , "LAT_User_Name", rownumber() over(partition by "Server_Name" order by "LAT_User_Name") as "rown" from "NT_System_H" ) t1) t2 order by "Server_Name","rown"


GOT IT !!!! THANKS A MILLION :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now