Want a SQL query
Hi All
This is mu use case
1. I have a table called Trades which stores Trades from different clients
2. In Trade Table I have a field called 'TRADE_FEE_REF' which store all the fees for a trade as a reference
3. In table 'TRADE_FEE_DETAILS' it stores the fees related to a perticular trade
4. Trades and TRADE_FEE_DETAILS tables have a foriegn key.
5. Now I want to display the following result for user.
I want a single row for Trades which includes Fee details as columns in that selection
ex: This is a Row from Trade table
Date, quantity, Price, Free_ref_no
1/1/2011, 2000, 12.45 , 1
This is the records from TRADE_FEE_DETAILS for the perticular trade above
Fee_Ref, Fee_type,Fee_amount
1 ,BR, 200
1 ,ST,300
1 ,STT,100
now I want to display the result as follow
Date, quantity, Price, BR,ST,STT
1/1/2011, 2000, 12.45,200,300,100
How do I retrieve this result from sql query
Thnks
Padma
You can use PIVOT function to convert rows to columns
with trade as
select to_DATE('1-JAN-2011','DD-MON-YYYY') col_date, 2000 quantity, 12.45 Price, 1 Free_ref_no from dual
trade_fee_details as
select 1 Fee_Ref, 'BR' Fee_type, 200 Fee_amount from dual union all
select 1, 'ST', 300 from dual union all
select 1, 'STT', 100 from dual
SELECT
td.col_date,
td.quantity,
td.price,
tf.BR,
tf.ST,
tf.STT
FROM
trade td,
SELECT
FROM
SELECT
FROM
trade_fee_details
PIVOT (SUM(Fee_amount) FOR Fee_type IN ( 'BR' AS BR,'ST' AS ST,'STT' AS STT ))
) tf
WHERE
td.Free_ref_no = tf.fee_ref;
COL_DATE QUANTITY PRICE BR ST STT
01-JAN-11 00:00:00 2000 12.45 200 300 100
Similar Messages
-
I want a sql query to diffrentiate the column names between two schemas
The structure of the dba_tab_cols table is owner, table_name, column_name, data_type,.....
So I have a ACCT_ALT_ID table under owner SEODS01 and in that therse are column names
IBD_ID
ACCT_ALT_ID_CNTX_CDE
EODS_ACCT_ID
DATA_GRP_CDE
UPDT_DTE
AND I have a ACCT_ALT_ID table under owner SEODS02 and in that therse are column names
IBD_ID
ACCT_ALT_ID_CNTX_CDE
EODS_ACCT_ID
DATA_GRP_CDE
CRTE_PGM
CRTE_TSTP
UPDT_PGM
UPDT_TSTP
so I want select query to display like this
SEODS1_ACCT_ALT_ID SEODS02_ACCT_ALT_ID (COLUMN HEADER)
IBD_ID IBD_ID
ACCT_ALT_ID_CNTX_CDE ACCT_ALT_ID_CNTX_CDE
EODS_ACCT_ID EODS_ACCT_ID
DATA_GRP_CDE DATA_GRP_CDE
UPDT_DTE CRTE_PGM
CRTE_TSTP
UPDT_PGM
UPDT_TSTP
AND ALSO I NEED ONE MORE QUERY THAT I DO NOT WANT DISPLAY HAVING SAME COLUMN_NAME LIKE ACCT_ALT_ID_CNTX_CDE ,EODS_ACCT_ID,IBD_ID
DATA_GRP_CDEHi,
Welcome to the forum!
876793 wrote:
The structure of the dba_tab_cols table is owner, table_name, column_name, data_type,.....
So I have a ACCT_ALT_ID table under owner SEODS01 and in that therse are column names
IBD_ID
ACCT_ALT_ID_CNTX_CDE
EODS_ACCT_ID
DATA_GRP_CDE
UPDT_DTE
AND I have a ACCT_ALT_ID table under owner SEODS02 and in that therse are column names
IBD_ID
ACCT_ALT_ID_CNTX_CDE
EODS_ACCT_ID
DATA_GRP_CDE
CRTE_PGM
CRTE_TSTP
UPDT_PGM
UPDT_TSTP
so I want select query to display like this
SEODS1_ACCT_ALT_ID SEODS02_ACCT_ALT_ID (COLUMN HEADER)
IBD_ID IBD_ID
ACCT_ALT_ID_CNTX_CDE ACCT_ALT_ID_CNTX_CDE
EODS_ACCT_ID EODS_ACCT_ID
DATA_GRP_CDE DATA_GRP_CDE
UPDT_DTE CRTE_PGM
CRTE_TSTP
UPDT_PGM
UPDT_TSTPYou may have noticed that this site normally doesn't display multiple spaces in a row.
Whenever you post formatted text (such as query results) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
That way, your message will look like this:SEODS1_ACCT_ALT_ID SEODS02_ACCT_ALT_ID (COLUMN HEADER)
IBD_ID IBD_ID
ACCT_ALT_ID_CNTX_CDE ACCT_ALT_ID_CNTX_CDE
EODS_ACCT_ID EODS_ACCT_ID
DATA_GRP_CDE DATA_GRP_CDE
UPDT_DTE CRTE_PGM
CRTE_TSTP
UPDT_PGM
UPDT_TSTP
This sounds like a jon for a FULL OUTER JOIN:SELECT s1.column_name AS seods1_acct_alt_id
, s2.column_name AS seods2_acct_alt_id
FROM dba_tab_cols s1
FULL OUTER JOIN dba_tab_cols s2 ON s1.column_name = s2.column_name
WHERE s1.owner = 'SEODS1'
AND s1.table_name = 'ACCT_ALT_ID'
AND s2.owner = 'SEODS2'
AND s2.table_name = 'ACCT_ALT_ID'
AND ALSO I NEED ONE MORE QUERY THAT I DO NOT WANT DISPLAY HAVING SAME COLUMN_NAME LIKE ACCT_ALT_ID_CNTX_CDE ,EODS_ACCT_ID,IBD_ID
DATA_GRP_CDESorry, I don;t understand.
Post the results you want (formatted, between \ tags, of course).
Do you want only the column names that appear in one table or the other, but not in both?
In that case, you can add a condition like this to the WHERE clause:
AND ( s1.column_name IS NULL
OR s2.column_name IS NULL
) -
I want SQL query to check if any of our important concurrent programs
I want a sql query to check if any of our important concurrent programs are currently scheduled or not
SinghP wrote:
I want a sql query to check if any of our important concurrent programs are currently scheduled or notThat's nice,
What scheduling tool are you using? are you referring to dbms_job?
what version of Oracle are you using? are you using some custom job management system that involves writing to user created tables in your database? -
Hi,
I want a sql query as follows (but it seems not be as simple as that...)
select A1, A2,
select sum(B3)
from B1 = A2
where
from A
where A1 = '1001'
is it possible, in some way, to have A2 from outer select statement as a parameter to inner select statement?Hi,
user8819407 wrote:
Hi,
I want a sql query as follows (but it seems not be as simple as that...)
select A1, A2,
select sum(B3)
from B1 = A2
where
from A
where A1 = '1001'
is it possible, in some way, to have A2 from outer select statement as a parameter to inner select statement?Yes, you can reference columns from a parent query within its child query.
For example:
select A1, A2,
select sum (b.B3)
from b
where b.B1 = a.A2
from A
where A1 = '1001'
;Within the sub-query, you don't have to qualify columns from b; that is, b3 is assumed to mean b.b3. If the column name is unique, you don't have to qualify columns from table a, either. That is, if b does not have a column called a2, then a2 in the sub-query could only mean a.a2, and Oracle doesn't make you spell out which table a2 comes from, but it makes the code a lot easier to understand and debug if you do qualify the columns.
By the way, a sub-query that references its parent query like this is called a Correlated Sub-Query .
"Parameter" doesn't really apply to this situation. -
Neea a sql query for inverting the table
Hi,
I am facing the problem in forming a SQL statement in oracle 10g
Table structure:
user field1 field2 field3 field4 field5 field6 .......
one 11 12 13 14 15 16
two 21 22 23 24 25 26
three 31 32 33 34 35 36
i want a SQL query where the out put is
field1 11 21 31
field2 12 22 32
field3 13 23 33
field4 14 24 34
Regards,
Balu CHcheck this Forums page
<http://forums.oracle.com/forums/search.jspa?threadID=&q=Rows+to+Columns&objID=f75&dateRange=last90days&userID=&numResults=15>
will help u.
Rgds
Sudar -
HI
I want a sql query i.e., all employees list which are having lessthan maximum salary of manager in same departmnet;
my table is like this
employees
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
if need the department table
departments:
DEPARTMENT_ID
NOT NULL NUMBER(4)
DEPARTMENT_NAME
NOT NULL VARCHAR2(30)
MANAGER_ID
NUMBER(6)
LOCATION_ID
NUMBER(4)Try this:
select
employees.last_name || ', ' || employees.first_name “Employee”,
employees.salary “Emp Salary”,
sub.salary “Mgr Salary”,
employees.department_id “Department”
from
employees,
( select
mgr.department_id dept,
max(mgr.salary) salary
from
employees,
employees mgr
where
employees.mgr = mgr.employee_id
group by
mgr.department_id) sub
where
employees.department_id = sub.dept
and employees.salary < sub.salary
Jeff -
Facing problem in constructing a sql query
I want a SQL Query through which I can Select all the values in a column of a table( which is Primary Key and another field in the table refers this field) I want to select all the values of the primary key which are either reffered by a particular value of the referring field or all the parents corrosponing to the value or their parents or their parents.... until I get a value which doen't refers any value.
I want a SQL Query through which I can Select all the values in a column of a table( which is Primary Key and another field in the table refers this field) I want to select all the values of the primary key which are either reffered by a particular value of the referring field or all the parents corrosponing to the value or their parents or their parents.... until I get a value which doen't refers any value. Hi
Did u try using Self Join
I think that might solve the problem
Sri -
Need a SQL query(Pls Its urgent)
Hi,
I want a SQL query for the foll..
Details r below..
Table AFPFTRAN
BU TRansactiondate Amt
13202 10-04-05 10,000
13203 11-05-05 20,000
13202 20-04-05 5,000
Table AFGENCOD
BU Clerk Name Clerk Code
13202 Amit TFBG
13203 Anand TFMG
I want a query to get data as below..
Tr Date TFBG TFMG
Apr 15,000 0
May 0 20,000
JUn 0 0
Jul 0 0
Aug
Sep
Aug
Nov
DEc
Jan
Feb
Mar
I want this to achieve using a single query...?How can I achive this?
Pls give the sol...
Adios...
Prashanth DeshmukhDoes it look like you need ?
SQL> select * from t;
BU TDATE AMT
13202 10.04.05 10000
13203 11.05.05 20000
13202 20.04.05 5000
SQL> select * from t1;
BU NAME CODE
13202 Amit TFBG
13203 Anand TFMG
SQL> SELECT pivot_month.mname, tops.TFBG, tops.TFMG
2 from
3 (
4 select to_char(t.tdate,'MM') month_no,
5 sum(decode(t1.code,'TFBG',t.amt,0)) TFBG,
6 sum(decode(t1.code,'TFMG',t.amt,0)) TFMG
7 from t1, t
8 where t.bu=t1.bu
9 and to_char(t.tdate,'yyyy') = 2005
10 GROUP BY t1.code, to_char(t.tdate,'MM')
11 ) tops,
12 (select rownum mn, to_char(to_date(rownum,'MM'),'MON') mname,
13 case when rownum between 1 and 3 then rownum + 9
14 else rownum - 3 end rn
15 from dict where rownum < 13) pivot_month
16 where pivot_month.mn = tops.month_no (+)
17 order by pivot_month.rn
18 /
MNA TFBG TFMG
APR 15000 0
MAY 0 20000
JUN
JUL
AUG
SEP
OCT
NOV
DEC
JAN
FEB
MAR
12 rows selected.Rgds. -
SQL query to find sqlnet.ora parameters
Hi All,
I am working 11g RAC /solaris 9
I want have sql query to find values of sqlnet.ora parameters
May be values of SQLNET.INBOUND_CONNECT_TIMEOUT
SQLNET.SEND_TIMEOUT, SQLNET.RECV_TIMEOUT
Thanks
rajThe sqlnet.ora file is the profile configuration file. It resides on the client machines and the database server.
Profiles are stored and implemented using this file. The database server can be configured with access control parameters in the sqlnet.ora file.
These parameters specify whether clients are allowed or denied access based on the protocol.
The sqlnet.ora file enables you to do the following:
Specify the client domain to append to unqualified names
Prioritize naming methods
Enable logging and tracing features
Route connections through specific processes
Configure parameters for external naming
Configure Oracle Advanced Security
Use protocol-specific parameters to restrict access to the database
By default, the sqlnet.ora file is located in the ORACLE_HOME/network/admin directory. The sqlnet.ora file can also be stored in the directory specified by the TNS_ADMIN environment variable.
You can not query on this file sqlnet.ora as tnsnames.ora file.
Regards
Mahir M. Quluzade -
hi i am face with problem that is i want to show the how much approximate space required for backup or .bak file to get backup using sql query because i want to show the required memory for backup file on my java application
Hi FIROZ
TENNALI
Is this still an issue, or can we close the thread?
* closing a thread by marking the answer/s (there is a link beneath each response) and you can vote for useful responses as well (there is a link to vote on the left of each response)
Ronen Ariely
[Personal Site] [Blog] [Facebook] -
Hi,
I want to use the SQL query IF EXIST to update or insert data in a ms access table, but it doesn´t work
(fault number -2147217900)
I want to search for a value in a ms access table , if it exist i want to update it, if not i want to insert a new row.
Working with LabView 7.1, database con. toolset.
Who can HELP?
Thanks a lot
MarcoHello,
I think that If exist is not a standar SQL command (I know it exists I think in Oracle and SQL server), MS access doesn't support it, so I think the best way to do it is first make a Select and then either an Update or an insert, sorry...
Paulo -
I want to know when a sql-query was excuted in DBA_HIST_SQL_PLAN !!
Hi, all.
I want to know when a sql query wqas excuted in past 3 month.
select * from DBA_HIST_SQL_PLAN
where plan_hash_value='1844343569'
is working?
The plan_hash_value is from v$sql_plan of current instance.
There ware 3times database shutdown/startup.
When a oracle instance is initiated, is the plan_hash_value of the same query changing?
Thanks in advance.You can't go back 3 months unless your retention period is set to that length of time.
select retention from DBA_HIST_WR_CONTROL;
If you want to retain data for longer then you need to modify you snapshots settings, but this can impact your DB if it grows too large.
BEGIN
DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS( retention => 100800);
END;
Then you could get the details out by using something like
select a.SNAP_ID, begin_interval_time, end_interval_time, SQL_ID, PLAN_HASH_VALUE,VERSION_COUNT, FETCHES_TOTAL, EXECUTIONS_DELTA , EXECUTIONS_TOTAL
from DBA_HIST_SQLSTAT a, DBA_HIST_SNAPSHOT b
where b.snap_id=a.snap_id
and b.instance_number=a.instance_number
and sql_id='&sql_id'
; -
I want sql query for this output
hi guys
could u tell how can i write sql query for this out put
i have one table like this
ID ACCOUTID TAX
1 1 A
2 1 B
3 2 C
4 2 D
5 3 E
7 NULL F
8 NULL G
MY OUT PUT MUST BE LIKE THIS
ID AID TAX
2 1 A
4 2 D
7 NULL F
8 NULL G
HERE IN THIS OUTPUT I SHOULD HAVE
MAXIMAM ID VALUE FOR A REPEATED AID VALUES
AND
THE ROWS AID VALUES IS NULL ALSO MUST PAPULATED IN THE OUTPUT.
I KNOW ONE SOLUTION LIKE THIS
SELECT MAX(ID),AID,TAX
FROM TABLE T
GROUP BY AID,TAX
UNION ALL
SELECT ID, AIC,TAX
FROM TABLE T
WHERE AID IS NULL;
BUT I WANT SAME RESULT WITH OUT USING LOGICAL OPERATORS.
COULD U PLZ TELL A SOL.Will this help:
SQL> with t as
2 (
3 select 1 ID, 1 ACCOUTID, 'A' TAX from dual union all
4 select 2, 1, 'B' from dual union all
5 select 3, 2, 'C' from dual union all
6 select 4, 2, 'D' from dual union all
7 select 5, 3, 'E' from dual union all
8 select 7, NULL, 'F' from dual union all
9 select 8, NULL, 'G' from dual
10 )
11 --
12 select id, ACCOUTID AID, Tax
13 from
14 (
15 select t.*
16 ,count(1) over (partition by t.ACCOUTID) cn
17 ,row_number() over (partition by t.ACCOUTID order by id desc) rn
18 from t
19 )
20 where cn > 1
21 and (rn = 1 or ACCOUTID is null)
22 /
ID AID T
2 1 B
4 2 D
8 G
7 F
-- If I leave out the OR condition then you'll get this:
SQL> ed
Wrote file afiedt.buf
1 with t as
2 (
3 select 1 ID, 1 ACCOUTID, 'A' TAX from dual union all
4 select 2, 1, 'B' from dual union all
5 select 3, 2, 'C' from dual union all
6 select 4, 2, 'D' from dual union all
7 select 5, 3, 'E' from dual union all
8 select 7, NULL, 'F' from dual union all
9 select 8, NULL, 'G' from dual
10 )
11 --
12 select id, ACCOUTID AID, Tax
13 from
14 (
15 select t.*
16 ,count(1) over (partition by t.ACCOUTID) cn
17 ,row_number() over (partition by t.ACCOUTID order by id desc) rn
18 from t
19 )
20 where cn > 1
21* and rn = 1
SQL> /
ID AID T
2 1 B
4 2 D
8 G
--which follows the description you've given, but not the output -
I want a smiple sql query to create a calender
Hi,
I can not find my sql query to create a calender.
It is just the one who already shows when you create a calender.
I am writing documentation and I have no acces to Oracle Portal anymore.
Can somebody give me that sql query ?
Thanks a lot,
Chu LamHi,
This the one.
select
EMP.HIREDATE the_date,
EMP.ENAME the_name,
null the_name_link,
null the_date_link,
null the_target
from SCOTT.EMP
order by EMP.HIREDATE
Thanks,
Sharmila -
How to modify a SQL query?
Hi all,
I am using Crystal Reports version 10. I have a number of reports that have been written by a software vendor wherby the name of the database they were connected to when the report was written is coded into the FROM command of the reports SQL query, eg "GCUK_2" in the of the SQL snippet below.
SELECT "Clients"."NAME", "Quotes"."QUOTE_ID", "Quote_Items"."UPRICE", "Quote_Items"."QTY", "Quote_Items"."UOM", "Quote_Items"."QUSAGE_ID", "Report_Control"."QUSAGEID", "Quote_Items"."STANDARD", "Quote_Items"."SECT_FLAG", "Quote_Items"."DISPORDER", "Quotes"."DESCRIPT", "Report_Control"."SECT_NAME", "Quote_Items"."CNT", "Category_and_Type"."TYPEDESC", "Quote_Items"."DESCRIPT", "Report_Control"."DISP_SORT"
FROM ((("GCUK_2"."schedwin"."QTE_CTRL" "Report_Control" INNER JOIN "GCUK_2"."schedwin"."QUSAGE" "Quote_Items" ON "Report_Control"."QUSAGEID"="Quote_Items"."QUSAGE_ID") LEFT OUTER JOIN "GCUK_2"."schedwin"."QUOTES" "Quotes" ON
I have tried setting the Datasource Location, but it doesn't change the query at all. I have read on another forum that you can generate another SQL query using the Database Expert, Current Connections, then right click the Add Command for the database you want to create a SQL command. Is this the only way to update the database names in the query?
Thanks,
Scott.Hi Sourashree,
Thanks for that. All the reports were created by the vendor using tables as opposed to the command object. I would have thought that changing the datasource would automatically cause Crystal to rewrite the SQL query syntax, but this doesn't appear to be the case.
Yes, I've noticed that modifying the record selection will change the sql query. The only way I can see to change the database name in the query is to change to the desired databsource and then remove and re-insert the tables, which will then update the query with the correct name. However, this seems to be a convoluted way of changing the db name in the query.
Maybe you are looking for
-
Can I zoom in or out using the Camera app on the iPhone 5?
Can I zoom in or out using the Camera app on the iPhone 5?
-
Hello, could someone help me figure out what I did wrong? I am not receiving e-mail nor I am able to compose them... the app just freezes when I tap it and if I go to my settings once i tap the e-mail,contacts, calendars row it freezes for a while th
-
Hi, We have workbook and we have done some formatting for displaying of filters in the workbook. Initially when it is displayed we have cut pasted the display of filter values on rightside of the workbook and saved the workbook. Now we changed the Qu
-
Updating a mysql table from several machines
Hi, I have a big table with about 100,000 records (fileID, etc) and I need get the fileID from this table, then analyze the files, and making new tables based on the results. Since the table is huge, I want to do the analysis on multiple machines. Cu
-
Would like to see Adobe Shape CC for android too... I'm Mac user Pro & Macbook Pro, but happen to not like Apple phones.