Query using where clause
I am not able to run a SQL query using where clause.
the query is as follows:
I extract the text input by the user in a text field say 'a' and
store it in string 'y'.
String y= a.getText();
//running the query
Select A from B where B.x=y;
how do we run a where clause when y is a string variable instead of value?
Use the following :
String y = a.getText();
String query ="select A.CODE from Port A where A.NAME=" + "'" + y + "'" ;
ResultSet rs= stmt.executeQuery(query);
Similar Messages
-
Performance hit using "where" clause in the query
Hi All,
I am facing a huge performance hit in the java code when using "where" clause in queries. Following are the details:
1. SELECT * FROM Employee
2. SELECT * FROM Employee where employeeid in (26,200,330,571,618,945)
There is no difference in Query Execution Time for both queries.
Business Logic Time is huge in second case as compared to first one (ratio - 1:20).
Rows returned are more in first case as compared to second case.(ratio - 1:4)
Business Logic is same for both the cases where I iterate through the ResultSet, get the objects and set them in a data structure.
Does anybody know the reason of unexpected time difference for the business logic in the second case?Since you're mentioning clustering your index, I'll assume you are using Oracle. Knowing what database you are using makes it a lot easier to suggest things.
Since you are using Oracle, you can get the database to tell you what execution plan it is using for each of the 2 SQL statements, and figure out why they have similar times (if they do).
First, you need to be able to run SQL*Plus; that comes as part of a standard database installation and as part of the Oracle client installation - getting it set up and running is outside the scope of this forum.
Second, you may need your DBA to enable autotracing, if it's not already:
http://asktom.oracle.com/~tkyte/article1/autotrace.html
http://www.samoratech.com/tips/swenableautotrace.htm
Once it's all set up, you can log in to your database using sql*plus, issue "SET AUTOTRACE ON", issue queries and get execution plan information back.
For example:
SQL> set autotrace on
SQL> select count(*) from it.ticket where ticket_number between 10 and 20;
COUNT(*)
11
Execution Plan
Plan hash value: 2983758974
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 4 | | |
|* 2 | INDEX RANGE SCAN| TICKET_N10 | 12 | 48 | 1 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access("TICKET_NUMBER">=10 AND "TICKET_NUMBER"<=20)
Statistics
0 recursive calls
0 db block gets
1 consistent gets
0 physical reads
0 redo size
515 bytes sent via SQL*Net to client
469 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> This tells me that this query used an INDEX RANGE SCAN on index TICKET_N1; the query can't do much better than that logically... In fact, the statistic "1 consistent gets" tells me that Oracle had to examine only one data block to get the answer, also can't do better than that. the statistic, "0 physical reads" tells me that the 1 data block used was already cached in Oracle's memory.
the above is from Oracle 10g; autotrace is available back to at least 8i, but they've been adding information to the output with each release.
If you have questions about sql_plus, check the forums at asktom.oracle.com or http://forums.oracle.com/forums/category.jspa?categoryID=18
since sql*plus is not a JDBC thing...
Oh, and sql*plus can also give you easier access to timing information, with "set timing on". -
Can we use where clause in Update on Merge statement?
Hi All,
I tried to execute the following Merge Query:
When this query is executed without ‘Where clause’ in Update statement its working fine. When executed with ‘Where clause’ it throwing the following error:
ORA-00905: missing keyword.
Following is the sample query which I tried to execute:
MERGE INTO TABLE_NAME
USING (SELECT COLUMN FORM TABLES)
ON (CONDITION)
WHEN MATCHED THEN
UPDATE SET
COLUMN UPATES
WHERE CONDITION -- Can we use where clause here?
WHEN NOT MATCHED THEN
INSERT
INSERT VALUES;
Can some one help on this?
Thanks in advance.
DariusYes:
SQL> drop table emp1;
Table dropped.
SQL> create table emp1 as select * from emp where deptno = 30;
Table created.
SQL> update emp1 set sal = sal*2;
6 rows updated.
SQL> commit;
Commit complete.
SQL> select ename,sal from emp1;
ENAME SAL
ALLEN 3200
WARD 2500
MARTIN 2500
BLAKE 5700
TURNER 3000
JAMES 1900
6 rows selected.
SQL> MERGE INTO emp1
2 USING(select * from emp) emp
3 ON (emp1.empno = emp.empno)
4 WHEN MATCHED THEN
5 UPDATE SET sal = emp.sal WHERE ename = 'TURNER'
6 WHEN NOT MATCHED THEN
7 INSERT(ename,sal) VALUES(emp.ename,emp.sal);
9 rows merged.
SQL> select ename,sal from emp1;
ENAME SAL
ALLEN 3200
WARD 2500
MARTIN 2500
BLAKE 5700
TURNER 1500
JAMES 1900
SMITH 800
JONES 2975
CLARK 2450
SCOTT 3000
KING 5000
ENAME SAL
ADAMS 1100
FORD 3000
MILLER 1300
14 rows selected.
SQL> SY. -
How to Get the required List Item values by using Where Clause
I have two tables named "TAX_RULES","BILL"
1. " Tax_Rules" (Sub_Head_Code,Tax_ID,Tax_Percentage)
{ While "Sub_Head_Code" field is unique,
Tax_ID describes that there are two kinds of Tax_IDs based on Tax_percentage which is 6% for some Sub_Head_Codes and 3.5% for the remainng Sub_Head_Codes. I have given Tax_ID 1 for 6% and Tax_ID 2 for Sub_Head_codes having 3.5%. }
2. "BILL" (Bill_ID,Sub_Head_Code,Tax_ID,Bill_Amount)
{While "Bill_ID" describes unique Bill Identification Number,
Sub_Head_Code and Tax_ID are behaving like composite foreign key from Tax_Rules table}
I have design form for both tables.
In BILL form i am trying to get values of Tax_ID from Tax_Rules table at run-time by using WHEN-NEW-FORM-INSTANCE.
For this whenever i select a Sub_Head_Code in Bill Form, all the Tax_IDs that i have recorded against each Sub_Head_Code which is either 1 or 2 against 6% and 3.5% respectively.
But I want that whenever i select a "Sub_Head_Code", Only that Tax_ID which is associated with that Sub_Head_Code ahould come in the LIST ITEM.
I have used WHERE clause but that is useless.
Following is the Code that i have applied in the WHE-NEW-ITEM-INSTANCE Trigger at FORM LEVEL
<CODE>
DECLARE
rg_n1 VARCHAR2(40) :='TAX_ID';
rg_idn1 RecordGroup;
gc_idn1 GroupColumn;
errcode NUMBER;
BEGIN
CLEAR_LIST('BILL.TAX_ID');
rg_idn1 := Find_Group(rg_n1);
IF Id_Null(rg_idn1) then
rg_idn1 := Create_Group(rg_n1);
gc_idn1 := Add_Group_Column(rg_idn1,'EXPLAIN',CHAR_COLUMN,60);
gc_idn1 := Add_Group_Column(rg_idn1,'TAX_ID',CHAR_COLUMN,3);
END IF;
errcode := Populate_Group_With_Query(rg_idn1,'select TAX_ID,TAX_ID from TAX_RULES');
POPULATE_LIST('BILL.TAX_ID',RG_IDn1);
END;
</CODE>In the Tax Rules table, you state:
"Sub_Head_Code" field is unique
In the Bill table, you state:
Sub_Head_Code and Tax_ID are behaving like composite foreign key from Tax_Rules table
If the Sub_Head_Code is unique, then it's acting as a primary key, so Sub_Head_Code and Tax_ID in the Bill table are not behaving like a composite foreign key. The Sub_Head_Code is a foreign key, and the Tax_ID is irrelevant as far as keys are concerned.
It is not clear what it is you want to do.
If you want to display the Tax_IDs from the Bill table when you select a Sub_Head_Code from the Tax_Rules table, then change your query to:
select TAX_ID, TAX_ID from BILL where Sub_Head_Code = :Tax_Rules.Sub_Head_Code
If you want to display the Tax_IDs from the Tax_Rules table when you select a Sub_Head_Code from the Bill table, then change your query to:
select TAX_ID, TAX_ID from TAX_RULES where Sub_Head_Code = :Bill.Sub_Head_Code
If this is not what you want, then clarify what it is you want to do. Don't say:
"I want that whenever i select a "Sub_Head_Code", Only that Tax_ID which is associated with that Sub_Head_Code"
because it is not clear what tables you are referring to. -
How to use WHERE Clause in BPEL
Hi ,
i am trying to use where clause in DB adapter but its not working.
my query is "select empname from employee where empid = &a"
can any body tell me what is the reason its npt working?
and how can i retrive data form DB?Hi,
You are thinking perfectly fine. The solution what I suggested was
select ename from emp where empno=?
And the solution provided by you was
SELECT first_name, last_name FROM per_all_people_f WHERE person_id = #p_person_id
There is no problem with the above mentioned select statements.
I just want to say that if you see closely, internal logic changes #parameter name to ?. So both the logics are right.
Cheers,
Abhi... -
Hierarchical query with where clause
Hi,
How can I query hierarchically a query with WHERE clause? I have a table with three fields session_id,id and root_id.
When I try with the following query,
select id, level from relation
where session_id = 79977
connect by prior id = root_id start with id = 5042;
It gets duplicate values.
I want the query to show in the hierarchical manner with a filter condition using WHERE clause. Please help me how can I achieve this. If you know any link that describes more about this, please send it.
Thanks in Advance.
Regards,
-ParmyHi Sridhar Murthy an others,
Thanks a lot for your/the answer. It's working for me. It saved a lot of other work around without the proper knowledge of hierarchical query. Please send me any link that describes these issues in detail and also I hope as I have mentioned in the other message, same cannot be achieved on views or ( on two different tables ???)
Any way thanks for your reply,
It's working for me.
With happiness,
-Parmy -
In Query Bean (Auto Customization Criteria) , OA FWK generate
where clause automatically .
I want to disable this criteria in some cases. Is there any way exists?
(Disable means remove some condition )
I want to see runtime query with where clause(In case of Query Bean) . Is there is any way to find out it?
Thanks, AvaneeshYou have couple of options to do this,
1. You can use getCriteria API on the queryBean which will stop the query execution by framework and it's the responsibility of the developer to use the dictionary returned by getCriteria and bind the complete where clause yourself.
2. You can use getNonViewAttrCriteria API on queryBean. This provides a dictionary of criteria on the column which are not mapped to a viewAttribute, for example if you want a poplist in the search region which has values like "Last one week", "Last 2 weeks", "Last 15 days" and so on and want to bind the criteria your self by interpreting the value selected.
In this case, framework still binds the where clause for the other criteria's which are mapped to a vo based column in the table and expects the developer to bind the criteria for these non viewattribute mappings in the controller.
I think your requirement fits in the second approach. -
Populate list by executing query with where clause
Hi,
I m populating my list from database table using following command.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("learning", new HashMap());
EntityManager em = emf.createEntityManager();
String query = "select d from Dept d";
List list = em.createQuery(query).getResultList();
My list is getting populated with all of rows in my database table(dept). The problem is, I cant use where clause in my query string.
String query = "select d from Dept d where d.dept_name='ANYNAME'";
Moreover I cant select even indivisual columns by using following line
String query = "select d.dept_code,d.dept_name from Dept d";
It looks like that my query is getting all rows from table as Object.
What to do to
1. put some where clause
2. get some particular colums instead of all columns
3. apply some table joins in query
plz help.Hi Alex,
I misunderstood your question. You had mentioned "My list is getting populated with all of rows in my database table(dept). The problem is, I cant use where clause in my query string. "
I thought you might not have permissions to modify the code in the class, and that's why I suggested that you could write new classes.
1) write new classes,? means if I need just two columns from a table >(instead of all columns), do I need to write new class with just two >columns (getters and setters)?You can write a SQL statement anyway you want , there are no restrictions in JDBC , so if you need just 2 columns you can write a custom query for that.
"select column1 as a, column2 as be from table_name where column3 = xyz" etc.
2) Is not anyway to write SQL statement like
select dept_code,dept_name from dept where dept_name='anything'The above SQL is correct, it should work.
3) Stored procedures? can I get some example(code) to get an idea >how to implement complex queries with where clause and joinings >using stored procedures?Stored procedures are specific to the database you are using, Oracle has a particular syntax for store procdures.
You can call stored procedures from JDBC also, search on Google for
"Calling stored procedures from JDBC"
4) I m using hibernate. can I get any example of using hibernate own >query?Tutorial on Hibernate Query Language:
http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html -
The issue with using the multiple columns sub-query in WHERE clause
Hi All,
my database version is 10.2.
the problem i am trying to deal with is that when I use multiple column sub-query in the WHERE clause of the SELECT statement, the actual row number returned from the sub-query is different from the whole statement.
And what I found is that, whenever there is NULL in any of those columns returned from the SUB-QUERY, the outer query will just return NULL for that whole row.
Here is an example:
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t;
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT NULL 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
select * from EMP where (EMPNO, ENAME, job, MGR, HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 If I specifically deal with the NULL situations for the columns which might return NULL, I can get the result right.
select * from EMP where (EMPNO, ENAME, job, NVL(MGR,-1), HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from EMP
intersect
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT null 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 the problem is that, I feel this is a very lame way of handling it.
So, I wonder or am asking if there is any better or standard way to do it?
any help would be highly appreciated.
ThanksHi,
As you discovered, INTERSECT treats NULL as a value, but IN does not.
What you did with NVL is one way to handle the situation. If there was a chance that any of the columns could be NULL, then you might prefer something like this:
select *
from EMP
where ( EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
) in (
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from emp
intersect
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from t
);This assumes that you can identify some string (I used '~') that never occurs in the strings in these tables.
This is implicitly converting the NUMBERs. That's usually not a good thing to do. but explicitly converting them would make this even more tedious.
You should explicitly convert any DATEs to strings, however. Depending on your default format, and your data, you might get away with implicit conversions even for DATEs, but don't bet on it.
If you had to do this often, you might consider writing a user-defined function:
delimited_string (empno, ename, job, mgr, hiredate, sal, deptno) would return a string like
'7839~KING~PRESIDENT~~17-NOV-1981~5000~10'
This will make the coding easier, but beware: it will make the execution slower. -
Query Builder - Where Clause - Could not format error using date comparison
We've come across a bug in the Query Builder, under the Create Where Clause tab, if you select a column of Date type plus one of the comparison operators =, !=, <, >, <=, >=, BETWEEN or NOT BETWEEN it displays an error in the Logging Page:
Level: Severe
Source: o.d.r.queryBuilder.SQLGenerator
Message: Could not format :2010-09-02 16:20:31.0
Then under the Show SQL tab it doesn't display the date(s) you selected, e.g.
WHERE LAST_UPDATE BETWEEN AND
Also the View Results tab does not display any results.
You can still press Apply to add the SQL as is to the editor window and from there you have to manually code in the date parameters.
We're using the latest version of SQL Developer 2.1.1.64.45 Windows 32bit version with JDKHi Gordon,
When I add the following lines:
declare @refdt1 date
set @refdt =
/*select 1 from jdt1 t0 where t0.RefDate*/ '[%1]'
declare @refdt2 date
set @refdt =
/*select 1 from jdt2 t0 where t0.RefDate*/ '[%2]'
WHERE T0.RefDate >= @refdt1 and T0.RefDate <= @refdt2
... the error message is now:
Must declare the scalar variable @refdt1
Note: Before adding these lines, the query works perfectly, and returns totals from the whole database (with dynamically generated column headings!)
Thanks
Leon Lai
AMENDED QUERY:
declare @refdt1 date
set @refdt1 =
/*select 1 from jdt1 t0 where t0.RefDate*/ '[%1]'
declare @refdt2 date
set @refdt2 =
/*select 1 from jdt1 t0 where t0.RefDate*/ '[%2]'
--------- I inserted the 6 lines above ---------------------
DECLARE @listCol VARCHAR(2000)
DECLARE @query VARCHAR(4000)
SELECT @listCol =
STUFF
( SELECT DISTINCT '],[' + CAST(month(T0.RefDate) AS varchar)
FROM JDT1 T0
FOR XML PATH('')
), 1, 2, ' ') + ']'
SET @query =
'SELECT * FROM
(SELECT Account, month (T0.RefDate) Month , Debit
FROM JDT1 T0
------------------- I add the WHERE clause below --------------------
WHERE T0.RefDate >= @refdt1 and T0.RefDate <= @refdt2
GROUP BY Account, RefDate, Debit
) S
PIVOT
Sum(Debit)
FOR Month IN ('+@listCol+')
) AS pvt'
EXECUTE (@query)
Edited by: LEONLAI on Oct 21, 2011 2:36 PM -
Can Portal Report from SQL Query use where column IN (:bind_variable)
I would like to create a portal report from sql query with IN (:bind_variable) in the where clause. The idea is that the user would enter comma-separated or comma-quote-separated values for the bind_variable. I have tried this several ways but nothing seems to work. Can this be done?
TrentonHi,
Which version of portal are you using. This is a bug. It has been fixed in 30984.
Thanks,
Sharmila -
Dynamic query in where clause while looping in an internal table.
Hi,
Had a small question : How can i make a dynamic query for the WHERE clause while looping at an internal table.
i want to implement a dynamic where clause query for the below example.
it_cfx_col is an internal table and wa_cfx_col is a work area for it_cfx_col
DATA :
i_cfx_col TYPE TABLE OF cfx_col,
wa_cfx_col LIKE LINE OF i_cfx_col.
DATA : count TYPE i VALUE 0.
DATA : l_where_clause TYPE string,
l_where_clause2 TYPE string,
l_name type string.
l_name = 'NANDANOM'.
l_scenario = 'collaboration'.
LOOP AT it_cfx_col INTO wa_cfx_col
WHERE CREATED_BY = l_name
AND SCENARIO = l_scenario.
count = count + 1.
some business logic implemented using the work area wa_cfx_col
endloop.
Now i want to write a dynamic query for the where clause.
DATA : count TYPE i VALUE 0.
DATA : l_where_clause TYPE string,
l_where_clause2 TYPE string,
l_name type string.
l_name = 'NANDANOM'.
l_scenario = 'collaboration'.
l_where_clause = 'CREATED_BY = l_name'.
l_where_clause2 = 'AND SCENARIO = l_scenario'.
if l_scenario is not initial.
CONCATENATE l_where_clause l_where_clause2
INTO l_where_clause SEPARATED BY space.
endif.
LOOP AT i_cfx_col INTO wa_cfx_col
WHERE (l_where_clause).
count = count + 1.
some business logic implemented using the work area wa_cfx_col
endloop.
when i compile this i get an error message as { Statement concluding with "...(l_where_clause)" ended unexpectedly}
Even i changed the initilization of the variable l_where_clause2 to [ l_where_clause2 = 'AND SCENARIO = l_scenario.'. ]
added the end of line demarkation ".", but still i got the same error message.
Is it a limtation in ABAP that i cannot write a dynamic query for the where clause while looping at an internal table?
Regards,
omHi savita,
there in no such 1 limitaion in abap for dynamic query .. i think the error meassge is only beacuse of your synatx delcartaion.
>> LOOP AT i_cfx_col INTO wa_cfx_col
WHERE (l_where_clause).
count = count + 1.
some business logic implemented using the work area wa_cfx_col
endloop.
afted delclarataion also , in the where statement you should specify both the field name and value bname
LOOP AT i_cfx_col INTO wa_cfx_col
WHERE l_where_clause = 'CREATED_BY = l_name' .
count = count + 1.
hope it helps.
regads
priya. -
Issue in select query with where clause
Hi guys,
I'm facing an issue while using select query with the where clause. When I'm selecting all the data from the table it returns the correct result. But when I'm using the where clause to get the specific rows from the table it returns no rows. But the data I'm trying to fetch using the where condition exists in the table.
Here is my query which causing the issue,
select * from mytable where myfield = 'myvalue'
But if I use the following query it returns the result correctly.
select * from mytable
Also the myfield value 'myvalue' exists in the table.
I have tried by running this query in both SQL Developer and SQL Plus. I have tried this query in mssql as well. It works perfectly and returns correct result sets for both the queries I have mentioned above. I'm unable to predict the issue as I'm new to ORACLE. Please help.
Thanks,
Ram.Hi Ram,
I experienced an issue similar to this with a varchar2 field. Some of our records had a hidden newline character at the end of them, which was making queries like the one below fail:
select * from employees
where email = '[email protected]'The best way I found to detect this was to use
select 'XX'||email||'XX' from employeesTo make sure that there were no newlines. But that is just a guess. If you could provide some example table data and the outputs of your selects, it would be helpful.
Jeff -
Does derived table use where clause fillters inside
dear all.
Table "A" has two cols aid and col1 , aid is pk.
Table "B" has two cols bid , aid , and colb .
followin query can be formulated in other ways ,but i wanted to ask something about query processing.
select aid,aa.ca,aa.cb
from A
left join
(select aid, b.colb ca ,b1.colb cb
from A
left join B on a.aid=b.aid and b.colb='r'
left join B b1 on a.aid=b1.aid and b1.colb='e'
) aa
where a.aid (1,2)
i wanted to know if i put the where clause "where a.aid(1,2) in side derived table aa
then will it change its plan in any case or it is sufficant to put where clasue in last
or i should put the where clause in side the derive table "aa" also.
yours sincerleyPlease post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior ON SQL forums. Is your boss this rude to you??
CREATE TABLE Alphas
(alpha_id INTEGER NOT NULL PRIMARY KEY,
col1 INTEGER NOT NULL);
Thanks to your rude, poor narrative I have to guess about the second table:
CREATE TABLE Betas
(beta_id INTEGER NOT NULL PRIMARY KEY,
alpha_id INTEGER NOT NULL
REFERENCES Alphas(alpha_id),
colb CHAR(1) NOT NULL);
Your query is awful. OUTER JOINs are rare in a properly designed schemA. The DRI should guarantee inner joins! We seldom need to re-name columns.
>> I wanted to know if I put the where clause WHERE A.alpha_id(1,2) in side derived table aa <<
That is not a syntactically invalid search condition; did you mean
“WHERE A.alpha_id IN (1,2)” ??
Why did you de-normalize your results? My guess is that you can get the same data with this:
SELECT alpha_id, colb
FROM Betas AS B
WHERE colb IN ('r', 'e')
AND alpha_id IN (1, 2);
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
I have the following code which keeps throwing an error:
select distinct game_code, count(player_id)
from customer_data.cd_nhl_gm_tender_stats t
where
(select distinct player_id
from customer_data.cd_nhl_gm_tender_stats
where season_id = 200818)
group by game_code, player_id
having count(player_id) > 1Error states that there's a missing expression right before the group by, any thoughts?
The subquery is basically used to find a unique player_id value, (so there's no duplicates rows in the result set.)
Sample data;
customer_data.cd_nhl_gm_tender_stats t
Game_code player_id
88484 545454
88484 545454
88484 545454There's obviously other columns in the table above, but ultimatly there is mutliple rows for each game.
I'm just trying to return each game_code where more then one player_id is found.Michaels2 i'm not sure your code is doing quite what i need
select *
from (select t.*,
count (distinct player_id) over (partition by game_code) cnt
from cd_nhl_gm_tender_stats t where season_id = 200818)
where cnt > 1The subquery in the from clause is just selecting every game_code where season_id 200818 and then you add a where clause where cnt > 1 in the main query(whcih doesn't accomplish what I really wanted). What I need is the count of distinct player_id where season_id = 200818 AND cnt of (distinct player_id) over (partition by game_code) cnt > 2**. The issue i'm having is, that i'm not quite sure how to format the code to get these results.
To clairfy what i need one more time ( i didn't explain things well in my original post)...
I want each game_code where a player_id count is greather then two. And as stated earlier the gm_tender_stats table has numerous entries with the same player_id for the same_game code, which is why this makes writing this query a little more difficult.
Edited by: user652714 on May 11, 2009 7:52 AM
Maybe you are looking for
-
Cannot get windows 7 to install on new imac
I have a new "Thunderbolt" imac. Using the boot camp assistant disk that came with it (or the downloaded one), I cannot get windows 7 successfully installed. Mostly won't start up. Long hang-ups, mouse won't work. Have used Boot Camp on older mac Pro
-
I have resently purchased a new N73 from T-Mobile (through contract ) . Unfortunately after 5 weeks the phone is now frozen . All I get when I switch the phone on is the back-light and the blue nokia symbol . Also , clock and date is working and show
-
HD / HDR-CX7 Footage Import (.MTS file format)
Anybody have a way to use .MTS footage in FCP? Captured with a new Sony HD High Definition Handycam® Camcorder HDR-CX7. Looks as if it's Windows PC friendly, but can't find anything anywhere on how to get it to work with a Mac/FCP/QT, etc.. http://ww
-
Default values in selection screen
hI, EXPERTS Requirement is : 1) in selection screen input = "werERT" , output = "werERT". normally, it will give "WERERT". 2) in selection screen input = "werert" , output = "werert". normally, it will give "WERERT". 1) in selection screen input
-
Possible solution to ATV2 slow library streaming. iTunes movie buffering issues
All, I'm not a common user of forums so forgive ettiqute/nomenclature errors. We were recently having issues with non-stop buffering when trying to stream movies from our iTunes library. I've seem numerous topics and posts on this same issue. Si