single-row subquery returns more than one row .what should i do in such case - oracle11g

UPDATE STG_ABS_DSD_RECEIPTS_PI_WRK1 A
SET
(
RECEIPT_HEADER_KEY,
ORIG_ACNTNG_EFF_DATE,
GL_DEPT_ID,
RECEIPT_DATE,
RECEIPT_TIME,
TOTAL_INVOICE_COST_HDR,
SUM_EXTENDED_COST_AMT
) =
(SELECT
HDR_RECEIPT_HEADER_KEY,
HDR_ORIG_ACNTNG_EFF_DATE,
HDR_GL_DEPT_ID,
HDR_RECEIPT_DATE,
HDR_RECEIPT_TIME,
HDR_SUM_TOTAL_INVOICE_COST,
PIEDW_EXTENDED_COST_AMT
FROM
STG_ABS_DSD_RECEIPTS_PI_WRK4 B
WHERE
A.SUPPLIER_KEY = B.PIEDW_SUPPLIER_KEY
AND
A.STORE_KEY = B.PIEDW_STORE_KEY
AND
RTRIM(LTRIM(A.SUPPLIER_INVOICE_NBR,0)) = RTRIM(LTRIM(B.PIEDW_SUPPLIER_INVOICE_NBR,0))
AND
TO_DATE(A.PIEDW_INV_TRAN_DATE,'YYYYMMDD') = B.PIEDW_INVOICE_DATE
AND
B.HDR_FOUND_FLAG IN ('N', 'MY'))
WHERE EXISTS
(SELECT 1 FROM STG_ABS_DSD_RECEIPTS_PI_WRK4 B
WHERE
A.SUPPLIER_KEY = B.PIEDW_SUPPLIER_KEY
AND
A.STORE_KEY = B.PIEDW_STORE_KEY
AND
RTRIM(LTRIM(A.SUPPLIER_INVOICE_NBR,0)) = RTRIM(LTRIM(B.PIEDW_SUPPLIER_INVOICE_NBR,0))
AND
TO_DATE(A.PIEDW_INV_TRAN_DATE,'YYYYMMDD') = B.PIEDW_INVOICE_DATE
AND
B.HDR_FOUND_FLAG IN ('N', 'MY'));

You should ensure that the subquery only returns the row that you want by providing the correct joins from the table you are updating, or if you just want one row out of many that might be returned then use "WHERE ROWNUM = 1" (or a LIMIT clause in other RDBMSs or Oracle 12c)

Related

Query to Fetch Data from Database

I am fetching data from SQLite database using following query:
SELECT p.sent,
e.*,
e.no _id
FROM ecare e
LEFT OUTER JOIN pweb p ON e.h_id = p.h_id
WHERE (ant = 'N' or ant = 'D')
GROUP BY e.h_id
ORDER BY p.sent
By using the above SQLite query, I am getting all the records belonging to sent (where sent = 1 and sent = 0).
Now, I would like to get only those records from database, where sent status is sent = 0. (In short, I don't want to fetch all the records belonging to sent, or records where sent = 1).
Have you tried with below query ?
SELECT p.sent,e.*, e.no _id from ecare e LEFT JOIN pweb p ON
e.h_id=p.h_id WHERE (ant = 'N' or ant = 'D') AND p.sent = '0' GROUP
BY e.h_id
Check below query for your question:
return db.rawQuery("SELECT p.sent,e.*, e.no _id from ecare e LEFT OUTER JOIN pweb p ON e.h_id=p.h_id WHERE (ant = 'N' or ant = 'D') AND p.sent = '1' GROUP BY e.h_id ORDER BY p.sent ", null);
You are already filtering on the ant column.
Adding a filter for sent works the same way:
SELECT ...
...
WHERE (ant = 'N' OR ant = 'D')
AND sent = 0
...

Converting an PLSQL select statement into a update

Guys I have such a problem.
I know how to write a good select statement but I have no idea how to turn it into a corresponding update.
Im still learning plsql
Here is my select
select * --count(*)
from POLISY_OT ot
join polisy p on p.poli_id = ot.ot_poli_id
join sou.rai_skl rs on rs.ot_id = ot.ot_id
where ot_under_promil = 0
and ot_skladka_rok <> ot_skladka_netto_rok
and ot_rodzaj_um = 'OP'
and ot_rodzaj = 'D'
and ot_produkt_id = 17
and p.poli_status in ('AK', 'CZ')
and rs.skl_roczna = ot.ot_skladka_rok;
now I would like to wrap it up with an update and create something like this
update (
select * --count(*)
from POLISY_OT ot
join polisy p on p.poli_id = ot.ot_poli_id
join sou.rai_skl rs on rs.ot_id = ot.ot_id
where ot_under_promil = 0
and ot_skladka_rok <> ot_skladka_netto_rok
and ot_rodzaj_um = 'OP'
and ot_rodzaj = 'D'
and ot_produkt_id = 17
and p.poli_status in ('AK', 'CZ')
and rs.skl_roczna = ot.ot_skladka_rok)
set ot_skladka_rok = ot_skladka_netto_rok;
First, I really hope you aren't a student asking for homework help. That really bugs me.
On the assumption it's not, it's a little hard to tell exactly which columns belong to which tables, given that you didn't include the table aliases throughout.
I read this as that you wanted to update a column based on the value in another table, restricting the table updates to records that match a third table).
So I think you want something like this:
UPDATE polisy_ot ot
SET ot_skladka_rok =
(SELECT ot_skladka_netto_rok
FROM sou.rai_skl rs
WHERE rs.ot_id = ot.ot_id
AND rs.skl_roczna = ot.ot_skladka_rok)
WHERE ot_under_promil = 0
AND ot_skladka_rok <> ot_skladka_netto_rok
AND ot_rodzaj_um = 'OP'
AND ot_rodzaj = 'D'
AND ot_produkt_id = 17
AND EXISTS (SELECT NULL
FROM polisy p
WHERE p.poli_id = ot.ot_poli_id
AND p.poli_status IN ('AK', 'CZ'))
Good luck,
Stew

equivalent to INSERT INTO TABLE SET in Oracle

I want to add data to table STATISTICS using INSERT statements.
I also want to move new counts to old counts and new date to old date as the new data comes in.
This is where it gets lil tricky because I don't know if there is such a thing as INSERT INTO table with SET in Oracle.
INSERT INTO STATISTICS
SET
MODEL = '&MY_MODEL',
NEW_COUNT =
(
SELECT COUNT(*)
FROM TABLE CLIENTS
),
NEW_DATE = SYSDATE,
OLD_COUNT = NEW_COUNT,
OLD_DATE = NEW_DATE,
PRNCT_CHANGE = ((NEW_COUNT) - (OLD_COUNT)) / (NEW_COUNT)*100
);
How do I accomplish this in Oracle?
This should upsert statistics, adding new ones as you go. It presumes a unique key on MODEL; if that's not true, then you'd have to do inserts as Angelina said, getting only the most recent row for a single MODEL entry.
MERGE INTO STATISTICS tgt
using (SELECT '&MY_MODEL' AS MODEL,
(SELECT COUNT(*) FROM CLIENTS) AS NEW_COUNT,
SYSDATE AS DATE_COUNT,
NULL AS OLD_COUNT,
NULL OLD_DATE,
NULL AS PRCNT_CHANGE
FROM DUAL) src
on (TGT.MODEL = SRC.MODEL)
WHEN MATCHED THEN UPDATE
SET TGT.NEW_COUNT = SRC.NEW_COUNT,
TGT.NEW_DATE = SRC.NEW_DATE,
TGT.OLD_COUNT = TGT.NEW_COUNT,
TGT.OLD_DATE = TGT.NEW_DATE,
TGT.PRCNT_CHG = 100 * (SRC.NEW_COUNT - TGT.NEW_COUNT) / (SRC.NEW_COUNT)
-- NEEDS DIV0/NULL CHECKING
WHEN NOT MATCHED THEN INSERT
(MODEL, NEW_COUNT, NEWDATE, OLD_COUNT, OLD_DATE, PRCNT_CHANGE)
VALUES
(src.MODEL, src.NEW_COUNT, src.NEWDATE, src.OLD_COUNT, src.OLD_DATE, src.PRCNT_CHANGE);
INSERT INTO STATISTICS(MODEL,NEW_COUNT,NEW_DATE,OLD_COUNT,OLD_DATE,PRNCT_CHANGE)
SELECT MODEL,
( SELECT COUNT(*)
FROM TABLE(USERS)
),
SYSDATE,
NEW_COUNT,
NEW_DATE,
(((NEW_COUNT) - (OLD_COUNT)) / (NEW_COUNT)*100)
FROM SEMANTIC.COUNT_STATISTICS
WHERE MODEL = '&MY_MODEL'
AND trunc(NEW_DATE) = trunc(NEW_DATE -1)
;

Select multiple tables with same id

I need to select data from four tables based on only one.
In my 'calculated' table, I have all the records I need.
But I need to retrieve some other info for each record, from 'programs', 'term' and 'imported' tables.
'calculated' has ID from 'programs'.
But, to achieve a record from 'imported', I need to join the 'item' table, because 'item' has ID from 'programs' and from 'imported'.
'term' has ID from 'imported'.
So, I tried this:
select c.date,
p.name,
c.name1,
c.name2,
t.date,
i.version,
c.price1,
c.price2,
c.price3
from calculated c, programs p, term t, imported i, item it
where c.programs_id = p.programs_id
and c.programs_id = it.programs_id
and it.imported_id = i.imported_id
and i.term_id = t.term_id;
But when I use count(*) on 'calculated', I get 30k of records, and from my select statement I get more than 130 millions of records.
What am I doing wrong?
What should I do for this to work?
If all duplicates rows are equivalent, u can try smth like this
select c.date,
p.name,
c.name1,
c.name2,
t.date,
i.version,
c.price1,
c.price2,
c.price3
from calculated c, programs p, term t, imported i
where c.programs_id = p.programs_id and
(select imported_id from item it where c.programs_id = it.programs_id and rownum = 1) = i.imported_id
and i.term_id = t.term_id;
where "rownum = 1" is restriction on the selection of one line for oracle.
you forgot to join term table.
Probably you need to add
and t.term_id = i.term_id

Copy a subset of column data from one table to another

I have two tables with identical schema. Let's name them TestTable and TestTableTemp. I need to copy just two columns from TestTableTemp to TestTable without disrupting other data. The rows in TestTable are a subset of those in TestTableTemp. Let's say the columns that I need to copy are named Column1 and Column2 and that they have identical primary keys reference by column primaryKey.
In mysql I believe this could be done as such or something similar:
UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey
Sqlite does not allow for multiple tables to be defined on the update statement as can been seen in their reference data here: http://www.sqlite.org/lang_update.html
The best I could come up with is such:
UPDATE TestTable SET
Column1 = (select TestTableTemp.Column1 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey),
Column2 = (select TestTableTemp.Column2 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey)
WHERE EXISTS(select * from TestTableTemp where TestTable.primaryKey = TestTableTemp.primaryKey"
This gives me a syntax error near "." I am guessing this is because I cannot reference TestTable in the scalar expressions.
Can anyone point me in the right direction? Any help is much appreciated.
EDIT:
I cleaned up the second query a bit. It seems to just set the Column1 and Column2 to the first row from that column from TestTableTemp.
Your original query for comparison:
UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1
, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey
Here is the working query (I just slightly changed your version):
http://sqlfiddle.com/#!5/f3a19/9
UPDATE TestTable
SET
Column1 = ( SELECT TestTableTemp.Column1
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
,Column2 = ( SELECT TestTableTemp.Column2
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
WHERE EXISTS( SELECT NULL
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
;

Resources