ORA-00937: not a single-group group function, count function - oracle

select officer.forename || ' ' || Officer.surname || ' is working on : ' ||
count(crime.crime_id) "Officers Current Crimes"
From officer, crime
where officer.officer_id = crime.officer_id
order by crime.crime_id;
Can anyone tell me how to fix this error please, its something to do with the count function, thankyou in advance.

select officer.forename, Officer.surname || ' is working on : ' ||
count(crime.crime_id) "Officers Current Crimes"
From officer, crime
where officer.officer_id = crime.officer_id
GROUP BY officer.forename, Officer.surname;
You cant use a ORDER BY on non grouped column..

Related

Not getting multiple data for binding parameters in select statement in oracle [duplicate]

I have this query that I'm passing 2 parameters, COUNTRY_REGION parameter and COST_CENTER parameter
I have the possibility to pass both parameters at the same time COST_CENTER and COUNTRY_REGION..... Or pass one or the other... This part is OK.
You can see in the first image below
SELECT
dwg.GEOGRAPHY_ID as geographyId
,INITCAP (lower (dwc.COUNTRY_REGION)) as countryRegion
,INITCAP (lower (dwc.COUNTRY_NAME)) as countryName
,dp.PROJECT_ID as projectId
FROM
DATALAKE.DWL_GEOGRAPHIES dwg
,DATALAKE.DWB_PROJECT dp
,DATALAKE.DWL_GEOGRAPHY_COUNTRIES dwgc
,DATALAKE.DWL_COUNTRY dwc
,DATALAKE.DWB_PROJECT_FINANCIAL dpf
,DATALAKE.DWL_GOLIVE dgl
where dwg.geography_id = dp.project_geography_id
and dwg.geography_id = dwgc.geography_id
and dwc.country_id = dwgc.country_id
and dp.PROJECT_ID = dpf.PROJECT_ID
and dpf.PROJECT_ID = dgl.PROJECT_ID
and dpf.FLAG_ACTIVE = 1
and ((dp.cost_center = (:costCenter) and INITCAP (lower (dwc.COUNTRY_REGION)) = (:countryRegion))
or (:costCenter IS null and INITCAP (lower (dwc.COUNTRY_REGION)) = (:countryRegion))
or (dp.cost_center = (:costCenter) and :countryRegion IS null)
)
order by dwc.COUNTRY_REGION
But I WANT TO HAVE THE OPTION TO PASSA TWO OR MORE IN THE SAME PARAMETER as in the image below...
example:
COUNTRY_REGION: Peru, Chile, Argentina
or
COST_CENTER : 10500, 1000, ... , ....
I would like help, I've tried several things and I can't proceed, thank you very much.
You cannot pass multiple values with a single bind variable.
What you can do is pass in a single string that contains a delimited list and match a sub-string of the list to the value:
SELECT *
FROM table_name
WHERE ', ' || :country_region_list || ', ' LIKE '%, ' || country_region || ', %'
OR ', ' || :cost_centre_list || ', ' LIKE '%, ' || cost_centre || ', %'
Which would make your query:
SELECT dwg.GEOGRAPHY_ID as geographyId
, INITCAP(dwc.COUNTRY_REGION) as countryRegion
, INITCAP(dwc.COUNTRY_NAME) as countryName
, dp.PROJECT_ID as projectId
FROM DATALAKE.DWL_GEOGRAPHIES dwg
INNER JOIN DATALAKE.DWB_PROJECT dp
ON (dwg.geography_id = dp.project_geography_id)
INNER JOIN DATALAKE.DWL_GEOGRAPHY_COUNTRIES dwgc
ON (dwg.geography_id = dwgc.geography_id)
INNER JOIN DATALAKE.DWL_COUNTRY dwc
ON (dwc.country_id = dwgc.country_id)
INNER JOIN DATALAKE.DWB_PROJECT_FINANCIAL dpf
ON (dp.PROJECT_ID = dpf.PROJECT_ID)
INNER JOIN DATALAKE.DWL_GOLIVE dgl
ON (dpf.PROJECT_ID = dgl.PROJECT_ID)
WHERE dpf.FLAG_ACTIVE = 1
AND (
', ' || :costCenter || ', ' LIKE '%, ' || dp.cost_center || ', %'
OR :costCenter IS null
)
AND (
', ' || :countryRegion || ', '
LIKE '%, ' || INITCAP(dwc.COUNTRY_REGION) || ', %'
OR :countryRegion IS null
)
AND (:costCenter IS NOT NULL OR :countryRegion IS NOT NULL)
order by dwc.COUNTRY_REGION

ORA-01427: single-row Subquery Returns More Than One Row for Update

Hi I am trying to execute but getting error single row subquery returns more than one row.
update upld_mktprice
set (upld_mktprice.orig_security, upld_mktprice.stk_exch, upld_mktprice.stk_group, upld_mktprice.instr_type) =
(select security.security,nvl(upld_mktprice.stk_exch,
nvl(security.stk_exch,'DIRECT')),
security.stk_group,
decode(upld_mktprice.source,'FOREX','X','S') as instr_type -- HLAMUAT-1457146: Passing DIRECT as a default value for HLAM. confirmed by Dheeren/Prasanth.
from v_security_all security
where upld_mktprice.security = Decode(security.asset_type,'OPT',security.stk_sec_id||' '||substr(security.security,instrb(security.security, ' ', 1,1)+3, length(security.security)),security.stk_sec_id)
and rectype = 'L'),
upld_mktprice.currency = nvl(upld_mktprice.currency,''),
upld_mktprice.value_date = nvl(upld_mktprice.value_date,''),
upld_mktprice.amc_code = 'AMC'
Where exists (select 1
from v_security_all security
where upld_mktprice.security = Decode(security.asset_type,'OPT',security.stk_sec_id||' '||substr(security.security,instrb(security.security, ' ', 1,1)+3, length(security.security)),security.stk_sec_id)
and rectype = 'L')
and upld_mktprice.orig_security is null
and upld_mktprice.user_id = 'SRINIVAS'
and upld_mktprice.source = 'MKTPRICEMAN';
I think the error pretty much speaks for itself. The portion of your query below is returning more than one row. There is no way for us to validate because we do not have the data that you have. It might also be a bit easier to troubleshoot if you used table aliases in your update statement.
(SELECT security.security,
NVL (upld_mktprice.stk_exch, NVL (security.stk_exch, 'DIRECT')),
security.stk_group,
DECODE (upld_mktprice.source, 'FOREX', 'X', 'S') AS instr_type -- HLAMUAT-1457146: Passing DIRECT as a default value for HLAM. confirmed by Dheeren/Prasanth.
FROM v_security_all security
WHERE upld_mktprice.security =
DECODE (security.asset_type,
'OPT', security.stk_sec_id
|| ' '
|| SUBSTR (security.security,
INSTRB (security.security,
' ',
1,
1)
+ 3,
LENGTH (security.security)),
security.stk_sec_id)
AND rectype = 'L')

Oracle function to select multiple rows in a single row not working

I am loading a file into a staging record using Application Engine. when ever there is blank in BU,Deptid etc... I have to capture what all columns are blank and update error text field with those values.
UPDATE SYSADM.PS_VI_EMP_TS SET ERR_TEXT =
SELECT ERROR FROM (SELECT * FROM (
SELECT CASE WHEN BUSINESS_UNIT = ' ' THEN 'BUSINESS_UNIT IS NULL' ELSE ' ' END AS ERROR FROM SYSADM.PS_VI_EMP_TS WHERE USERID='JCOOPER' AND ACTION = 'E' AND PROCESS_INSTANCE = '7852429'
UNION
SELECT CASE WHEN DEPTID = ' ' THEN 'DEPTID IS NULL' ELSE ' ' END AS ERROR FROM SYSADM.PS_VI_EMP_TS WHERE USERID='JCOOPER' AND ACTION = 'E' AND PROCESS_INSTANCE = '9852429'
UNION
SELECT CASE WHEN PROJECT_ID =' ' THEN 'PROJECT_ID IS NULL' ELSE ' ' END AS ERROR FROM SYSADM.PS_VI_EMP_TS WHERE USERID='JCOOPER' AND ACTION = 'E' AND PROCESS_INSTANCE = '9852429'
)) WHERE ERROR <> ' '
WHERE USERID='JCOOPER' AND ACTION = 'E' AND PROCESS_INSTANCE = '9852429'
The above script results as below.
ERROR
BUSINESS_UNIT IS NULL
DEPTID IS NULL
I want the result as below.
ERROR
BUSINESS_UNIT IS NULL,DEPTID IS NULL
I am using ListAgg function but facing errors as below.Any help would be great.
SELECT LISTAGG(BUSINESS_UNIT, ';') WITHIN GROUP(ORDER BY USERID)
FROM SYSADM.PS_VI_EMP_TS WHERE USERID='JCOOPER'
GROUP BY BUSINESS_UNIT
Facing the error:
ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"
*Cause:
*Action:
Error at Line: 47 Column: 43
You overcomplicated things. You don't need two unions and listagg at all. Simple use concatenation of case... when, write your select like here:
select case business_unit when ' ' then 'BUSINESS_UNIT IS NULL; ' end ||
case deptid when ' ' then 'DEPTID IS NULL; ' end ||
case project_id when ' ' then 'PROJECT_ID IS NULL; ' end as error
from ps_vi_emp_ts
where userid = 'JCOOPER' and action = 'E' and process_instance = '9852429'
demo
If your table has primary key then use it for update. Otherwise you can use merge with rowid. Or do it the way you did it if it worked for you, especially if there is only one row meeting criteria.

DAX expression equivalent to SQL "WHERE ColumnA='Yes' OR ColumnB='No' OR ColumnC='Yes' OR ColumnD='No'

I'm trying to find the DAX expression that will produce the result from the SQL query below. Thank you
Select Count(ID) AS CNT
FROM myTale
WHERE
ColumnA='Yes' OR ColumnB='No' OR ColumnC='Yes' OR ColumnD='No'
Try a pattern like this:
CALCULATE(COUNT(Column), FILTER(Table, Condition || Condition || Condition || Condition))
Thank you Alexis -
CALCULATE(COUNT(Column), FILTER(Table, Condition || Condition || Condition || Condition))

oracle apex shuttle saving deselections

I'm using Apex 4.2.4 and Oracle 11g. I have a mailing list application where I'm maintaining multiple mailing lists. A given recipient may belong to one or more lists. I'm using an Apex Shuttle to maintain the lists.
The source of all mailing recipients is in the table: mail_recipient. There are four important fields in mail_recipient:
prim_key
first_name
last_name
email_address
There is another table that stores the selected recipients for a given mailing list: mail_recipient_category: The important fields in mail_recipient_category are:
prim_key
recipient_fkey (this stores the prim_key from the mail_recipient table)
category
merge_check
There are two displayed items on the page. The category drop down list (P31_email_list) and the shuttle (P31_email_list_assignments)
The LOV for P31_email_list_assignments is:
Select last_name || ', ' || first_name || ' -- ' || email_address, prim_key from mail_recipient
order by 1;
The PL/SQL function body for the shuttle source is:
declare
emp_list apex_application_global.vc_arr2;
i number := 1;
begin
for r in (Select mr.last_name || ', ' || mr.first_name || ' -- ' || mr.email_address, mr.prim_key
From mail_recipient mr left outer join
mail_recipient_category mrc
On mr.prim_key = mrc.recipient_fkey
Where mrc.category = :P31_EMAIL_LIST)
loop
emp_list(i) := r.prim_key;
i := i + 1;
end loop;
return APEX_UTIL.TABLE_TO_STRING(emp_list, ':');
end;
There is also a single page process to update the database table mail_recipient_category. The process is executed on submit after computations and validations.
Begin
MERGE INTO MAIL_RECIPIENT_CATEGORY ss
USING (
Select
shuttle.column_value shuttle_st
, db.recipient_fkey db_st
from
table(string_to_coll(:P31_email_list_assignments)) shuttle
left outer join mail_recipient_category db
on shuttle.column_value = db.recipient_fkey
and db.category = :P31_email_list) t
on (ss.recipient_fkey = t.db_st
and ss.category = :P31_email_list
)
when matched
then
update
set
ss.merge_check = ss.merge_check
delete
where
t.shuttle_st is null and ss.category = :P31_email_list
-- t.shuttle_st is null
when not matched
then
insert
(recipient_fkey, category)
values
(t.shuttle_st, :P31_email_list);
end;
The shuttle works fine to load from left to right and save items. The problem I'm having is deselecting items from the right side to the left side of the shuttle. After moving a a mail recipient from the right to the left side, when I press the submit button, the items don't leave the right side of the shuttle and the process doesn't delete the row from the mail_recipient_category table.
Thanks for your help with this.
After working with a very experienced colleague, we determined that the page process was at fault. Specifically, the Delete clause of the Merge statement doesn't seem to work for this shuttle. We ended up adding a separate Delete statement after the the Merge statement in the page process.
I also dropped the mail_recipient_category table and am now using the more meaningful name mail_recipient_list.
The shuttle field name is (p31_email_list_assignments).
The working shuttle LOV Definition code is:
Select last_name || ', ' || first_name || ' -- ' || email_address, prim_key
From mail_recipient
order by 1;
The working shuttle Source code is:
declare
mail_list apex_application_global.vc_arr2;
i number := 1;
begin
for r in (Select mr.last_name || ', ' || mr.first_name || ' -- ' || mr.email_address, mr.prim_key
From mail_recipient mr left join
mail_recipient_list mrl
On mr.prim_key = mrl.recipient_fkey
Where mrl.list = :P31_EMAIL_LIST)
loop
mail_list(i) := r.prim_key;
i := i + 1;
end loop;
return APEX_UTIL.TABLE_TO_STRING(mail_list, ':');
end;
The working After Submit page process code is:
Begin
Merge Into MAIL_RECIPIENT_LIST mrl
Using (
Select
shuttle.column_value shuttle_cv
, db.recipient_fkey db_rfk
From
table(string_to_coll(:P31_email_list_assignments)) shuttle
left outer join mail_recipient_list db
on shuttle.column_value = db.recipient_fkey
and db.list = :P31_email_list) t
On (mrl.recipient_fkey = t.db_rfk
And mrl.list = :p31_email_list
)
When Matched
Then
Update
Set
mrl.merge_check = mrl.merge_check
-- Delete
-- Where
-- t.shuttle_cv is null
When Not Matched
Then
Insert
(recipient_fkey, list)
Values
(t.shuttle_cv, :P31_email_list);
/* The commented-out delete clause of the Merge statement never worked with this shuttle.
The following delete statement will every time this page process is called
*/
Delete from MAIL_RECIPIENT_LIST
Where instr(':' || :P31_email_list_assignments || ':',':' || recipient_fkey || ':') = 0
And list = :P31_email_list;
end;
I hope someone finds this useful.

Resources