How to achieve this result using sql query?
hello gurus,
i have a table like this
id name
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
11 k
12 l
13 m now my result should be like this
id name id name id name
1 a 6 f 11 k
2 b 7 g 12 l
3 c 8 h 13 m
4 d 9 i
5 e 10 jhow to achieve it by sql query ?
thanks and regards,
friend
Edited by: most wanted!!!! on Feb 22, 2012 5:55 AM
hi,
Did you mean this:
with a as
(select 1 id ,'a' name from dual
union all select 2 id ,'b' name from dual
union all select 3 id ,'c' name from dual
union all select 4 id ,'d' name from dual
union all select 5 id ,'e' name from dual
union all select 6 id ,'f' name from dual
union all select 7 id ,'g' name from dual
union all select 8 id ,'h' name from dual
union all select 9 id ,'i' name from dual
union all select 10 id ,'j' name from dual
union all select 11 id ,'k' name from dual
union all select 12 id ,'l' name from dual
union all select 13 id ,'m' name from dual
select
id_1
,name_1
,id_2
,name_2
,id_3
,name_3
from
select
id id_1
,name name_1
,lead(id,5) over (order by id) id_2
,lead(name,5) over (order by id) name_2
,lead(id,10) over (order by id) id_3
,lead(name,10) over (order by id) name_3
,rownum r
from
a
where
r <=5
D_1 NAME_1 ID_2 NAME_2 ID_3 NAME_3
1 a 6 f 11 k
2 b 7 g 12 l
3 c 8 h 13 m
4 d 9 i
5 e 10 j Regards,
Peter
Similar Messages
-
How to get this output using sql query?
Hi,
How to get this output using sql query?
Sno Name Age ADD Result
1 Anil 23 delhi Pass
2 Shruti 25 bangalor Pass
3 Arun 21 delhi fail
4 Sonu 23 pune Pass
5 Roji 26 hydrabad fail
6 Anil 28 delhi pass
Output
Sno Name Age ADD Result
1 Anil 23 delhi pass
28 delhi passHi Vamshi,
Your query is not pretty clear.
write the select query using Name = 'ANIL' in where condition and display the ouput using Control-break statements.
Regards,
Kannan -
How to hide repeated details using SQL Query?
How to hide repeated details using SQL Query?
For Ex:
------------------------+
DEPTNO| ENAME | JOB |
------|-------| --------|
10 | JAMES | MANAGER |
10 | BLAKE | CLERK |
10 | FORD | SALESMAN|
20 | SCOTT | MANAGER |
20 | ADAMS | CLERK |
20 | KING | SALESMAN|
------------------------+
How we can display the above details in the following way?
------------------------+
DEPTNO| ENAME | JOB |
------|-------| --------|
10 | JAMES | MANAGER |
| BLAKE | CLERK |
| FORD | SALESMAN|
20 | SCOTT | MANAGER |
| ADAMS | CLERK |
| KING | SALESMAN|
------------------------+
Thanks AdvanceHi,
you can use BREAK ON DEPTNO in SQL*Plus or use LAG.
SQL> ed
Wrote file afiedt.buf
1 select nullif(department_id
2 , lag(department_id) over (partition by department_id order by last_name)
3 ) dept_id
4 , last_name, job_id
5* from employees where department_id in (30,50) and rownum <=10
SQL> /
DEPT_ID LAST_NAME JOB_ID
30 Baida PU_CLERK
Colmenares PU_CLERK
Himuro PU_CLERK
Khoo PU_CLERK
Raphaely PU_MAN
Tobias PU_CLERK
50 Fripp ST_MAN
Kaufling ST_MAN
Vollman ST_MAN
Weiss ST_MAN
10 rows selected. -
How to pass a result of SQL query to shell script variable
Hi all,
I am trying to pass the result of a simple SQL query that only returns a single row into the shell script variable ( This particular SQL is being executed from inside the same shell script).
I want to use this value of the variable again in the same shell scirpt by opening another SQL plus session.
I just want to have some values before hand so that I dont have to do multiple joins in the actual SQL to process data.Here an example :
SQL> select empno,ename,deptno from emp;
EMPNO ENAME DEPTNO
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10
14 rows selected.
SQL> select * from dept;
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
$ cat my_shell.sh
### First query #####
ENAME_DEPTNO=`sqlplus -s scott/tiger << EOF
set pages 0
select ename,deptno from emp where empno=$1;
exit
EOF`
ENAME=`echo $ENAME_DEPTNO | awk '{print $1}'`
DEPTNO=`echo $ENAME_DEPTNO | awk '{print $2}'`
echo "Ename = "$ENAME
echo "Dept = "$DEPTNO
### Second query #####
DNAME_LOC=`sqlplus -s scott/tiger << EOF
set pages 0
select dname,loc from dept where deptno=$DEPTNO;
exit
EOF`
DNAME=`echo $DNAME_LOC | awk '{print $1}'`
LOC=`echo $DNAME_LOC | awk '{print $2}'`
echo "Dept Name = "$DNAME
echo "Dept Location = "$LOC
$ ./my_shell.sh 7902
Ename = FORD
Dept = 20
Dept Name = RESEARCH
Dept Location = DALLAS
$ -
How to insert the records using sql query
hi,
i insert the record uisng sql query and DOTNET programme successfully and increase the doc num .ubut when i try to add record using SAP B1 the old Doc no show.It means its not consider the docnums which are not inserted by sql query.
how can i solve this problem?
Regards,
M.Thippa ReddyYou are not support use Insert / Update / Delete on SAP Databases directly.
SAP will not support any database, which is inconsistent, due to SQL-Queries, which modify datasets or the datastructure of the SAP Business One Database. This includes any update-, delete- or drop-statements executed via SQL-Server Tools or via the the query interface of SAP Business One
Check SAP Note: 896891 Support Scope for SAP Business One - DB Integrity
[https://websmp130.sap-ag.de/sap(bD1lbiZjPTAwMQ==)/bc/bsp/spn/smb_searchnotes/display.htm?note_langu=E¬e_numm=896891] -
How to create this CSV in Sql query?
I have a order_detail table with the primary key order_detail_id and the foregin keys order_id and product_id. Also I have three other fields, A, B and C, to describe the nature of this order detail. A product_id can be existed on different order_id
but not in a single order_id (ie no duplicate product_id on a order). What I want the dataset to return is when passing the order_id, it could be single or multiple order_id, the query will give me the order_detail_id in comma seperate string if two or more
of the same product_id found on the multiple order_ids and the same value on the other three fields, A, B and C, ie I would like to group by all the order_detail_id in a single row if those conditions are met. Thanks a million.
KahluaPlease post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums.
>> I have a order_detail table with the PRIMARY KEY order_detail_id and the foreign keys order_id and product_id. <<
WRONG. Order details are called weak entities in data modeling. They need a strong entity (the order) to exist. Therefore, the key has to include the order id as part of the PRIMARY KEY. There is no separate “order_detail_id” in a correct model.
>> Also I have three other fields [sic], A, B and C, to describe the nature of this order detail. A product_id can existed on different order_id but not in a single order_id (i.e. no duplicate product_id on a order). <<
Columns are not anything like fields. You need to learn basic terms. Here is what a polite person might have posted:
CREATE TABLE Orders
(order_nbr CHAR(15) NOT NULL PRIMARY KEY,
CREATE TABLE Order_Details
(order_nbr CHAR(15) NOT NULL
REFERENCES Orders(order_nbr)
ON DELETE CASCADE,
product_id CHAR(15) NOT NULL,
PRIMARY KEY (order_nbr, product_id),
order_qty INTEGER DEFAULT 1 NOT NULL
CHECK (order_qty > 0),
-- special attributes
alpha CHAR(5) NOT NULL,
beta CHAR(5) NOT NULL,
gamma CHAR(5) NOT NULL,
>> What I want the data set to return is when passing the order_id, it could be single or multiple order_id, the query will give me the order_detail_id in comma separate string if two or more of the same product_id found on the multiple order_ids and
the same value on the other three fields [sic], A, B and C, i.e. I would like to group by all the order_detail_id in a single row if those conditions are met. <<
WRONG! You want to violate First Normal Form (1NF) and do display formatting in the database! You have not ever read a book or had a class on RDBMS. CSV? No!
Now, where is the sample data? More bad Netiquette! I have done this before; you can read the details at:
http://www.tdan.com/view-perspectives/5343
It is a special case of a relational division. Since you did not post DDL, I will not post DML.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
How to achieve this result ?
I've two date. I want to know the month and year name and the days between this two.
Like. First date is 10/01/2012
Second date is 20/04/2012
I want..
January 2012 21 Days
February 2012 29 Days
March 2012 31 Days
April 2012 20 DaysAny idea ?Hi,
In addition to Frank's solution, you can also do it as follow so you don't have to group by :SQL> l
1 select
2 to_char(add_months(trunc(to_date('&&dstart.','dd/mm/yyyy'),'mm'),level-1),'fmMonth yyyy') mthy
3 ,least(
4 last_day(add_months(trunc(to_date('&&dstart.','dd/mm/yyyy'),'mm'),level-1))
5 ,to_date('&&dend.','dd/mm/yyyy')
6 )
7 - greatest(
8 add_months(trunc(to_date('&&dstart.','dd/mm/yyyy'),'mm'),level-1)-1
9 ,to_date('&&dstart.','dd/mm/yyyy')
10 ) days
11 from dual
12* connect by level <= trunc(months_between(to_date('&&dend.','dd/mm/yyyy'), to_date('&&dstart.','dd/mm/yyyy')))+1
SQL> define dstart="10/01/2012"
SQL> define dend="20/04/2012"
SQL> /
MTHY DAYS
Janvier 2012 21
Fevrier 2012 29
Mars 2012 31
Avril 2012 20
4 ligne(s) selectionnee(s).
SQL> define dstart="10/01/2012"
SQL> define dend="20/01/2012"
SQL> /
MTHY DAYS
Janvier 2012 10
1 ligne selectionnee.
SQL> define dstart="10/01/2012"
SQL> define dend="20/03/2013"
SQL> /
MTHY DAYS
Janvier 2012 21
Fevrier 2012 29
Mars 2012 31
Avril 2012 30
Mai 2012 31
Juin 2012 30
Juillet 2012 31
Aout 2012 31
Septembre 2012 30
Octobre 2012 31
Novembre 2012 30
Decembre 2012 31
Janvier 2013 31
Fevrier 2013 28
Mars 2013 20
15 ligne(s) selectionnee(s).There is 1 row generated for each month in the interval instead of 1 for each day.
That shouldn't be a big benefit, unless the interval spans several centuries !
:-) -
How to achieve this by using flex feild
Hi, I am using financial tax register report in r12 etax module
when am entering parameters,
I have one flex feild for jurisdiction, based on this value two parameters are enabling, if I enter this value this is getting enabled and getting corresponding values,
If I don't enter this I want all values to be displayed,
and I have below code snippet in my form
where jurisdiction_Code=nvl(:$flex$.jurisdiction,table_jurisdiction)
but it is not happening,
Your help is appreciated,
Thanks in advancebut my requirement is not give a static value there right?
my requirement is
where jurisdiction_Code=nvl(:$flex$.jurisdiction,jurisdiction_Code)
should be worked. and if it is null corresponding parameters should get enabled. -
How to achieve this result? Colored object, rest is B&W
Where an object is in color and the rest is in black & white?
In photoshop elements 9 an easy way is to use the Smart Brush Tool.
Pick one the Black and white presets, then paint on the areas in the image you want black and white.
To add to the area pick the brush with + sign, to subtract from the area use the brush with the - sign.
You can change to any of the other black and white presets to see how they look and if the smart brush
tool won't select or deselect some areas easily, you can use the Detail Smart Brush Tool to select or
deselect those areas. (click on the screenshot below to enlarge)
For more questions about photoshop elements, the folks over on the photoshop elements forum could probably answer them better
since elements doesn't have the same features as the full photoshop version.
http://forums.adobe.com/community/photoshop_elements -
How to achieve this effect using Adobe Photoshop Touch?
Hi,
I'm new to photoshop of all kinds, as you'll soon learn, so I struggle to work out what effect(s), if any, certain images I find online have. At the moment I am trying to work out what effect(s) the images on the below links have, can anyone help? I assume they are the same effects on different images.
https://pbs.twimg.com/media/B6h7rb_IEAAXR3a.png
https://pbs.twimg.com/media/B6ipKsDIUAAmMOU.png
https://pbs.twimg.com/media/B6iRV_XCIAAcHM6.png
https://pbs.twimg.com/media/B6iQBbWCMAAlCD2.png
https://pbs.twimg.com/media/B6iOePUCIAAy7tV.png
Thanks in advance!Are you asking about the gradient mask towards the bottom of the images?
You can try inserting a new layer then use More (looks like an ampersand or "&"; it's the 2nd icon from the upper right) > Gradient. Use a linear gradient. Tap and drag the gradient to where you want it. Tap and drag the end handles on the gradient to adjust where you want the gradient to begin and end.
Tap the palette icon to change colors, opacity and add/subtract stops. -
Two different results using one query
Hi Friends
Oracle version that I am using is : Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
I have a scenario where one account can be related to two customers. Hence, in table have many rows for one account. Here’s the sample data:
Account_ID | product_code | cust_id | relationship_code | entity_code
1111 | ABC | 1234 | SOL | CUST
1111 | ABC | 2222 | ZZZ | LINK
1111 | ABC | 4455 | ABC | LINK
2222 | ABC | 7890 | SOL | CUST
2222 | ABC | 5678 | ZZZ | LINK
3333 | JFK | 5878 | TST | CUST
3333 | JFK | 3254 | PRI | CUST
3333 | JFK | 3299 | PRI | CUST
4444 | JFK | 2535 | SOL | CUST
4444 | JFK | 4565 | SOL | CUST
5555 | DEF | 6666 | PRI | CUST
5555 | DEF | 6667 | TST | CUST
5555 | DEF | 9667 | TST | CUSTIn this scenario, I need two outputs differently:
Output 1: When an account has relationship_code = ‘SOL’ then take the least cust_id
1111 | ABC | 1234 | SOL | CUST
2222 | ABC | 5678 | ZZZ | LINK
4444 | JFK | 2535 | SOL | CUST Output 2: else take the highest cust_id
3333 | JFK | 5878 | TST | CUST
5555 | DEF | 9667 | TST | CUSTHow can I get this result using one query?Not sure what you mean. Works OK:
SQL> with t as (
2 select 1111 account_ID,'ABC' product_code,1234 cust_id,'SOL' relationship_code,'CUST' entity_code from dual union all
3 select 1111,'ABC',2222,'ZZZ','LINK' from dual union all
4 select 1111,'ABC',4455,'ABC','LINK' from dual union all
5 select 2222,'ABC',7890,'SOL','CUST' from dual union all
6 select 2222,'ABC',5678,'ZZZ','LINK' from dual union all
7 select 3333,'JFK',5878,'TST','CUST' from dual union all
8 select 3333,'JFK',3254,'PRI','CUST' from dual union all
9 select 3333,'JFK',3299,'PRI','CUST' from dual union all
10 select 4444,'JFK',2535,'SOL','CUST' from dual union all
11 select 4444,'JFK',4565,'SOL','CUST' from dual union all
12 select 5555,'DEF',6666,'PRI','CUST' from dual union all
13 select 5555,'DEF',6667,'TST','CUST' from dual union all
14 select 5555,'DEF',9667,'TST','CUST' from dual union all
15 select 6666,'XYZ',8877,'SOL','CUST' from dual
16 )
17 select account_ID,
18 product_code,
19 cust_id,
20 relationship_code,
21 entity_code
22 from (
23 select account_ID,
24 product_code,
25 cust_id,
26 relationship_code,
27 entity_code,
28 case max(case relationship_code when 'SOL' then 1 else 0 end) over(partition by account_ID)
29 when 1 then dense_rank() over(partition by account_ID order by cust_id)
30 else dense_rank() over(partition by account_ID order by cust_id desc)
31 end rnk
32 from t
33 )
34 where rnk = 1
35 /
ACCOUNT_ID PRO CUST_ID REL ENTI
1111 ABC 1234 SOL CUST
2222 ABC 5678 ZZZ LINK
3333 JFK 5878 TST CUST
4444 JFK 2535 SOL CUST
5555 DEF 9667 TST CUST
6666 XYZ 8877 SOL CUST
6 rows selected.
SQL> SY. -
How to Achieve this in SQL Query?
How to Achieve this ?
I have a table with numeric value populated like this
create table random_numeral (numerals Number(10));
insert into random_numeral values (1);
insert into random_numeral values (2);
insert into random_numeral values (3);
insert into random_numeral values (4);
insert into random_numeral values (5);
insert into random_numeral values (6);
insert into random_numeral values (56);
insert into random_numeral values (85);
insert into random_numeral values (24);
insert into random_numeral values (11);
insert into random_numeral values (120);
insert into random_numeral values (114);
Numerals
1
2
3
4
5
6
56
85
24
11
120
114
I want to display the data as follows
col1 / col2 / col3
1 / 2 / 3
4 / 5 / 6
11 / 24 / 56
85 / 114 / 120
Can anyone Help me?I hope there might be some simple way to do this and waiting for experts to reply.
Try the below query.
SQL> select * from random_numeral;
NUMERALS
1
2
3
4
5
6
56
85
24
11
120
NUMERALS
114
100
140
14 rows selected.
SQL> select a.numerals ||' / '||b.numerals||' / '||c.numerals from
2 (select numerals,rownum rn1 from
3 (
4 select numerals,mod(row_number() over(partition by 1 order by numerals),3)
5 from random_numeral
6 )
7 where rn=1) a,
8 (select numerals,rownum rn1 from
9 (
10 select numerals,mod(row_number() over(partition by 1 order by numerals),3)
11 from random_numeral
12 )
13 where rn=2) b,
14 (select numerals,rownum rn1 from
15 (
16 select numerals,mod(row_number() over(partition by 1 order by numerals),3)
17 from random_numeral
18 )
19 where rn=0) c
20 where a.rn1=b.rn1(+)
21 and b.rn1=c.rn1(+)
22 /
A.NUMERALS||'/'||B.NUMERALS||'/'||C.NUMERALS
1 / 2 / 3
4 / 5 / 6
11 / 24 / 56
85 / 100 / 114
120 / 140 /
SQL>Cheers,
Mohana -
How to create a Matrix table using this data in SQL Query Analyzer
Hello all,
I have a problem while I am trying to represent my Sql Table namely table1 in Matrix form
my table Format is
city1 city2 Distance--------------------------------------------------------
Mumbai Delhi 100
Delhi Banaras 50
Mumbai Rajasthan 70
Banaras haryana 40
Mumbai Mumbai 0
784 entries
there are 784 cities each having link to other
Now i want my output as
Mumbai Delhi Banaras haryana
Mumbai 0 100 -- --
Delhi 100 0 50 --
Banaras
haryana
respective distance from one city to other should be shown
final Matrix would be 784*784
I am using SQL Query Analyser for this
Please help me in this regardI'm pretty much certain that you don't want to do this in pure SQL. So that means that you want to do it with a reporting tool. I'm not familiar with SQL Query Analyzer, but if it is in fact a reporting tool you'll want to consult its documentation looking for the terms "pivot" or perhaps "cross tab."
-
How to compare result from sql query with data writen in html input tag?
how to compare result
from sql query with data
writen in html input tag?
I need to compare
user and password in html form
with all user and password in database
how to do this?
or put the resulr from sql query
in array
please help me?Hi dejani
first get the user name and password enter by the user
using
String sUsername=request.getParameter("name of the textfield");
String sPassword=request.getParameter("name of the textfield");
after executeQuery() statement
int exist=0;
while(rs.next())
String sUserId= rs.getString("username");
String sPass_wd= rs.getString("password");
if(sUserId.equals(sUsername) && sPass_wd.equals(sPassword))
exist=1;
if(exist==1)
out.println("user exist");
else
out.println("not exist"); -
How to get tax break up of TDS using SQL query ?
Hi all,
We are developing a TDS report using SQL query
Report will contain VendorCode,Date(ap inv date),Vendor name,
Bill value,TDS Amount,
Bill Value 100.000,
TDS (2%) - 2.000,
TDS Surcharge(10% on TDS) - 0.2,
TDS Cess(2%(TDS+TDS Surcharge)) - 0.044,
TDS HeCess(1%(TDS+TDS Surcharge)) - 0.022.
We have developed this report which displays upto
VendorCode,Date(ap inv date),Vendor name,
Bill value,TDS Amount.
How to show tax break up of TDS in SQL query ?
Thanks,
With regards,
Jeyakanthan.Hi gauraw,
Thank for your reply.
I modified the query , pasted the query
as below in query generator,
Select T0.DocNum,T0.DocDate,T0.CardCode as 'Ledger',T1.TaxbleAmnt As 'Bill value',T1.WTAmnt as 'TDSAmt',(TDSAmt * 0.1) as 'TDS_Surch',
(((TDSAmt0.1) + TDSAmt)0.02) as 'TDSCess',
(((TDSAmt0.1) + TDSAmt)0.01) as 'TDSHCess'
FROM OPCH T0 INNER JOIN PCH5 T1 ON T0.DocEntry = T1.AbsEntry
WHERE (T0.DocDate >= '[%0]' and T0.DocDate <= '[%1]')
on clicking execute its showing error message invalid column
name 'TDSAmt'.
With regards,
Jeyakanthan
Maybe you are looking for
-
Pass exit code from batch file to MDT 2012?
So i have batch file that creates some directories, sets up the path for log files before i install an application ( msi installer). It goes something like this : MD C:\mylogfolders Set logfilename = C:\mylogfolders\appl1-date.log msiexec /i app1.msi
-
SQL, static classes and sessionBeans
Hi, my JSF-project has one static class where it handle all sql-queries and I asked me if that is the "best" solution. I.e. a User-Sessionbean call that static class to save his data into the user-sql-table. I asked myself, if it might not better to
-
Cs5.5 install hangs at 99%
Moving from vista to win8.1 computer, installing cs5.5 install hung at 99% with error msg saying to close iexplore even when iexplore and all related processes were closed per Task manager. Please don't advise to open Task Mangaer and close related
-
Nikon D300 auto / manual focus metadata
Hello, When viewing photos in my D300 (i.e. using the D300 to view photos I have just taken), I can see displayed whether the photo was taken using auto or manual focus. I have been unable to see this information after importing the photos from the c
-
Error 3200 when updating iPhone 4 to iOS 5.
Need help.