Here is my SQL (Oracle) code:
SELECT a.Task_id, a.Activity_desc AS "ACTIVITY DESCRIPTION" ,
a.Activity_date || ' (' || a.Activity_day || ')' AS "ACTIVITY DATE",
pr.Project_name, COUNT(a.Volunteer_id)
FROM task_activity a
JOIN task t
ON a.Task_id = t.Task_id
JOIN project pr
ON t.Project_id = pr.Project_id
GROUP BY a.Task_id
ORDER BY Task_id
An error :
ORA-00979: not a GROUP BY expression
Without group statemen table looks like that :
I want to count number of volunteers for each task and group by task_id, but after hours of tries I gave up )
You need to group by all non aggregated values. Start off with something small and build up e.g.
SELECT a.Activity_desc, COUNT(a.Volunteer_id) countVolunteer
FROM task_activity a
GROUP BY a.Activity_desc
Check that returns you a count per activity, and then slowly add your other attributes to both the SELECT and GROUP BY sections e.g.
SELECT a.Activity_desc, t.Task_id, COUNT(a.Volunteer_id) countVolunteer
FROM task_activity a
JOIN task t
ON a.Task_id = t.Task_id
GROUP BY a.Activity_desc,t.Task_id
Related
Is it possible to rewrite the following query
SELECT CT.GROUP, CT.EMP_ID, HT.EFF_DT
FROM CURR_TABLE CT
JOIN (SELECT GROUP, EMP_ID, MAX(EFF_DT) AS EFF_DT
FROM HIST_TABLE
WHERE STAT = 'A'
GROUP BY GROUP, EMP_ID) HT ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID
WHERE CT.GROUP = :1
AND CT.EMP_ID = :2
in a way that is similar to CROSS JOIN style?
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]
The reason is that I want to create such query in Peoplesoft, and the above can only be achieved by creating a separate view for the selection with the group by clause. I want to do this just in one query without creating additional views.
You may try writing your query as a single level join with an aggregation:
SELECT
CT.GROUP,
CT.EMP_ID,
MAX(HT.EFF_DT) AS EFF_DT
FROM CURR_TABLE CT
LEFT JOIN HIST_TABLE HT
ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID AND
HT.STAT = 'A'
WHERE
CT.GROUP = :1 AND
CT.EMP_ID = :2
GROUP BY
CT.GROUP,
CT.EMP_ID;
Note that GROUP is a reserved SQL keyword, and you might have to escape it with double quotes to make this query (or the one in your question) work on Oracle.
Why can't I use a subquery factoring clause result in the where clause of as depicted in the following sql:
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = rpt.id
and
tg.gene not in('TMB','MS')
The subquery is named rptand used in the select statement's where clause. When executed encountering the following error: ORA-00904: "RPT"."ID": invalid identifier
UPDATE:
In fact nested query for the same thing is also giving me the same issue. The nested subquery is only returning a single column value from a single row:
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = (select id from reports where caseid = :case_id and
rownum=1 order by created desc)
and
tg.gene not in('TMB','MS')
You missed to add the table rpt in your query, thus that error.
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
join
rpt on rt.reportid = rpt.id
where
tg.gene not in('TMB','MS')
i need to calculate percentage grosssales with regionwise breakup on a particular segment ie Domestic Corp .
My Query is
select REGION_NAME,
round(ratio_to_report(nvl(sum(gross_sales_amt_t),0)) over ()*100) as Gr_Sales_domcorp
from fact_mfdex_segment A
JOIN dim_location B
ON a.branch_code = b.branch_code
WHERE to_date(DATE_SK,'YYYYMMDD') between '01-feb-2016' and '01-mar-2016'
AND week_flag='Y'
AND a.segments = 'Domestic Corp.'
group by REGION_NAME;
I need to repeat the same query for HNI,RETAIL ETC. instead of Domestic Corp. rather than writing different query can i use my first query to add other segments. I have tried case statement but not working. Any help appreciated
Just include all the records for required segments by putting them inside "IN" clause and then group by segment also and here you go for expected results.
select REGION_NAME,
round(ratio_to_report(nvl(sum(gross_sales_amt_t),0)) over ()*100) as Gr_Sales_domcorp
from fact_mfdex_segment A
JOIN dim_location B
ON a.branch_code = b.branch_code
WHERE to_date(DATE_SK,'YYYYMMDD') between '01-feb-2016' and '01-mar-2016'
AND week_flag='Y'
AND a.segments IN ( 'Domestic Corp.','HNI','RETAIL')
group by REGION_NAME, a.segments;
Add a.segments to the selected columns, remove the "AND a.segments = ..." line, partition ratio_to_report by a.segments, and group by a.segments and REGION_NAME. You may want to change the output column name from Gr_Sales_domcorp to Gr_Sales. Something like this:
select REGION_NAME, a.segments,
round(ratio_to_report(nvl(sum(gross_sales_amt_t),0))
over (partition by a.segments)*100) as Gr_Sales
from fact_mfdex_segment A
JOIN dim_location B
ON a.branch_code = b.branch_code
WHERE to_date(DATE_SK,'YYYYMMDD') between '01-feb-2016' and '01-mar-2016'
AND week_flag='Y'
group by a.segments, REGION_NAME
order by a.segments, REGION_NAME
I am working in SSRS querying against an oracle database.
So I have a data source and this report is supposed to find duplicate Work Orders based on multiple open workorders on a unique unitid. So I only want to show groups that have more then one entry as they are grouped by unitid.
SELECT
COMPSTSB.UNITID,
COMPSTSB.UNITTYPE,
ACTDEFN.ACTDESC,
ACTDEFN.ACTCODE,
HISTORY.WONO,
HISTORY.COMPFLAG,
HISTORY.ADDDTTM,
HISTORY.COMMENTS
FROM (IMSV7.COMPSTSB COMPSTSB INNER JOIN IMSV7.HISTORY HISTORY ON
COMPSTSB.COMPKEY=HISTORY.COMPKEY) INNER JOIN IMSV7.ACTDEFN ACTDEFN ON
HISTORY.ACTKEY=ACTDEFN.ACTKEY
WHERE HISTORY.COMPFLAG='Y' AND NOT (ACTDEFN.ACTCODE='DBR' OR ACTDEFN.ACTCODE='IN')
ORDER BY COMPSTSB.UNITTYPE, COMPSTSB.UNITID, HISTORY.ADDDTTM
Can anyone point me in the right direction? And yes before someone says this has been asked a million times, I did search. Point me to the million times and I will see if I feel they match my question. Ideally in the SQL I could make new column that returned a count of the unitid and I did attempt this but failed, and then I could filter on that column to remove any that only had one count. I really don't think this should be difficult but I have spent about 4 hours on it so far.
Thanks in advance!
Steven
If I understood your question right, the following should give you what's needed :
SELECT * FROM
(
SELECT
COMPSTSB.UNITID,
COMPSTSB.UNITTYPE,
ACTDEFN.ACTDESC,
ACTDEFN.ACTCODE,
HISTORY.WONO,
HISTORY.COMPFLAG,
HISTORY.ADDDTTM,
HISTORY.COMMENTS,
COUNT(1) OVER (PARTITION BY COMPSTSB.UNITID) AS numDups
FROM (IMSV7.COMPSTSB COMPSTSB INNER JOIN IMSV7.HISTORY HISTORY ON
COMPSTSB.COMPKEY=HISTORY.COMPKEY) INNER JOIN IMSV7.ACTDEFN ACTDEFN ON
HISTORY.ACTKEY=ACTDEFN.ACTKEY
WHERE HISTORY.COMPFLAG='Y' AND NOT (ACTDEFN.ACTCODE='DBR' OR ACTDEFN.ACTCODE='IN')
)a
WHERE a.numDups >1
ORDER BY COMPSTSB.UNITTYPE, COMPSTSB.UNITID, HISTORY.ADDDTTM
I expect you are concerned about having duplicate values of UNITID in the IMSV7.COMPSTSB table. If so adding this join to your query should enable ou to identify them:
JOIN (SELECT COMPSTSB.UNITID
FROM IMSV7.COMPSTSB
GROUP BY COMPSTSB.UNITID
HAVING COUNT(COMPSTSB.UNITID) > 1) dups
ON DUPS.UNITID = COMPSTSB.UNITID
JOIN IMSV7.HISTORY HISTORY
Here's the full query:
SELECT COMPSTSB.UNITID
, COMPSTSB.UNITTYPE
, ACTDEFN.ACTDESC
, ACTDEFN.ACTCODE
, HISTORY.WONO
, HISTORY.COMPFLAG
, HISTORY.ADDDTTM
, HISTORY.COMMENTS
FROM IMSV7.COMPSTSB COMPSTSB
JOIN (SELECT COMPSTSB.UNITID
FROM IMSV7.COMPSTSB
GROUP BY COMPSTSB.UNITID
HAVING COUNT(COMPSTSB.UNITID) > 1) dups
ON DUPS.UNITID = COMPSTSB.UNITID
JOIN IMSV7.HISTORY HISTORY
ON COMPSTSB.COMPKEY = HISTORY.COMPKEY
JOIN IMSV7.ACTDEFN ACTDEFN
ON HISTORY.ACTKEY = ACTDEFN.ACTKEY
WHERE HISTORY.COMPFLAG = 'Y'
AND ACTDEFN.ACTCODE NOT IN ('DBR','IN')
ORDER BY COMPSTSB.UNITTYPE
, COMPSTSB.UNITID
, HISTORY.ADDDTTM;
Since the above query didn't work you can try outer joins to similar sub queries on each of your tables and limit to only records where the outer joined table returns data. This will show you which tables in your query are cuasing your extra rows.:
SELECT COMPSTSB.UNITID
, COMPSTSB.UNITTYPE
, ACTDEFN.ACTDESC
, ACTDEFN.ACTCODE
, HISTORY.WONO
, HISTORY.COMPFLAG
, HISTORY.ADDDTTM
, HISTORY.COMMENTS
, DUPS.UNITID UNITID_DUP
, DUPS2.COMPKEY COMPKEY_DUP
, DUPS3.ACTKEY ACTKEY_DUP
FROM IMSV7.COMPSTSB COMPSTSB
JOIN IMSV7.HISTORY HISTORY
ON COMPSTSB.COMPKEY = HISTORY.COMPKEY
JOIN IMSV7.ACTDEFN ACTDEFN
ON HISTORY.ACTKEY = ACTDEFN.ACTKEY
LEFT JOIN (SELECT COMPSTSB.UNITID
FROM IMSV7.COMPSTSB
GROUP BY COMPSTSB.UNITID
HAVING COUNT(COMPSTSB.UNITID) > 1) dups
ON DUPS.UNITID = COMPSTSB.UNITID
LEFT JOIN (SELECT HISTORY.COMPKEY
FROM IMSV7.HISTORY
GROUP BY HISTORY.COMPKEY
HAVING COUNT(HISTORY.COMPKEY) > 1) dups2
ON DUPS.UNITID = COMPSTSB.UNITID
LEFT JOIN (SELECT ACTDEFN.ACTKEY
FROM IMSV7.ACTDEFN
GROUP BY ACTDEFN.ACTKEY
HAVING COUNT(ACTDEFN.ACTKEY) > 1) dups3
ON DUPS.UNITID = COMPSTSB.UNITID
WHERE HISTORY.COMPFLAG = 'Y'
AND ACTDEFN.ACTCODE NOT IN ('DBR','IN')
AND ( DUPS.UNITID IS NOT NULL OR
DUPS2.COMPKEY IS NOT NULL OR
DUPS3.ACTKEY IS NOT NULL)
ORDER BY COMPSTSB.UNITTYPE
, COMPSTSB.UNITID
, HISTORY.ADDDTTM;
I need to write a dynamic query inside In clause of pivot query in oracle 11g. With Pivot xml, it is possible, but I do not need the xml one. Here is the code snippet.
WITH pivot_data AS (
select cu.id, u.topic, cu.first_name, cu.last_name, cu.email,
trunc(cu.REGISTRATION_DATE) Register_Date,
trunc(min(u.view_date)) First_Visit,
trunc(max(u.view_date)) Last_Visit,
nvl(sum(u.user_visits),0) Visits,
nvl(sum(u.time_in_topic),0) Time_in_Topic,
ffl.label label, ffv.field_value val
from ACTIVE_USER_VIEWS_BY_TOPIC u
LEFT join cl_user cu
on u.user_id=cu.id
LEFT JOIN CL_PROFILE_FIELD_LABEL ffl
on ffl.cl_customer_accounts_id=cu.cl_customer_accounts_id
LEFT JOIN CL_PROFILE_FIELD_VALUE ffv
on ffl.id = ffv.profile_field_label_id and ffv.user_id= cu.id
LEFT JOIN CL_PROFILE_FIELD_ASSIGNMENT ffa
on ffl.id = ffa.profile_field_label_id
where ffl.cl_customer_accounts_id = (
select cl_customer_accounts_id
from CL_USER where ID = cu.id)
and ffl.ENABLED = 'Y'
and u.PURCHASED_PRODUCT_ID = 582002861
and REGEXP_LIKE (u.topic,
'difficult_interactions|customer_focus|leading_people' )
group by cu.id, u.topic, cu.first_name, cu.last_name, cu.email,
cu.REGISTRATION_DATE, trunc(u.view_date,'MONTH'), ffl.label,
ffv.field_value
)
SELECT *
FROM pivot_data
PIVOT (
max(val)
FOR label
IN ('Flexfield1' AS Flexfield1, 'Flexfield2' AS Flexfield2,
'Flexfield3' AS Flexfield3, 'Flexfield4' AS Flexfield4,
'Flexfield5' AS Flexfield5, 'Flexfield6' AS Flexfield6,
'flexfield021' AS flexfield021, 'sdcs' AS sdcs)
)
I have the query for dynamic data creation i.e.
SELECT DISTINCT
LISTAGG('''' || label || ''' AS ' || label,',')
WITHIN GROUP (ORDER BY label) AS temp_in_statement
FROM (
select distinct label
from cl_profile_field_label
where cl_customer_accounts_id=(
select cl_customer_accounts_id
from cl_purchased_product
where id=582002861));
But if I put the dynamic query inside pivot IN clause, I am getting the error as ORA-00936: missing expression. Please help me out.