Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Query Pattern in DB2

Posted on 2009-07-02
16
Medium Priority
?
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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
 
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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

610 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