Select query succeeds however Insert using same query fails with ORA-00979 (not a GROUP BY expression) - oracle

I am having a hard time finding why the below insert would fail while the select alone executes successfully.
INSERT INTO I6_POC_ADJ_OUTPUT
(LOADID,ADJ_ID,ADJ_CATEGORY,ADJ_COMMENT,ITEM,CHANNEL,FDATE,ADJ_TOT_QTY,FQTY)
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY) ADJ_TOT_QTY,
SUM(ADJ.TP_SPLIT) FQTY
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE;
Select and Insert Executions
Update:
Tried with a simple CTAS and it works.
CREATE TABLE TEST_ADJ_OUTPUT AS
SELECT * FROM (
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY),
SUM(ADJ.FQTY*ADJ.TP_SPLIT)
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE
);
Truncated the same table and tried inserting into the same but if fails with the same error. "SQL Error: ORA-00979: not a GROUP BY expression"
TRUNCATE TABLE TEST_ADJ_OUTPUT;
INSERT INTO TEST_ADJ_OUTPUT
SELECT * FROM (
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY),
SUM(ADJ.FQTY*ADJ.TP_SPLIT)
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE
);
Appreciate your help.
Thanks,
Manoj

Related

Access union all into new table

I have a saved query (MyUnion) for a union, appending monthly files (linked views):
select * from RawTrade1801
union all
select * from RawTrade1802
Trying to write this to a new table is proving problematic:
SELECT MyUnion.* into RawTrade2
FROM MyUnion
WHERE Field8 = 'ZAR';
I get an error: Cannot open database "
My aim is to once a month create a master table by appending every monthly file.
The following steps should generate a table from your UNION query:
Create a new query. In the SQL View, now type:
SELECT * FROM (SELECT * FROM RawTrade1801
UNION ALL
SELECT * FROM RawTrade1802);
Save the query. In its Design View click the Make Table button. Type in the table name (e.g. NewTable) that you want the output of this query to be.
Save and close the query.
The query icon will have changed. Double click on it and it should generate your table.
Access actually changes the SQL to
SELECT * INTO NewTable FROM
(SELECT * FROM RawTrade1801
UNION ALL
SELECT * FROM RawTrade1802) AS [%$###_Alias];
when you view the SQL after step 4.
Try using INSERT INTO ... SELECT:
INSERT INTO RawTrade2
SELECT * FROM RawTrade1801 WHERE Field8 = 'ZAR'
UNION ALL
SELECT * FROM RawTrade1802 WHERE Field8 = 'ZAR';
Of course, even if this works it still does not explain why your original query does not work. I expect it is a slight technical problem, though I don't know enough Access to see it immediately.
Create the Union query, save it with the name like 'qUnion'.
From menu open:
Create-->Query--> Make Table.
In Add Table Menu chose "Queries" and
Add your union query ('qUnion').
Select the fields that you need.
Open query.
Anew table will be created.

Query create table as with inner join select and group by (ORACLE)

This is my query, if i run the error is : Error in query: ORA-00907: missing right parenthesis, anybody can solve my problem?
Create table r_tcash_loci_act_tmp AS (
SELECT DISTINCT
R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI,
R_TCASH_ACT_MSISDN.MSISDN AS MSISDN_ACT,
R_TCASH_LOCI_ACT.AREA,
R_TCASH_LOCI_ACT.REGIONAL,
R_TCASH_LOCI_ACT.BRANCH,
R_TCASH_LOCI_ACT.SUB_BRANCH,
R_TCASH_LOCI_ACT.CLUSTERX,
R_TCASH_LOCI_ACT.UPDATED,
R_TCASH_ACT_MSISDN.DAILY,
R_TCASH_ACT_MSISDN.TOTAL_TRX,
R_TCASH_ACT_MSISDN.TOTAL_VOL
FROM R_TCASH_ACT_MSISDN
INNER JOIN R_TCASH_LOCI_ACT
ON R_TCASH_LOCI_ACT.MSISDN = R_TCASH_ACT_MSISDN.MSISDN
GROUP BY R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI
HAVING COUNT(R_TCASH_LOCI_ACT.MSISDN ) > 10);
It's a simple syntax error. You included the column alias in the GROUP BYclause (probably a cut'n'paste error).
GROUP BY R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI
So just remove the AS MSISDN_LOCI.
Although given that you have a DISTINCT clause and no aggregated columns it's a mystery why you have the GROUP BY at all. You should remove the whole line.

Sample clause using Group by and Having clauses

I am trying to using Sample clause using Group by and Having clauses.
My requirement is getting a Company ID which has only one record, I have tried many ways below is one of the methods I have tried.
With TESTA AS(
select TA.COMPANY_ID from(
select t1.COMPANY_ID from Table11 t1
where t1.COMPANY_ID in (select t2.COMPANY_ID from Table22 t2)
group by t1.COMPANY_ID having count(t1.COMPANY_ID)=1)TA )
select * FROM TESTA Sample(1);
When I execute the above query, It is throwing the below error.
ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT,
GROUP BY, etc.
01446. 00000 - "cannot select ROWID from, or sample, a view with DISTINCT, GROUP BY, etc."
*Cause:
*Action:
But, When I execute the below query, I am getting the results but it is not satisfying my requirement.
select COMPANY_ID from Table11 Sample(10)
where COMPANY_ID in (select company_id from Table22 )
Group by COMPANY_ID HAVING Count(COMPANY_ID)=1
It is executing all the possible records, can someone help me on this, please.

can i set up an SSRS report where users input parameters to a table

I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id

Oracle Insert, Update, Delete Trigger with Join

I'm trying to implement Oracle triggers for child views but I need to be able to join the child views to their parents in order to do a role permission check.
In SQL Server I'm able to stuff like this:
ALTER TRIGGER [dbo].[ASetTrt_I] ON [dbo].[UCV_ASet_TRT]
INSTEAD OF Insert AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT 1 from INSERTED i INNER JOIN [Analysis_Sets] p on i.[key] = p.[ID]
WHERE ([dbo].IsMemberOf(p.[UpdateRole]) <> 1 and [dbo].IsMemberOf('db_owner') <> 1))
RAISERROR ('Update failed due to insufficient permission',11,1)
INSERT INTO [Set_Trts] ( [ID], [Name], [key], [f_lTreatmentKey], [f_lOrder] )
SELECT
inserted.[ID], inserted.[Name], inserted.[key], inserted.[f_lTreatmentKey], inserted.[f_lOrder]
FROM inserted
INNER JOIN [Sets] parentT
on inserted.[key] = parentT.[ID]
WHERE (([dbo].IsMemberOf('db_owner')=1) or ([dbo].IsMemberOf(parentT.[UpdateRole])=1))
END
Is there anything I can do in Oracle to replicate the join functionality?
I've tried selecting from :New the way that SS selects from inserted but that doesn't seem to work..
Thanks.
You don't need to join. The:new pseudorow is just available and can be referenced like a record type. It isn't a table-like structure, and is only available in a for each row trigger. So your insert would be something like:
INSERT INTO Analysis_Set_Trts ( ID, Name, f_lAnalysisSetKey, f_lTreatmentKey,
f_lOrder )
SELECT :new.ID, :new.Name, :new.f_lAnalysisSetKey, :new.f_lTreatmentKey,
:new.f_lOrder
FROM Analysis_Sets parentT
WHERE parentT.ID = :new.f_lAnalysisSetKey
AND ((IsMemberOf('db_owner')=1) or (IsMemberOf(parentT.UpdateRole)=1));
... although not quite sure what the last line is doing or what the equivalent is.

Resources