IF statement in Query

Hi
I have a query / recordset that  would be looking at 12000 rows in a database and 10 different variables and potential filters chosen by end users.
Should I put 10 wild card / url Where statements in my recordset query or should I put IF statements in my query.
ie.
If URL colname then ",and BetType= "xzz"" .
Is that the most efficient way to run my queries.
I will be running about 10 recordsets on my page all looking at these url variables so it will be a busy page.
If that is the answer can someone please tell me how to insert the if statement - i've tried allsorts but it wont work.
$colname_Recordset4 = "%";
if (isset($_GET['colname'])) {
  $colname_Recordset4 = $_GET['colname'];
mysql_select_db($database_racing_analysis, $racing_analysis);
$query_Recordset4 = sprintf("SELECT BetType, sum(if(season='2006-2007', Bet, 0)) AS '2006-2007',  sum(if(season='2007-2008', Bet, 0)) AS '2007-2008',  sum(if(season='2008-2009', Bet, 0)) AS '2008-2009' FROM dataextract WHERE BetType Like %s and TrackID = 1 and Distance = 1000 and Class = 1 GROUP BY BetType", GetSQLValueString($colname_Recordset4, "text"));
$Recordset4 = mysql_query($query_Recordset4, $racing_analysis) or die(mysql_error());
$row_Recordset4 = mysql_fetch_assoc($Recordset4);
$totalRows_Recordset4 = mysql_num_rows($Recordset4);
hope someone can help.
Simon

That part of the query cross tabs my data into three columns - not intended to confuse the issue. I'd have the same problem with a basic query.
So Here is a very basic version:
I want an IF statement to go around: "WHERE Distance = 1000" and "AND Class = 1"
That I believe will reduce the effort on the MYSQL Server as it wouldn't be running a bunch of wild card queries and would only run if there is a URL paramter delivered.??
I just don't get how to put the IF statements in PHP.
thanks
$maxRows_Recordset3 = 5;
$pageNum_Recordset3 = 0;
if (isset($_GET['pageNum_Recordset3'])) {
  $pageNum_Recordset3 = $_GET['pageNum_Recordset3'];
$startRow_Recordset3 = $pageNum_Recordset3 * $maxRows_Recordset3;
mysql_select_db($database_racing_analysis, $racing_analysis);
$query_Recordset3 = "SELECT * FROM dataextract WHERE Distance = 1000 AND Class = 1";
$query_limit_Recordset3 = sprintf("%s LIMIT %d, %d", $query_Recordset3, $startRow_Recordset3, $maxRows_Recordset3);
$Recordset3 = mysql_query($query_limit_Recordset3, $racing_analysis) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
if (isset($_GET['totalRows_Recordset3'])) {
  $totalRows_Recordset3 = $_GET['totalRows_Recordset3'];
} else {
  $all_Recordset3 = mysql_query($query_Recordset3);
  $totalRows_Recordset3 = mysql_num_rows($all_Recordset3);
$totalPages_Recordset3 = ceil($totalRows_Recordset3/$maxRows_Recordset3)-1;

Similar Messages

  • Charactersitic values to be used in IFTHEN ELSE STATEMENT IN QUERY

    scenario: i need to use the characteristic values(not the attributes) to be used in an IF THEN ELSE STATEMENT in query. so i created a formula variable for the characteritic of processing type replacement path, replace with key.The values are not fetched in the result and is displayed as X.
    what should i replace with key,lable, constant or other options whereas  my requirement is the all the values present in the characteristc should be used.
    eg. if char name is xyz and has the values 1,2,3,4,5 till -
    20.
    then in a calculated key figure :(formulavariable =1)* kefig1keyfig2 +not(formulavariable =1)kef2keyfig3.
    this formula variable should have all the values of the characteristic values ie 1,2,3---20.
    2. how should i do get the values of characterstics values in the equation give me the steps in detail..
    3. i do not want to use the attributes, though i tried initally while creating the formual variable i replaced with attribute value, there is was an error and there were no reult.
    i tried to create char. variable  for the characteristic  of manual entr , not ready for input values and gave all the values 1,2,3 --20 as default values and then created formaul variable and repalce with the variable, however this characteristic variable is not at all displayed from the list of variables which will be displayed while creating the formula variable.
    help me out.
    Thanks

    hi srini,
    i have includedthe char in rows. i want you to explain in detail how to use these values in the if then else statement in query.
    i have created a calculated key figure 2= (formual variable =1 & keyfig a =0)* keyfig1kefig2 +not(formual variable =1 & keyfig a =0)keyfig2 *calculated keyfig1.
    so this formula variable of replacement path have ref. to then char. and with what value should it replace, key, name, external char or attribut etc so that i should get the values of the char from 1 to20.
    i donot to repalce with the attribute. i just want the char. values and not thier attributes (1. description 2. the second attribute has the values say abc1,efg2,ghi3 for respective charteristic values.
    ie
    char value               desc                 2nd attrib
    1                            hi                     abc1
    2                            bye                  defg2
    3                            ciao                  ghi3

  • [Help] statement.executeQuery(query) opens MULTIPLE cursors

    Hey everyone,
    I'm having a real hard time finding anything about this on the net so I've turned here. When we call:
    resultSet = statement.executeQuery(query)
    from a servlet for some reason that one executeQuery opens anywhere from 9-12 cursors in our oracle DB. All the executeQuery documentation I could find just said it should open 1 cursor for that particular ResultSet. query is a fairly simple SQL statement, it searches multiple tables but that doesn't explain why it should open so many cursors.
    On a side note the resultSet and statement are global to the method and are closed in the finally block, no SQLExceptions are thrown further adding to my confusion.
    If anyone has seen something like this where executing 1 query opens multiple cursors please let me know any information you might have.
    Thanks in advance
    -Dave

    Hi Dave
    I had a similar problem using the Oracle 8i, but my query was a more complicated than yours and opened a lot of cursors.
    The answer is to close every ResultSet and the Statement:
    while (xxResult.next()){ ... }
    xxResult.close();
    xxResult.getStatement().close();
    This worked for me,
    Good luck,
    Daniel.

  • Need help with select statement or query

    Not familiar with what to call it, but here is what i need...
    To give our analyst a better idea of warranty on some of our
    equipment, i
    would like to add to the page a column that displays if the
    device is still
    under warranty
    I currently capture the date the equipment was returned from
    repair, so what
    could i use within my select statement or query to display a
    warranty
    expiration date or display on the page...
    example :
    Returned from repair 10/20/2006 warranty expires on
    11/20/2006
    each equipment has different warranties, so i need a formula
    or something to
    say... device #1 has 60 day warranty ( so 10/20/2006 + 60days
    =
    12/19/2006 )
    I would imagine this to be a query
    Table 1 would contain the equipment type and warranty time
    Table 2 would contain the current status of the equipment
    Query would take the back from repair date + warranty =
    expiration date

    Simple. Join the two tables and create a derived column for
    the expiration date. The exact syntax is dependant on your DBMS, so
    check the manual for whichever you are using and look at the date
    functions. There will be a function that will allow you to add a
    number of date units (day, month, year, etc) to a date
    field.

  • Pass parameter to sql statement in query manager

    Hai to all,
               I want to pass the percentage  as the parameter into the sql statemnet.i what to execute it in the query manager.
              If i execute that statement then cann't found the tablename error is coming.
             Other than the data in the table (general data)  pass to the parameter in the sql at runtime.
    for example:
    select [%0] *100
    how to pass 10 to that sql statement.
    Please help me...
    Regards,
    Raji.

    Hi Ramya,
    You can create a SP with parameters to accept and then execut this SP from SAP Business One Query Manager by passing the parameter (in your case 10). The result will be as desired.
    Ex:
    Create this Procedure in SQL Management Studio
    create proc Test(@a as int)
    as
    begin
    select (@a*100)
    end
    To Execute the Query use this Query and pass the desired values with parameters
    execute Test 10
    Regards,
    Reno

  • How to pass column name in slect statement in query

    hi,
    i want to make a report where in query select statement using variable as a column name. but its not working plz guide me how can i do this.
    i have created a function which return column name through variable & that variable i want to to use in select statement
    select :m1 from table1;
    regards

    Hi,
    Create a user parameter (say P_field), and assign a valid field name as initial value (say NAME), And In the Query, write
    SELECT CODE, &P_field FN_FIELD FROM <table_name> WHERE <condition>And in the BEFORE PARAMETER FORM Trigger under the Report Triggers, write,
    function BeforePForm return boolean is
    begin
      :P_field := <your_function_call>;
      return (TRUE);
    end;And use that FN_FIELD field in the report.
    Hope this will clear your issue.
    Regards,
    Manu.

  • Hiding SQL statement in Query Result Window

    Hi everyone,
    Is it possible to hide the SQL statement from a query result window? This can usually be done by click the orange triangle that is positioned to the left of the SQL string box (to expand and collapse it). The System Queries have this box collapsed per default, is it possible to have this setup as default for all queries, including the custom? If so, please let me know where this can be done.
    Cheers

    You should do this only with SDK programming - dont allow to show the syntax. The next, but problematic way, is the code you have do inside stored procedure and call only this sp inside the query generator window - if you have there some input parameters, you should use filling variables before executing sp and get it as input parameters to sp.

  • Select Statement to query smallest ID

    <My_Table>
    ID
    PHOENIX005
    PHOENIX006
    PHOENIX007
    PHOENIX008
    TUCSON001
    TUCSON002
    TUCSON003
    TEMPE010
    TEMPE011
    TEMPE002
    CHANDLER030
    CHANDLER031
    CHANDLER032
    Whats the best way to write a select statement that would only bring back the IDs with the smallest numerical value? I ultimately want my Query to display:
    ID
    PHOENIX005
    TUCSON001
    TEMPE010
    CHANDLER030

    Or may be like this...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (
      2             select 'PHOENIX005' id from dual union all
      3             select 'PHOENIX006' from dual union all
      4             select 'PHOENIX007' from dual union all
      5             select 'PHOENIX008' from dual union all
      6             select 'TUCSON001' from dual union all
      7             select 'TUCSON002' from dual union all
      8             select 'TUCSON003' from dual union all
      9             select 'TEMPE010' from dual union all
    10             select 'TEMPE011' from dual union all
    11             select 'TEMPE002' from dual union all
    12             select 'CHANDLER030' from dual union all
    13             select 'CHANDLER031' from dual union all
    14             select 'CHANDLER032' from dual
    15            )
    16  select *
    17  from
    18  (
    19     select id, row_number()over(partition by trim(translate(id,'0123456789', rpad(' ',10))) order by
    20     to_number(trim(translate(id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', rpad(' ',26)))) ) as rno
    21     from t
    22* ) where rno=1
    SQL> /
    ID                 RNO
    CHANDLER030          1
    PHOENIX005           1
    TEMPE002             1
    TUCSON001            1
    SQL>

  • Adding State To Query

    Hello -
    We have this query and I would like to add the state but wasn't able to.  Please see below and advise how we can edit query so it shows state:
    SELECT T0.CreateDate, T0.CardType 'Type', T0.CardCode 'Acct #', T0.CardName 'Company Name',T0.CntctPrsn, T0.Address, T0.City,T0.ZipCode 'Zip', T0.Phone1 'Phone', T0.E_Mail FROM OCRD T0 WHERE T0.CardType in('C','L') and T0.CreateDate >= [%0] and T0.CreateDate <= [%1] ORDER BY T0.ZipCode
    Thanks,
    Mike

    Hello Mike,
    Try this one:
    SELECT T0.CreateDate, T0.CardType 'Type', T0.CardCode 'Acct #', T0.CardName 'Company Name',T0.CntctPrsn, T0.Address, T0.City,T0.State1, T0.ZipCode 'Zip', T0.Phone1 'Phone', T0.E_Mail
    FROM dbo.OCRD T0
    WHERE T0.CardType in('C','L') and T0.CreateDate >= [%0\] and T0.CreateDate <= [%1\] ORDER BY T0.State1, T0.ZipCode
    Thanks,
    Gordon

  • Removing Stats improves query performance

    Hi,
    Today i have got a rare question regarding Stats collection,
    In some cases removing existing stats will improve the query performance...
    What is the reason behind this?
    After a rigorous search i have landed here to here from you all.
    Thanks in-advance.
    Vara

    Hi,
    Before 11gR2 and adaptive cursor sharing, skewed data could result in wrong execution plan (histogramms and bind variable peaking could not fix it).
    Sometimes statistics cannot be fresh enough, for example with temporary table, but also for regular tables which are massively loaded/modified, resulting in poor execution plan.
    Also the optimizer might use guesses for complex predicates... which could have same consequences.
    When no statistics are available, dynamic sampling may give a more accurate estimation of the cardinalities than statistics.
    https://blogs.oracle.com/optimizer/entry/dynamic_sampling_and_its_impact_on_the_optimizer

  • Get Stuck At stat.executeQuery(query) Function

    Hi,
    Can Any one Tell.
    What Can be The possible reasons For The executeQuery function get stucked?
    Table from where i am getting the value has only two row.
    and i am using mysql-connector-java-5.1.6-bin.jar driver to get connected to mysql.
    Some times the query works fine , but sometimes it get Stuck at this function , then i have to restart the mysql to work again properly.
    Thanks

    Ashutoshklkkkkkkkkkkkkkkk wrote:
    And the i don't close the connection i just kill the application .
    What can be the side effect of not closing the connection only just killing the application on linux.If you don't close a connection, it stays open until it times out in the DB side. Which may be one hour, but it can also be one day or more. The more unclosed connections you have, the lesser resources the database will have left and it will finally die.
    Always, I repeat, always close expensive resources explicitly. Do that in the finally block. Acquire and close Connection, (Prepared)Statement and ResultSet in the shortest possible scope.

  • SQL statement for query of all values

    I am trying to pull all the EE's that do not have a value in a specific fields for any row in history.
    Example
    Employee table
    Each employee can have 5-10 rows each
    The field employeeX can be either Y or N
    I only want to pull the distinct employee's name that have N for every single row.

    Here's one way:
    DECLARE @employees TABLE (employeeID INT, currentlyEmployed CHAR(1), updateDateTime DATETIME)
    INSERT INTO @employees (employeeID, currentlyEmployed, updateDateTime)
    VALUES
    (1, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(1, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(1, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(1, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),
    (1, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(2, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(3, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(3, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),
    (3, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(3, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(3, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(4, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),
    (4, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(5, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(5, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(5, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),
    (5, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(5, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(6, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(6, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),
    (6, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(6, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(7, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(8, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),
    (8, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(8, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(9, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),(10, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP)),
    (10, CASE WHEN ROUND(((2 - 0 - 1)*rand())+1,0) = 1 THEN 'Y' ELSE 'N' END, DATEADD(DAY,ROUND(((30 - 1 - 1)*rand())+1,0),CURRENT_TIMESTAMP))
    SELECT *
    FROM @employees e
    LEFT OUTER JOIN (SELECT employeeID FROM @employees WHERE currentlyEmployed = 'Y' GROUP BY employeeID) e2
    ON e.employeeID = e2.employeeID
    WHERE e2.employeeID IS null
    In future please provide at least example data, if not DML and DML as well.

  • Query statement of 'Get BP Code of last AR Invoice'

    Hi,
    I have the query for A/P Invoice to show the last A/P BP when I click Shift + F2. The statement in Query is as follows:
    Select T0.CardCode from OPCH T0 where T0.DocNum = $[$8.0.Number] - 1
    Could anybody tell me what the statement would be if I want to have the last A/R BP automatically show in A/R Invoice when I click Shift + F2?
    Thanks!

    Yes, Shift + F2 is for FMS. I would like to have FMS for the field of BP Code under A/R invoice to be linked with the query - - show the BP code of last A/R Inovice.
    Could you please tell me what the query statement (conneted to FMS) would be? Thanks!
    Currently I have the Query workable under A/P Invoice, the statement for this query is:
    Select T0.CardCode from OPCH T0 where T0.DocNum = $[$8.0.Number] u2013 1

  • Query on Stat Cube

    Hi,
    I am trying to get query list based on last used date.
    I ran query on stat cube 0BWTC_...
    Also, i got list of queries from the view v_compdir_compic in bw backend.
    But the query list i get from stat cube query is less than what i get from View table.
    there are aprox 300 less queries.
    can u please tell me why the list is different ?
    thanks,
    KS
    Edited by: Keral Smith on Nov 4, 2008 10:04 AM

    Hi Abhijit,
    We have all BW statistic cubes loaded on november 1st.
    which are 0bwtc_c01,.......they are for aggregate, for OLAP, for OLAP navigation etc.
    so how come the list of queries are different than V_COMPDIR_COMPIC ?
    any clue ?
    thanks,
    KS

  • Printing Prepared Statement Query

    Hi all,
    Is it possible to print Prepared Statement query. When i give System.out.println(query);, i get the query with '?' and not with the actual value sent. How can i print the query with the value passed in place of the '?' ?

    Hi all,
    Is it possible to print Prepared Statement
    tatement query. When i give
    System.out.println(query);, i get the query with '?'
    and not with the actual value sent. How can i print
    the query with the value passed in place of the '?' ?The simple answer is that you can't (AFAIK), but some database drivers are able to log the sql statements.
    Kaj

Maybe you are looking for