Sql query to build average value

I'm trying to build a query capture the below information. Any help is really appreciated.
Table looks something like below.
Room
Subject
Students
A1
Science
10
A1
Maths
20
B1
Science
15
B2
English
25
C1
Deutsch
20
A1
French
15
C1
English
10
How can i write a query so that i takes the average of students in each room and display the output? I need to write a query so that it also picks up any new rooms and students added.
Average is sum of total students in room by number of times each room is specified.
eg: A1 is listed 3 times a the result would be 10+20+15 divived by 3.
A1    B1    B2    C1
15    15    20    15
Thanks You
KK

This is called pivot. And you need to know all rooms upfront. Anyway, assuming you are on 11G:
SQL> with t as (
  2             select 'A1' room,'Science' subject,10 students from dual union all
  3             select 'A1','Maths',20 from dual union all
  4             select 'B1','Science',15 from dual union all
  5             select 'B2','English',25 from dual union all
  6             select 'C1','Deutsch',20 from dual union all
  7             select 'A1','French',15 from dual union all
  8             select 'C1','English',10 from dual
  9            )
10  select  "'A1'_X" A1,
11          "'B1'_X" B1,
12          "'B2'_X" B2,
13          "'C1'_X" C1
14    from  t
15    pivot(
16          avg(students) x,
17          max(subject) y
18          for room in ('A1','B1','B2','C1')
19         )
20  /
        A1         B1         B2         C1
        15         15         25         15
SQL>
SY.

Similar Messages

  • Write the sql query to find largest value in row wise without using   great

    write the sql query to find largest value in row wise without using
    greatest fuction?

    Another not so good way, considering you want greatest of 4 fields from a single record:
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (Select 100 col1,200 col2,300 col3,400 col4 from dual
      2  union select 500,600,700,800 from dual
      3  union select 900,1000,1100,1200 from dual
      4  union select 1300,1400,1500,1600 from dual
      5  union select 1700,1800,1900,2000 from dual
      6  union select 2100,2200,2300,2400 from dual
      7  union select 2800,2700,2600,2500 from dual
      8  union select 2900,3000,3100,3200 from dual)
      9  SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) Max_value
    10  FROM
    11  (SELECT (CASE WHEN col1_col2 > col2_col3 THEN col1_col2 ELSE col2_col3 END) col1,
    12         (CASE WHEN col2_col3 > col3_col4 THEN col2_col3 ELSE col3_col4 END) col2,
    13         (CASE WHEN col3_col4 > col4_col1 THEN col3_col4 ELSE col4_col1 END) col3
    14  FROM
    15  (SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) col1_col2,
    16         (CASE WHEN col2 > col3 THEN col2 ELSE col3 END) col2_col3,
    17         (CASE WHEN col3 > col4 THEN col3 ELSE col4 END) col3_col4,
    18         (CASE WHEN col4 > col1 THEN col4 ELSE col1 END) col4_col1
    19* FROM t))
    SQL> /
    MAX_VALUE
           400
           800
          1200
          1600
          2000
          2400
          2800
          3200
    8 rows selected.
    SQL> Edited by: AP on Sep 21, 2010 6:29 AM

  • How to check the sql:query is return null value

    I have use :
    <sql:query var="sql1" dataSource="${db}">
    select col_name from table_name
    where a=<c:out value="${row.test1}"/>
    and b='<c:out value="${row.test2}"/>'
    </sql:query>
    So, how can I check this statement return null value which is no record within this table?

    The Result should never be null but can be empty. You can check if the Result is empty using an if tag and checking the rowCount property:
        <sql:query var="books"
          sql="select * from PUBLIC.books where id = ?" >
          <sql:param value="${bookId}" />
        </sql:query>
         <c:if test="${books.rowCount > 0}">
         </c:if>http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSTL7.html#wp84217
    Look for query Tag Result Interface

  • SQL Query to find cumulative values for a Financial Year

    Dear users,
    My requirement is to create a SQL query for a table/view where I have day-wise data. I want to find out cumulative values for financial year by giving any date. It should add the values from start of financial year till that date in the financial year.
    I think creating a view of such type will post heavy burden on resources since accuimulation will be done for each day upto that day.
    Thanks

    Dear users,
    My requirement is to create a SQL query for a
    table/view where I have day-wise data. I want to
    find out cumulative values for financial year by
    giving any date. It should add the values from start
    of financial year till that date in the financial
    year.
    I think creating a view of such type will post heavy
    burden on resources since accuimulation will be done
    for each day upto that day.
    ThanksKumar's solution will serve yours purpose but m not agreed
    I think creating a view of such type will post heavy
    burden on resources since accuimulation will be done
    for each day upto that day. Khurram

  • SQL query returns varchar - default value type!!!

    On a field in my portal form there is a default value type of 'SQL query returns varchar', where can I put this sql to get a default value from a table?

    Hi again
    I have this code in 'additional pl/sql code' - '...before displaying the page'. But it comes up with an error when I run the form.
    declare
    prop_v varchar2(20);
    group_v varchar2(3);
    el1_v varchar2(3);
    blk varchar2(10) := 'DEFAULT';
    begin
    prop_v := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT', p_attribute_name => 'l_property_p');
    group_v := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT', p_attribute_name => 'l_group_p');
    select element_code
    into el1_v
    from eh_risk_element_detail
    where property_ref = prop_v
    and group_code = group_v
    and line_no = 1;
    p_session.set_value(p_block_name => blk, p_attribute_name => 'L_ELEMENT1_P', p_value => el1_v);
    end;
    Rich

  • LOV SQL query dependent on column value

    Hi Gurus,
    I have a tabular form, a column of which I would like to display as a dropdown whose list of values is a SQL query containing a where clause on another column value.
    For example, say the tabular form displays questions, and that the answers to the different questions are to be picked in list of values that are different for each question. I have tried the following queries for the dropdown list of values:
    1- select value_text, value_text from list_of_values_table where question_id=#QUESTION_ID#
    2- select value_text, value_text from list_of_values_table where question_id=:QUESTION_ID
    3- select value_text, value_text from list_of_values_table where question_id=&QUESTION_ID.
    In the 3 cases, I get an error when I run the application saying that the SQL statement is incorrect.
    Any ideas?
    Thanks in advance,
    Guillauem

    Anton,
    Yes, I am creating a region based on a sql query. The query is the following:
    select q.id quest_id, q.name, q.data_type_id, q.lov_flag, a.id answer_id, a.answer_text, a.org_id
    from gwb_questions q left join gwb_answers a on q.id=a.question_id, gwb_answers ans right join gwb_orgs o on ans.org_id=o.id
    where q.MODULE_ID=:P2_MODULE_ID and o.id=:P2_ORG_ID
    Now, in the 'answer_text' column, I would like to display a dropdown containing values that come from a separate table and that depend on the value of the quest_id column. So in the Column Attributes for 'answer_text', I selected Select List (query based LOV) for the Tabular Form Element, and I entered the query mentioned before in the List of Values section (select value_text, value_text from list_of_values_table lov where lov.question_id= ?).
    I am not using any API.
    Does that make sense?
    Thanks for your help,
    Guillaume

  • Need of SQL query in selecting distinct values from two tables

    hi,
    I need a query for selecting distinct values from two tables with one condition.
    for eg:
    there are two tables a & b.
    in table a there are values like age,sex,name,empno and in table b valuses are such as age,salary,DOJ,empno.
    here what i need is with the help of empno as unique field,i need to select distinct values from two tables (ie) except age.
    can anybody please help me.
    Thanks in advance,
    Ratheesh

    Not sure what you mean either, but perhaps this will start a dialog:
    SELECT DISTINCT a.empno,
                    a.name,
                    a.sex,
                    b.salary,
                    b.doj
    FROM    a,
            b
    WHERE   a.empno = b.empno;Greg

  • Sql query - Selecting last recorded values for each date in specified period

    Hello,
    Can someone please help me with my problem.
    I'm trying to get last recorded balance for each day for specific box (1 or 2) in specified period of days from ms access database using ADOTool.
    I'm trying to get that information with SQL query but so far unsuccessfully...  
    My table looks like this:
    Table name: TestTable
    Date Time Location Box Balance
    20.10.2014. 06:00:00 1 1 345
    20.10.2014. 12:00:00 1 1 7356
    20.10.2014. 18:45:00 1 1 5678
    20.10.2014. 23:54:00 1 1 9845
    20.10.2014. 06:00:02 1 2 35
    20.10.2014. 12:00:04 1 2 756
    20.10.2014. 18:45:06 1 2 578
    20.10.2014. 23:54:10 1 2 845
    21.10.2014. 06:00:00 1 1 34
    21.10.2014. 12:05:03 1 1 5789
    21.10.2014. 15:00:34 1 1 1237
    21.10.2014. 06:00:00 1 2 374
    21.10.2014. 12:05:03 1 2 54789
    21.10.2014. 15:00:34 1 2 13237
    22.10.2014. 06:00:00 1 1 8562
    22.10.2014. 10:00:00 1 1 1234
    22.10.2014. 17:03:45 1 1 3415
    22.10.2014. 22:00:00 1 1 6742
    22.10.2014. 06:00:05 1 2 562
    22.10.2014. 10:00:16 1 2 123
    22.10.2014. 17:03:50 1 2 415
    22.10.2014. 22:00:10 1 2 642
    23.10.2014. 06:00:00 1 1 9876
    23.10.2014. 09:13:00 1 1 223
    23.10.2014. 13:50:17 1 1 7768
    23.10.2014. 19:47:40 1 1 3456
    23.10.2014. 21:30:00 1 1 789
    23.10.2014. 23:57:12 1 1 25
    23.10.2014. 06:00:07 1 2 976
    23.10.2014. 09:13:45 1 2 223
    23.10.2014. 13:50:40 1 2 78
    23.10.2014. 19:47:55 1 2 346
    23.10.2014. 21:30:03 1 2 89
    23.10.2014. 23:57:18 1 2 25
    24.10.2014. 06:00:55 1 1 346
    24.10.2014. 12:30:22 1 1 8329
    24.10.2014. 23:50:19 1 1 2225
    24.10.2014. 06:01:00 1 2 3546
    24.10.2014. 12:30:26 1 2 89
    24.10.2014. 23:51:10 1 2 25
    Let's say the period is 21.10.2014. - 23.10.2014. and I want to get last recorded balance for box 1. for each day. The result should look like this:
    Date Time Location Box Balance
    21.10.2014. 15:00:34 1 1 1237
    22.10.2014. 22:00:00 1 1 6742
    23.10.2014. 23:57:12 1 1 25
    So far I've managed to write a query that gives me balance for ONLY ONE date (date with highest time in whole table), but I need balance for EVERY date in specific period.
    My incorrect code (didn't manage to implement "BETWEEN" for dates...):
    SELECT TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
    FROM TestTable
    WHERE Time=(SELECT MAX(Time)
    FROM TestTable
    WHERE Location=1 AND Box=1 );
    Tnx!
    Solved!
    Go to Solution.

    For loop
    following query keep day (here 24 in below query) Variable from ( 1 to 28-29/30/31 as per month)
    SELECT TOP 1 TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
    FROM Test Table.
    WHERE  Time=(SELECT MAX(Time) FROM TestTable WHERE Location=1 AND Box=1 )
    AND DATE = "2014-10-24";
    PBP (CLAD)
    Labview 6.1 - 2014
    KUDOS ARE WELCOMED.
    If your problem get solved then mark as solution.

  • SQL Query constructing  for searching values in Tables single Column

    I have Table USER_SERVICES with 2 Columns USER_NAME, Services. Please find below sample data on USER_SERVICES Table.
    A user can be assigned with multiple services(service1, service2 etc.,). So we store data as below :
    USER_NAME Services
    user1 service1
    user1 service2
    user1 service3
    user2 service1
    user2 service2
    user3 service3
    user4 service4
    I need to frame a SQL Query to 'select users assigned with Service A and Service B and Service n'.
    Note: 'And' condition is used when Searching for multiple services.
    Example: The SQL Query has to dynamically handle condition if i give Single Service, multiple Services(service1 and service2 e.t.c,)
    If i say select users assigned with service1 and service2, from the above Table, result should be user1 and user2
    If i say select users assigned with service1 and service2 and service3, from the above Table, result should be user1
    If i say select users assigned with service4 from the above table, result should be user4
    If i say select users assigned with service1 from the above table, result should be user2,user1
    If i say select users assigned with service1 and service2 from the above table, result should be user2,user1
    How do i construct the SQL Query. Any help will be appreciated.
    Thanks,
    Sathish
    Alternate email: [email protected]

    How it will be stored? In a string or a table or a collection type?
    If you have it in a table you can do like this.
    with t
    as
    select 'user1' user_name, 'service1' services from dual union all
    select 'user1', 'service2' from dual union all
    select 'user1', 'service3' from dual union all
    select 'user2', 'service1' from dual union all
    select 'user2', 'service2' from dual union all
    select 'user3', 'service3' from dual union all
    select 'user4', 'service4' from dual
    t1 as
    select 'service1' services from dual union all
    select 'service2' from dual
    select distinct user_name
      from (
    select t.user_name, t.services, t1.services, count(t1.services) over(partition by t.user_name) cnt, t1.cnt cnt_1
      from t
      left join (select t1.*, count(*) over() cnt from t1) t1 on t1.services = t.services
    where cnt = cnt_1

  • SQL query to store float values in french culture.

    Here is how my requirement goes..
    I have an application where users can store float values into the SQL server. Here float values are period(.) based. I have now started supporting the users who use the french culture where the float values are Comma(,) based. Example: 13.75 is represented
    as 13,75. Instead of making changes to my code i.e the way i am inserting/retrieving the date now, is there a way i can handle this requirement based on culture. Like, if I specify the culture, will I be able to insert float inputs as comma(,) based values
    without disturbing my existing code?
    Any help is appreciated
    Thanks,
    Chandrahas.
    -Chandrahas

    is there a way i can handle this requirement based on culture.
    Hello Chandrahas,
    SQL Server don't store any culture info for numeric values; they are plain numeric.
    It's part of the frontend to represent / accept values depending on the culture setting of the client.
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • SQL Query / Insert Builder

    I am making a Java program to access / update a database via JDBC. I need to make string queries like this:
    db.update(
    "INSERT INTO sample_table(str_col,num_col) VALUES('Ford', 100)");
    The query above is simple, but you can see how assembling the query made out of constant string fragments and variables that my program updates can become more complicated. For example, I need to also store a date. Is there a utility similar to StringBuilder for building strings that can make this process easier? I am looking for a function specifically to build SQL queries. It might look something like this:
    querybuilder.insert(<table>, <name1>, <value1>, <name2>, <value2>....<nameN>, <valueN>);
    Where table is the name of the table that I would be inserting into and name is the column name of the table, value would be the value to be stored under the column name. Are there SQL functions for this?

    I made such a library myself (called it an SQLFormatter), but haven't put it on the web because I've not thought there would be a general interest in it. Essentially, I can create an Insert Into statement like this:
    SQLFormatter formatter = SQLFormatter.get(myStatement);
    String sql = formatter.createInsertIntoStatement("MyTable", new String[] {"Field1", "Field2", "Field3"}, new Object[] {value1, value2, value3});I'd be happy to share it with you, but I'm not sure about how I can make it reach you. Do I need to post it on a web site?

  • SQL Query - Multiple records from value of field

    I have a table that has the following lay out
    ID
    AreaUnitServes
    Filter1Quantity
    Filter1Size
    Filter2Quantity
    Filter2Size
    And I’m trying to convert it to
    FkPSAUnitsID
    Size
    Location
    I’ve got the following query started but I’m stuck on how to INSERT INTO multiple records by the value of Filter1Quantity. I also have to query for Filter2Quantity.
    INSERT
    INTO RapidServ.dbo.PSAFilters
    (FkPSAUnitsID,Size,Location)
    SELECT ID
    AS FkPSAUnitsID,Filter1Size
    AS Size,
    AreaUnitServes
    AS Location
    FROM RapidServ.dbo.PSA
    Thanks,
    why767

    Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need
    to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. And you probably need to read and download the PDF for: 
    https://www.simple-talk.com/books/sql-books/119-sql-code-smells/
    >> I have a table that has the following lay out <<
    How nice for you! Why did you draw ASCII pictures?  It has all kinds of magical names. The table is nameless because it is forbidden by a wizard to speak its name? There are generic, universal “size” and “location”of nothing in particular. Is the “fk-”
    prefix a meta data design error for “foreign key”? We never do that in RDBMS; name a data element for what it is by its nature, not for how it was used in one place in one schema. This is why we have no magical “id” in a valid schema. Data elements do not
    change names from table to table. 
    You seem to have inherited a pile of garbage with a repeated group in it. Look up First Normal Form (1NF). You also do not know why row is not a record and why fields are  not columns. Did you notice that “area_unit_serves” is a sentence and not
    a name? 
    >> I’ve got the following query started but I’m stuck on how to INSERT INTO multiple records [sic] by the value of Filter1Quantity. I also have to query for Filter2Quantity. <<
    My guess until I get a usable business rules and DDL is that you can use a UNION something like this skeleton: 
    INSERT INTO PSA_Filters (psa_units_id, filter_size, area_service_unit)
    SELECT psa_units_id, filter_1_size, area_service_unit
      FROM Magic_Nameless_Stuff
    UNION ALL
    SELECT psa_units_id, filter_2_size, area_service_unit
      FROM Magic_Nameless_Stuff;
    --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

  • SQL query concat - numeric or value error

    I am getting numeric or value error when concatenating sql script..
    Here is the sample, I have in the stored proc.. firstpart:_ works fine if I comment the secondpart:_. (I modified the query because I am posting in the public forums)
    V_SAMPLEQUERY VARCHAR2(2000);
    firstpart:_
    V_SAMPLEQUERY := '      
    SELECT
    AB.ABCDID AS ABCDID,
    CD.MEMBERID AS MEMBERID,
    CD.FNAME AS FNAME,
    CD.LNAME AS LNAME,
    CD.ACCOUNT AS ACCOUNT,
    EF.ZIP AS ZIP,
    AB.INSERTTIME AS INSERTTIME,
    AB.INSERTBY AS INSERTBY,
    ES.NAME AS STATUS,
    MYFUNCTION(E.ABCDID) AS AMOUNT,
    PA.NAME AS SOUSENAME,
    R.CODE AS BUDGETCODE,
    WX.ACCOUNTID as ACCOUNTID,
    AB.SOMETYPE AS SOMETYPE,
    AB.ACCTREP,
    ED.TYPE AS TYPE,
    AB.NOTES AS NOTES,
    GH.AREACODE || '''' || GH.PHONE_NUMBER AS PHONENUMBER,
    ED.REQUESTEDDATE,
    ED.BOOKED,
    ED.CALLERINFO
    FROM PEOPLES AB,
    LOCATIONS CD,
    LOCATIONTYPES EF,
    ACCOUNTS GH,
    ACCOUNTdetails IJ,
    ACCOUNTstatus KL,
    ACCOUNTMEMBERDETAILS MN,
    MEMBERS OP,
    BUDGET QR,
    BCODES ST,
    ACCOUNTSYSTEM UV,
    INFOCALL WX,
    PHONE_NUMBERS YZ
    WHERE AT.NAME = ''SAVINGS''
    andCD.MEMBERID = PH.MEMBERID
    andCD.MEMBERID =EF.MEMBERID
    and AT.ADDRESSTYPEID =EF.ADDRESSTYPEID
    andCD.MEMBERID =AB.MEMBERID
    andAB.STATUS = ES.STATUS(+)
    andAB.ABCDID = ED.ABCDID
    andAB.ABCDID = EP.ABCDID(+)
    and EP.MEBER_ID = PA.MEBER_ID(+)
    andAB.BID = R.BID(+)
    andAB.SAMPLEID = PR.SAMPLEID(+)
    andAB.ABCDID = BSG.ABCDID(+)
    andAB.INFOCALLID = C.INFOCALLID(+)
    and YZ.SOMETYPEID = ''10000''
    ANDAB.INSERTTIME > SYSDATE - 35
    ANDAB.INSERTTIME < ''
    AND WX.NAME IN (''ABCD'',
    ''EFGH'',
    ''IJKL'',
    ''MNOP'',
    ''QRST'',
    ''UVWX'')
    AND MN.NAME = ''FINISHED'' '
    secondpart:_ ***ERROR PART***
    V_SAMPLEQUERY := V_SAMPLEQUERY || ' ORDER BY AB.USER'; -- Here I am getting error saving ORA-06502 - numeric or value error

    Hi,
    Wheneve you have a problem, prlease post a complete test script that people can run to re-create the problem and test their ideas. Include CREATE TABLE and INSERT statements for any tables used, abnd the results you want it to produce.
    SeshuGiri wrote:
    I am getting numeric or value error when concatenating sql script.. Are you really getting the error when you concatenate, or when you try to execute the resulting string?
    Here is the sample, I have in the stored proc.. firstpart:_ works fine if I comment the secondpart:_. (I modified the query because I am posting in the public forums)
    V_SAMPLEQUERY VARCHAR2(2000);Is that big enough? It looks like the string is right around 1000 characters; depending on your character set, that could be more than 2000 bytes. VARCHAR2s in PL/SQL can hold up to 32767 bytes.
    firstpart:_
    V_SAMPLEQUERY := '      
    SELECT
    AB.ABCDID AS ABCDID,
    CD.MEMBERID AS MEMBERID,
    CD.FNAME AS FNAME,
    CD.LNAME AS LNAME,
    CD.ACCOUNT AS ACCOUNT,
    EF.ZIP AS ZIP,
    AB.INSERTTIME AS INSERTTIME,
    AB.INSERTBY AS INSERTBY,
    ES.NAME AS STATUS,
    MYFUNCTION(E.ABCDID) AS AMOUNT,
    PA.NAME AS SOUSENAME,
    R.CODE AS BUDGETCODE,
    WX.ACCOUNTID as ACCOUNTID,
    AB.SOMETYPE AS SOMETYPE,
    AB.ACCTREP,
    ED.TYPE AS TYPE,
    AB.NOTES AS NOTES,
    GH.AREACODE || '''' || GH.PHONE_NUMBER AS PHONENUMBER,
    ED.REQUESTEDDATE,
    ED.BOOKED,
    ED.CALLERINFO
    FROM PEOPLES AB,
    LOCATIONS CD,
    LOCATIONTYPES EF,
    ACCOUNTS GH,
    ACCOUNTdetails IJ,
    ACCOUNTstatus KL,
    ACCOUNTMEMBERDETAILS MN,
    MEMBERS OP,
    BUDGET QR,
    BCODES ST,
    ACCOUNTSYSTEM UV,
    INFOCALL WX,
    PHONE_NUMBERS YZ
    WHERE AT.NAME = ''SAVINGS''
    andCD.MEMBERID = PH.MEMBERID
    andCD.MEMBERID =EF.MEMBERID
    and AT.ADDRESSTYPEID =EF.ADDRESSTYPEID
    andCD.MEMBERID =AB.MEMBERIDYou need some whitespace after the keyword "and", in the line above and also several places below.
    andAB.STATUS = ES.STATUS(+)
    andAB.ABCDID = ED.ABCDID
    andAB.ABCDID = EP.ABCDID(+)
    and EP.MEBER_ID = PA.MEBER_ID(+)
    andAB.BID = R.BID(+)
    andAB.SAMPLEID = PR.SAMPLEID(+)
    andAB.ABCDID = BSG.ABCDID(+)
    andAB.INFOCALLID = C.INFOCALLID(+)
    and YZ.SOMETYPEID = ''10000''
    ANDAB.INSERTTIME > SYSDATE - 35
    ANDAB.INSERTTIME < '' Did something get cut off here?
    AND WX.NAME IN (''ABCD'',
    ''EFGH'',
    ''IJKL'',
    ''MNOP'',
    ''QRST'',
    ''UVWX'')
    AND MN.NAME = ''FINISHED'' 'It looks like you're constructing a string for EXECUTE IMMEDIATE, but why not just use a static query?
    secondpart:_ ***ERROR PART***
    V_SAMPLEQUERY := V_SAMPLEQUERY || ' ORDER BY AB.USER'; -- Here I am getting error saving ORA-06502 - numeric or value error
    USER is the name of a built-in function, which means it's not a good name for your own column. Try user_name instead.

  • Dynamic SQL query to get single value from list in a column

    I have two tables: tblWorkers and tblSkills. tblWorkers has a
    column, Skills, which is populated from a multiple-checkbox form
    field with one or more skill_IDs from tblSkills, so each
    tblWorkers.Skills consists of a list of one or more comma-delimited
    values. For any Skill_ID, I need to generate a listing of all
    Workers with the corresponding skill, so I have tried to do
    something along the lines of SELECT WorkerName FROM tblWorkers
    WHERE Skills IN (Skills, #FORM.Skill_ID#) ... or WHERE Skills IN
    (ListFind(Skills, #FORM.Skill_ID#)) ... etc. ??? My results (once I
    got data type mismatches out of the way) return all Workers, not
    just those with the desired Skill. There must be an easy way to do
    this ... How do people with a bit more CF/SQL experience than I
    have do this???

    quote:
    Originally posted by:
    lucapac
    I have two tables: tblWorkers and tblSkills. tblWorkers has a
    column, Skills, which is populated from a multiple-checkbox form
    field with one or more skill_IDs from tblSkills, so each
    tblWorkers.Skills consists of a list of one or more comma-delimited
    values. For any Skill_ID, I need to generate a listing of all
    Workers with the corresponding skill, so I have tried to do
    something along the lines of SELECT WorkerName FROM tblWorkers
    WHERE Skills IN (Skills, #FORM.Skill_ID#) ... or WHERE Skills IN
    (ListFind(Skills, #FORM.Skill_ID#)) ... etc. ??? My results (once I
    got data type mismatches out of the way) return all Workers, not
    just those with the desired Skill. There must be an easy way to do
    this ... How do people with a bit more CF/SQL experience than I
    have do this???
    As Kronin implied, we normalize our databases. If you don't
    understand that answer, the book "Database Design for Mere Mortals"
    is often mentioned on this forum.

  • SQL query to build search form

    I have a table Department, with primary key DeptId.
    Table: Department
    DeptId Text
    1 xx
    2 yy
    3 zz
    I have another table Person, with key (not primary, and not unique) PersonId, and foreign key DeptId
    Table: Person
    PersonId DeptId
    10 1
    11 1
    11 2
    12 2
    I want to build a search form, with search fields PersonId and DeptId, so that in the resulting table, some rows from the Department table are returned. It should obey the following:
    - If both PersonId and DeptId are selected, and there is a row in the table Person that matches the selection, the row from the Department table should be returned.
    - If only PersonId is selected, any row in Department table that matches a DeptId corresponding to that PersonId should be returned.
    - If only DeptId is selected, return that row from Department exactly once (even if that DeptId doesn't appear in Person table, like DeptId 3 in this example)
    - If neither is selected, return all rows from Department
    I'm using JDeveloper 11.1.2.2.0.
    Thanks,
    Zach

    Hi Navaneeth,
    Taking the example from your link:
    SELECT org.org_id, org.name FROM organisations org WHERE EXISTS
    (SELECT 1 FROM events evt WHERE evt.org_id = org.org_id
    AND evt.contact_name = 'Eddie Harris')
    Let's say I make evt.contact_name a dropdown list, and when I query it, I leave it unselected. Would it return org.org_id, org.name from a row that has no corresponding event row?

Maybe you are looking for