SQL Query?  The question is how?

Hi folks,
I am developing a finance program and I am having difficulty writing a SQL query to get the info that I need.
In short I have two tables:
TABLE 1: Currency Rates
FROM_CURRENCY (CURRENCY_ABRV)
TO_CURRENCY (CURRENCY_ABRV)
EXCHANGE_RATE
DATE
TABLE 2: Currency Country
CURRENCY_ABRV
CURRENCY_NAME
CURRENCY_SYMBOL
COUNTRY
REGION
COUNTRY_FLAG_IMAGE
I want to get the following information:
1. FROM_CURRENCY_FLAG
2. FROM_COUNTRY
3. TO_CURRENCY_FLAG
4. TO_COUNTRY
5. RATE
So basically using Table 1 FROM_CURRENCY & TO_CURRENCY and getting the relevant Country & Flag for each. The problem is that I cannot seem to get this info from one select statement. I at the moment I am getting all the records from Table 1. Then for each From & To currency searching Table 2.
This is very very time consuming. Anyone can help how to do this better.
Many thanks
James

SELECT
FROM_CURRENCY FROM_CURRENCY_FLAG,
a.COUNTRY FROM_COUNTRY,
TO_CURRENCY TO_CURRENCY_FLAG,
b.COUNTRY TO_COUNTRY,
EXCHANGE_RATE RATE
FROM TABLE1, TABLE2 a, TABLE2 b
WHERE FROM_CURRENCY = a.CURRENCY_ABRV
AND TO_CURRENCY = b.CURRENCY_ABRV;
You join TABLE1 with TABLE2 by FROM_CURRENCY and again with TABLE2 by TO_CURRENCY.
TABLE2 uses once alias name a and then alias name b, so a and b seem like different tables in the query.
All columns are assigned alias names in the SELECT clause to match your requirements.

Similar Messages

  • In  a SQL query whihc has join, How to reduce Multiple instance of a table

    in a SQL query which has join, How to reduce Multiple instance of a table
    Here is an example: I am using Oracle 9i
    is there a way to reduce no.of Person instances from the following query? or can I optimize this query further?
    TABLES:
    mail_table
    mail_id, from_person_id, to_person_id, cc_person_id, subject, body
    person_table
    person_id, name, email
    QUERY:
    SELECT p_from.name from, p_to.name to, p_cc.name cc, subject
    FROM mail, person p_from, person p_to, person p_cc
    WHERE from_person_id = p_from.person_id
    AND to_person_id = p_to.person_id
    AND cc_person_id = p_cc.person_id
    Thnanks in advance,
    Babu.

    SQL> select * from mail;
            ID          F          T         CC
             1          1          2          3
    SQL> select * from person;
           PID NAME
             1 a
             2 b
             3 c
    --Query with only ne Instance of PERSON Table
    SQL> select m.id,max(decode(m.f,p.pid,p.name)) frm_name,
      2         max(decode(m.t,p.pid,p.name)) to_name,
      3         max(decode(m.cc,p.pid,p.name)) cc_name
      4  from mail m,person p
      5  where m.f = p.pid
      6  or m.t = p.pid
      7  or m.cc = p.pid
      8  group by m.id;
            ID FRM_NAME   TO_NAME    CC_NAME
             1 a          b          c
    --Expalin plan for "One instance" Query
    SQL> explain plan for
      2  select m.id,max(decode(m.f,p.pid,p.name)) frm_name,
      3         max(decode(m.t,p.pid,p.name)) to_name,
      4         max(decode(m.cc,p.pid,p.name)) cc_name
      5  from mail m,person p
      6  where m.f = p.pid
      7  or m.t = p.pid
      8  or m.cc = p.pid
      9  group by m.id;
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 902563036
    | Id  | Operation           | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT    |        |     3 |   216 |     7  (15)| 00:00:01 |
    |   1 |  HASH GROUP BY      |        |     3 |   216 |     7  (15)| 00:00:01 |
    |   2 |   NESTED LOOPS      |        |     3 |   216 |     6   (0)| 00:00:01 |
    |   3 |    TABLE ACCESS FULL| MAIL   |     1 |    52 |     3   (0)| 00:00:01 |
    |*  4 |    TABLE ACCESS FULL| PERSON |     3 |    60 |     3   (0)| 00:00:01 |
    PLAN_TABLE_OUTPUT
    Predicate Information (identified by operation id):
       4 - filter("M"."F"="P"."PID" OR "M"."T"="P"."PID" OR
                  "M"."CC"="P"."PID")
    Note
       - dynamic sampling used for this statement
    --Explain plan for "Normal" query
    SQL> explain plan for
      2  select m.id,pf.name fname,pt.name tname,pcc.name ccname
      3  from mail m,person pf,person pt,person pcc
      4  where m.f = pf.pid
      5  and m.t = pt.pid
      6  and m.cc = pcc.pid;
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 4145845855
    | Id  | Operation            | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT     |        |     1 |   112 |    14  (15)| 00:00:01 |
    |*  1 |  HASH JOIN           |        |     1 |   112 |    14  (15)| 00:00:01 |
    |*  2 |   HASH JOIN          |        |     1 |    92 |    10  (10)| 00:00:01 |
    |*  3 |    HASH JOIN         |        |     1 |    72 |     7  (15)| 00:00:01 |
    |   4 |     TABLE ACCESS FULL| MAIL   |     1 |    52 |     3   (0)| 00:00:01 |
    |   5 |     TABLE ACCESS FULL| PERSON |     3 |    60 |     3   (0)| 00:00:01 |
    PLAN_TABLE_OUTPUT
    |   6 |    TABLE ACCESS FULL | PERSON |     3 |    60 |     3   (0)| 00:00:01 |
    |   7 |   TABLE ACCESS FULL  | PERSON |     3 |    60 |     3   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - access("M"."CC"="PCC"."PID")
       2 - access("M"."T"="PT"."PID")
       3 - access("M"."F"="PF"."PID")
    PLAN_TABLE_OUTPUT
    Note
       - dynamic sampling used for this statement
    25 rows selected.
    Message was edited by:
            jeneesh
    No indexes created...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • SQL Query - The number of columns specified in "SQL Query" does not match t

    I am creating new UDM for tablespace alert, below is my query,however its failing with error
    SQL Query - The number of columns specified in "SQL Query" does not match the value specified in "SQL Query Output"
    I selected Metric type is number
    SQL Query Format : Two columns
    Query:
    SELECT d.tablespace_name,round(((a.bytes - NVL(f.bytes,0))*100/a.maxbytes),2)
    used_pct FROM sys.dba_tablespaces d,(select tablespace_name, sum(bytes) bytes, sum(greatest(maxbytes,bytes)) maxbytes from sys.dba_data_files group by tablespace_name) a,(select tablespace_name, sum(bytes) bytes from sys.dba_free_space group by tablespace_name) f
    WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+)
    AND NOT (d.extent_management = 'LOCAL' AND d.contents = 'TEMPORARY');
    Any clues why i am getting error.

    SQL> SELECT d.tablespace_name,round(((a.bytes - NVL(f.bytes,0))*100/a.maxbytes),2) used_pct
    2 FROM sys.dba_tablespaces d,(select tablespace_name, sum(bytes) bytes, sum(greatest(maxbytes,bytes)) maxbytes from sys.dba_data_files group by tablespace_name) a,(select tablespace_name, sum(bytes) bytes from sys.dba_free_space group by tablespace_name) f
    3 WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+)
    4 AND NOT (d.extent_management = 'LOCAL' AND d.contents = 'TEMPORARY');
    TABLESPACE_NAME USED_PCT
    MGMT_TABLESPACE .82
    SYSAUX 1.52
    UNDOTBS1 .32
    RMAN .02
    CORRUPT_TS 10.63
    USERS 0
    SYSTEM 2.26
    MGMT_ECM_DEPOT_TS .04
    MGMT_AD4J_TS 0

  • Hi there, i'm trying to change a PDF document written in arabic to Word document, the problem is (( i can't find the arabic language in adobe )) the question is how to install the arabic language

    hi there, i'm trying to change a PDF document written in arabic to Word document, the problem is (( i can't find the arabic language in adobe )) the question is how to install the arabic language

    <moved from Downloading, Installing, Setting Up to Creating, Editing & Exporting PDFs>

  • The question remains; how often?

    This post was merged (inappropriately, in my opinion) into this morning's over-arching topic of mail server outage.  And yet the question remains, how often do these mail server outages happen?  Please post replies to this topic with your experiences (OTHER THAN TODAY, Sept 12) concerning such:
    how often do outages happen?
    what duration?
    what scope? (local? app-only? system-wide?)
    An inquiring mind would like to know.  Thank you.

    I think in 8 years I have probably experienced maybe 3 or 4 email outages, all brief, generally less than 2 hours. After my awful experience with Charter Cable's email, Verizon's has been incredibly reliable in comparison. And I have not experienced the outage today that so many people are posting about, both my PC-based client and the Verizon webmail interface have worked just fine so far today (and continue to work).
    Justin
    FiOS TV, 25/25 Internet, and Digital Voice user
    QIP7232, QIP7100-P2, IMG 1.9.4
    Keller, TX 76248 (VHO 1)

  • When I go to youtube and view a video clip it usually asks if i want to save the video I'm watching. Now all of a sudden it no longer asks the question. How do I get it back?

    when I go to youtube and view a video clip it usually asks if i want to save the video I'm watching. Now all of a sudden it no longer asks the question. How do I get it back?

    I believe that might be related to this:
    See: http://real.custhelp.com/app/answers/detail/a_id/7303/p/3/c/2222/r_id/119487
    Real is always, it seems, late in releasing updates for new browser updates. They are given the same opportunity during the Firefox pre-release testing as other developers to be sure that their products will work with new Firefox releases.
    But, alas, always the same problem!

  • Got a new iPhone that I've been using for about a month now, so the question is how can I get the pictures from my old iPhone and keep the pictures I have on the new iPhone?

    Got a new iPhone that I've been using for about a month now, so the question is how can I get the pictures from my old iPhone and keep the pictures I have on the new iPhone?

    Connect old iPhone to computer and save the pictures on computer.
    Connect new iPhone and sync the pictures to your new iPhone.

  • Error in SQL Query The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. for the query

    hi Experts,
    while running SQL Query i am getting an error as
    The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. for the query
    select  T1. Dscription,T1.docEntry,T1.Quantity,T1.Price ,
    T2.LineText
    from OQUT T0  INNER JOIN QUT1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN
    QUT10 T2 ON T1.DocEntry = T2.DocEntry where T1.DocEntry='590'
    group by  T1. Dscription,T1.docEntry,T1.Quantity,T1.Price
    ,T2.LineText
    how to resolve the issue

    Dear Meghanath,
    Please use the following query, Hope your purpose will serve.
    select  T1. Dscription,T1.docEntry,T1.Quantity,T1.Price ,
    CAST(T2.LineText as nvarchar (MAX))[LineText]
    from OQUT T0  INNER JOIN QUT1 T1 ON T0.DocEntry = T1.DocEntry LEFT OUTER JOIN
    QUT10 T2 ON T1.DocEntry = T2.DocEntry --where T1.DocEntry='590'
    group by  T1. Dscription,T1.docEntry,T1.Quantity,T1.Price
    ,CAST(T2.LineText as nvarchar (MAX))
    Regards,
    Amit

  • Help with sql query the status of  A/P Reserve Invoice

    Hi,
    I am trying to write a query which lists all A/P Reserve Invoice info with OPEN status.
    I check the OPCH table and cannot find the rule to tell the status.
    The "DocStatus" field has two values only: 'O' for open and 'C' for closed.
    However, the status of A/P Reserve Invoice are OPEN, CLOSED, DELIVERED, PAID, etc.
    I try to use DocStatus field to filter, but the result does not match what I see in SAP.
    Could you please give me some hints about how to get the data I need? Thank you.
    Best regards,
    Sylvia
    Edited by: csylvia on Jun 23, 2011 5:54 AM

    Hi Darius,
    Thanks for your reply.
    However, I don't know what is the relationship between Purchase order and A/P Reserve Invoice.
    Do you mean using "SELECT T0.DocEntry FROM OPCH T0, OPOR T1 WHERE T0.DocNum = T1.DocNum AND T1.DocStatus = 'O';" to query the A/P Reserve Invoice data with OPEN status?
    But the result is not what I want.
    The result of "*SELECT * FROM OPOR WHERE DocStatus = 'O';*" is also not.
    I'd like to query A/P Reserve Invoice list with OPEN status, and I try the following sql query:
    SELECT DocEntry FROM OPCH WHERE DocStatus = 'O' AND InvntSttus = 'O';
    The result is close to what I need, but it's not exactly correct.
    Moreover, I don't think the sql query conditions is accurate.
    Please give me some advice. Thank you.
    Best regards,
    Sylvia

  • SQL Query returns question marks

    I am seeing a SQL Query, executed via ADODB in VB, from a 10G client (don't know exact version) to a Sun Solaris based 9.2.0.5.0 Server return all question marks...Table queried consists of 3 columns, 2 varchar2, one long: Here query:
    SELECT SETTINGVALUE FROM EWORKFLO.CONFIG WHERE SETTINGGROUP='New test' AND SETTINGNAME='Single Page'
    The query results in 2576 question marks...(????? etc).
    Any idea what may be going on here?
    Thanks...
    Leendert

    Hello Tak,
    Thanks for replying. When this query is run from SQLPLUS we get a proper result back, that is the actual values from the SETTINGVALUE column. This is the LONG column and contains data like this:
    [Scan Section]
    DeviceTimeout=15
    Display=1
    hDCCreate=0
    Unit=0
    DialogTitle=
    IniFileName=c:\winnt\temp\kf.ini
    IniSectionName=Scan Section
    DeviceCache=1
    etc....etc....
    Depending on the settings created, the return string may vary in length.
    Leen

  • SQL query performance question

    So I had this long query that looked like this:
    SELECT a.BEGIN_DATE, a.END_DATE, a.DEAL_KEY, (select name from ideal dd where a.deal_key = dd.deal_key) DEALNAME, a.deal_term_key
    FROM
    ideal d, ideal_term a,( select deal_key, deal_term_key, max(createdOn) maxdate    from Ideal_term B
    where createdOn <= '03-OCT-12 10.03.00 AM' group by deal_key, deal_term_key ) B
    WHERE  a.begin_date <= '20-MAR-09 01.01.00 AM'
    *     and a.end_date >= '19-MAR-09 01.00.00 AM'*
    *     and A.deal_key = b.deal_key*
    *     and A.deal_term_key = b.deal_term_key*
    *     and     a.createdOn = b.maxdate*
    *     and d.deal_key = a.deal_key*
    *     and d.name like 'MVPP1 B'*
    order by
    *     a.begin_date, a.deal_key, a.deal_term_key;*
    This performed very poorly for a record in one of the tables that has 43,000+ revisions. It took about 1 minute and 40 seconds. I asked the database guy at my company for help with it and he re-wrote it like so:
    SELECT a.BEGIN_DATE, a.END_DATE, a.DEAL_KEY, (select name from ideal dd where a.deal_key = dd.deal_key) DEALNAME, a.deal_term_key
    FROM ideal d
    INNER JOIN (SELECT deal_key,
    deal_term_key,
    MAX(createdOn) maxdate
    FROM Ideal_term B2
    WHERE '03-OCT-12 10.03.00 AM' >= createdOn
    GROUP BY deal_key, deal_term_key) B1
    ON d.deal_key = B1.deal_key
    INNER JOIN ideal_term a
    ON B1.deal_key = A.deal_key
    AND B1.deal_term_key = A.deal_term_key
    AND B1.maxdate = a.createdOn
    AND d.deal_key = a.deal_key + 0
    WHERE a.begin_date <= '20-MAR-09 01.01.00 AM'
    AND a.end_date >= '19-MAR-09 01.00.00 AM'
    AND d.name LIKE 'MVPP1 B'
    ORDER BY a.begin_date, a.deal_key, a.deal_term_key
    this works much better, it only takes 0.13 seconds. I've bee trying to figure out why exaclty his version performs so much better. His only epxlanation was that the "+ 0" in the WHERE clause prevented Oracle from using an index for that column which created a bad plan initially.
    I think there has to be more to it than that though. Can someone give me a detailed explanation of why the second version of the query performed so much faster.
    Thanks.
    Edited by: su**** on Oct 10, 2012 1:31 PM

    I used Autotrace in SQL developer. Is that sufficient? Here is the Autotrace and Explain for the slow query:
    and for the fast query:
    I said that I thought there was more to it because when my team members and I looked at the re-worked query the database guy sent us, our initial thoughts were that in the slow query some of the tables didn't have joins and because of that the query formed a Cartesian product and this resulted in a huge 43,000+ rows matrix.
    In his version all tables had joins properly defined and in addition he had that +0 which told it to ignore the index for the attribute deal_key of table ideal_term. I spoke with the database guy today and he confirmed our theory.

  • XML SQL - query the table with XML in column [urgent]

    I have table which have to be query-ied with
    oracle.xml.sql.query.OracleXMLQuery class to produce XML
    document for web.
    Problem is that in one column is already XML data (well formed
    HTML) like this:
    <font face="Arial">click</font>
    and when I call OracleXMLQuery.getXMLString() it encode all's '<'
    as ';lt' and so on.
    Can I turn off that kind of encoding some way or is it better
    sollution for this?
    thanks in advance
    Bojan
    null

    You can use an XSLT Stylesheet to achieve what you are wanting to do.
    Say the name of the column containing
    the markup is named "DOC", then
    transforming the output of OracleXMLQuery
    by the following stylesheet using
    the oracle.xml.parser.v2.XSLProcessor...
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl utput method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>
    <xsl:template match="DOC">
    <xsl:value-of select="." disable-output-escaping="yes"/>
    </xsl:template>
    </xsl:stylesheet>
    Will give the results you are looking for.
    You can use the XSQL Servlet to make this combination even easier if that is applicable to your situation.

  • Sql query the store that has best sell quality in pubs sample database

    In pubs database, I want to query the store in which has highest sales quality. I exec following query,
    select stores.stor_id, sum(sales.qty) as sumqty
    from stores
    join sales on stores.stor_id = sales.stor_id
    group by stores.stor_id, stores.stor_name
    Results:
    6380 Eric the Read Books 8
    7066 Barnum's 125
    7067 News & Brews 90
    7131 Doc-U-Mat: Quality Laundry and Books 130
    7896 Fricative Bookshop 60
    8042 Bookbeat 80
    The result I need is 
    7131 Doc-U-Mat: Quality Laundry and Books 130
    Could anyone give me some suggestions?
    Thanks.

    Hello,
    If you don't want to use TOP , you can try to used nested query to get MAX(SUM()) value. Please refer to the following statement:
    SELECT stores.stor_id, sum(sales.qty) as sumqty
    FROM stores
    JOIN sales ON stores.stor_id = sales.stor_id
    GROUP BY stores.stor_id, stores.stor_name
    HAVING sum(sales.qty)=
    ( SELECT max(sumqty) FROM (
    SELECT stores.stor_id, sum(sales.qty) as sumqty
    FROM stores
    JOIN sales ON stores.stor_id = sales.stor_id
    GROUP BY stores.stor_id, stores.stor_name) AS s)
    Regards,
    Fanny Liu
    Fanny Liu
    TechNet Community Support

  • HT201363 I do not remember the questions? How do I find?

    I do not remember the answer to my questions, how can I find? Please help me. I'm deaf. I really appreciate the help you gave me. 

    You need to ask Apple to reset your security questions; ways of contacting them include phoning AppleCare and asking for the Account Security team, clicking here and picking a method for your country, and filling out and submitting this form.
    They wouldn't be security questions if they could be bypassed without Apple verifying your identity.
    (104936)

  • SQL Query date question

    Hello,
    I'm searching a query that would display me the number of rows made of a table for the last 4 weeks, grouped by each week.
    I basicly have the query but I don't think it will be correct when we go to the next year.
    Does anyone know how I can solve this problem?
    Select count(*),to_char( create_dt, 'iw' )
    from dossiers
    where to_char( create_dt, 'iw' ) > to_char(SYSDATE, 'iw') - 4
    group by to_char( create_dt, 'iw' )
    order by to_char( create_dt, 'iw' ) desc;
    Thanks in advance
    OLI

    This is what I use to count all my audits for the past 4 weeks:
    T10> SELECT TRUNC(weeks.start_date) || ' - ' || TRUNC(weeks.start_date + 6)  audit_date_span
      2       , COUNT(*)  count
      3    FROM asset_audits,
      4         (
      5           SELECT NEXT_DAY(
      6                    ADD_MONTHS(SYSDATE, -1) - 1
      7                  , 'SUNDAY') + ((ROWNUM - 1) * 7) start_date
      8             FROM (
      9                    SELECT 1
    10                      FROM DUAL
    11                   CONNECT BY LEVEL < 6
    12                  )
    13         ) weeks
    14   WHERE TRUNC(audit_date) BETWEEN TRUNC(weeks.start_date) AND TRUNC(weeks.start_date + 6)
    15   GROUP BY TRUNC(weeks.start_date) || ' - ' || TRUNC(weeks.start_date + 6)
    16   ORDER BY TO_DATE(SUBSTR(audit_date_span, 1, 9), 'DD-MON-YY') ASC
    17  /
    AUDIT_DATE_SPAN                COUNT
    29-OCT-2006 - 04-NOV-2006        105
    05-NOV-2006 - 11-NOV-2006        201
    12-NOV-2006 - 18-NOV-2006        120
    19-NOV-2006 - 25-NOV-2006        664
    4 rows selected.Cheers.

Maybe you are looking for