Attn: PDO Experts (COUNT query)

This is an example of the type of a PHP/MySQL query I use to display dynamic web pages. This particular query is associated with a website/section that displays URL's like MySite/People/Crazy_Horse, where Crazy_Horse = both $MyURL and a value in the field URL, table people...
$res = mysql_query("SELECT COUNT(URL)
  FROM people
  WHERE URL = '$MyURL' AND Site = 'PX'
  OR '$MyURL' = CONCAT('Washington/', URL) AND Site = 'SM'");
  if (!$res) {
    die('Invalid query: ' . mysql_error());
$result = mysql_result($res, 0);
// PART 2 - Interpret the results...
switch ($result)
case 1:
// include related files
break;
case 0:
// include 404 NOT FOUND error message
break;
default:
// multiple entries; deal with as needed
break;
Now I'm trying to learn PDO, and I assumed the simplest queries would be the simplest to convert to PDO. To my surprise, many online references say PDO can't perform COUNT queries! As I understand it, all the PDO queries that do this sort of thing are workarounds. After consulting several tutorials and forums and trying several scripts, I still don't have a working model. This is what I'm trying at the moment:
I start out with my PDO database connection (replacing "USERNAME" and "PASSWORD" with my username and password, of course...
try {
    $db = new PDO('mysql:host=localhost;dbname=db_new;charset=utf8', 'USERNAME', 'PASSWORD');
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
The above code is on a separate file that's included in every page on my website.
I then have this code on a downstream file...
$sql= "SELECT URL FROM people
WHERE URL = '$MyURL'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$total = $stmt->rowCount();
But it doesn't work, and there's no error message to indicate what the problem is. (However, if I modify my username or password in the connection script, I do get an error message referring to my database connection.) Once I do get it to work, I assume I can finish the job by simply replacing $result with $total...
switch ($total)
case 1:
// include related files
break;
case 0:
// include 404 NOT FOUND error message
break;
default:
// multiple entries; deal with as needed
break;
Can anyone tell me what I'm doing wrong, and how I can fix it?
Thanks.

How does this post relate to Web Design?   Any ideas?

Similar Messages

  • Incorrect count query

    I'm using CursoredStream to fetch a large result set. When doing a cursor.size() Toplink generates the wrong count query. I do have group value functions in the query as shown below.
    For e.g. this is the cursor query:
    --- Actual select query ---
    SELECT t0.AWARD_ID, t0.AWARD_NUMBER, t0.FINAL_AMOUNT, t2.NAME,
    SUM(t1.REPORT_AMOUNT), t4.FISCAL_YEAR, t5.DESCRIPTION
    FROM REF_ENTITY t6, T_CODE t5, SOL t4, APP t3, APP_VERSION t2, SUBG t1,
    AWARD t0
    WHERE ((((t4.FISCAL_YEAR = '1993') AND (t6.REF_ENTITY_ID = 5)) AND (t4.SUBG_
    REPORT = 'Y')) AND ((t5.T_CODE_ID = t0.T_CODE_ID_STATUS) AND ((t2.APP_ID = t3.A
    PP_ID) AND ((t6.REF_ENTITY_ID = t4.REF_ENTITY_ID) AND ((t4.SOL_ID = t3.SOL_ID)
    AND ((t3.APP_ID = t0.APP_ID) AND (t0.AWARD_ID = t1.AWARD_ID)))))))
    GROUP BY t0.FINAL_AMOUNT, t0.AWARD_ID, t0.AWARD_NUMBER, t4.FISCAL_YEAR, t5.D
    ESCRIPTION, t2.NAME ORDER BY t0.AWARD_NUMBER ASC
    The count query generated by Toplink is:
    SELECT COUNT(*)
    FROM REF_ENTITY t4, AWARD t3, APP t2, SOL t1, SUBG t0
    WHERE ((((t1.FISCAL_YEAR = ?) AND (t4.REF_ENTITY_ID = ?))
    AND (t1.SUBG_REPORT = 'Y')) AND ((t4.REF_ENTITY_ID = t1.REF_ENTITY_ID)
    AND ((t1.SOL_ID = t2.SOL_ID) AND ((t2.APP_ID = t3.GMS_APP_ID)
    AND (t3.AWARD_ID = t0.AWARD_ID)))))
    I have to provide my own ValueReadQuery to get the correct count in the above case. Are there any rules for determining when we have to explicitly provide our own count query?
    cheers
    p.s: Toplink version 9.0.3.4

    Abe,
    This sounds like a bug. I assume you are using a ReportQuery to generate the initial SQL. You should only have to provide a ValueReadQuery to a cursored query when using SQL, stored-procedure, or to optimize what is being generated.
    This looks like the group-by on your ReportQuery is being ignored on the size query. The best plan is to submit this to support with the TopLink code used and the information provided in this post.
    In the mean time the work-around of providing your own query is probably the best solution.
    Doug

  • Count Query Performance

    How can we improve the perfromace of a count query?
    Suppose I have a table A that holds more than 50 million rows.
    Now if i want to count the no of rows which is the best one
    1) Select count(*) from A.
    Definitely not as it is doing a full table scan
    2) Select count(primary_key) from A.
    3) Select count(row_id) from A.
    One more question whats the difference between select count(*) from table_name and select count(1) from table_a. Many people suggest count(1) and i dont see any reason though.
    Kindly guide me.

    > Please see my points 1,2 and 3.
    Can this change the execution plan (path) of the CBO in anyway?
    1. count rows
    2. count rows using primary key
    3. counting rows using physical row addresses
    The fact is that the rows, and all the rows, need to be counted. The CBO will choose the most "attractive" I/O path - i.e. the smallest one, the one with the least amount of I/O. It does not need tricks like COUNT(1) or COUNT(PK) or COUNT(ROWID) in order to make an appropriate decision.
    Example:
    SQL> create table tab1 nologging as select level as ID, LPAD('*',4000,'*') as STUFF from dual connect by level <= 10000;
    Table created.
    SQL> set autotrace on
    Running a SELECT COUNT(*) without any alternate I/O paths (no indexes exist)
    SQL> select count(*) from tab1;
    COUNT(*)
    10000
    Execution Plan
    Plan hash value: 899213575
    | Id | Operation | Name | Rows | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 2306 (4)| 00:00:28 |
    | 1 | SORT AGGREGATE | | 1 | | |
    | 2 | TABLE ACCESS FULL| TAB1 | 9982 | 2306 (4)| 00:00:28 |
    Note
    - dynamic sampling used for this statement
    Statistics
    28 recursive calls
    0 db block gets
    10087 consistent gets
    10000 physical reads
    0 redo size
    208 bytes sent via SQL*Net to client
    238 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    1 rows processed
    Creating an PK index
    SQL> alter table tab1 add constraint pk_tab1 primary key(id) using index;
    Table altered.
    Running the same SELECT COUNT(*) - but the CBO now sees that the PK index
    is smaller and cheaper to scan than scanning the table
    SQL> select count(*) from tab1;
    COUNT(*)
    10000
    Execution Plan
    Plan hash value: 1796789124
    | Id | Operation | Name | Rows | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 9 (23)| 00:00:01 |
    | 1 | SORT AGGREGATE | | 1 | | |
    | 2 | INDEX FAST FULL SCAN| PK_TAB1 | 9982 | 9 (23)| 00:00:01 |
    Note
    - dynamic sampling used for this statement
    Statistics
    194 recursive calls
    0 db block gets
    131 consistent gets
    20 physical reads
    0 redo size
    222 bytes sent via SQL*Net to client
    238 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    5 sorts (memory)
    0 sorts (disk)
    1 rows processed
    SQL>

  • Expert mode query in View objects and appended where clause

    My company is Oracle Member Partner and we are developing enterprise web applications using Oracle database and BC4J.
    I have the following problem...
    When I enable EXPERT MODE option in View Object I have trouble appending to query statement in my client code.
    I need expert mode because I must use "SELECT DISTINCT" insted of "SELECT" in my query.
    It looks something like this:
    viewObject.setWhereClause("CLA_ID = " + claId);
    viewObject.executeQuery();
    SQL query from View Object becomes sub-query and fails to execute:
    select * from (original view object query) where (... appended where clause)
    Order by part of the query causes sql errors because original query is now sub-query.
    Is there any way around this?

    I tried creating an expert mode SQL query:
    SELECT DISTINCT EMPNO, ENAME FROM EMP.
    Then at runtime I do:
    vo.setWhereClause("ENAME LIKE '%'||?||'%');
    vo.setWhereClauseParam(0,'A');.
    and this works fine. The trick is that since expert-mode view objects get wrapped as inline views (to allow runtime appending of WHERE clause, actually), you need to select any column in the select statement to which you want to later refer in a dynamically-appended where clause.
    If you want to prevent the inline-view wrapping, you can write the following code in your view object's ViewObjectImpl subclass to force the VO to NOT be treated as an expert-mode SQL VO.
      // Goes in your view object impl subclass
      public void create() {
         // Force this VO to NOT be treated as an expert-mode SQL, so that
         // its query does not get wrapped as an inline view.
         getViewDef().setFullSql(false);
      }I used this trick above to create an expert mode query like:
    SELECT DISTINCT deptno FROM empand then at runtime I add a dynamic where clause that refers to a column
    in EMP that is not in the select list like this:
        ViewObject vo = am.findViewObject("View1");
        vo.setWhereClause("ename like '%A%'");
        vo.executeQuery();
        System.out.println(vo.first().getAttribute(0));and this causes the query to come out as:
    SELECT DISTINCT deptno FROM emp WHERE ename like '%A%'.
    instead of:
    SELECT * FROM (SELECT DISTINCT deptno FROM emp) QRSLT WHERE ename like '%A%'which would cause an error due to the fact that ename is not in the select list of the original (wrapped, inline) query.

  • Count query taking time

    I have a query -->select c1,c2,c3 from table1 . This query takes only few milliseconds. But when I take count from the same query i.e. when I execute select count(c1,c2,c3) from table1 then it takes a very long time (about 1 min). The table1 contains about 25000 rows. Please help to improve performance of Count query.

    Satej wrote:
    I have a query -->select c1,c2,c3 from table1 . This query takes only few milliseconds. But when I take count from the same query i.e. when I execute select count(c1,c2,c3) from table1 then it takes a very long time (about 1 min).Classic misperception of Toad, SQL Navigator and similar tool users. All these tools fetch just first result screen and show time it took to fetch just that and not the time to fetch all rows. And in order to count you need to fetch all rows.l That is why select count(*) takes longer. But 1 min for 25000 rows is a bit long. Check execution plan to see what is going on.
    SY.

  • Invalid SQL generated for count query.

    A SQL statement was generated with the table name information missing when
    a count SQL query was issued (e.g. SELECT COUNT(*) FROM WHERE <SOME
    EXPRESSION>). The same query worked fine before switching to vertical
    class mapping.
    Basically no queries work with the result set to "count(this)". The
    temporary work-around was to retrieve the collection and get the size. I
    would rather not have to do this.
    Any ideas?

    Brendan,
    What version of Kodo is this happening with? Can you send a test case
    demonstrating this to [email protected]?
    -Patrick
    Brendan Brothers wrote:
    A SQL statement was generated with the table name information missing when
    a count SQL query was issued (e.g. SELECT COUNT(*) FROM WHERE <SOME
    EXPRESSION>). The same query worked fine before switching to vertical
    class mapping.
    Basically no queries work with the result set to "count(this)". The
    temporary work-around was to retrieve the collection and get the size. I
    would rather not have to do this.
    Any ideas?

  • Sub-Select Count query breaking TOAD

    Oracle 10.2.0.4.0
    Running TOAD 9.1
    I am running some SQL on our eBusiness Suite:
    SELECT pha.segment1
         , pha.type_lookup_code
         , (SELECT COUNT(DISTINCT pha2.po_header_id)
              FROM po.po_headers_all pha2
                 , po.po_lines_all pla
             WHERE pha2.po_header_id = pla.po_header_id
               AND pla.contract_id = pha.po_header_id) po_count
         , (SELECT MAX(pha2.creation_date)
              FROM po.po_headers_all pha2
                 , po.po_lines_all pla
             WHERE pha2.po_header_id = pla.po_header_id
               AND pla.contract_id = pha.po_header_id) latest_cpa_po
      FROM po.po_headers_all pha
         , po.po_vendors pv
         , po.po_vendor_sites_all pvsa
    WHERE pha.vendor_id = pv.vendor_id
       AND pha.vendor_site_id = pvsa.vendor_site_id
    --   AND pv.VENDOR_NAME LIKE 'H%'
       AND pha.vendor_id = 98
       AND pha.type_lookup_code = 'CONTRACT'
       AND pha.org_id IN(7041, 7042);The above query runs quicky (approx. 1 second). If I take out the AND pha.vendor_id = 98 then the query takes a few minutes to run.
    When I try to export it, or scroll down to view > 500 rows, TOAD crashes.
    I know this isn't a TOAD forum, but I think that this is probably an issue with my no doubt rubbish SQL.
    If I take out this sub-select, then the problem doesn't happen:
         , (SELECT COUNT(DISTINCT pha2.po_header_id)
              FROM po.po_headers_all pha2
                 , po.po_lines_all pla
             WHERE pha2.po_header_id = pla.po_header_id
               AND pla.contract_id = pha.po_header_id) po_countHowever, I can't work out a better way of getting the data I need.
    The sub-select counts POs which have been raised where the contractID on the PO line is the same as the PO Header ID from the main query.
    Any advice please, on what I could do to sort this out would be much appreciated.
    Thanks!

    Hi,
    It looks like you can replace both scalar sub-queries with a join, like this:
    WITH     header_lines_summary     AS
         SELECT    pla.contract_id
              ,       COUNT (DISTINCT pha2.po_header_id)     AS po_count
              ,       MAX (pha2.creation_date)          AS latest_cpa_po
              FROM        po.po_headers_all pha2
                 ,        po.po_lines_all   pla
             WHERE        pha2.po_header_id = pla.po_header_id
          GROUP BY       pla.contract_id
    )                                        -- Everything up to this line is new
    SELECT pha.segment1
         , pha.type_lookup_code
         , hls.po_count                              -- Changed
         , hls.latest_cpa_po                         -- Changed
      FROM po.po_headers_all     pha
         , po.po_vendors           pv
         , po.po_vendor_sites_all      pvsa
         , header_lines_summary     hls                    -- New
    WHERE pha.vendor_id          = pv.vendor_id
       AND pha.vendor_site_id     = pvsa.vendor_site_id
       AND pha.po_header_id          = hls.contract_id (+)          -- New
    --   AND pv.VENDOR_NAME      LIKE 'H%'
       AND pha.vendor_id           = 98
       AND pha.type_lookup_code      = 'CONTRACT'
       AND pha.org_id           IN (7041, 7042);Aside from the sub-query (which is entirely new), the query above is just what you posted, with 2 lines changed and 2 lines added, as marked.
    This should be more efficient, but I don't know for certain that it will solve the Toad problem.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
    It never hurts to say what version of Oracle you're using.

  • Help with a COUNT query...

    I have created the following tables:
    create table ZONE (
    ZoneNo number(2),
    State char(3),
    PRIMARY KEY (ZoneNo)
    create table SCHOOL (
    SchoolType char(1),
    SchoolNumber number(4),
    SchoolName char(40),
    SchoolZone number(2),
    PRIMARY KEY (SchoolNumber,SchoolType),
    FOREIGN KEY (SchoolZone) REFERENCES ZONE
    create table TEACHER (
    TeacherCode char(5),
    Surname char(30),
    FirstName char(30),
    Gender char(1),
    WorksFor number(4),
    YearCommenced number(4),
    LivesIn number(2),
    TeachingType char(1),
    PRIMARY KEY (TeacherCode),
    FOREIGN KEY (WorksFor, TeachingType) REFERENCES SCHOOL(SchoolNumber,SchoolType),
    FOREIGN KEY (LivesIn) REFERENCES ZONE
    create table COMPUTER (
    SerialNumber char(10),
    Description char(50),
    RegisteredTo char(5),
    PRIMARY KEY (SerialNumber),
    FOREIGN KEY (RegisteredTo) REFERENCES TEACHER
    My question is, how would i make a COUNT (or similar) query that would display each SchoolName and show the total amount of teachers who work at that school in the second column?

    Yes, it needs to join all columns on foreign key.
    This is usual, gentle, safety, and universal approach.
    insert into ZONE values (6,'CA');
    insert into SCHOOL values ('E',600,'Oracle Elemental School',6);
    insert into SCHOOL values ('J',600,'Oracle Middle School',6);
    insert into SCHOOL values ('S',600,'Oracle High School',6);
    insert into TEACHER values (12001,'Suzuki','Akiko','F',600, 2005, 6, 'E');
    insert into TEACHER values (12002,'Scott','Tiger','M',600, 2005, 6, 'E');
    insert into TEACHER values (12003,'Zhong','San','M',600, 2005, 6, 'J');
    insert into TEACHER values (12004,'Li','Si','M',600, 2005, 6, 'J');
    insert into TEACHER values (12005,'Wang','Wu','M',600, 2005, 6, 'J');
    insert into TEACHER values (12006,'Kim','Youngae','F',600, 2005, 6, 'S');
    insert into TEACHER values (12007,'Kim','Jiu','F',600, 2005, 6, 'S');
    insert into TEACHER values (12008, null,'Joe','M',600, 2005, 6, 'S');
    column schoolname format a30
    set lines 120
    select s.SchoolName
    ,count(*) "ALL"
    ,count(t.TeacherCode) "ALL(CODE)"
    ,count(distinct t.TeacherCode) "DISTINCT(CODE)"
    ,count(t.surname) "ALL(NAME)"
    ,count(distinct t.surname) "DISTINCT(NAME)"
    from School s, Teacher t
    where s.SchoolNumber = t.WorksFor
    and s.SchoolType = t.TeachingType
    group by s.SchoolName
    SCHOOLNAME                            ALL  ALL(CODE) DISTINCT(CODE)  ALL(NAME) DISTINCT(NAME)
    Oracle Elemental School                 2          2              2          2              2
    Oracle High School                      3          3              3          2              1
    Oracle Middle School                    3          3              3          3              3select s.SchoolName
    ,count(*) "ALL"
    ,count(t.TeacherCode) "ALL(CODE)"
    ,count(distinct t.TeacherCode) "DISTINCT(CODE)"
    ,count(t.surname) "ALL(NAME)"
    ,count(distinct t.surname) "DISTINCT(NAME)"
    from School s, Teacher t
    where s.SchoolNumber = t.WorksFor
    --and s.SchoolType = t.TeachingType
    group by s.SchoolName
    SCHOOLNAME                            ALL  ALL(CODE) DISTINCT(CODE)  ALL(NAME) DISTINCT(NAME)
    Oracle Elemental School                 8          8              8          7              6
    Oracle High School                      8          8              8          7              6
    Oracle Middle School                    8          8              8          7              6

  • Using a "Sum" Calculated Field on a "Count" query column?

    Here's my query using the Query Report Builder:
    SELECT Col1, COUNT(Col1) AS Count_Column
    FROM Table
    GROUP BY Col1
    It generates a report that lists all the values of column
    "Col1" and how many times each value was used in Col1
    For instance, if Col1 contained the value 'A' 12 times, 'B' 6
    times, and 'C' 19 times, the report would generate this:
    A - 12
    B - 6
    C - 19
    What i need as a column footer is the total count of all the
    values, which in this case 12+6+19=37
    I am using a calculated field, setting the data type to
    Double, the calcuation to Sum, and the perform calculation on to
    'query.Count_Column'. Reset Field When is set to None.
    When I run the report, it doubles the last number in the
    report's Count column (19) and displays 38 on the page. I tested
    this with another column and it doubled the last number in the
    report as well.
    How can I get it to properly Sum my Count_Column?

    Hi,
    You need to check note 208366.1 which explains why totals can be blank. Without knowing the detail of you decode function it is hard to say what needs to be changed. Try putting a sum in front of the decode e.g.
    sum(decode(period, 'Jan period', value, 0))
    Hope that helps,
    Rod West

  • Multiple count query

    I'm trying to write a function to compare two different counts.
    I have patient on multiple drugs...some drugs are allergy drugs and at least one allergy drug must be marked as primary.
    select count(drug_role) as vAllergyCount, count(primary_flag) as PF, ap.patient_id
    from ae_all_drugs aed, ae_patient ap
    where drug_role = 'Allergy'
    and aed.patient_id = ap.patient_id
    and ap.patient_id like '2012%'
    group by ap.patient_id,primary_flag
    order by ap.patient_id
    when grouped by patient id and primary flag I would receive a record like this:
    VAlllergyCount | PF | patient_id
    1 | 0 | 2012AB1
    3 | 1 | 2012AB1
    If i only group by patient_id I get this (which is what I want):
    VAlllergyCount | PF| patient_id
    4 | 1 | 2012AB1
    But if I try to compare the two:
    IF vAllergyCount > 1 and PF = 1 THEN 'SUCCESS' ELSIF 'FAIL'
    I receive the error message "exact fetch returns more than requested number of rows" becuase the results are really my first query.
    Can anyone help me re-write my query to compare the values?
    P.S. I'm very new to this and this forum and I apologize for not inserting code tags. I don't know how.

    Hi,
    979105 wrote:
    Thank you for your hlep.
    I need to write a function because the SQL query is going to be used as an edit check for another program. So yes, it will have to worth my time.
    You are correct. I have a patient on multiple drugs. I want to know if there is at least one allergy drug (drug_role) > 1 if there is at least ONE primary allergy drug.
    I did initially write my function with the patient_id as an input value. Why would I not need the vAllergyCount or the pf if that is an argument?If all you want to know is 'SUCCESS' or 'FAIL', then write a query that produces 'SUCCESS' or 'FAIL'. Inside the query, it may be handy to have something like vAllergyCount or pf, but that doesn't mean that they will be returned by the query, or that you need PL/SQL variables for them.
    Here's my original function:That's a PROCEDURE, not a function. Sometimes, people use the word "procedure" lossely, to mean any kind of stored code, including functions, but never the other way around.
    \Don't forget to put another \ tag at the end of the formatted section.
    \ tags are supposed to occur in pairs. If you have an odd number of \ tags, the last one has no effect, and is displayed as regular text.
    PROCEDURE COUNT_ALLERGY_MED (
         ivPatient_ID IN VARCHAR2,
         ovErrorText          OUT     VARCHAR2 )
    IS
         vCase_ID          VARCHAR2 (25);
         vSusCount           NUMBER (3);
         vSuspectMed          VARCHAR2 (1);
         --vPrimaryFlag     VARCHAR2(1);
    cursor c1 is
         select count(*) as vSusCount
         from ae_suspect_drugs aed
         where drug_role = 'S'
         and aed.case_id = ivCase_ID
         --group by ivcase_id;
    BEGIN
    open c1;
    fetch c1 into vSusCount;
    close c1;
    IF vSusCount < 1 /*AND vPrimaryFlag IS NULL*/ THEN
         ovErrorText := 'Suspect Drug Count is less than 1' || 'SusCount: ' || vSusCount;
         ELSIF vSusCount = 1 /*AND vPrimaryFlag IS NULL*/ THEN
         ovErrorText := 'Suspect Drug count is equal to 1' || 'SusCount: ' || vSusCount;
         ELSIF vSusCount > 1 /*AND vPrimaryFlag IS NULL*/ THEN
         ovErrorText := 'Suspect Drug count is greater than 1 ' || 'SusCount: ' || vSusCount;
         ELSE
         ovErrorText := 'All criteria met';
    END IF;
    END COUNT_SUSPECT_MED;It seems like it would be more convenient to have a FUNCTION, rather than a PROCEDURE; that way you could call the function directly in a SQL statement (e.g., in the WHERE clause of a query or DML statement) as well as in PL/SQL.
    The procedure you posted seems to be quite a bit differetn from the problem you asked eariler.
    Here's one way you might write a fucntion for the earlier problem:CREATE OR REPLACE FUNCTION count_allergy_med
    ( ivPatient_ID IN VARCHAR2
    RETURN VARCHAR2
    IS
    return_str VARCHAR2 (50);          -- To be returned
    BEGIN
    SELECT CASE
         WHEN COUNT (drug_role) > 1
              AND COUNT (primary_flag) = 1
              THEN 'SUCCESS'
              ELSE 'FAIL'
         END     
    INTO return_str
    FROM ae_all_drugs     aed
    ,      ae_patient           ap
    WHERE drug_role          = 'Allergy'
    AND     aed.patient_id      = ap.patient_id
    AND     ap.patient_id     = ivPatient_ID;
    RETURN return_str;
    END count_allergy_med;
    Notice that there are no columns in the result set, nor variables in the function, that correspond to vAllergyCount or pf.
    The function above returns 1 of 2 possible values.  You couold just as well write a function that had 4 possible outcomes, or 4 basic results, each containing a variable number, such asSuspect Drug Count is less than 1. SusCount: 0
    I'm not sure why you would want such as result, since 0 is the only possible count that is less than 1, but it's easy enough to produce results like that if you really want them.
    Once again, if you'd like help, post a clear example of what you want.  Include CREATE TABLE and INSERT statements for some sample data.  Post some code where you might use the function that you're thinking of.  Give a couple of different arguments for that functuion, and the results you would want for each argument given the same sample data.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Customer Count Query

    Hello All
    I need help in writing a Optimized query .
    Scenario :
    We have several customers across multiple vendors .We want to get Customercount by vendor for a year 
    In this scenario we wanted to claculate 
    Customer Count for Vendor 
    Example 
    2014  Kroger(Vendor) has 100 customers
    2014  Target(Vendor) has 50 customers
    I wanted to see out of those 100 customers for Kroger who were not customer of kroger in 2012 and 2013 but customer of target
    Similarly  out of those 50 customers for Target who were not customer of target in 2012 and 2013 but customer of
    kroger
    In 2012 same customer might have bought with different vendor.We are calculating new to vendor 
    C1  2012   Kroger 100
    C1  2012   Walmart 100
    C2  2012   target 100
    C2  2012   Kroger 100
    C1  2014   target 100
    C2  2014   Kroger 100
    C2  2014   target 100
    C2  2014   walmart 100
    In this case C1 is customer count for target   as he did not buy with target but with other vendors
    and c2 canot be counted towarsd kroger or walmart becauase be bought with both of them but can be counterd towarrds walmart as he did not buy with walmart
    I am trying to write a query to get 2012 and 2013 data for all vendors and then join with 2014 data set
    but the thing is i am getting wrong count 
    Please let me know

    this?
    SELECT Vendor,
    YEAR(DATEADD(yy,DATEDIFF(yy,0,Datefield),0)) AS Yr,
    COUNT(CustomerID) AS CustomerCount,
    COUNT(CASE WHEN Cnt = 0 THEN CustomerID ELSE NULL END) AS FirstTimeCustomerCount
    FROM Table t
    OUTER APPLY (SELECT COUNT(*) AS Cnt
    FROM Table
    WHERE CustomerID = t.CustomerID
    AND DATEDIFF(yy,0,Datefield) < DATEDIFF(yy,0,t.Datefield)
    AND Vendor = t.Vendor)
    GROUP BY Vendor,DATEDIFF(yy,0,Datefield)
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Problem with count query

    I am connecting to the excel and running the following query:
    Select Count(*) from [RIF$];
    I is working fine and giving me total count of rows
    Now when i try the following version of count
    Select Count(Distinct Status) from [RIF$];
    where Status is a column name.
    It is giving the following error:
    java.sql.SQLException: [Microsoft][ODBC Excel Driver] Syntax error (missing operator) in query expression 'Count(Distinct Status)'.
    Pls help me
    its as always urgent

    This shouldn't execute in the quiery analyzer too, you don't have a java problem. I am not sure this is the optimal but you can try something like:
    select count(c_id) from (select distinct pcid c_id from contragent) cntr HTH
    Mike

  • Bug SQL Dev 1.5.4 Build MAIN-5940 Exporting count(*) query results

    when trying to export query results containing an analytic count the export dialog fails to open:
    When running the following query
    select count(*) over (partition by dummy order by rownum) xx
      from dual;right clicking on the results grid and selecting export->xls (or any other format) fails to open the export dialog box.
    changing the asterisk (*) to another value e.g. 1 or dummy corrects this issue for this particular query as does changing the analytic fuction to an aggregate function.
    Edited by: Sentinel on Aug 12, 2009 11:05 AM

    I get the same error when exporting the same statement - however, there is a known problem with 1.5.4 and failing on exports of certain statements (see 1.5.3 - Export results to XLS ORA-00936 which talks about there still being problems in 1.5.4 although the thread is titled 1.5.3).
    theFurryOne

  • Hash Count Query

    Hi Experts,
    We have business requirement in which we need to create Hash Count Queries in SAP Abap by joining different tables.
    Are these queries similar to SAP Queries which we create using transaction SQ03, SQ02/SQ01/SQ00?
    I think hash table/hash count/hash algorithm is more to the SQL (database) side rather than SAP, also I didnu2019t find any documentation for Hash Queries.
    So could you please help me with above issue.
    If they are different could you please explain the process to create them?
    Also what are the advantages of Hash Queries?
    Thanks.

    These are normal infoset queries which we create using transaction SQ03, SQ02/SQ01/SQ00.

  • Simple Count query and fetching data

    Hello,
    I have a table Emp :
    Name, age, sal, dateUpdated
    A 21 100 6/4/10
    B 21 101 6/4/10
    C 32 101 2/2/2
    D 20 100 3/3/3
    I am trying to count the number of people in the same age group AND fetch that number.
    For ex, I want to count the number of people that were updated today and then fetch their age.
    for ex : something like this : select count(dateUpdated) from Emp where date updated is today
    will only return 2 as result.
    how should my query be if I want to even fetch the individual age along with the count ?
    cheers,

    You can use {noformat}{noformat} tags to preserve your code format. That would help every one to read your post better.                                                                                                                                                                                                                                                           

Maybe you are looking for

  • Fake aiff and real aiff from wav ?

    1) When in garageband if I just batch convert an entire folder of wav to aiff with Apple loop utility even if all the wav loops have different tempo and key all will have one meta data or information which will not hold true for all loops.....so isnt

  • Unable to upgrade to iTunes 10.0.1

    Software Update tells me there's a new version of iTunes (iTunes 10.0.1) and tells me to upgrade. If I try to do so, I get this error : The update "iTunes" can't be installed. The file "AppleMobileDeviceSupport.pkg" couldn't be found on the server "s

  • Ipod touch won't connect to itunes?

    I have tryed two different wires and none of them work. The computer makes the connecting noise then nothing shows up. Why is this?

  • How can I get my iPod Classic to play playlists in the order I selected them to play ?

    I've had my new iPod Classic for a couple of months, and with the help of a couple of third-party books (e.g., the missing manual), I'm getting comfortable with its operation. But here's a baffling issue: It appears there's no way to select, say, thr

  • ITunes 10.1.0.56 update kills network interface - Windows 7

    Every time I attempt to install 10.1.0.56, it kills my wired network interface (desktop, so only interface) and hangs mid-installation. There are no errors presented. To regain network connectivity, I have to perform a System Restore. At this point,