SQL Query constructing  for searching values in Tables single Column

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

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

Similar Messages

  • Library Filters: Filter for multiple values in a single column and Wildcards

    If you have a multi-value column in a library, can you filter to show only results where two (or more) specific values (terms) are applied? E.g. Year 2014 and Month January? Seems this would be particularly useful for Enterprise Keywords columns.
    Similarly, is there any way to filter on one value OR another value in a single column (e.g. Document Type Memo OR Fax)?
    Any way to use wildcards in searches (would be REALLY useful in the Title and Name columns when naming conventions have been followed).
    I'm guessing the answer is going to be 'move to SharePoint 2013' - if that is the case, please confirm whether all the scenarios mentioned above are catered for.

    Thanks for your replies !
    Is there any other way to achieve this ?
    There is 1 InfoObject which has 2 fields Employee and Department.
    Employee     Department
    001                A
    001                B
    001                C
    In the report there should be 1 row for Employee 001 with all the Departments displayed in the same cell separated by commas.
    Can this be done in the backend through a ABAP code?

  • Basic SQL query scripts for SAP B1

    hi guys,
    do you have any documents, guides or reference links regarding basic SQL query scripts for SAP B1?
    thanks and have a nice day!
    blake p.

    Hi
    You can also check SBONotes.com.
    For sql understanding , you can search in websearch - tsql and it will pop up with informations you need.
    You can also refer to forum once you understand the basic techniques
    Hope this helps
    Bishal

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

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

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

  • SQL Query report - how to evaluate the current sort column?

    Hi,
    I have a need to determine which column has been selected to sort the current report by. I need to pull this column name and pass it when a button is pressed so that the ensuing PDF report generated using PLPDF will be able to sort the data the same way.
    thanks in advance.

    Hi,
    As i think, you need to create report manually using SQL query and for sorting you can put a drop down box and user can select a field name and shorting type that is ASC or DESC from another drop down and than generate a report, additionally you can have another button for exporting to PDF that you can now manage through PLPDF and you can get the shorting column.
    hope this can help you.
    Thanks,
    Jaydip

  • SQL Query to find cumulative values for a Financial Year

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

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

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

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

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

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

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

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

  • Dynamic SQL Query to Find Special Characters in Table columns

    Hi,
    I am new to OTN FORUMS.
    I am trying to find the columnsi of a table which have special characters in them.
    I am planning on using this query
    select ' select INSTR('||column_name||', chr(0))
    from '||table_name||'where INSTR('||column_name||', chr(0)) >0' from user_tab_columns
    where table_name='Account'
    and spool the output to run as a script.
    Is this the right way or do u suggest any modifications to the query?
    Thanks in advance.

    Hi,
    I think your basic approach is right. Since you can't hard-code the table- or column names into the query, you'll need dynamic SQL.
    Instead SQL-from-SQL (that is, writing a pure SQL query, whose output is SQL code), you could do the whole job in PL/SQL, but I don't see any huge advantage either way.
    When you say "Special character<b>s</b>", do you really mean "one given special character" (in this case, CHR(0))?
    Will you ever want to search for multiple special characters at once?
    What if table foo has a column bar, and in 1000 rows of foo, bar contains CHR (0). Do you want 1000 rows of output, each showing the exact position of the first CHR(0)? If the purpose is to look at theese rows later, shouldn't you include the primary key in the output? What if CHR(0) occurs 2 or more times in the same string?
    If you'd rather have one row of output, that simply says that the column foo.bar sometimes contains a CHR(0), then you could do something like this:
    SELECT     'foo',     'bar'
    FROM     dual
    WHERE     EXISTS (
                SELECT  NULL
                FROM       foo
                WHERE       INSTR ( bar
                            , CHR (0)
                        ) > 0
                );

  • Sql query with multiple joins to same table

    I have to write a query for a client to display business officers' names and title along with the business name
    The table looks like this
    AcctNumber
    OfficerTitle
    OfficerName
    RecKey
    90% of the businesses have exactly 4 officer records, although some have less and some have more.
    There is a separate table that has the AcctNumber, BusinessName about 30 other fields that I don’t need
    An individual account can have 30 or 40 records on the other table.
    The client wants to display 1 record per account.
    Initially I wrote a query to join the table to itself:
    Select A.OfficerTtitle, A.OfficerName, B.OfficerTitle, B.OfficerName, C.OfficerTtitle, C.OfficerName, D.OfficerTitle, D.OfficerName where A.AcctNumber = B.AcctNumber and A.AcctNumber = C.AcctNumber and A.AcctNumber = D.AcctNumber
    This returned tons of duplicate rows for each account ( number of records * number of records, I think)
    So added
    And A.RecKey > B.RecKey and B.RecKey > C. RecKey and C.RecKey . D.RecKey
    This works when there are exactly 4 records per account. If there are less than 4 records on the account it skips the account and if there are more than 4 records, it returns multiple rows.
    But when I try to l join this to the other table to get the business name, I get a row for every record on the other table
    I tried select distinct on the other table and the query runs for ever and never returns anything
    I tried outer joins and subqueries, but no luck so far. I was thinking maybe a subquery - if exists - because I don't know how many records there are on an account, but don't know how to structure that
    Any suggestions would be appreciated

    Welcome to the forum!
    user13319842 wrote:
    I have to write a query for a client to display business officers' names and title along with the business name
    The table looks like this
    AcctNumber
    OfficerTitle
    OfficerName
    RecKey
    90% of the businesses have exactly 4 officer records, although some have less and some have more.
    There is a separate table that has the AcctNumber, BusinessName about 30 other fields that I don’t need
    An individual account can have 30 or 40 records on the other table.
    The client wants to display 1 record per account.As someone has already mentioned, you should post CREATE TABLE and INSERT statements for both tables (relevant columns only). You don't have to post a lot of sample data. For example, you need to pick 1 out of 30 or 40 rows (max) for the same account, but it's almost certainly enough if you post only 3 or 4 rows (max) for an account.
    Also, post the results you want from the sample data that you post, and explain how you get those resutls from that data.
    Always say which version of Oracle you're using. This sounds like a PIVOT problem, and a new SELECT .... PIVOT feature was introduced in Oracle 11.1. If you're using Oracle 11, you don't want to have to learn the old way to do pivots. On the other hand, if you have Oracle 10, a solution that uses a new feature that you don't have won't help you.
    Whenever you have a question, please post CREATE TABLE and INSERT statements for some sample data, the results you want from that data, an explanation, and your Oracle version.
    Initially I wrote a query to join the table to itself:
    Select A.OfficerTtitle, A.OfficerName, B.OfficerTitle, B.OfficerName, C.OfficerTtitle, C.OfficerName, D.OfficerTitle, D.OfficerName where A.AcctNumber = B.AcctNumber and A.AcctNumber = C.AcctNumber and A.AcctNumber = D.AcctNumber Be careful, and post the exact code that you're running. The statement above can't be what you ran, because it doesn't have a FROM clause.
    This returned tons of duplicate rows for each account ( number of records * number of records, I think)
    So added
    And A.RecKey > B.RecKey and B.RecKey > C. RecKey and C.RecKey . D.RecKey
    This works when there are exactly 4 records per account. If there are less than 4 records on the account it skips the account and if there are more than 4 records, it returns multiple rows.
    But when I try to l join this to the other table to get the business name, I get a row for every record on the other table
    I tried select distinct on the other table and the query runs for ever and never returns anything
    I tried outer joins and subqueries, but no luck so far. I was thinking maybe a subquery - if exists - because I don't know how many records there are on an account, but don't know how to structure that
    Any suggestions would be appreciatedDisplaying 1 column from n rows as n columns on 1 row is called Pivoting . See the following link fro several ways to do pivots:
    SQL and PL/SQL FAQ
    Pivoting requires that you know exactly how many columns will be in the result set. If that number depends on the data in the table, then you might prefer to use String Aggregation , where the output consists of a huge string column, that contains the concatenation of the data from n rows. This big string can be formatted so that it looks like multiple columns. For different string aggregation techniques, see:
    http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
    The following thread discusses some options for pivoting a variable number of columns:
    Re: Report count and sum from many rows into many columns

  • Query tuning for search within blob content.

    Hi,
    I ve got a query which searches across 3 tables. When I include a search condition : (CONTAINS(CF.blob_content,''{''||:P146_TEXT||''}'',1)>0 AND CF.REF_ID = CR.REF_ID) the query takes really long time to execute. Is there a way to reduce the execution time. If you need any more info on this, please let me know. I need to work out a solution for this issue really soon.
    I have indexed the blob_content column using a domain index type.
    Thanks,
    Vignesh

    Hi,
    an explain plan would be helpful.
    Is it possible to use the bind variable already include the "{", i mean :P146_TEXT := '{'||:P146_TEXT||'}' and then use :p146_text solely in the contains. Then you are using a real bind variabel. Maybe a second variable is necessary to make it work.
    Another possibility is that CF.REF_ID is not indexed or CR.REF_ID is not indexed, that's way it's handy to give an explain plan. Then you can see maybe the problem.
    Herald ten Dam
    Superconsult.nl

  • SQl Server - cant compare with values in table field have nchar as datatype

    Hello all!
    I am using jtds driver to connect with SQL server. My problem is i am saving arabic text into the SQL Server. The fields which contains arabic text have the datatype nchar. I can insert the values by simply placing N before the actual arabic text which i am reading from a file.
    I mean insert into [table] values(N'some arabic text') works fine.
    But when i try to select the values from the table and want to pass some paramerts (in arabic text) in where clause it doesnt return me anything.
    e.g select * from [table] where [column(nchar type)] = N'arabic text(read from file)' return nothing. Result set is always empty.
    Queryis fine as i printed the query in seperate file and run it on Query analyzer which gives me the right results.
    Any suggestions?? Please i am dying conditions tell me any solutions immediately
    Regards,
    Alam sher

    String url = "............";
    Properties props = new Properties();
    props.put("charset","cp1256");
    Connection con = DriverManager.getConnection(url,props);now this way if i get the resultset it would return me the expected resultset:)
    To compare an arabic word against the values in result set one also has to convert it into cp1256 encoding line this.
    new String([arabic word].getBytes(), "cp1256");
    The prolem was not to print the arabic words on console basically. No doubt on console it would print ???? as it doesnot support this encoding. Originial issu was comparing a string containig the arabic text with the values returned by the result set.
    This was the solution i could figure out and its working.
    Thanks anyways.
    Regards,
    Alam Sher

  • SQL Command code for multiple value string parameter

    Hi,
    I'm using crystal 2008 and there is a check box for multiple value  SQL Command  I need some help in writing the SQL Command code  for oracle (or sql server) for a multiple value STRING  parameter.
    Thanks in advance,
    Marilyn

    I could be wrong here, but I do not believe you can pass a multiple valued parameter to an SQL Command data source.  How I have gotten around this in the past is to put the "real" report into a subreport.  In the main report, create a formula field (basic syntax):
    formula = join({?parameter}, "|")
    Then, use this to pass the selected values to the subreport's parameter (call it {?sr-parm}).  The SQL Command in the subreport can then use that (MS SQL):
    select *
    from table
    where charindex(table.field, '{?sr-parm}') > 0
    HTH,
    Carl

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

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

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

  • Creating SQL-Loader script for more than one table at a time

    Hi,
    I am using OMWB 2.0.2.0.0 with Oracle 8.1.7 and Sybase 11.9.
    It looks like I can create SQL-Loader scripts for all the tables
    or for one table at a time. If I want to create SQL-Loader
    scripts for 5-6 tables, I have to either create script for all
    the tables and then delete the unwanted tables or create the
    scripts for one table at a time and then merge them.
    Is there a simple way to create migration scripts for more than
    one but not all tables at a time?
    Thanks,
    Prashant Rane

    No there is no multi-select for creating SQL-Loader scripts.
    You can either create them separately or create them all and
    then discard the one you do not need.

Maybe you are looking for

  • HT1918 help with changing rescue email account

    Hey folks, My apple ID is using an invalid email address...its really old.  I want to change it to the email address I submitted for my resue email address.  Itunes will not allow the change because the new email addy is already assigned as a rescue

  • Can't Import AAC file into Premier CS4

    Hello! As the title says, I'm not able to import AAC files into my Premier Pro projects.  The funny thing is that these files were created in Premier Pro CS4, so what gives?  I took the audio out of a video and exported ONLY the audio in AAC format. 

  • Flattening a JTreeTable to a JTable

    Has anybody done something along the following lines... Taking a JTreeTable that looks like this: Parent 1                 Child 1                 Child 2                 Child 3 Parent 2                 Child 4                 Child 5 and converting

  • Almost out of memory

    hello I enclose a screenshot of my memory I am almost out of memory but I have no idea what takes so much space? Ofcourse I am talking about the 320 GB SATA disk, the other are allright you can see it is called other, how can I find out what it is? I

  • Error code list for LV ?

    What are possible code error for the Copy VI ? I need to know if the file in destination already exist to ask if the user overwrite it or change name. What's the method. Thanks. In most general, is there a global list code error reserved for LV ? Tha