Query - restricting rows based on specific combinations of columns

I have two columns in two different tables T1 and T2 like below
T1.Column1 T2.Column2
1 a
2 b
3 c
4 d
5 e
I need all the combinations of values in the columns, which I can choose like
select T1.Column1, T2.Column2 from T1, T2 ------ (Without specifying any join ie. cartesian product)
But, in those, combinations, I need to restrict the following combinations, from not being selected - (1,b), (1,c),(3,b), in the same sql, or using a subquery. I am not able to achieve this after several attempts. Can any one help?

Just pad it and check
SQL> select no,name from a,b where to_char(no)||name not in ('1b','1c','3b')
  2  /
        NO N
         1 a
         1 d
         1 e
         2 a
         2 b
         2 c
         2 d
         2 e
         3 a
         3 c
         3 d
        NO N
         3 e
         4 a
         4 b
         4 c
         4 d
         4 e
         5 a
         5 b
         5 c
         5 d
         5 eThanks,
Karthick.

Similar Messages

  • Selecting duplicate rows based on a combination of columns as key

    Hi ,
    I have a table with 5 columns.
    Code ID S_DATE E_DATE Name
    1 23 01012001 null ABC
    1 09 01012001 null XYZ
    2 81 04022007 null TVU
    1 43 03092008 null XXX
    Now, I need write a select query to fetch the duplicate rows from the above table having the combination of (Code, S_DATE,E_DATE) as the key.
    So from the above example, I need to get Row1 and Row2 as output (but not Row3 as it has a different S_DATE)
    Thanks in advance for your suggestions.
    Thanks
    Edited by: thotaramesh on Mar 9, 2009 4:54 PM

    On XE;
    WITH sample_data AS (
       SELECT 1  code,23 ID, '01012001' s_date, null e_date, 'ABC' NAME FROM dual UNION ALL
       SELECT 1, 09, '01012001', null, 'XYZ' FROM dual UNION ALL
       SELECT 2, 81, '04022007', null, 'TVU' FROM dual UNION ALL
       SELECT 1, 43, '03092008', null, 'XXX' FROM dual)
    SELECT code, ID, s_date, e_date, NAME
    FROM (
       SELECT
          sample_data.*,
          COUNT(*) over (PARTITION BY code, s_date, e_date) dups
       FROM sample_data)
    WHERE dups > 1;
          CODE         ID S_DATE   E_DATE NAME
             1         23 01012001        ABC
             1          9 01012001        XYZ

  • How to query a row based on its rownum

    Dear all,
    what is the easiest way to query a row based on its rownum? Suppose I have the following query:
    select first_name from employees where rownum<5;
    FIRST_NAME
    Ellen
    Sundar
    Mozhe
    DavidSo Ellen has rownum 1, Sundar 2 and so on. How can I do it if I just want to query a row where Mozhe is the first_name, which is rownum 3?
    Best regards,
    Val

    JS1 wrote:
    Valerie Debonair wrote:
    I was thinking how oracle assign a rownum in a table is by how it populates the rows in the table.Hence a statement "based on its availability". Maybe my way of thinking it's not accurate?
    You are correct, your way of thinking is wholly innaccurate. Something that can easily be confirmed simply by reading the documentation:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns009.htm#i1006297
    okie doke... thanks for the correction

  • Trouble with joining based on various combinations of columns

    Hi
    I have a load of transaction data which I need to join to a rate table to find the correct rate to price on. The join should be one-to-one and the selection of the correct rate is based on various columns in both tables which should either match or, where
    the rate table contains 'NA', allow any value from the transaction table. Where there is a specific match, this should be selected rather than a join where 'NA' is used to allow any other value in the transaction.
    Here is the CREATE script for the transaction table:
    CREATE TABLE [dbo].[job_variables](
    [job_no] [int] NOT NULL,
    [product_code1] [nvarchar](2) NULL,
    [product_code2] [nvarchar](2) NULL,
    [customer_code] [nvarchar](8) NULL,
    [area1] [nvarchar](8) NULL,
    [area2] [nvarchar](8) NULL,
    [group_code] [nvarchar](8) NULL)
    And for the rate table:
    CREATE TABLE [dbo].[ratecriteria](
    [product_code1] [nvarchar](255) NULL,
    [product_code2] [nvarchar](255) NULL,
    [customer_code] [nvarchar](255) NULL,
    [area1] [nvarchar](255) NULL,
    [area2] [nvarchar](255) NULL,
      [group_code] [nvarchar](255) NULL,)
    The combinations used to join the tables should be attempted in this order, where X = a match and NA = ratecriteria value is 'NA' so allow all from job_variables. As the data tables need to join one-to-one, once a transaction meets the join criteria, it
    cannot be joined to another ratecriteria record.
    Firstly, where ratecriteria.group_code = 'NA' and customer_key matches:
    product_code1
    product_code2
    area1
    area2
    customer_code
    group_code
    X
    X
    X
    X
    X
    NA
    X
    X
    X <------>
    X
    X
    NA
    (area1 and area2 reciprocated)
    X
    X
    X
    NA
    X
    NA
    X
    X
    NA
    X
    X
    NA
    X
    X
    NA
    NA
    X
    NA
    NA
    X
    X
    X
    X
    NA
    NA
    X
    X
    NA
    X
    NA
    NA
    X
    NA
    X
    X
    NA
    NA
    X
    NA
    NA
    X
    NA
    Then, where ratecriteria.customer_code = 'NA' and group_code matches:
    product_code1
    area1
    area2
    customer_code
    group_code
    X
    X
    X
    NA
    X
    X
    X <------>
    X
    NA
    X
    (area1 and area2 reciprocated)
    X
    X
    NA
    NA
    X
    X
    NA
    X
    NA
    X
    X
    NA
    NA
    NA
    X
    NA
    X
    X
    NA
    X
    NA
    X
    NA
    NA
    X
    NA
    NA
    X
    NA
    X
    NA
    NA
    NA
    NA
    X
    And finally, where both ratecriteria.customer_code and ratecriteria.group_code = 'NA':
    product_code1
    area1
    area2
    customer_code
    group_code
    X
    X
    X
    NA
    NA
    X
    X <------>
    X
    NA
    NA
    (area1 and area2 reciprocated)
    X
    X
    NA
    NA
    NA
    X
    NA
    X
    NA
    NA
    X
    NA
    NA
    NA
    NA
    NA
    X
    X
    NA
    NA
    NA
    X
    NA
    NA
    NA
    NA
    NA
    X
    NA
    NA
    NA
    NA
    NA
    NA
    NA
    I am working with SQL Server 2012 in SSMS. Any help greatly appreciated, let me know if I need to provide more info.
    Thanks

    I think that you have got that correct, although I am not entirely sure what "REFERENCES" does, I will research that one. I do know that the database design is poor but, for context, I have imported all the data I am going to use, it
    is all in excellent shape and I will not do any further imports, creates, deletes, updates etc. so a poor schema should (and this may be me showing my lack of knowledge again) not matter at this stage. I am really looking for a solution to the above problem.
    In terms of a query which outputs a correctly selected rate code (see above post 5th March 15:01), my solution which is perhaps a sledgehammer to crack a nut is as such.
    I have created a table I have called ratecrit_junction which I use to generate all the different combinations of actual values from job_variables and 'NA' entries.
    CREATE TABLE [dbo].[ratecrit_junction](
    [selection_rank] INT IDENTITY(1,1),
    [product_code1] [char](2),
    [product_code2] [char](2),
    [area1] [varchar](6),
    [area2] [varchar](6),
    [customer_code] [varchar](8),
    [group_code] [varchar](10)
    INSERT INTO ratecrit_junction
    VALUES ('X', 'X', 'X', 'X', 'X', 'NA'),
    ('X', 'X', 'X', 'NA', 'X', 'NA'),
    ('X', 'X', 'NA', 'X', 'X', 'NA'),
    ('X', 'X', 'NA', 'NA', 'X', 'NA'),
    ('NA', 'X', 'X', 'X', 'X', 'NA'),
    ('NA', 'X', 'X', 'NA', 'X', 'NA'),
    ('NA', 'X', 'NA', 'X', 'X', 'NA'),
    ('NA', 'X', 'NA', 'NA', 'X', 'NA'),
    ('X', 'X', 'X', 'X', 'NA', 'X'),('X', 'X', 'X', 'NA', 'NA', 'X'),
    ('X', 'X', 'NA', 'X', 'NA', 'X'),
    ('X', 'X', 'NA', 'NA', 'NA', 'X'),
    ('NA', 'X', 'X', 'X', 'NA', 'X'),
    ('NA', 'X', 'X', 'NA', 'NA', 'X'),
    ('NA', 'X', 'NA', 'X', 'NA', 'X'),
    ('NA', 'X', 'NA', 'NA', 'NA', 'X'),
    ('X', 'NA', 'X', 'X', 'X', 'NA'),
    ('X', 'NA', 'X', 'NA', 'X', 'NA'),
    ('X', 'NA', 'NA', 'X', 'X', 'NA'),
    ('X', 'NA', 'NA', 'NA', 'X', 'NA'),
    ('NA', 'NA', 'X', 'X', 'X', 'NA'),
    ('NA', 'NA', 'X', 'NA', 'X', 'NA'),
    ('NA', 'NA', 'NA', 'X', 'X', 'NA'),
    ('NA', 'NA', 'NA', 'NA', 'X', 'NA'),
    ('X', 'NA', 'X', 'X', 'NA', 'X'),
    ('X', 'NA', 'X', 'NA', 'NA', 'X'),
    ('X', 'NA', 'NA', 'X', 'NA', 'X'),
    ('X', 'NA', 'NA', 'NA', 'NA', 'X'),
    ('NA', 'NA', 'X', 'X', 'NA', 'X'),
    ('NA', 'NA', 'X', 'NA', 'NA', 'X'),
    ('NA', 'NA', 'NA', 'X', 'NA', 'X'),
    ('NA', 'NA', 'NA', 'NA', 'NA', 'X')
    I can then join data from job_variables to ratecrit_junction using a COALESCE(NULLIF(...), ...) to get the different combinations and join to ratecriteria with a ranking to indicate which one of them should override the others and identify the correct rate
    code to use.
    I forgot to include rate code in my original post, correction below.
    CREATE TABLE [dbo].[ratecriteria](
    ID INT IDENTITY(1,1) PRIMARY KEY,
    [product_code1] [nvarchar](2) REFERENCES Product_Code,
    [product_code2] [nvarchar](2) REFERENCES Product_Code,
    [customer_code] [nvarchar](8) REFERENCES Customer_Code,
    [area1] [nvarchar](8) REFERENCES Area,
    [area2] [nvarchar](8) REFERENCES Area,
    [group_code] [nvarchar](8) REFERENCES Group_Code,
    [rate_code] [nvarchar](5),
    UNIQUE (product_code1, product_code2, customer_code, group_code)
    And here is the query that I am using
    SELECT v.job_no,
    j.selection_rank,
    rc.rate_code
    FROM job_variables v
    CROSS JOIN ratecrit_junction j
    INNER JOIN ratecriteria rc
    ON COALESCE(NULLIF(j.product_code1, 'X'), v.product_code1) = rc.product_code1
    AND COALESCE(NULLIF(j.product_code2, 'X'), v.product_code2) = rc.product_code1
    AND COALESCE(NULLIF(j.area1, 'X'), v.area1) = rc.area1
    AND COALESCE(NULLIF(j.area2, 'X'), v.area2) = rc.area2
    AND COALESCE(NULLIF(j.customer_code, 'X'), v.customer_code) = rc.customer_code
    AND COALESCE(NULLIF(j.group_code, 'X'), c.group_code) = rc.group_code
    And here is some sample data for the job_variables and ratecriteria tables, based on my earlier post with the coloured examples.
    INSERT INTO ratecriteria
    VALUES ('P1', 'PP1', 'C1', 'AA', 'BB', 'NA', 'R1'),
    ('P1', 'PP1', 'C1', 'AA', 'NA', 'NA', 'R2'),
    ('P1', 'PP1', 'NA', 'AA', 'BB', 'G1', 'R3'),
    ('NA', 'NA', 'NA', 'NA', 'NA', 'G1', 'R4')
    INSERT INTO job_variables
    VALUES (1, 'P1', 'PP1', 'C1', 'AA', 'BB', 'G3'),
    (2, 'P1', 'PP1', 'C1', 'BB', 'AA', 'G4'),
    (3, 'P1', 'PP1', 'C1', 'AA', 'FF', 'G1'),
    (4, 'P1', 'PP1', 'C2', 'AA', 'BB', 'G1'),
    (5, 'P1', 'PP2', 'C1', 'AA', 'BB', 'G1'),
    (6, 'P2', 'PP2', 'C4', 'AB', 'AA', 'G1')

  • Make 1 row turn into X rows, based on value X in column?

    Hello everyone, got a question for you! I've got a table as follows:
    ITEM_NUM,IN_STOCK
    1001A,2
    1001B,1
    1001C,2
    1001D,3
    I'd like to get the following output:
    1 1001A
    2 1001A
    3 1001B
    4 1001C
    5 1001C
    6 1001D
    7 1001D
    8 1001D
    So each row gets repeated for as many times as it has for the in_stock value. And down the left side, we get rownum.
    I can do this with PL/SQL code (a cursor to get both columns, and for each row, run a loop to place these into a collection). But if there's a way just to skip the loop and write a query, I'd rather do this!
    Thanks!
    -Thomas H

    Well you can pretty much use any row source here but I favour pipelined function for overall simplicity / clarity and performance.
    Personal Oracle Database 10g Release 10.1.0.2.0 - Production
    With the Partitioning, OLAP and Data Mining options
    SQL> CREATE TABLE t (item_num VARCHAR2 (5), in_stock NUMBER);
    Table created.
    SQL> INSERT INTO t VALUES ('1001A',2);
    1 row created.
    SQL> INSERT INTO t VALUES ('1001B',1);
    1 row created.
    SQL> INSERT INTO t VALUES ('1001C',2);
    1 row created.
    SQL> INSERT INTO t VALUES ('1001D',3);
    1 row created.
    SQL> CREATE OR REPLACE FUNCTION many (
      2    p_rows IN NUMBER)
      3    RETURN number_table PIPELINED
      4  IS
      5  BEGIN
      6    FOR l_row IN 1..p_rows LOOP
      7      PIPE ROW (l_row);
      8    END LOOP;
      9    RETURN;
    10  END;
    11  /
    Function created.
    SQL> SELECT ROWNUM, item_num
      2  FROM   t, TABLE (many (in_stock));
        ROWNUM ITEM_
             1 1001A
             2 1001A
             3 1001B
             4 1001C
             5 1001C
             6 1001D
             7 1001D
             8 1001D
    8 rows selected.
    SQL>

  • Combine rows based on column value

    I'm trying to combine rows based off of a common value.
    Table Data:
    Result A
    Player_ID   Player_Name  Team_Name  Points Sequence
    99999       John Smith    Bulls              50         1
    99999       John Smith    Pistons          14         2
    What i want in the output
    Result B
    Player_ID Player_Name Team_Name  Points
    99999     John Smith      Bulls           64 (50 for Bulls)Here's my main query...
    select player_id, player_name, team_name, points
    from
              SELECT player_id, player_name, team_name, sum(points) sum_points
              FROM stats_data
              WHERE season_id = 2008
              AND points > 0
              GROUP BY player_id, player_name, team_name
              ) t
    ORDER BY sum_points DESC, last_name;**The current query gives me the output from Result A
    1) Not entirely sure how to combine data based of player_id
    2) Once i figure out 1, im sure by adding the ('sum_points' for 'team_name') to the output can be done with a case statement, i think?

    Sorry about that Frank.
    Using: Oracle 10.2g
    i was incorrect about the output of the query
              SELECT player_id, sum(points) sum_points
              FROM stats_data
              WHERE season_id = 2008
              AND points > 0
              GROUP BY player_id
    )Output
    Player_ID      Sum_Points    
    99999              20        
    55455               6          
    66489               2  So the subquery is outputing the correct data
    The output of the original entire query(that you had posted) is:
    Player_ID    Player_Name   Team_Name    Points    
    99999        John Smith      Bulls               16 (8 for  Bulls)    
    55455        Bob Johnson    Rockets          6   (3 for Rockets)
    66489        Chris Terry       Kings              2   (1 Kings)Here's some more raw sample data from the table:
    Player_ID    Player_Name   Team_Name    Points     Sequence  Game_Type_ID Team_ID
    99999        John Smith      Bulls               12          1                       1                  5
    99999        John Smith      Bulls               0            1                      -1                  5
    99999        John Smith      Pistons            4           2                       1                 12
    99999        John Smith      Pistons            0           2                       -1                12
    55455        Bob Johnson    Rockets          4           1                        1                  3 
    55455        Bob Johnson    Rockets          0           1                       -1                  3 
    66489        Chris Terry       Kings              2           1                        1                  8
    66489        Chris Terry       Kings              0           1                       -1                  8So your code frank is very close, the issue is that:
    A) All players have a ('pt value' for 'team name') even if they played on the same team, which can prob. be solved with a case statement, which i tried to do in my last post.
    WITH       grouped_data     AS
              SELECT player_id, sum(points) sum_points
              FROM stats_data
              WHERE season_id = 2008
              AND points > 0
              GROUP BY player_id
    SELECT     g.player_id
    ,     s.player_name
    ,     s.team_name
    (case when sequence = 2 then g.sum_points  <==
         ||  '('
         ||  TO_CHAR (s.points)
         ||  ' for '
         ||  s.team_name
         || ')'
    else to_char (g.sum_points)
    END)     AS points
    FROM     grouped_data     g
    JOIN     stats_data     s     ON g.player_id     = s.player_id
    WHERE     s.sequence     in (1,2)     <==
    AND     s.season_id     = 2008
    AND     s.points     > 0;the output has two issues, for players who played on more than one team, like Josh Smith the output comes out as
    99999        John Smith      Pistons           20  (2 for Pistons)        
    99999        John Smith      Bulls               20While the players who played on one team came out correctly. So issue with my edit of your code is:
    A) josh smith is output 2, needs to be output once
    B)josh smith should say (Josh Smith Bulls 20 (4 for Pistons) not "99999 John Smith Pistons 20 (2 for Pistons)"
    c) Issues with "99999 John Smith Pistons 20 (2 for Pistons)" is that it listing the wrong team for him: should be Bulls and listing half of his total with the pistons in the prentices should be 4. --The reason that is because s.points needs to be summed as based off of the data there's two entries
    Hope this clears it up, thanks!
    Edited by: user652714 on Mar 11, 2009 9:28 AM

  • SQL Query of an OWB map (row based)

    If I trace a session, execute an OWB map (row based), will the trace file contain the actual SQL query ?
    The problem with me is that when I am executing this row -based OWB map, it is throwing me an error CursorFetchMapTerminationRTV20007 BUT ( plus taking a long time) when I am taking out the intermediate SQL insert query,it is working fine ( and also within a very short period of time)
    Execution status = COMPLETE
    message text = ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    message text = CursorFetchMapTerminationRTV20007
    No. task errors = 0
    No. task warnings = 2
    No. errors = 1
    Since this OWB map (Truncate Insert)  is row based hence I cannot cannot get the back end query from the OWB generated pl/sql package so wondering if I trace the session, check the trace file, may be I will able to see the exact SQL query generated. But wanted to confirm the same.

    Yes, the actual SQL run in the session will be in the trace file.

  • How to suppress a row based on current date -  at query level?

    In an Bex query report i have suppress rows based on current date.
    There is no current date available in query.
    there is a date field in the query.
    If by chance the date in that field is lesser than current date, I have to suppress that row.
    How can this be achieved?

    What is the code ofr creating a variable to get values >= to current date?
    I have implemented the following code which is not working.
    data L_S_range like line of E_T_range[].
    CLEAR L_S_RANGE.
    L_S_RANGE-SIGN = 'I'.
    L_S_RANGE-OPT = 'GE'.
    L_S_RANGE-LOW = SY-DATUM.
    APPEND L_S_RANGE TO E_T_RANGE[].
    Actually i have written in class, which will be inherited in superclass.
    Edited by: akshara20 on Feb 2, 2011 1:21 PM

  • MSSQL Query/View Single Line Output For Combined Multiple Data Elements - Possible Pivot Table?

    HELLO...
    I hope you experts out there can help me.  Consider the following (2) Tables in MSSQL:
    1. TENDERED --> Primary Key = DATE / DOC_NO / PAYMENT_SEQ_NO
    DATE
    DOC_NO
    PMNT_SEQ_NO
    PAYCODE_TYPE
    AMOUNT
    2. TENDERED_CR_CARD -->Primary Key = DATE / DOC_NO / PAYMENT_SEQ_NO
    DATE
    DOC_NO
    PMNT_SEQ_NO
    CR_CARD_NO_MASKED
    CR_CARD_NAME
    CR_CARD_EXP_DATE
    These two tables are certainly related, based on their Primary Key values.
    Now, consider the following data in those two tables:
    DATE            
    DOC_NO      PMNT_SEQ_NO              
    PAYCODE_TYPE               
    AMOUNT
    03/10/2014         100001 
    1             
    CASH            
    100.00
    03/10/2014         100001 
    2             
    CASH                             
    -9.75
    03/10/2014         100002 
    1             
    CASH                             
    50.00
    03/10/2014         100002 
    2             
    VISA                             
    100.00
    03/10/2014         100002 
    3             
    VISA             
                   250.00
    03/10/2014         100003 
    1             
                            MC
    125.00
    03/10/2014         100003 
    2             
    AMEX           
    75.00
    DATE          
    DOC_NO PMNT_SEQ_NO  CR_CARD_MASKED     
    NAME            
    CR_CARD_EXP
    03/10/2014  100002   2                       4225******801289  
    MARY JONES   2016/08/31
    03/10/2014  100002   3                       4121******637442  
    JOHN DOE      2015/04/30
    03/10/2014  100003   1                       5428******971134  
    MIKE BAKER   2018/09/30
    03/10/2014  100003   2                       3732*****344756    
    LINDA LIU      2017/07/31
    OK...so what we NEED...is a Combined, SINGLE RECORD Audit Report type query. 
    The resulting query should show, based on the Data from above, the SINGLE LINE represented in the Attached Spreadsheet. 
    NOTE...what's important to point out here..is that ONLY the 'CASH' Tender gets "summed"...EACH INDIVIDUAL Credit Card record MUST have its own Field...as represented in the corresponding Columns of the Spreadsheet (i.e. PMT_TYP_1, AMT_1, PMT_TYP_2,
    AMT_2, and so forth).
    PLEASE HELP!  Any suggestions/advice would be most appreciated! 
    Thank You!...Mark

    I would not do this in SQL if I could possibly avoid it.  Instead do it in the front end.
    If you must do it in SQL, this is a dynamic pivot on multiple columns.  Naomi Nosonovsky has a blog at
    http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dynamic-pivot-on-multiple-columns/ on how to do that.  Look especially at her second example using the claims table.  Of course, you must do some manipulation even before you do the multi-column
    pivot, since you must first combine all the cash entries.
    So one way to do it would be to build a temp table with all the entries you have except the cash entries combined into one payment sequence number.  To do that you may need specifications that are not clear to me from what you have given us.  For
    example, if PMT SEQ 1 is VISA,  PMT SEQ 2 is CASH, PMT SEQ 3 is VISA, PMT SEQ 4 is CASH, and PMT SEQ 5 is VISA, you want to combine the two cash payments.  So they become PMT SEQ 2?  If so, what happens to PMT SEQ 4 - is it left N/A or does
    PMT SEQ 5 become PMT SEQ 4?
    But once you have this temp table with the cash payments combined in the algorithm you need, then you can use Naomi's method to do the multi-column pivot.  Note that Naomi uses the code
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_Name = 'Claims'
    to get the column names from the permanent table Claims.  To get the column names from a temp table use code like the following.  To find the column names in a temp table named #MyTempTable, do
    From tempdb.sys.columns
    Where object_id = OBJECT_ID('#MyTempTable')
    But as I say, if feasible, I would do this in the front end, not in SQL.  T-SQL is a very good language for storing and retrieving data, not so good at formatting it. 
    Tom

  • Query Performance Issue based on the same Multiprovider

    Hi All,
    I am facing a performance issue with one of the BEx Query 1 based on a particular Multiprovider.
    I have done all the kind of testing and came o a conclusion that the OLAP: Data Transfer time for the query is the max at around 820 Secs.
    The surprising part is another Query 2 based on the same multiprovider runs absolutely fine without any hassles with OLAP : Data Transfer time with merely 3 Sec.
    Another, surprise is that Query 2 has even more Restricted Key Figures and Calculated Key Figures but it runs smoothly.
    Both the queries use cache memory.
    Please suggest a solution to this.
    Thanks.

    Hi Rupesh,
    There is no much difference between the 2 queries.
    Query 1 - which has performance issue has the same filters as that of Query 2 - which is working fine.
    The only difference is that the selection of Fiscal Week is in the Default Values Pane for Query 2 whereas its in Characteristics Restrictions in Query 1. I doubt whether this setting will have any effect on the performance.
    Both restriction i.e Fiscal Week restriction is based on Customer exit and the rows include Hierarchy for both the queries.
    I assume creating aggregates on hierarchy for the Cube can be a solution.

  • Adding rows based on current and next row

    I got some excellent help on multiplying rows based on start and end date in this
    thread, resulting in the query below. It helps me follow Vehicle activity and Vehicle allocation of our vehicles day by day. Now I would like to add another feature to the query, if it is possible.
    The problem is that in our database, only actual tasks are registered, which means that the time when the vehicle is between tasks is not showing. I could of course calculate total available time per vehicle and month, but that would not tell me where the
    vehicles are waiting, when during the day, etc.
    So I would like to insert rows for when the vehicles are standing still, and the logic would be something like this:
    If vehicle number on current row equals vehicle number on the next row and End (date/time) of current row < Start (date/time) of next row, insert row after current row. Set Start=End of current row and End=Start of next row. Set From=To
    of current row and To=To of current row. Set Vehicle activity to "Not active". Finaly copy all other fields from current row.
    Is this possible to achieve in Power Query?
    Brgds,
    Caj
    let
        Source = Sql.Databases("sql10"),
        SLM = Source{[Name="SLM"]}[Data],
        dbo_V_LKPI = SLM{[Schema="dbo",Item="V_LKPI"]}[Data],
        RenamedColumns = Table.RenameColumns(dbo_V_LKPI,{{"ActualDeparture", "Start"}, {"ActualArrival", "End"}}),
         Records = Table.ToRecords(V_LocoKPI),
          DateTime.IsSameDay = (x, y) => Date.Year(x) = Date.Year(y) and Date.Month(x) = Date.Month(y) and Date.Day(x) = Date.Day(y),
          Expand = (x) => List.Generate(
              () => Record.Combine({x, [End=Date.EndOfDay(x[Start])]}),
              (record) => record[Start] <= x[End],
              (record) => let
                  NextStart = Date.StartOfDay(Date.AddDays(record[Start], 1)),
                  NextEnd = Date.EndOfDay(NextStart),
                  ThisEnd = List.Min({NextEnd, x[End]})
              in
                  Record.Combine({record, [Start=NextStart, End=ThisEnd]})),
          Transformed = List.Transform(Records, each if DateTime.IsSameDay([Start], [End]) then {_} else Expand(_)),
          Combined = List.Combine(Transformed),
          Result = Table.FromRecords(Combined)
      in
          Result
    Csten

    Here's some sample code. Again, we use List.Generate to build either a record or a list of records and then use List.Combine to bring the results back together before converting them to a table.
    let
        CombineTwoRows = (x, y) =>
            let
                combine = x[Vehicle] = y[Vehicle] and x[End] < y[Start],
                added = Record.Combine({x, [Start=x[End], End=y[Start], Active=false]}),
                result = if combine then {added, y} else {y}
            in result,
        GenerateStandingRows = (table, combine) =>
            let
                Handle = (x, y) => {x, y},
                buffered = Table.Buffer(table),
                n = Table.RowCount(buffered),
                windows = List.Generate(
                    () => {1, {buffered{0}}},
                    (x) => x{0} <= n,
                    (x) => {x{0} + 1, if x{0} < n then combine(buffered{x{0}-1}, buffered{x{0}}) else {buffered{x{0}}}},
                    (x) => x{1})
            in
                windows,
        InsertInactivity = (table) => Table.FromRecords(List.Combine(GenerateStandingRows(table, CombineTwoRows))),
        TestData = Table.FromRows({
            {1, #datetime(2014, 2, 23, 13, 0, 0), #datetime(2014, 2, 23, 13, 10, 0), true},
            {1, #datetime(2014, 2, 23, 13, 20, 0), #datetime(2014, 2, 23, 13, 30, 0), true},
            {2, #datetime(2014, 2, 23, 13, 20, 0), #datetime(2014, 2, 23, 14, 0, 0), true},
            {2, #datetime(2014, 2, 23, 14, 20, 0), #datetime(2014, 2, 23, 14, 40, 0), true},
            {2, #datetime(2014, 2, 23, 16, 0, 0), #datetime(2014, 2, 23, 17, 0, 0), true},
            {2, #datetime(2014, 2, 24, 2, 0, 0), #datetime(2014, 2, 24, 3, 0, 0), true},
            {3, #datetime(2014, 2, 24, 1, 0, 0), #datetime(2014, 2, 24, 8, 0, 0), true},
            {3, #datetime(2014, 2, 24, 9, 0, 0), #datetime(2014, 2, 24, 10, 0, 0), true}
            }, {"Vehicle", "Start", "End", "Active"})
    in
        InsertInactivity(TestData)

  • Split single row in multiple rows based on date range

    I need sql script that can split single row in multiple rows based on start date and end date column in table.
    Thank you

    I agree to your suggestion of having a dates table permanently in the database. Thats how we also do for most of our projects as well
    But in most projects the ownership  of table creation etc lies with the client as they will be the DBAs and will be design approval authorities. What I've seen is the fact that though
    many of them are in favour of having calendar table they dont generally prefer having a permanent table for numbers in the db. The best that they would agree is for creating a UDF which will have
    tally table functionality built into it based on a number range and can be used in cases where we need to multiply records as above.
    Wherever we have the freedom of doing design then I would also prefer creating it as a permanent table with required indexes as you suggested.
    >> many of them are in favour of having calendar table they dont generally prefer having a permanent table
    Those people do not understand about database and are not DBAs :-)
    It is our job to tell them what is right or wrong.
    ** This is a real story! I had a client several years back, who was the CEO of a software company.
    He use the query:
    select * from table_name
    In order to get the last ID!
    The table_name was actually a view that took data from several tables, and the main table that he wanted to get the ID included several string columns, with lot of data!
    he actually pulled all this data to the application, just to get the lat ID in a specific table!
    It is our job as Consultants or DBAs to fix's his misunderstanding :-)
      Ronen Ariely
     [Personal Site]    [Blog]    [Facebook]

  • How not show duplicate rows based on one field

    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0
    Hello
    I have a query that looks for certain scripts that did not run on a particular day compared to what ran the same day a week ago. We want to include the start_datetime and end_datetime but when I add it to the select statement it brings up all instances of the jobs that run multiple times during the day. Is there a way to exclude the extra rows based on the script_name field?
    SELECT instances.script_name,
                             instances.instance_name,
                             REGEXP_REPLACE(master.description,
                                            chr(49814),  -- em-dash
                                            '-') description
                                            --instances.start_datetime
                      FROM   xxcar.xxcar_abat_instances Instances,
                             xxcar.xxcar_abatch_master  Master
                      WHERE  1 = 1
                      AND    TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS')) - (:p_NumOfWeeks * 7)
                      AND    Instances.SCRIPT_NAME = Master.SCRIPT_NAME (+)
                      MINUS
                      SELECT script_name,
                             instance_name,
                             NULL
                             --NULL
                      FROM   xxcar.xxcar_abat_instances
                      WHERE  1 = 1
                      AND    TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS'))

    MINUS does a set operation - removing rows from the first set that exactly match the second set.
    When you add columns to the first set, you want a more restricted filtering - try a multi-column NOT IN:
    To remove multiple runs, group and get min/max
    SELECT instances.script_name,
                             instances.instance_name,
                             REGEXP_REPLACE(master.description,
                                            chr(49814),  -- em-dash
                                            '-') description,
                             min(instances.start_datetime) start_datetime,
                             min(instances.end_datetime) end_datetime
                      FROM   xxcar.xxcar_abat_instances Instances,
                             xxcar.xxcar_abatch_master  Master
                      WHERE  1 = 1
                      AND    TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS')) - (:p_NumOfWeeks * 7)
                      AND    Instances.SCRIPT_NAME = Master.SCRIPT_NAME (+)
                      AND (script_name, instance_name) NOT IN
                    ( SELECT script_name,
                             instance_name
                      FROM   xxcar.xxcar_abat_instances
                      WHERE  1 = 1
                      AND    TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS'))
    group by instances.script_name, instances.instance_name, master.descriptionYou didn't give table definitions, and the query has schemas in it, so I didn't test it.
    regards,
    David

  • Count the occurences of a specific combination of chars

    Hi there
    I want to count the occurences of a specific combination of characteristic in a query.
    Example count the occurences of combination CHAR1 CHAR2 CHAR3
    CHAR1 CHAR2 CHAR3 Counter
    A     BB     CCC    1
    A     BB     CCC    1
    A     BB     DDD    1
    A     SS     DDD    1
    Query Result should be
    CHAR3 Count
    CCC    1
    DDD    2
    In the HowTo Paper: Count the occurrences of a
    characteristic relative to one or
    more other characteristics it is only a solution with one characteristic but not with a combination of chars.
    I don't know if this possible, because I think it is necessary to do a expeption aggregation based on several chars.
    Anyone an Idea?
    Thanks a lot in advance
    Joe

    Joe,
    I think in your case it is the case of multiple characteristics and I think the straight way is to do with exception aggregation.
    See the below link
    http://help.sap.com/saphelp_nw04/helpdata/en/d2/e0173f5ff48443e10000000a114084/frameset.htm
    See the below link for some more discussions using formulas which may help you to arrive a decision.
    Count elements in a characteristic
    Regs
    Gopi.

  • Displaying Datagrid Rows Based on User Login Id

    Hey Everyone,
    So the problem I am having is that I am making a product configurator and when a user logs in I want to display there previous creations in a data grid so they can select edit or reorder them....the problem I am having is for some reason I can't get the designs to show up based on specific users...here is the process i am currently using in Flex 4.5
    I create 2 tables in the database (Users and Designs)
         the id for the design is based on the id of the user that created it
    In flex I create 2 php services
         -One generated from the users table for creating a new user for the configurator
         -The other one is generated from the Design table and I use the getAllDesigns(); to display the designs in the data grid
    All of the login info is validated through php files that are used by an httpservice call
    So my idea was to run an if statement to display the designs for specific user, something like this (i know the syntax isn't right this is just to get my idea accross
    Var designId = datagrid.row.id;
    (allready have userId var)
    if(userId != designId)
         row.visible = "false"
    I have tried many different approaches but cant get anything to work
    Any Ideas would be helpful
    Thanks In advance

    Your right, i was trying to wrap my head around that idea earlier but decided to go with this option to just see if i could get this to work then work on the server side filtering...
    How would I do this in theory,
    my thinking is that i query in my php file something like
    SELECT * FROM DESIGNS WHERE DESIGNS.ID = USERS.ID;
    then take the filtered data and pass it to a xml document
    then in flex create an array list out of the xml to insert into the datagrid?
    or do you have a better idea?
    also will that information even be able to be editable because i am parsing the data through the xml?
    Thanks for any input

Maybe you are looking for