Help in this query

How to do query for this:
Table: Acct_To_Print
Fields:
SELECT * FROM Acct_To_Print where BILL_DATE='19-FEB-07'
BILL_DATE ACCT_NO BP_PARTN_NUM
19-FEB-07 A500088 197
19-FEB-07 A306004 197
19-FEB-07 A570105 197
19-FEB-07 R500088 197
19-FEB-07 L306004 197
19-FEB-07 T570105 197
19-FEB-07 F500088 197
19-FEB-07 G306004 197
19-FEB-07 V570105 197
19-FEB-07 C500088 197
19-FEB-07 B306004 197
I want all the BP_PARTN_NUM field to change to 101 THEN 102 THEN 103 THEN 104 and so on
The division is depend on table INV_ADDR, count all the records in the INV_ADDR then divide by total partition needed for example
total partition needed = 4
select ceil(count(*)/4) from INV_ADDR where BILL_DATE='19-FEB-07';
let say it returns 3
Tht means we need to start from partition 101 3 times then 102 3 times then 103 3 times then so on
then the output should be
Table: Acct_To_Print
Fields:
BILL_DATE ACCT_NO BP_PARTN_NUM
19-FEB-07 A500088 101
19-FEB-07 A306004 101
19-FEB-07 A570105 101
19-FEB-07 R500088 102
19-FEB-07 L306004 102
19-FEB-07 T570105 102
19-FEB-07 F500088 103
19-FEB-07 G306004 103
19-FEB-07 V570105 103
19-FEB-07 C500088 104
19-FEB-07 B306004 104
If can make the total partition needed,bill_date and start partition number as parameters
Anyone can help me out with this query

This will work :
select BILL_DATE, ACCT_NO , 101+floor((rownum-1)/cnt)
from
(select BILL_DATE,
ACCT_NO ,
(select ceil(count(*)/4) from INV_ADDR where BILL_DATE='19-FEB-07') as cnt
from Acct_To_Print
)temptab

Similar Messages

  • Please need help with this query

    Hi !
    Please need help with this query:
    Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
    Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
    select distinct a.id,
    create_date,
    a.loanid,
    a.rate,
    a.pays,
    a.gracetime,
    a.emailtosend,
    d.first_name,
    d.last_name,
    a.user_id
    from CLAL_LOANCALC_DET a,
    loan_Calculator b,
    bv_user_profile c,
    bv_mr_user_profile d
    where b.loanid = a.loanid
    and c.NET_USER_NO = a.resp_id
    and d.user_id = c.user_id
    and a.is_partner is null
    and a.create_date between
    TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
    TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
    order by a.create_date

    Perhaps something like this...
    select id, create_date, loanid, rate, pays, gracetime, emailtosend, first_name, last_name, user_id
    from (
          select distinct a.id,
                          create_date,
                          a.loanid,
                          a.rate,
                          a.pays,
                          a.gracetime,
                          a.emailtosend,
                          d.first_name,
                          d.last_name,
                          a.user_id,
                          max(create_date) over (partition by a.user_id, a.loadid) as max_create_date
          from CLAL_LOANCALC_DET a,
               loan_Calculator b,
               bv_user_profile c,
               bv_mr_user_profile d
          where b.loanid = a.loanid
          and   c.NET_USER_NO = a.resp_id
          and   d.user_id = c.user_id
          and   a.is_partner is null
          and   a.create_date between
                TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
                TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
    where create_date = max_create_date
    order by create_date

  • Help on this query

    Hi , i have a table of 3000000 rows, and am executing this script
    update products set cas_num = trim(cas_num
    due to which it is taking 99.2% of UNDO TS ,
    I was just exactly thinking the same if i can put it in a simple SQL loop statement and have it commit for like every 10,000 rows.
    Could you please help me a better way to to accomplish this simple SQL loop statement?
    Thanks
    Vk

    Check your original post. You wouldn't get different replies as the same set of people check here too.
    Help on this query ( taking 99% of UNDO TS on 10g )

  • Please help me this query

    Plz help me this query:
    Create a query that displays the employees last name and indicates the amounts of their annual salaries with *. Each * signifies a thousand $'s.
    EG. If the salary of an employee with last name king is 2000 then it should appear as follows:
    King **
    2 (*) for 2 thousand.
    Thanks in advance

    Hi,
    I was able to solve this query on my own. For those who might face a problem in future the solutionto it is:
    SELECT rpad(last_name, length(last_name) + (salary/1000), '*') AS employee_and_their_salaries
    FROM employees;
    Thanks

  • Need help in this query using Case Statement

    I have the following query which is currently existing and I am adding few more conditions based on the new requirements which is based on a particular flag set as 1 or 0.
    If it is set to 1 then I should use the old query as it is and if it is set to 0 then I should add the new conditions.
    Basically when the flag is set to 0, I shouldnt be including some of the records that already exists and should include only new records. This is based on the plan_type_ids in (1,2,3,4).
    Hence I am using the Case statement to check if the plan_type_id is in (1,2) then do a set of not exists and if the plan_type_id in (3,4) then do set of not exists.
    But when I run this query it is giving me error. What am I doing wrong?
    Is there any other simple way to combine all the not exists for all of those select statements as given after the line ------------------------- into a single one?
    What am I doing wrong?
    I tried putting the NOT EXists before the case too but that too didnt work.
    Please help. Appreciate it.
    Thank you in advance.
    SELECT
    ee.employee_id
    ,'WELCOMEMSG'
    ,DECODE( me.member_enrollment_id
    ,first_enr.enrollment_id
    ,20
    ,23
    ) status_id
    ,me.member_enrollment_id
    ,wk.welcome_msg_id
    FROM wk
    ,employees ee
    ,MEMBER_ENROLLMENTS me
    ,plans pl
    ,( SELECT employee_id
    ,plan_type_id
    ,start_date
    ,plan_id
    ,MIN(MEMBER_ENROLLMENT_ID) member_enrollment_id
    FROM ( SELECT me.employee_id
    ,DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id
    ,pl.start_date
    ,wk.plan_id
    ,me.member_enrollment_id
    FROM wk
    ,PLANS pl
    ,MEMBER_ENROLLMENTS me
    WHERE wk.done_by = nvl('TEST' ,wk.done_by)
    AND wk.welcome_msg_name <> 'NONE'
    AND pl.employer_id = wk.employer_id
    AND wk.employer_id = 5
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
    AND pl.plan_id = NVL(wk.plan_id,pl.plan_id)
    AND me.plan_id = pl.plan_id
    AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
    AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
    MINUS
    SELECT me.employee_id
    ,DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id
    ,pl.start_date
    ,NULL plan_id
    ,me.member_enrollment_id
    FROM wk
    ,PLANS pl
    ,MEMBER_ENROLLMENTS me
    WHERE wk.done_by = nvl(NULL,wk.done_by)
    AND wk.welcome_msg_name <> 'NONE'
    AND pl.employer_id = wk.employer_id
    AND wk.employer_id = 5
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
    AND pl.plan_id = wk.plan_id
    AND me.plan_id = pl.plan_id
    AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
    AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
    WHERE employee_id = 100
    GROUP BY employee_id ,plan_type_id,start_date ,plan_id
    )first_enr
    ,MEMBER_EVENTS mv
    WHERE wk.done_by = nvl(NULL,wk.done_by)
    AND wk.employer_id = ee.employer_id
    AND ee.employee_id = me.employee_id
    AND ee.employee_id = 100
    AND me.plan_id = pl.plan_id
    AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
    AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
    AND is_expired(me.employee_id,me.plan_id) = 'Y'
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
    AND pl.plan_id=nvl(wk.plan_id,pl.plan_id)
    AND me.employee_id = first_enr.employee_id
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = first_enr.plan_type_id
    AND pl.start_date = first_enr.start_date
    AND me.member_enrollment_id = mv.member_enrollment_id (+)
    AND 'WELCOMEMSG' = mv.event_name(+)
    AND mv.member_enrollment_id IS NULL
    AND wk.welcome_msg_name <> 'NONE'
    AND NVL(first_enr.plan_id,0) = NVL( wk.plan_id,0)
    AND (FN_get_all_participant(wk.employer_id) = 1
    OR
    (FN_get_all_participant(wk.employer_id) = 0
    AND (CASE WHEN pl.plan_type_id IN (1,2)
    THEN NOT EXISTS (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = me.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date < pl.START_DATE
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id in (1, 2)
    UNION
    (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = me.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date = (pl.start_date - 365)
    \ AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id = wk.plan_type_id
    UNION
    (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = men2.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id = 2
    UNION
    (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = men2.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id = 1
    WHEN pl.plan_type_id IN (3, 4)
    THEN NOT EXISTS (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = men2.employee_id
    AND nvl(ma.account_end_date, sysdate) <= trunc(sysdate)
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date <= pl2.START_DATE
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id in (3, 4)
    END
    AND (CASE WHEN pl.plan_type_id IN (1,2)
    ERROR at line 89:
    ORA-00936: missing expression

    Maybe
    SELECT ee.employee_id,
           'WELCOMEMSG',
           DECODE(me.member_enrollment_id,first_enr.enrollment_id,20,23) status_id,
           me.member_enrollment_id,
           wk.welcome_msg_id
      FROM wk,
           employees ee,
           MEMBER_ENROLLMENTS me,
           plans pl,
           (SELECT employee_id,
                   plan_type_id,
                   start_date,
                   plan_id,
                   MIN(MEMBER_ENROLLMENT_ID) member_enrollment_id
              FROM (SELECT me.employee_id,
                           DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id,
                           pl.start_date,
                           wk.plan_id,
                           me.member_enrollment_id
                      FROM wk,
                           PLANS pl,
                           MEMBER_ENROLLMENTS me
                     WHERE wk.done_by = nvl('TEST',wk.done_by)  /* same as wk.done_by = 'TEST' */
                       AND wk.welcome_msg_name 'NONE'
                       AND pl.employer_id = wk.employer_id
                       AND wk.employer_id = 5
                       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
                       AND pl.plan_id = NVL(wk.plan_id,pl.plan_id)
                       AND me.plan_id = pl.plan_id
                       AND me.coverage_effective_date != NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
                       AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date,me.coverage_effective_date + 1)
                    MINUS
                    SELECT me.employee_id,
                           DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id,
                           pl.start_date,
                           NULL plan_id,
                           me.member_enrollment_id
                      FROM wk,
                           PLANS pl,
                           MEMBER_ENROLLMENTS me
                     WHERE wk.done_by = nvl(NULL,wk.done_by)  /* same as 1 = 1 */
                       AND wk.welcome_msg_name 'NONE'
                       AND pl.employer_id = wk.employer_id
                       AND wk.employer_id = 5
                       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
                       AND pl.plan_id = wk.plan_id
                       AND me.plan_id = pl.plan_id
                       AND me.coverage_effective_date NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
                       AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
             WHERE employee_id = 100
             GROUP BY employee_id,
                      plan_type_id,
                      start_date,
                      plan_id
           ) first_enr,
           MEMBER_EVENTS mv
    WHERE wk.done_by = nvl(NULL,wk.done_by)
       AND wk.employer_id = ee.employer_id
       AND ee.employee_id = me.employee_id
       AND ee.employee_id = 100
       AND me.plan_id = pl.plan_id
       AND me.coverage_effective_date != NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
       AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
       AND is_expired(me.employee_id,me.plan_id) = 'Y'
       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
       AND pl.plan_id = nvl(wk.plan_id,pl.plan_id)
       AND me.employee_id = first_enr.employee_id
       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = first_enr.plan_type_id
       AND pl.start_date = first_enr.start_date
       AND me.member_enrollment_id = mv.member_enrollment_id(+)
       AND 'WELCOMEMSG' = mv.event_name(+)
       AND mv.member_enrollment_id IS NULL
       AND wk.welcome_msg_name != 'NONE'
       AND NVL(first_enr.plan_id,0) = NVL(wk.plan_id,0)
       AND (
            FN_get_all_participant(wk.employer_id) = 1
        OR
            (FN_get_all_participant(wk.employer_id) = 0
       AND   NOT EXISTS(SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = me.employee_id
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date < pl.START_DATE
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id in (1,2)
                           and pl.plan_type_id IN (1,2)
                        UNION
                        SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = me.employee_id
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date = (pl.start_date - 365)
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id = wk.plan_type_id
                           and pl.plan_type_id IN (1,2)
                        UNION
                        SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = men2.employee_id
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id = (1,2)
                           and pl.plan_type_id IN (1,2)
                        UNION
                        SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = men2.employee_id
                           AND trunc(nvl(ma.account_end_date,sysdate)) <= trunc(sysdate)
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date <= pl2.START_DATE
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id in (3,4)
                           and pl.plan_type_id IN (3,4)
           )Regards
    Etbin

  • Help on this query that is using sysdate

    Hi
    I'm trying to get the objects created on the current date using the following query
    select owner,object_name,object_type,created from dba_objects where created = TRUNC(SYSDATE);
    but i don't see any rows for this query;
    but when i use this query .
    SELECT owner,object_name,object_type,created FROM DBA_OBJECTS ORDER BY CREATED DESC;
    i see some objects created today. Can you help me out if i missed any thing in the sysdate query.
    thanks

    Try this..
    select owner,object_name,object_type,created
    from dba_objects
    where trunc(created) = TRUNC(SYSDATE);                                                                                                                                                                                                                                                       

  • Can any one help with this query please

    I have a table something as below
    Things_t
    Things Characteristic Char Value
    Item 1 Colour Red
    Item 1 Packaging
    Item 2 Shape Square
    Item 2 Brand Spunk
    Now i want to reterive an item with none of its char values as Null. Using the query “ select distinct things from things_t where char value is Null ” will fetch the item 1 also together with item 2. i want to fetch a record from thing for which none of the char values are Null such as Item 2. Can you please help me with this query.

    Try this:
    WITH t AS
    (SELECT 1 item_id, 17436 chr_id, 14225034 chr_val_id FROM dual UNION
    SELECT 1 item_id, 39 chr_id, 14276173 chr_val_id FROM dual UNION
    SELECT 1 item_id, 17774 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 1 item_id, 265 chr_id, 20502978 chr_val_id FROM dual UNION
    SELECT 1 item_id, 16978 chr_id, 797233 chr_val_id FROM dual UNION
    SELECT 1 item_id, 13092 chr_id, 5666917 chr_val_id FROM dual UNION
    SELECT 1 item_id, 15228 chr_id, 1209758 chr_val_id FROM dual UNION
    SELECT 2 item_id, 112 chr_id,  12705342 chr_val_id FROM dual UNION
    SELECT 2 item_id, 6945 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 2 item_id, 70 chr_id, 12597376 chr_val_id FROM dual UNION
    SELECT 2 item_id, 16832 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 2 item_id, 7886 chr_id, 9588619 chr_val_id FROM dual UNION
    SELECT 2 item_id, 6986 chr_id, 2659351 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9531 chr_id, 8910943 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9798 chr_id, 8717531 chr_val_id FROM dual UNION
    SELECT 3 item_id, 17446 chr_id, 12266441 chr_val_id FROM dual UNION
    SELECT 3 item_id, 4830 chr_id, 13683090 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9518 chr_id, 834772 chr_val_id FROM dual UNION
    SELECT 3 item_id, 11031 chr_id, 20233753 chr_val_id FROM dual UNION
    SELECT 3 item_id, 12564 chr_id, 2282478 chr_val_id FROM dual)
    SELECT DISTINCT item_id
    FROM   t
    MINUS
    SELECT DISTINCT item_id
    FROM   t
    WHERE  chr_val_id IS NULLOr this:
    SELECT item_id
    FROM  (SELECT   item_id,
                    MIN(NVL(chr_val_id, -1)) min_chr_val_id
           FROM     t
           GROUP BY item_id)
    WHERE  min_chr_val_id != -1Edited by: lee200 on Oct 15, 2012 9:22 AM

  • Pls help with this query

    Hello
    If I run the following query I correctly get a sum of the invoices for a particular BP for Jan 09
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] -  T2.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    However I need the value of credit notes to be reflected in the figure so I amended the query to be:
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] -  T2.[VatSum])ELSE 0 END) - SUM(CASE WHEN  T3.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T3.[DocTotal] -  T3.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode INNER JOIN ORIN T3 ON T0.CardCode = T3.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    When I run this query the output is incorrect ................ Is the fault to do with my JOINS?
    Thanks

    Hi Just to clarify
    This query, which just totals any invoices for a BP for January, correctly returns a value of £9660.44 :
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] -  T2.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode  WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    This next query, which just totals any credit notes for a BP in January, correctly returns a value of £567.73 :
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T3.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T3.[DocTotal] -  T3.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN ORIN T3 ON T0.CardCode = T3.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    I want a query which return a value of 9660.44 minus 567.73 (ie £9092.71)
    My original effort at this returned -£82,608.65 !!!!!!!!!
    and Gordon's amended version returned -£40,281.74 !!!!!
    Thanks for your patience
    Steve

  • Need help with this query

    Hi,
    I am using SQL Server 2008 Enterprise edition 64 bit on Windows serer 2008 enterprise edition 64 bit.
    The below query works fine. I am trying to rewrite the code without using the sub query. In the end i should get the same results for both the query which we are going to rewrite and the below mentioned query.
    Can any help me please.
    SELECT
    t1.DOCUMENT_NO,
    RTRIM(CAST(t1."ENVIRONMENT_CD" AS VARCHAR(5))) + '*' + RTRIM(CAST(t1."ORDER_NO" AS VARCHAR(25)))
    +
    CASE WHEN (SELECT COUNT(DISTINCT S1.TEST_REPORTING_MATERIAL_SID)
    FROM dbo.TBLTEST S1 ON S1.DOCUMENT_NO = T1.DOCUMENT_NO
    WHERE S1.TEST_REPORTING_MATERIAL_SID > 0
    AND S1.COMPANY_CD = T1.COMPANY_CD AND S1.INVOICE_SEQ_NO = T1.INVOICE_SEQ_NO
    AND S1.DOCUMENT_ITEM_NO = T1.DOCUMENT_ITEM_NO
    AND S1.ORDER_NO = T1.ORDER_NO) > 1 THEN
    RTRIM(CAST(t1."TEST_REPORTING_MATERIAL_SID" AS VARCHAR(10)))
    ELSE
    END as sum_key,
    t1.SALES_ANALYSIS_CD
    FROM
    dbo.TBLTEST_ALL T1
    WHERE
    t1.TIME_SID >= 20001001 ;
    I tried to use a left outer Join , it did not work out. I tried to take the subquery and assign it to a variable and use in the above mentioned query. But i am not getting the same results.
    I don't want to use the above query so i am planning to rewrite the code.
    Thank You,

    I doubt that the query you posted works fine, because:
    FROM dbo.TBLTEST S1 ON S1.DOCUMENT_NO = T1.DOCUMENT_NO
    This is a syntax error. I will have to assume that your inteded query reads:
    SELECT t1.DOCUMENT_NO,
           rtrim(cast(t1."ENVIRONMENT_CD" AS varchar(5))) + '*' +
           rtrim(cast(t1."ORDER_NO" AS varchar(25))) +
          CASE WHEN (SELECT COUNT(DISTINCT S1.TEST_REPORTING_MATERIAL_SID)
                     FROM   dbo.TBLTEST S1
                     WHERE  S1.DOCUMENT_NO = T1.DOCUMENT_NO
                       AND  S1.TEST_REPORTING_MATERIAL_SID > 0
                       AND  S1.COMPANY_CD = T1.COMPANY_CD
                       AND  S1.INVOICE_SEQ_NO = T1.INVOICE_SEQ_NO
                       AND  S1.DOCUMENT_ITEM_NO = T1.DOCUMENT_ITEM_NO
                       AND  S1.ORDER_NO = T1.ORDER_NO) > 1 THEN
                     RTRIM(CAST(t1."TEST_REPORTING_MATERIAL_SID" AS VARCHAR(10)))
               ELSE  '*-'
          END as sum_key, t1.SALES_ANALYSIS_CD
    FROM   dbo.TBLTEST_ALL T1
    WHERE  t1.TIME_SID >= 20001001
    Here is a query with a left join. Whether it is actually better than the one you have I am not sure.
    SELECT t1.DOCUMENT_NO,
           rtrim(cast(t1."ENVIRONMENT_CD" AS varchar(5))) + '*' +
           rtrim(cast(t1."ORDER_NO" AS varchar(25))) +
          CASE WHEN S1."EXISTS" IS NOT NULL
               THEN  RTRIM(CAST(t1."TEST_REPORTING_MATERIAL_SID" AS varchar(10)))
               ELSE  '*-'
          END as sum_key, t1.SALES_ANALYSIS_CD
    FROM   dbo.TBLTEST_ALL T1
    LEFT   JOIN (SELECT 1 AS "EXISTS"
                 FROM   dbo.TBLTEST
                 WHERE  TEST_REPORTING_MATERIAL_SID > 0
                 GROUP  BY DOCUMENT_NO, COMPANY_CD, INVOICE_SEQ_NO,
                           DOCUMENT_ITEM_NO, ORDER_NO
                 HAVING COUNT(DISTINCT TEST_REPORTING_MATERIAL_SID) > 1) AS S1
             ON S1.DOCUMENT_NO = T1.DOCUMENT_NO
           AND  S1.COMPANY_CD = T1.COMPANY_CD
           AND  S1.INVOICE_SEQ_NO = T1.INVOICE_SEQ_NO
           AND  S1.DOCUMENT_ITEM_NO = T1.DOCUMENT_ITEM_NO
           AND  S1.ORDER_NO = T1.ORDER_NO
    WHERE  t1.TIME_SID >= 20001001
    Erland Sommarskog, SQL Server MVP, [email protected]

  • SQL I need help with this query Please help

    List the names of all the products whose weight unit measure is “Gram”.  Order the list by product name.  Do not use JOINS, but use the IN clause with a sub-query.
    select Name
    from UnitMeasure
    where Name= 'Gram'
    order by Name
    I did this, but it seem that the requirement is different.

    As a guess:
    Select Name from Product
    where UnitMeasure in (Select Name from unitmeasure where name = 'Gram')
    Andy Tauber
    Data Architect
    The Vancouver Clinic
    Website | LinkedIn
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click
    "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

  • Please help on this query

    Hello everyone,
    i am pretty new and still learning SQL for my work.
    i have a query to pull all the data greater than 5,000,000 as below which worked sucessfully
    select a.bf_fund_cd fund, a.bf_bdob_cd object, b.lev5, b.bf_orgn_cd,sum(a.data) data
    from   cp_o_pbuser.bf_data a
    inner join cp_o_pbuser.bf_orgn_cnsl_tbl b
    on a.bf_orgn_cd = b.bf_orgn_cd
    WHERE a.bf_tm_perd_cd in (select bf_tm_perd_chld_cd from cp_o_pbuser.bf_tm_perd_cnsl_tbl where bf_tm_perd_select_cd='FINALTOT')
    and a.bf_bdob_cd in ('R_9001')
    having sum(a.data)>5000000
    group by a.bf_fund_cd, a.bf_bdob_cd, b.lev5,b.bf_orgn_cd
    order by a.bf_bdob_cd, b.lev5
    here is the result
    SS_26300_1 - Lev5 (everything rollup to this one)
    SS_26460_2 -  $  5,000,000 -->bf_orgn_cd (showed)
    SS_26461_2 - $6,000,000 -->bf_orgn_cd (showed)
    SS_26473_2 - $5,500,000 -->bf_orgn_cd (showed)
    SS_26475_2 -$4,000,000 -->bf_orgn_cd (not showed, i want to this record to appear too)
    However,  according to the new change, i will need to pull out all the data even though they are smaller than 5,000,000 in the
    lev5 colunm.  ex: there is one record missing in SS_26300_1 with SS_26475_2 in BF_ORGN_CD and the data is 4,000,000 which i need to include in my query.
    i really appreciate of your time and help.
    Sincerely,

    clear example: thank you
    SS_21111_1 -Lev5 (dont need to show) ---> less than 5,000000
    SS_21112_2 -$1,500,000(dont need to show)
    SS_21113_2 -$1,750,000(dont need to show)
    SS_26300_1 - Lev5 (everything rollup to this one) greater than 5,000,000 (everything needs to show)
    SS_26460_2 - $ 5,000,000 -->bf_orgn_cd (showed)
    SS_26461_2 - $6,000,000 -->bf_orgn_cd (showed)
    SS_26473_2 - $5,500,000 -->bf_orgn_cd (showed)
    SS_26475_2 -$4,000,000(i want to this one to show)

  • Can anyone help with this query

    create table customer(
    customer_email varchar(100) not null
    ,cust_id int not null
    ,constraint pk__customer primary key clustered(cust_id)
    Please help ASAP
    create table purchase (
    purchase_id int not null identity(1,1)
    ,cust_id int not null references customer(cust_id)
    ,product_name varchar(200) not null
    ,amount int not null
    ,constraint pk__purchase primary key clustered (purchase_id)
    Write a query that returns one row per customer with the following columns. Don't forget about customers with no purchases.
    customer_email
    qty of purchase
    sum of purchase amount

    Homework deadline looming?
    This will work in versions of Oracle from 9 up, and in all versions of Sybase/ SQL Server (which you seem to wnat based on the CREATE TABLE syntax).
    SELECT customer_email, COUNT(*) number_purchaes, SUM(amount) total_purchases
    FROM customer c
         LEFT OUTER JOIN purchases p ON c.cust_id = p.cust_id
    GROUP BY customer_emailTTFN
    John

  • Please help with this query!

    Hi All,
    I have this this table:
    Term Grade
    term_A A
    term_A A
    term_A B
    term_A B
    term_B D
    term_B F
    term_B F
    term_C C
    How do I display so that it appears this way?
    term_A A 2
    term_A B 2
    term_B D 1
    term_B F 2
    term_C C 1
    Thank you. Appreciate all the help!

    Please read about [url http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/functions001.htm#sthref962]aggregate functions in the manual.
    Regards,
    Rob.

  • Need help over this Query

    Hi, I need the Result set in the given below form. 
    Can any one please help me with this. 
    I am not able to get the 2nd most recent login date in the separate column. 
    Concerned Columns: Login date, login date2, login difference 
    Login date = Most recent login by the user
    Login date 2= 2nd most recent login by the user
    Login difference =  difference between last 2 logins 
    Help!!
    DECLARE @ownerid INT=304,@offset INT=330,@totalsec INT=86400
    SELECT USERNAME, LOGIN_ID,EMAIL,DEPARTMENT,LOGIN_DATE, LOGOUT_DATE, IP_ADDRESS,MACHINE_NAME,TOTAL_LOGIN_TIME,LOGIN_NUMBER,
    ROW_NUMBER() OVER (ORDER BY USERNAME, LOGIN_ID) AS ROWNUMBER
    FROM
    SELECT Usr.UserID [USERID], usr.UserName [USERNAME],usr.LoginID [LOGIN_ID],
    usrcontact.Email [EMAIL],
    UsrContact.Department [DEPARTMENT],
    MAX(DateAdd(mi,@offset, UsrHis.LoginDate)) [LOGIN_DATE],
    MAX(DateAdd(mi,@offset, UsrHis.LogoutDate)) [LOGOUT_DATE],
    UsrHis.IPAddress [IP_ADDRESS],UsrHis.MachineName [MACHINE_NAME],
    convert( NVARCHAR(50),(DATEDIFF(SECOND,UsrHis.LoginDate,UsrHis.LogoutDate))/@totalsec) + ' '+'Days'+ ' '+
    convert( NVARCHAR(50),((DATEDIFF(SECOND,UsrHis.LoginDate,UsrHis.LogoutDate))%@totalsec)/3600) + ' '+'hours'+ ' '+
    convert( NVARCHAR(50),(((DATEDIFF(SECOND,UsrHis.LoginDate,UsrHis.LogoutDate))%@totalsec)%3600/60)) + ' '+'Minutes'+ ' '+
    convert( NVARCHAR(50),(((DATEDIFF(SECOND,UsrHis.LoginDate,UsrHis.LogoutDate))%@totalsec)%3600%60)) + ' '+'Seconds'
    [TOTAL_LOGIN_TIME],
    ROW_NUMBER() OVER (PARTITION BY USR.USERID ORDER BY USRHIS.LOGINDATE) LOGIN_NUMBER
    FROM Az_User Usr Inner Join LoginHistory UsrHis
    ON UsrHis.OwnerID=Usr.AppOwnerID
    and
    Usr.UserID = UsrHis.UserID
    inner join UserContact UsrContact
    ON UsrContact.CompanyID = Usr.AppOwnerID
    and UsrContact.UserID = Usr.UserID
    WHERE usr.AppOwnerID = @ownerid
    GROUP BY Usr.UserID,Usr.UserName , usrcontact.Email , UsrHis.LoginDate,usr.LoginID,UsrContact.Department,
    UsrHis.LogoutDate,UsrHis.IPAddress,UsrHis.MachineName,
    Dateadd(D, Datediff(D, 0, Dateadd(mi, @offset, UsrHis.LoginDate)), 0)
    ) AS TEMP
    WHERE TEMP.LOGIN_NUMBER <=2
    Hi here is the Data type of the columns i am selecting.. and the result i am getting. 
    But here the total_login_time is wrong. .
    I need difference between last 2 logins not length of the session.  

    It is unclear what you are trying to achieve... 
    DATEDIFF(d,Logindate,Logindate2)
    AS [Login difference]
    FROM.....
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Help with this query

    eid manager
    1 111
    2 1
    3 2
    4 2
    5 2
    6 3
    7 5
    this is a table and i have to prduce output in the form which employee reports
    to which manager
    like
    row one: 7>5>2>1
    row two 6>3>2>1
    here in row one 7 reports to 5 which report to 2 which reports to 1.
    query should be only in sql no pl/sql.

    For a good discussion on this topic, see:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:2165134263446

Maybe you are looking for

  • My JTree is ugly, please help

    i want to add nodes to tree with nice style, that is: windows c++ calls the style as "has button and lines start at root". in other words, a small box with cross lines on the left side of label (node). (i can't find tree sample with the style). if u

  • Problem in delimiting the Relationship

    Hi, When we try to create a create a new relationship using PP01 for a Org. Unit, it is not delimiting the existing relationship. It just creates another relationship.  But for some of SAP Standard relationships when i try to create the new relations

  • Missing effects in photoshop 7

    Hi I am unable to find Reduce Noise option in Filters tab. Please help me out this problem. Sohail [email protected]

  • Transactional Buyer in SAP

    can anybody please send me documentation about Transactional Buyer or else tell me roles and responsibilities of it??Its an urgent.. Thanks in Advance

  • Connecting 6234 to 2006 Mercedes ML Bluetooth syst...

    I'm having difficulty. The car seams to recognise the phone but wont authorize connection. Any advice would be welcome.