How can I write this query In parameterize form so That I can prevent sql Injection
String strQry = "INSERT INTO tblVoucherType (VhrTypeCode,moduleCode,transCCode,"
+ "voucherType,OrderNumber,active,AccountId) "
+ " values('" + txtVhrCode.Text + "','" + ddlModule.SelectedValue.ToString() + "',"
+ "'" + ddlTrans.SelectedValue.ToString() + "','" + txtVhrName.Text + "','" + btnRadio.SelectedValue + "'"
+ ", '" + status.Checked + "', '" + txtAccount.Text + "')";
Basically it will look like:
String strQry = "INSERT INTO tblVoucherType (VhrTypeCode,moduleCode,transCCode, ...)"
+ " values(@VhrCode, @moduleCode, @transCCode, ....)";
sqlCommand.Parameters.AddWithValue("@VhrCode", txtVhrCode.Text );
sqlCommand.Parameters.AddWithValue("@moduleCode", ddlModule.SelectedValue.ToString() );
sqlCommand.Parameters.AddWithValue("@transCCode", ddlTrans.SelectedValue.ToString() );
.. and so on
sqlCommand.ExecuteNonQuery();
Olaf Helper
[ Blog] [ Xing] [ MVP]
Similar Messages
-
[USERNAME]
[GROUP]
User1
New Member
User2
New Member
User1
All Members
User1
Gold Member
How would I write a query so that I could select * from the above table where a user has more than 1 group (user1) and if one of the groups = 'All members' then ignore that row and just return the all the other rows for the user that has > 1 group.
Thanks in advance. I can write the query for counting if a user has more than 1 group, but I stuck on the rest.
Thanks againTry this
declare @tab table(USERNAME varchar(10), Usergroup varchar(15));
insert into @tab values
('User1','New Member'),
('User2','New Member'),
('User1','All Members'),
('User1','Gold Member');
select * from
(Select *,ROW_NUMBER() OVer(partition by username Order by case when Usergroup ='All members' then 0 else 1 end desc) RN From @tab ) t
where t.usergroup= case when t.rn >1 and t.Usergroup='All members' then 'Do not return' else t.Usergroup end
Satheesh
My Blog |
How to ask questions in technical forum -
How would you write this query efficiently?
I am having a problem doing a particular join in the most optimal fashion, and this kind of join I do often, so I'd like to learn the best way to do it so I can re-use the same logic every time and be comfortable that it is the most efficient means.
I am using Oracle 11g, and the situation goes like this -- well actually there is a variation of this particular situation, I'll explain the easier one first):
Table PARENT (
PARENT_ID NUMBER [PK],
(OTHER IRRELEVANT DATA COLUMNS)
Table CHILD (
CHILD_ID NUMBER [PK],
PARENT_ID NUMBER,
CHILD_VALUE VARCHAR2(50),
START_DATE DATE NULL(NO),
END_DATE DATE NULL(YES)
I want to query from PARENT, joining to CHILD when possible (e.g. an outer join) but I only want the MOST RECENT record. MOST RECENT means that it is the record with the latest START_DATE, which is a column that cannot be NULL.
I usually try a query where I make a inner SELECT in the FROM clauses, then do an outer join against it. But this seems ot be a terrible practice that slows the query down drastically, because looks like the DB analyzes that sub query for the entire DB before joining on my particular result set. Like the main query might filter it down very quickly to a small set of records, but that sub query still analyzes the entire table then joins to my subset. So something like this:
SELECT
p.(WHATEVER COLUMNS FROM PARENT),
x.CHILD_VALUE,
x.START_DATE,
x.END_DATE
FROM PARENT p,
(SELECT c1.PARENT_ID, c1.CHILD_VALUE, c1.START_DATE, c1.END_DATE
FROM CHILD c1
WHERE c1.START_DATE = (SELECT MAX(START_DATE) FROM CHILD)
) x
WHERE
p.(FILTERING ON PARENT)
AND x.PARENT_ID(+) = p.PARENT_ID
Then a variation of this is that the CHILD table has one other column:
CHILD_TYPE (NUMBER)
And I need to join on the CHILD getting the MOST RECENT record for each CHILD_TYPE... for this I do two inner queries usually, and again I know its not the right way to do it.with most_recent_child as (
select *
from (
select c.*,
row_number() over(partition by parent_id order by start_date desc) rn
from child c
where rn = 1
select *
from parent p
left join
most_recent_child c
on p.parent_id = c.parent_id
/SY. -
How to write this query to filter combination of few values
Hi,
I have a table CHIMM which is a transaction table and contains information of the vaccines given to a child.
columns are: child_id, vacc_id, vacc_given_dt. I have to query for remaining vaccines.
HEXA is a vaccine_id which is composite vaccine of DPT1,POL1,HBV1 & HIB1 (vaccine ids).
I want to write to query if any of DPT1,POL1,HBV1 & HIB1 given then HEXA should not be displayed in the result.
OR
if HEXA is given then of course any of DPT1,POL1,HBV1 & HIB1 should not be displayed in the result.
How to write this query?
RegardsHi,
I'm still not sure what the output you want from that sample data is. Do you just want the child_ids, like this
CHILD_ID
3
4? If so, here's one way to get them:
WITH all_vacc_ids AS
SELECT c.child_id
, c.vacc_id AS child_vacc_id
, v.vacc_id
, COUNT ( CASE
WHEN c.vacc_id = 'HEXA'
THEN 1
END
) OVER ( PARTITION BY c.child_id
) AS hexa_itself
FROM vacc v
LEFT OUTER JOIN chimm c PARTITION BY (c.child_id)
ON c.vacc_id = v.vacc_id
WHERE v.vacc_desc = 'HEXA' -- See note below
SELECT child_id
FROM all_vacc_ids
WHERE child_vacc_id IS NULL
AND vacc_id != 'HEXA'
AND hexa_itself = 0
GROUP BY child_id
rha2 wrote:there are alot of vaccines, i just put 3 for example. this query gives error: invalid relational operatorAre you saying that the vacc table contains other rows, but those other rows are not needed for this problem? It would be good if you included an example in the sample data. The query above considers only the rows in vacc where vacc_desc='HEXA'. You can have other rows in the vacc table, but they won't affect the output of this query. The query above makes no assumptions about the number of rows that have vacc_desc='HEXA'; it will report all child_ids who are missing any of them, regardless of the number (assuming the child does not have the 'HEXA' vacc_id itself, like child_id=1).
You still haven't said which version of Oracle you're using. The query above will work in Oracle 10 (and higher). -
How to write this query ?
how to write this query ?
list the emp name who is working for the highest avg sal department.
I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?
the row_number version is like this
select emp.* from emp ,
select deptno, row_number() over (order by avg(sal) desc) r from emp
group by deptno
)e
where e.r = 1
and emp.deptno = e.deptnoHi,
806540 wrote:
how to write this query ?
list the emp name who is working for the highest avg sal department.
I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?ROW_NUMBER is just plain SQL, and has been since Oracle 8.1.
ROW_NUMBER (or its close relative, RANK) is the simplest and most efficient way to solve this problem. Why not do this the right way?
the row_number version is like this
select emp.* from emp ,
select deptno, row_number() over (order by avg(sal) desc) r from emp
group by deptno
)e
where e.r = 1
and emp.deptno = e.deptno
If there happens to be a tie (that is, two or more departments have the same average sal, and it is the highest), then the query above will only arbitrarily treat one of them (no telling which one) as the highest. Change ROW_NUMBER to RANK to get all departments with a claim to having the highest average sal.
You could use the ROWNUM pseudo-column instead of ROW_NUMBER, if all you want to do is avoid ROW_NUMBER.
Without using ROW_NUMBER or RANK, there are lots of ways involving other analytic functions, such as AVG and MAX.
If you really, really don't want to use analytic functions at all, you can do this:
SELECT *
FROM scott.emp
WHERE deptno IN (
SELECT deptno
FROM scott.emp
GROUP BY deptno
HAVING AVG (sal) = (
SELECT MAX (AVG (sal))
FROM scott.emp
GROUP BY deptno
; -
How to re-write this big SELECT Query with INNER JOINs?
Hi Experts
I have a performance killer SELECT query with an inner join of 3 tables u2013 VBAP, VBAK and VBEP together, which populates records to an internal table INT_COLL_ORD. Based on these records selected, in another SELECT query, records are fetched from VBUK table to the internal table INT_VBUK.
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV D~AUART E~ETTYP E~EDATU
INTO TABLE INT_TAB_RES
FROM VBAP AS A INNER JOIN VBAK AS D
ON D~VBELN EQ A~VBELN AND D~MANDT EQ A~MANDT
INNER JOIN VBEP AS E
ON E~VBELN EQ A~VBELN AND E~POSNR EQ A~POSNR AND E~MANDT EQ A~MANDT
WHERE A~VBELN IN s_VBELN AND
D~auart in s_auart AND
D~vkorg in s_vkorg AND
D~vbtyp eq 'C' AND
( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
A~ABGRU EQ SPACE AND
A~MTVFP IN R_MTVFP AND
A~PRCTR IN R_PRCT AND
E~ETENR EQ '1'.
SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
DELETE ADJACENT DUPLICATES FROM INT_TAB_RES COMPARING VBELN POSNR.
CHECK NOT INT_TAB_RES [] IS INITIAL.
SELECT VBELN UVALL CMGST INTO TABLE INT_VBUK
FROM VBUK FOR ALL ENTRIES IN INT_TAB_RES
WHERE VBELN = INT_TAB_RES-VBELN AND UVALL NE 'A'.
Now, the requirement is:
I want to split this query. Like, first join VBAK and VBUK first. With this selection, go to the inner join of VBAP and VBEP (on key VBELN) to get the results. How can I re-write this Query?
Please help.
Thx n RgdsHi Nagraj
As of your suggestion, I have re-written the query as below:
* Declarations
TYPES: BEGIN OF TYP_COLL_ORD,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBUP-POSNR,
MATNR LIKE VBAP-MATNR,
KWMENG LIKE VBAP-KWMENG,
KBMENG LIKE VBAP-KBMENG,
ERDAT LIKE VBAK-ERDAT,
ERZET LIKE VBAK-ERZET,
PSTYV LIKE VBAP-PSTYV,
AUART LIKE VBAK-AUART, u201Calready exists in type
ETTYP LIKE VBEP-ETTYP,
EDATU LIKE VBEP-EDATU.
TYPES: END OF TYP_COLL_ORD.
DATA: INT_COLL_ORD TYPE TABLE OF TYP_COLL_ORD WITH HEADER LINE.
TYPES: BEGIN OF TYP_VBUK,
AUART LIKE VBAK-AUART, u201Chave added this field
VBELN LIKE VBUK-VBELN,
UVALL LIKE VBUK-UVALL,
CMGST LIKE VBUK-CMGST.
TYPES: END OF TYP_VBUK.
DATA: INT_VBUK TYPE TABLE OF TYP_VBUK WITH HEADER LINE.
*QUERY#1 u2013 for VBAK & VBUK Join
SELECT A~AUART B~VBELN B~UVALL B~CMGST
INTO TABLE INT_VBUK
FROM VBAK AS A INNER JOIN VBUK AS B
ON A~VBELN EQ B~VBELN
WHERE A~VBELN IN s_VBELN AND
A~auart in s_auart AND
A~vkorg in s_vkorg AND
A~vbtyp eq 'C' AND
B~UVALL NE 'A'.
IF NOT INT_VBUK[] IS INITIAL.
SORT INT_VBUK BY VBELN.
DELETE ADJACENT DUPLICATES FROM INT_VBUK COMPARING VBELN.
*QUERY#2 u2013 for VBAP & VBEP Join
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV B~ETTYP B~EDATU
INTO TABLE INT_COLL_ORD
FROM VBAP AS A INNER JOIN VBEP AS B
ON B~VBELN EQ A~VBELN AND B~POSNR EQ A~POSNR AND B~MANDT EQ A~MANDT
FOR ALL ENTRIES IN INT_VBUK
WHERE A~VBELN = INT_VBUK-VBELN AND
( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
A~ABGRU EQ SPACE AND
A~MTVFP IN R_MTVFP AND
A~PRCTR IN R_PRCT AND
B~ETENR EQ '1'.
ENDIF.
SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
DELETE ADJACENT DUPLICATES FROM INT_COLL_ORD
COMPARING VBELN POSNR.
CHECK NOT INT_COLL_ORD[] IS INITIAL.
LOOP AT INT_COLL_ORD.
CLEAR: L_MTART,L_ATPPR,L_ETTYP.
IF L_PREVIOUS_ETTYP NE INT_COLL_ORD-ETTYP OR
L_PREVIOUS_AUART NE INT_COLL_ORD-AUART.
READ TABLE INT_OVRCTL WITH KEY AUART = INT_COLL_ORD-AUART ETTYP = INT_COLL_ORD-ETTYP.
CHECK SY-SUBRC NE 0.
Now, the issue is:
Please note that declaration for INT_COLL_ORD has a field AUART, which is used in further parts of program (see the statement just above)
But, since neither VBAP nor VBEP contains AUART field, it cannot be fetched through the QUERY#2. So this value is not populated into INT_COLL_ORD through SELECT Query.
Since this field is used in later part of program & that the internal table has no value for this field, it dumps!!
How to include this value into the INT_COLL_ORD?
Plz suggest.... -
How do I write this SQL command in Oracle
Hi all
I wriote this SQ L statement in Ms SQL Server. How do I write this sql command in Oracle?
ALTER VIEW dbo.ConsumptionAS SELECT TOP 100 PERCENT ID,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200711' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Nov2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200712' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Dec2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200801' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jan2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200802' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Feb2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200803' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Mar2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200804' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Apr2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200805' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS May2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200806' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jun2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200807' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jul2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200808' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS Aug2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200809' AND NbrDaysUsed != 0 THEN (QtyUsed NbrDaysUsed) * 748.05 ELSE 0 END)) AS Sep2008
FROM dbo.MasterConsumption WHERE YEAR_MONTH >= '200710' AND YEAR_MONTH <= '200809' GROUP BY ID ORDER BY ID
I am very interested in this part:
SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007
thanks
Edited by: user631364 on Oct 27, 2008 8:25 AM
Edited by: user631364 on Oct 27, 2008 8:26 AM
Edited by: user631364 on Oct 27, 2008 8:27 AMThank you!!
Now let me aslk the second part of my question.
This sql command:
ALTER VIEW dbo.ConsumptionAS SELECT TOP 100 PERCENT ID,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200711' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Nov2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200712' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Dec2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200801' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jan2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200802' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Feb2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200803' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Mar2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200804' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Apr2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200805' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS May2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200806' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jun2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200807' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jul2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200808' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS Aug2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200809' AND NbrDaysUsed != 0 THEN (QtyUsed NbrDaysUsed) * 748.05 ELSE 0 END)) AS Sep2008
FROM dbo.MasterConsumption WHERE YEAR_MONTH >= '200710' AND YEAR_MONTH <= '200809' GROUP BY ID ORDER BY ID
was created with this query in SQL Server and then I saved it in a store procedure, that I scheduled to run montlhy
SET ANSI_NULLS ON
DECLARE @SQLString NVARCHAR(4000)
/* Build the SQL string once.*/
SET @SQLString = 'ALTER VIEW dbo.Consumption AS SELECT TOP 100 PERCENT ID, CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = ' +
"'" + dbo.CONLastMonth_fn(getdate(), month(getdate()) - 12) +
"'" +
' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS ' +
dbo.CONMonthInEnglish(getdate(), month(getdate()) - 12) +
… (GOES FROM current month -12 to current month -1)
, CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = ' +
"'" + dbo.CONLastMonth_fn(getdate(), month(getdate()) - 1) +"'" +
' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS ' +
dbo.CONMonthInEnglish(getdate(), month(getdate()) - 1) +
' FROM dbo.MasterConsumption WHERE YEAR_MONTH >= ' +
"'" + dbo.CONLastMonth_fn (getdate(), month(getdate())-12 ) +"'" +
' AND YEAR_MONTH <= ' +
"'" + dbo.CONLastMonth_fn (getdate(), month(getdate())-1 ) +"'" +
' GROUP BY ID ORDER BY ID '
EXEC sp_executesql @SQLString
Is that something that can be done in Oracle in the same way?
Do you use another approach?
please advice
Edited by: user631364 on Oct 27, 2008 10:19 AM
Edited by: user631364 on Oct 27, 2008 10:21 AM
Edited by: user631364 on Oct 27, 2008 10:21 AM
Edited by: user631364 on Oct 27, 2008 10:22 AM
Edited by: user631364 on Oct 27, 2008 10:23 AM
Edited by: user631364 on Oct 27, 2008 10:23 AM
Edited by: user631364 on Oct 27, 2008 10:24 AM -
How do I write this as a loop?
How do I write this code to function as follows.
When btn1 is clicked, SenderLoad should be "clay" AND when btn2 is clicked, SenderLoad should be "iron", etc...
var mgB:Array = [btn1, btn2, btn3, btn4];
var mgBW:Array = ["clay", "iron", "cotton", "leather"];
var SenderLoad:LoadVars = new LoadVars();
var ReceLoad:LoadVars = new LoadVars();
var mgNum:Number=0;
for (i=0; i<mgBW.length ; i++){
mgBW[i].onRelease = function () {
mgNum++
SenderLoad.word = mgB[mgNum];
SenderLoad.sendAndLoad("http://www.web.com/my.php",ReceLoad,"POST");
ReceLoad.onData = function(src) {
var messageA:Array = src.split(",,,");
ldrB2.html=true;
for(var i:Number=0; i<messageA.length; i++){
var a:Array=messageA[i].split(",,");
ldrB1.htmlText +=a[0];
ldrB2.htmlText +=a[1];if you want different words in different colors you can use:
ldrB1.html=true;
ldrB1.text = formatF("REJECT",0xff0000,ldrB1)
ldrB1.text = formatF("ACCEPT",0x000000,ldrB1);
ldrB1.text = "formatF("BANNED",0xc0c0c0,ldrB1);
function formatF(s:String, col:Number, tf:TextField):Void {
if(tf.text.indexOf(s)==-1){
return;
tfor.color = col;
var startindex:Number = 0;
while (startindex>-1 && startindex<tf.text.length-1) {
var bindex:Number = tf.text.indexOf(s, startindex);
var eindex:Number = bindex+s.length;
tf.setTextFormat(bindex,eindex,tfor);
startindex = eindex;
var tfor:TextFormat = new TextFormat(); -
Please help to re-write this query using exists or with
Hi please help to re-write this query using exists or with, i need to write same code for 45 day , 90 days and so on but sub query condition is same for all
SELECT SUM (DECODE (t_one_mon_c_paid_us, 0, 0, 1)) t_two_y_m_mul_ca_
FROM (SELECT SUM (one_mon_c_paid_us) t_one_mon_c_paid_us
FROM (
SELECT a.individual_id individual_id,
CASE
WHEN NVL
(b.ship_dt,
TO_DATE ('05-MAY-1955')
) >= SYSDATE - 45
AND a.country_cd = 'US'
AND b.individual_id in (
SELECT UNIQUE c.individual_id
FROM order c
WHERE c.prod_cd = 'A'
AND NVL (c.last_payment_dt,
TO_DATE ('05-MAY-1955')
) >= SYSDATE - 745)
THEN 1
ELSE 0
END AS one_mon_c_paid_us
FROM items b, addr a, product d
WHERE b.prod_id = d.prod_id
AND d.affinity_1_cd = 'ADH'
AND b.individual_id = a.individual_id)
GROUP BY individual_id)
Edited by: user4522368 on Aug 23, 2010 9:11 AMPlease try and place \ before and after you code \Could you not remove the inline column select with the following?
SELECT a.individual_id individual_id
,CASE
when b.Ship_dt is null then
3
WHEN b.ship_dt >= SYSDATE - 90
3
WHEN b.ship_dt >= SYSDATE - 45
2
WHEN b.ship_dt >= SYSDATE - 30
1
END AS one_mon_c_paid_us
FROM items b
,addr a
,product d
,order o
WHERE b.prod_id = d.prod_id
AND d.affinity_1_cd = 'ADH'
AND b.individual_id = a.individual_id
AND b.Individual_ID = o.Individual_ID
and o.Prod_CD = 'A'
and NVL (o.last_payment_dt,TO_DATE ('05-MAY-1955') ) >= SYSDATE - 745
and a.Country_CD = 'US' -
How to performance tune this query
I need some inputs on how to do performance tuning on this query to improve performance.
It takes around 45 secs to run. Is it possible to make any improvements in this by putting hints or writing in another way?
select count(*)
as nCount from A ,
B ,
C
WHERE A.COL1 = B.COL1 AND
A.COl2 <> 'COM' AND
B.COL2 = C.COL1 AND
B.COl3 IS NULL AND
B.COL4 = 'TEST'
This is the query plan:
Operation Object Name Rows Bytes Cost Object Node In/Out PStart PStop
SELECT STATEMENT Optimizer Mode=CHOOSE 1 51
SORT AGGREGATE 1 37
HASH JOIN 48 K 1 M 51
TABLE ACCESS FULL A 68 K 998 K 32
NESTED LOOPS 98 K 2 M 5
TABLE ACCESS BY INDEX ROWID B 142 K 2 M 4
INDEX SKIP SCAN XIF37B 142 K 6
INDEX UNIQUE SCAN XPKC 1 5Mcka
As well as EXPLAIN PLAN, let us know what proportion of rows are visited by this query. It may be that it is not using a full table scan when it should (or vice versa).
And of course we'd need to know what indexes are available, and how selective they are for the predicated you have in this query ...
Regards Nigel -
How to speed up this query?
I have created a demo table:
create table demo1(d date);
and insert some data to table:
begin
-- add 6000000 rows
for i in 1..1000000 loop
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
end loop;
commit;
end;
The query
select * from demo1
where d=to_date('25.10.2004','DD.MM.YYYY')
executed three times faster than
select from demo1 where d=trunc(sysdate-1);
Why? How to speed up this query if I do not want to use index?
I have created index:
create index demo1_indx on demo1(d);
Execution time of queries became identical (for this volume of data).Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> create table demo1(d date);
Table created.
SQL> begin
2 for i in 1..1000000 loop
3 insert into demo1 values(trunc(sysdate-i));
4 insert into demo1 values(trunc(sysdate-i));
5 insert into demo1 values(trunc(sysdate-i));
6 insert into demo1 values(trunc(sysdate-i));
7 insert into demo1 values(trunc(sysdate-i));
8 insert into demo1 values(trunc(sysdate-i));
9 insert into demo1 values(trunc(sysdate-i));
10 insert into demo1 values(trunc(sysdate-i));
11 end loop;
12 commit;
13 end;
14 /
PL/SQL procedure successfully completed.
SQL> alter session set timed_statistics=true;
Session altered.
SQL> alter session set sql_trace=true;
Session altered.
SQL> set timing on;
SQL> set autotrace on;
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:10.70
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
29 recursive calls
1 db block gets
28988 consistent gets
13030 physical reads
1035300 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:03.35
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12837 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:04.95
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12757 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:03.82
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12752 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:17.53
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
6 recursive calls
0 db block gets
14503 consistent gets
12758 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:15.82
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12753 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:14.56
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12758 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:11.84
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12757 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> alter session set sql_trace=false;
Session altered.
Elapsed: 00:00:00.00
SQL> alter session set timed_statistics=false;
Session altered.
Elapsed: 00:00:00.01
SQL> -
How do I correct this query ?
here is a query:
select roleid from Apps_USERROLE where guid in (select guid from Apps_USER_MASTER order by userid)
This does not work ...because the internal subquery retruns multiple result.
How do I correct this query ?Satish Kandi wrote:
select roleid from Apps_USERROLE where guid in (select guid from Apps_USER_MASTER order by userid)ORDER BY clause is not supported in subqueries since it makes no sense.
If you want an ordered result set, you need add order by clause to the main query.
select roleid from Apps_USERROLE where guid in (select guid from Apps_USER_MASTER)
order by roleid;
HTH...we have a problem then . Apps_USER_MASTER has userid as primary key .
select guid from Apps_USER_MASTER => this could give duplicate guid then . -
I just got a wireless printer and we followed the directions on how to hook it up to the iPad. Somehow it seems as if we hooked it up to the wrong printer. How do we disconnect this printer from the iPad so that we can connect the correct printer?
AirPrint finds any air print enabled printers on your wifi network, and won't find any printers that are not. So if you print from, say, safari, and it finds that printer, then that is your printer. It may have a different name than you were expecting.
Turn the printer off, and see if the print job still sees the printer. That will confirm that the one it sees is yours.
What happens when you send the print job?
If you are sharing a wifi network with others, and one of them has a AirPrint printer on the network, you will see that one. There is not way to have it forget one, so you will need to know your printer from the other printer, and select the right one. -
How to attach a PDF file TO the form so that recipients can access the file as a resource?
How to attach a PDF file TO the form so that recipients can access the file as a resource?
Sorry we do not support this. What you can do though is add a Formatted Text element and add a link to a PDF. To insert a link add some text, select the text and right click on the text (or look under the Insert menu in the top right of the scene)
Gen -
How can prevent duplication of condition types in sales order??
How can prevent duplication of condition types in sales order.
While we want Amount field Changeable by user.
Please give me solution.Dear Rohit,
Can you explain your scenario in detail with an example or any screen shot so that we will help you in easy way ...
Regards,
C.B Reddy
Maybe you are looking for
-
Storing a field of length 500 char in a database table
I want to create a ztable in which I need to store the data of length more than 256 , so please let me know , how can we create the field of length 500 chars.. I have tried using STRING and LCHR , but it's not working. Thank You, Ankur Goyal
-
Business Package for Asset Services, implementation & integration
Hello, We have installed Business Package for Asset Services in EP6.0 and integrated it with back-end system. The package works fine but on some iViews (equipmentpartners, functionallocationpartners, equipment, functionallocation, configurationshow)
-
I have a IPad 4.3.5 and would like to upgrade to ios7 how do I do it?
I do not have a software update button on my general setting. Is this normal?
-
Hi guys, I did a validation a few weeks ago which was simply: P10_DATEDATE < SYSDATE. I came back and tested it and it still worked as expected. I then tried to recreate it again and the new attempt doesn't work and I don't know why. To begin with it
-
How to minimize createDRMSession delay in offline mode with Objective-C API
Hi all ! We are now having our application playing in offline mode (license and files are local, network is off). But the createDRMSession method take 4 to 5 seconds to complete. In the sample code of the API they imply that there is some way to redu