Help to write a query for cursor

Hi
I have a table trans_table which has records for all Credit (C) and Debit(D) entries for customers with the trans_date.
Now there is a column adj_amt which contains 0 for all D entries. But for C entries it contains current trans_amt - Prev trans_amt. If there is no prev record with C type then it is 0. i.e. If this the first C entry for a particular account then adj_amt will contain 0.
Below is the data.
create table trans_table (cust_id number, tran_date date, tran_amt number, tran_type char(1), adj_amt number);
1 01/01/2007 100     D 0
1 01/01/2007 100     D 0
1 01/02/2007 80     C 0
1 05/02/2007 200     C 120
1 08/02/2007 300     C 100
2 01/01/2007 100     D 0
2 05/01/2007 100     D 0
2 08/01/2007 100     D 0
2 09/01/2007 100     D 0
2 01/02/2007 90     C 0
2 05/02/2007 200     C 110
3 01/01/2007 100     D 0
3 05/01/2007 100     D 0
3 08/01/2007 100     D 0
4 01/01/2007 100     D 0
4 05/01/2007 100     D 0
4 06/01/2007 100     D 0
4 08/01/2007 100     D 0
4 10/01/2007 100     D 0
4 01/02/2007 100     C 0                    
Now hv to write a query whioch gives the cust_id , max(tran_amt) with D entry, max(trans_amt) wth C entry and if no C entry record then this column will be 0.
The output wud be klike this..
1 100 300
2 100 200
3 100 0
4 100 100
Since cust_id 3 has no C entry so 0 value in 3rd column.
Now i wrote a query for cust_id and 2nd column, but am not able to get the 3rd column.
Is it possible to get the above output..
I wud be using this query in a cursor which wud fetch from 20 million transactions.
Thanks in advance
Piks
(I am pasting the insert script if required)
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (4, to_date('01-02-2007', 'dd-mm-yyyy'), 100, 'C', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (4, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (4, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (4, to_date('10-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (4, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (4, to_date('06-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (3, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (3, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (3, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (2, to_date('05-02-2007', 'dd-mm-yyyy'), 200, 'C', 110);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (2, to_date('01-02-2007', 'dd-mm-yyyy'), 90, 'C', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (2, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (2, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (2, to_date('09-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (2, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (1, to_date('08-02-2007', 'dd-mm-yyyy'), 300, 'C', 100);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (1, to_date('05-02-2007', 'dd-mm-yyyy'), 200, 'C', 120);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (1, to_date('01-02-2007', 'dd-mm-yyyy'), 80, 'C', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (1, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
values (1, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);

Hitherto an attempt:
test@ORA92>
test@ORA92> select * from trans_table;
   CUST_ID TRAN_DATE             TRAN_AMT T    ADJ_AMT
         4 02/01/2007 00:00:00        100 C          0
         4 01/05/2007 00:00:00        100 D          0
         4 01/01/2007 00:00:00        100 D          0
         4 01/10/2007 00:00:00        100 D          0
         4 01/08/2007 00:00:00        100 D          0
         4 01/06/2007 00:00:00        100 D          0
         3 01/08/2007 00:00:00        100 D          0
         3 01/05/2007 00:00:00        100 D          0
         3 01/01/2007 00:00:00        100 D          0
         2 02/05/2007 00:00:00        200 C        110
         2 02/01/2007 00:00:00         90 C          0
         2 01/05/2007 00:00:00        100 D          0
         2 01/01/2007 00:00:00        100 D          0
         2 01/09/2007 00:00:00        100 D          0
         2 01/08/2007 00:00:00        100 D          0
         1 02/08/2007 00:00:00        300 C        100
         1 02/05/2007 00:00:00        200 C        120
         1 02/01/2007 00:00:00         80 C          0
         1 01/01/2007 00:00:00        100 D          0
         1 01/01/2007 00:00:00        100 D          0
20 rows selected.
test@ORA92>
test@ORA92> select
  2    cust_id,
  3    max(case when tran_type = 'D' then tran_amt else 0 end) as max_c,
  4    max(case when tran_type = 'C' then tran_amt else 0 end) as max_d
  5  from trans_table
  6  group by cust_id;
   CUST_ID      MAX_C      MAX_D
         1        100        300
         2        100        200
         3        100          0
         4        100        100
4 rows selected.
test@ORA92>
test@ORA92>Cheers
pratz

Similar Messages

  • Need help to write a query for Update statement with  join

    Hi there,
    The following update statement gives me error as the given table in set statement is invalid. But its the right table .
    Is the statement correct? Please help .
    update (
           select distinct(vpproadside.VEHICLE_CRED_OVERRIDE.vin)            
             from vpproadside.VEHICLE_CRED_OVERRIDE
             join vpproadside.vpp_vehicle
               on vpproadside.vpp_vehicle.vin = vpproadside.VEHICLE_CRED_OVERRIDE.vin
            where VPP_CARRIER_SEQ_NUMBER = 90
              and EXPIRY_DATE = '17-MAR-10'
       set vpproadside.VEHICLE_CRED_OVERRIDE.EXPIRY_DATE = '15-SEP-10';Edited by: Indhu Ram on Mar 12, 2010 1:00 PM
    Edited by: Indhu Ram on Mar 12, 2010 1:22 PM
    Edited by: Indhu Ram on Mar 12, 2010 2:35 PM
    Edited by: Indhu Ram on Mar 15, 2010 8:04 AM
    Edited by: Indhu Ram on Mar 15, 2010 8:06 AM
    Edited by: Indhu Ram on Mar 15, 2010 8:28 AM

    Ask Tom has very good discussion about this, if UPDATE does not work for PK issue, you can use MERGE
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:760068400346785797

  • Need to write SQL query for hierarchy

    Hi Guys,
    I need help to write a query for the below scenario.
    I have one organization, under that I have many branches. when i provide input to query on last branch, i need to display all parents of that branch like below
    Organization   ---parent
      branch 1  ---child
         branch11
         branch 12
        branch 2   ---child
          branch 21
         branch 3 ----child
          branch 31
          branch 32
          branch 33
    here, when i provide input as branch 3, then I need to fetch results branch 2, branch 1 till organization.
    can any one help me on this.
    Thanks in advance!
    Regards,
    LKR

    Hi,
    Is this the same question as
    https://community.oracle.com/thread/2616828
    Please don't post the same question over and over.  Mark this thread as "Answered" right away, and continue in the other thread (if necessary; the other thread has answers.)

  • How to write sql query for counting pairs from below table??

    Below is my SQL table structure.
    user_id | Name | join_side | left_leg | right_leg | Parent_id
    100001 Tinku Left 100002 100003 0
    100002 Harish Left 100004 100005 100001
    100003 Gorav Right 100006 100007 100001
    100004 Prince Left 100008 NULL 100002
    100005 Ajay Right NULL NULL 100002
    100006 Simran Left NULL NULL 100003
    100007 Raman Right NULL NULL 100003
    100008 Vijay Left NULL NULL 100004
    It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
    I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains  both left_leg and right_leg id, then it is called pair.)
    I know there are three pairs under id "100001" :-
    1.  100002 and 100003
    2.  100004 and 100005
    3.  100006 and 100007
        100008 will not be counted as pair because it does not have right leg..
     But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
    Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005

    Sounds like this to me
    DECLARE @ID int
    SET @ID = 100001--your passed value
    SELECT left_leg,right_leg
    FROM table
    WHERE (user_id = @ID
    OR parent_id = @ID)
    AND left_leg IS NOT NULL
    AND right_leg IS NOT NULL
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Write a query for finding STDDEV for OLAP Cube

    Can anybody post a sample query which will find the standard deviation of sales for each month in the sample GLOBAL OLAP schema for Oracle 11g.
    The OLAP option automatically generates a set of relational views on cubes, dimensions, and hierarchies in Oracle 11g. So how can i write a query for finding the Standard deviation on these views.

    The easiest way to do this is to do this directly within the AW using a custom calculated measure. In AWM11g you can use the 11g custom measure wrapper to execute the STDDEV function:
    olap_dml_expression('function',data type)
    For example
    olap_dml_expression('STDDEV(cube_name, time_dimension_name)',number)
    You can then simply expose this calculated measure as another column in your SQL View and then there is no need to use the SQL equivalent. This means the calculation is performed inside the AW (which is as close to the source data as you can get) ensuring performance of the whole query remains high.
    If necessary you can take this a step further, if required, and wrap the STDDEV calculation within an OLAP DML program that would allow you to manipulate the status of time within the calculation. For example, you may want the STDDEV calc to only take into account the last 12 time periods rather than all time periods currently in status.
    Hope this helps
    Keith Laker
    Oracle EMEA Consulting
    OLAP Blog: http://oracleOLAP.blogspot.com/
    OLAP Wiki: http://wiki.oracle.com/page/Oracle+OLAP+Option
    DM Blog: http://oracledmt.blogspot.com/
    OWB Blog : http://blogs.oracle.com/warehousebuilder/
    OWB Wiki : http://wiki.oracle.com/page/Oracle+Warehouse+Builder
    DW on OTN : http://www.oracle.com/technology/products/bi/db/11g/index.html

  • Need a help to write the query

    Hi,
    I need a small help to write the query.
    I have a table contains couponid,coupon,createdate,expirationdate,assigndate from couponday table
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.
    Thanks in advance

    Hi,
    user586 wrote:
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.If you mean expirationdate (datatype: DATE) is not within 30 days (past or future) of run time, then:
    SELECT  coupon          -- or whatever columns you want
    FROM    table_x
    WHERE   expirationdate  NOT BETWEEN  SYSDATE - 30
                                AND      SYSDATE + 30
    ;

  • Write a query for which has its field name as input parameter

    Hi All,
    I need to write a query for SSRS report. StartDate, EndDate and ChangedField are the input parameters.
    Let's consider that we are taking employee details. The table contains lot of fields like emp name,add1,add2,city,state,country,designation,rank etc. We have almost 30 fields are there. These field name are coming as input parameter "ChangedField"
    . I need to show the records which belong to the start date and end date also data change for the field which is given in the input parameter "ChangedField".  That meand If I give ChangedField="city", we need to check for data which
    has been updated with new City name in the given time period.
    Please suggest.
    Regards,
    Julie

    Hi Julie,
    Per my understanding that you have many fields in the table to be displayed in the report, some of the fields will always show and others should be hide and show based on the selection of the parameters which include the columns names, right?
    By default, we can't display columns dynamically in the report automatically without insert this column in the report before. So, in your scenario, I will suggest you to add all the columns in the reports first and then add show/hide expression on
    the column visibility based on the selection of the parameter "ChangedField", when some of the fields selectted they will be display, otherwise they will be hidden. For the fields which you want to be always displayed will not need to add the expression
    in the column visibility.
    Details steps about how to acheive this, please referencet to the  article below:
    Displaying Dynamic Columns in SSRS Report
    If you still have any problem, please feel free to ask.
    Regards,
    Vicky Liu
    Vicky Liu
    TechNet Community Support

  • Please help to modifiy this query for better performance

    Please help to rewrite this query for better performance. This is taking long time to execute.
    Table t_t_bil_bil_cycle_change contains 1200000 rows and table t_acctnumberTab countains  200000 rows.
    I have created index on ACCOUNT_ID
    Query is shown below
    update rbabu.t_t_bil_bil_cycle_change a
       set account_number =
           ( select distinct b.account_number
             from rbabu.t_acctnumberTab b
             where a.account_id = b.account_id
    Table structure  is shown below
    SQL> DESC t_acctnumberTab;
    Name           Type         Nullable Default Comments
    ACCOUNT_ID     NUMBER(10)                            
    ACCOUNT_NUMBER VARCHAR2(24)
    SQL> DESC t_t_bil_bil_cycle_change;
    Name                    Type         Nullable Default Comments
    ACCOUNT_ID              NUMBER(10)                            
    ACCOUNT_NUMBER          VARCHAR2(24) Y    

    Ishan's solution is good. I would avoid updating rows which already have the right value - it's a waste of time.
    You should have a UNIQUE or PRIMARY KEY constraint on t_acctnumberTab.account_id
    merge rbabu.t_t_bil_bil_cycle_change a
    using
          ( select distinct account_number, account_id
      from  rbabu.t_acctnumberTab
          ) t
    on    ( a.account_id = b.account_id
           and decode(a.account_number, b.account_number, 0, 1) = 1
    when matched then
      update set a.account_number = b.account_number

  • How to write a query for grouping them the columns and give the sequence order to each group/

    Hi i have table that contains country columns .
    India,USA,UK like these when ever the group changed into the differt country i make a group and arrange them the sequence into those Countries
    like below
    1)India
    2)India
    1)USA
    2)USA
    like these to write a query ..........pls help me for this query

    Assuming you're using SQL Server you can ask here:
    http://www.sqlteam.com/forums/forum.asp?FORUM_ID=23
    Otherwise, please ask in the relevant forum for the type of technology you're using.
    Basically it's either:
    select *
    from [table name]
    order by country
    If you want to do something with groups do something like:
    select (max) income, country
    from [table name]
    group by country
    Kind regards,
    Margriet Bruggeman
    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

  • How to write selection Query for the following requirment.

    Hi All,
    I am new to ABAP, I need a help ,
    I need to select all plants(WERKS) from MARC at Plant/Material level,
    then I need to take all sales organozation(VKORG) from T001w,
    then I need the company code(BUKRS) from TVKO based on VKORG,
    then I need the currency key(WAERS) from T001 based on BUKRS,
    Can any one help me in writing selection Query for the same?
    Thanks All,
    Debrup.

    Hi,
    Its easy for you if you learn SELECT with JOIN to complete your task. So SEARCH the forum with SELECT statement and you will get a lot of examples using which you can write your own.
    If you struck up anywhere revert back.
    Regards
    Karthik D

  • How to write sql query for below example.

    Hi,
    I have requirement.
    There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
    example: table TEST.
    COLA COLB COLC COLD
    MANAGER 5 NULL null
    SR.MANAGE 6 3 NULL
    VP 5 5 4
    I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
    I need output as below.
    COLB COLC COLD
    5(MANGER) 3(sr.manger) 5(vp)
    Please provide the for above mentioned output.
    Thanks

    <<if COLA IS MANAGER THEN CONSIDER MANGER RECORD>>
    What does this sentence means? COLA does not cnatin a single record but the number of records with diffrent values.
    Regards
    Arun

  • How to write sql query for below mentioned eaxmple.

    Hi,
    I have requirement.
    There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
    example: table TEST.
    COLA COLB COLC COLD COLE COLF MANAGER 5 NULL NULL 3 NULL
    SR.MANAGER 6 3 NULL NULL NULL
    VP 5 5 4 5 5
    I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
    I need output as below.
    COLB COLC COLD COLE COLF
    5(manager) 3(sr.manger) 4(vp) 3(manger) 3(vp)
    Please provide the for above mentioned output.
    Thanks

    Duplicate thread. please view the answer posted in your first thread.
    how to write sql query.
    And, please don't post any duplicate thread.
    Regards.
    Satyaki De.

  • How to write a query for the following issue

    Hello,
    I would like to write a query to display the result in the following format 
    Item
    Categort1
    Categort2
    Categort3
    Categort4
    Categort5
    Categort6
    Min
    Max
    Avg
    Min
    Max
    Avg
    Min
    Max
    Avg
    Min
    Max
    Avg
    Min
    Max
    Avg
    Min
    Max
    Avg
    01
    02
    03
    04
    For every item for the category i need to find Min,Max and Avg from the Value column
    Table structure is as follows
    ID
    Item Id
    Item
    Category
    value
    1
    01
    A
    Categort1
    1
    2
    01
    A
    Categort1
    2
    3
    01
    A
    Categort1
    3
    4
    02
    B
    Categort2
    7
    5
    02
    B
    Categort2
    8
    6
    03
    C
    Categort3
    6
    7
    04
    D
    Categort4
    12
    8
    04
    D
    Categort4
    14

    SELECT ItemID,
    MIN(CASE WHEN Category = 'Categort1' THEN value END) AS Min_category1,
    MAX(CASE WHEN Category = 'Categort1' THEN value END) AS Max_category1,
    AVG(CASE WHEN Category = 'Categort1' THEN value END) AS Avg_category1,
    MIN(CASE WHEN Category = 'Categort2' THEN value END) AS Min_category2,
    MAX(CASE WHEN Category = 'Categort2' THEN value END) AS Max_category2,
    AVG(CASE WHEN Category = 'Categort2' THEN value END) AS Avg_category2,
    MIN(CASE WHEN Category = 'Categort6' THEN value END) AS Min_category6,
    MAX(CASE WHEN Category = 'Categort6' THEN value END) AS Max_category6,
    AVG(CASE WHEN Category = 'Categort6' THEN value END) AS Avg_category6
    FROM Table
    GROUP BY ItemID
    The format can be achieved using tools like SSRS
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Need to write a query for extractions

    How can I extract the following data from Oracle:
    1. Segment1 and description from mtl_system_items_b
    2. Onhand quantity that is orderable (Not under the WIP manufacturing or PO)
    3. Onhand quantity that not orderable (That is under WIP manufacturing or PO)
    4. Retail Price of these items.
    There is only one organization_id =21 needs to be considered. This extract needs to be a daily kind of an extract. If you can help me in writing the query for all the fields above with proper joins, I'll really appreciate.
    Thanks & Regards
    KM

    Hi,
    Primary_uom_code and Primary_transaction_quantity shows the items quantity on the basis of primary code
    whereas Secondary_uom_code and secondary_transction_quantity shows the items quantity on different code.
    e.g. suppose you have an item Pen opened on the primary code as Each. 1 Each = 1 Pen.
    You have a conversion set as 10 Each = 1 Pkt.
    At the time of transaction if you selects as 1 Pkt,
    The primary_transaction_quantity will show 10 Each where as the secondary_transaction_quantity will show 1Pkt.
    Now as the transaction has done as Pkt the column Transaction_quantity will display 10Pkt.
    If you have done this transaction on the primary code as 10 Each.
    The primary_transaction_quantity would show the same 10 Each but the Secondary_transaction_quantity will by
    null and the transaction_quantity will be 10 Each.
    Hope this will clarify the issue.
    2) why you want to join these tables
    How can I join the following tables: qp_list_lines and mtl_system_items_b? I want to find the price of each
    item. I tried using inventory_item_id but it does not work as inventory_item_id can be null in qp_list_lines table.i have not been able to find any thing in table qp_list_lines.

  • How  to write select query for this

    Hi,
    I had a html form and in the for i had drop down box and it needs to select multiple values from the drop down box. When i select multiple values then i have to write the SQL select statement  query .
    When i try to write the select statement and trying to run i am getting error.
    select * from Table
    where emo_no = '1,2,3'
    this is how i write query please suggest me how  to write query for selecting multiple values from the drop down box.
    Thanks

    select * from Table
    where emo_no in ( 1,2,3)
    for integer values
    select * from Table
    where emo_no in ('1','2','3')
    for characters
    If we talk about large scale applications that may have millions of records, I would suggest this.
    declare @t table (v int)
    insert into t (v) values (1)
    insert into t (v) valves (2)
    insert into t (v) values (3)
    select *
    from table
         inner join @t t on table.emo_no = t.v
    Using "in" for a where clause is not so bad for filtering on a few values, but if you are filtering a lot of rows and a lot of values (emo_no) the performance degrades quickly for some reasons beyond the scope of this.
    This is just one solution, I'll through this out as well, instead of an in memory (@t) table, doing a disk based temp table (#t) and creating an index on the column "v".
    create table #t (v int)
    insert into #t (v) values (1)
    insert into #t (v) valves (2)
    insert into #t (v) values (3)
    create index ix_t on #t (v)
    select *
    from table
         inner join #t t on table.emo_no = t.v
    drop table #t
    Pardon any syntax errors and careful using a drop statement.
    Sometimes in memory tables work better than disk temp tables, it takes some testing and trial and error depending on your datasets to determine the best solution.
    Probably too much info  ;-)
    Byron Mann
    [email protected]
    [email protected]
    Software Architect
    hosting.com | hostmysite.com
    http://www.hostmysite.com/?utm_source=bb

Maybe you are looking for