Trying to return defects between two dates using query - rally

Trying to use the following query:
((CreationDate < "today-25") and (CreationDate > "today-30") and (Priority = "High") and (State <= Fixed))
This returns no records, but I have defects that should meet that criteria. If I remove the second CreationDate, I start getting records that match the new query. What am I missing in the syntax here?

Related

Oracle BIP OOTB SQL returning ORA-01427: single-row subquery returns more than one row

I am facing ORA-01427: single-row subquery returns more than one row error in one of my report. The report was build by previous vendor and it is a big SQL which seems like was generated by some tool and used in the report. I am having a tough time to figure out what's wrong. All the sub queries are either using aggregate functions or ROWNUM = 1 to return 1 record.
Please see if you can give me any pointers to which part I need to focus on. I checked the log files generated by the BIP but it just narrowed down it to the dataset.
Due to large size of the file, I provide the SQL in this file:
SQL
You'll have to scan the query line by line and check all subqueries that do not select an aggregated function (e.g. MIN(..)) and don't contain the ROWNUM = 1 limitation.
Example
(
SELECT
( ko.extn_attribute_timestamp006 )
FROM
moo_ref_entities ko
WHERE
ko.extn_attribute_number001 = lea.id
AND ko.attribute_category = 'RenewalAndOtherOptions_c'
AND nvl(ko.extn_attribute_timestamp003, sysdate) >= sysdate
AND ko.extn_attribute_timestamp005 = (
SELECT
MIN(ko.extn_attribute_timestamp005)
FROM
moo_ref_entities ko
WHERE
ko.extn_attribute_number001 = lea.id
AND ko.attribute_category = 'RenewalAndOtherOptions_c'
AND nvl(ko.extn_attribute_timestamp003, sysdate) >= sysdate
)
) AS option1_exc,
This is a typical trap as you constrain the extn_attribute_timestamp005 with a MIN subquery, but if you have ties in the timestamps you get exact the error you observe.
Here's a SELECT nested in a SELECT list that isn't guaranteed to yield just one row.
(
SELECT FL.lookup_code
FROM fnd_lookups FL
,hz_organization_profiles HO1
WHERE ho1.party_id = LEA.extn_attribute_number010
AND FL.lookup_code = HO1.extn_attribute_char035
AND FL.lookup_type = 'FND_SALES_CATEGORY'
) AS Sales_Category
It wouldn't have been hard for somebody to INSERT something to that fnd_lookups table to make that subquery yield more than one row. You could go for AND ROWNUM=1 or SELECT MAX(FL.lookup_code) lookup_code to see if it helps.

SQL count show 0 if no rows including where condition

I'm doing a COUNT but cannot get the value 0 when there are no rows in the result.
If I remove the where condition:
AND documentstats.OPENINGDATE >= '2021-01-01T00: 00: 00.000'
it works fine and I get the value 0 when there are no rows in the result.
I am looking for an option to return value 0 in the NumberOfViews column when no rows are found in my count.
Can anyone help me?
SELECT
customertodocument.DocId,
COUNT (documentstats.DocId) AS NumberOfViews
FROM
customertodocument
LEFT JOIN
documentstats ON customertodocument.DocId = documentstats.DocId
AND customertodocument.customerId = documentstats.customerId
WHERE
customertodocument.customerId = '1111'
AND documentstats.openingdate >= '2021-01-01T00:00:00.000'
GROUP BY
customertodocument.DocId
ORDER BY
NumberOfViews ASC
The second condition in the WHERE clause is filtering out all non-matches. Because you have an explicit GROUP BY, the query will return no rows if the FROM clause has no rows.
If you want counts of 0, then move the condition to the ON clause of the LEFT JOIN. Note: Conditions on the second table go in the ON clause.
The query should look like:
SELECT cd.DocId, COUNT(ds.DocId) AS NumberOfViews
FROM customertodocument cd LEFT JOIN
documentstats ds
ON cd.DocId = ds.DocId AND
cd.customerId = ds.customerId AND
ds.openingdate >= '2021-01-01'
WHERE ds.customerId = 1111
GROUP BY cd.DocId
ORDER BY NumberOfViews ASC;
Notes:
Table aliases make the query easier to write and to read.
customerId looks like a number. If it is, then the comparison should be to a number. If the id is really a string, put the single quotes back in.
You have a date constant. There is no need to include the time. No real harm, except it clutters the query.
My guess is that there are no records in the data that meet both of the conditions. You are grouping by customertodocument.DocId, but if no values of customertodocument.DocID exist after filtering with the WHERE clause, the aggregation will have nothing to group by and you'll get no results. You can test this by running the following query:
SELECT *
FROM customertodocument
LEFT JOIN documentstats on customertodocument.DocId = documentstats.DocId and customertodocument.customerId = documentstats.customerId
WHERE customertodocument.customerId = '1111' AND documentstats.openingdate >= '2021-01-01T00:00:00.000'
your WHERE condition returns nothing so you are not getting any record. Count alone in select clause can give you 0 but you have one column and then count so you are not getting any record
What value you are expecting in the customertodocument.DocId if no matching record found.
You can get the 0 count if you remove the customertodocument.DocId from select clause keeping only count in select clause and removing the GROUP BY clause

Executing subquery to insert data on another table

I am getting this error while executing below query:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
This is the query I am using:
UPDATE BatchRecords SET Fees = (
SELECT
(Fees/NoOfPayments) as AvgTotal
FROM BatchFx
WHERE BatchRecords.BatchId = BatchFx.BatchId
AND BatchFx.CreatedOn < '2019-05-04'
);
Your error is happening because your WHERE clause is returning more than 1 record. So the update statement is confused which row does it need to update (because you most probably have more than 1 record per ID in the BatchFX table). I replicated your issue in this link and got the same output.
So to solve this issue you need to use an aggregator to group all the rows and output one record from the subquery.
UPDATE BatchRecords
SET Fees = (
SELECT
AVG(Fees / NoOfPayments) as AvgTotal
FROM BatchFx
WHERE BatchRecords.BatchId = BatchFx.BatchId
AND BatchFx.CreatedOn < '2019-05-04'
);
Hope this helps :)
The alias AvgTotal seems to imply that you want to take an average of something, so why not try doing that:
UPDATE BatchRecords br
SET Fees = (SELECT AVG(Fees/NoOfPayments) AS AvgTotal
FROM BatchFx bf
WHERE br.BatchId = bf.BatchId AND bf.CreatedOn < '2019-05-04');
Note that the error message you are seeing implies that the subquery in some cases would return more than one record. Selecting an aggregate function would be one way to get around this problem.

Update table with combined field from other tables

I have a table with actions that are being due in the future. I have a second table that holds all the cases, including the due date of the case. And I have a third table that holds numbers.
The problems is as follows. Our system automatically populates our table with future actions. For some clients however, we need to change these dates. I wanted to create an update query for this, and have this run through our scheduler. However, I am kind of stuck at the moment.
What I have on code so far is this:
UPDATE proxima_gestion p
SET fecha = (SELECT To_char(d.f_ult_vencim + c.hrem01, 'yyyyMMdd')
FROM deuda d,
c4u_activity_dates c,
proxima_gestion p
WHERE d.codigo_cliente = c.codigo_cliente
AND p.n_expediente = d.n_expediente
AND d.saldo > 1000
AND p.tipo_gestion_id = 914
AND p.codigo_oficina = 33
AND d.f_ult_vencim > sysdate)
WHERE EXISTS (SELECT *
FROM proxima_gestion p,
deuda d
WHERE p.n_expediente = d.n_expediente
AND d.saldo > 1000
AND p.tipo_gestion_id = 914
AND p.codigo_oficina = 33
AND d.f_ult_vencim > sysdate)
The field fecha is the current action date. Unfortunately, this is saved as a char instead of date. That is why I need to convert the date back to a char. F_ult_vencim is the due date, and hrem01 is the number of days the actions should be placed away from the due date. (for example, this could be 10, making the new date 10 days after the due date)
Apart from that, there are a few more criteria when we need to change the date (certain creditors, certain departments, only for future cases and starting from a certain amount, only for a certain action type.)
However, when I try and run this query, I get the error message
ORA-01427: single-row subquery returns more than one row
If I run both subqueries seperately, I get 2 results from both. What I am trying to accomplish, is that it connects these 2 queries, and updates the field to the new value. This value will be different for every case, as every due date will be different.
Is this even possible? And if so, how?
You're getting the error because the first SELECT is returning more than one row for each row in the table being updated.
The first thing I see is that the alias for the table in UPDATE is the same as the alias in both SELECTs (p). So all of the references to p in the subqueries are referencing proxima_gestion in the subquery rather than the outer query. That is, the subquery is not dependent on the outer query, which is required for an UPDATE.
Try removing "proxima_gestion p" from FROM in both subqueries. The references to p, then, will be to the outer UPDATE query.

oracle sql query generating some unexected resutls

I have an sql query in which I have table named attan for the attendance and column named
S_TIME in which I stored in time and out time with a setting a flag of in with I and for O
Now I am trying to get in time as well as out time using the flag. I have written a query but result from this query are making some wrong sense.
Here is my query
SELECT X.AC_NO, X.IN_TIME, Y.OUT_TIME, X.S_DTE, X.CHK_IN, Y.CHK_OUT
FROM (SELECT A.AC_NO, A.S_TIME IN_TIME, A.AC_CHECKTYPE CHK_IN, A.S_DTE
FROM ATTN A
WHERE A.AC_CHECKTYPE = 'I' AND A.S_DTE = :P_DTE) X,
(SELECT B.AC_NO, B.S_TIME OUT_TIME, B.AC_CHECKTYPE CHK_OUT
FROM ATTN B
WHERE B.AC_CHECKTYPE = 'O' AND B.S_DTE = :P_DTE) Y
WHERE X.AC_NO = Y.AC_NO(+)
Through this query I always get the number of records of the employees who are in and only get 2 number of reocrds with out time.
Whereas in table there are 234 number of emps who are in and 256 are out but results don't match the data.
Please any one help me if any problem in my query.
SUGGESTION:
Run the subselects and see if you're getting all the rows you expect, with and without the "A.S_DTE = :P_DTE" date filter:
SELECT A.AC_NO, A.S_TIME IN_TIME, A.AC_CHECKTYPE CHK_IN, A.S_DTE
FROM ATTN A
WHERE A.AC_CHECKTYPE = 'I' AND A.S_DTE = :P_DTE
My first guess is that maybe you have a datetime, and unless the hours/minutes/seconds match exactly, you won't get the row. You can use to_char() to have just the "date" portion in your "from":
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:204714700346328550
In any case, seeing what rows you do (and don't) get in the subqueries should show you what's wrong with the complete query.
Please post back what you find!