Solved

SQL Query Pattern in DB2

Posted on 2009-07-02
16
489 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

762 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

23 Experts available now in Live!

Get 1:1 Help Now