How to join output of two queries - visual-studio-2010

Good morning.
I need help.
I have this code:
select A.AREA_CODE, A.OUTLET_NAME, SUM(B.AMOUNT) as NETSALES
from M_OUTLET A, T_SALES_DETAIL B
where A.OUTLET_NO = B.OUTLET_NO and A.OUTLET_TYPE_DESC not
like '%head%' and A.OUTLET_TYPE_DESC not like '%prod%' and
A.OUTLET_TYPE_DESC not like '%stor%'
and B.SYSTEM_DATE between CONVERT (datetime, '3/1/2014')
and CONVERT (datetime, '3/31/2014')
and B.VOID = 'N' group by A.AREA_CODE , A.OUTLET_NAME order by A.AREA_CODE
and this is the output:
AREA_CODE OUTLET_NAME NETSALES
1 MAKATI BU CAFE 2 226202.52
2 MAKATI BU CART 170305.01
The other code is this:
SELECT A.AREA_CODE, SUM (C.AMOUNT) AS E_SALES
FROM E_SALES_DETAIL C , M_OUTLET A
WHERE C.SYSTEM_DATE
between CONVERT (datetime, '3/1/2014') and
CONVERT (datetime, '3/31/2014') and C.VOID = 'N'
GROUP BY A.AREA_CODE ORDER BY A.AREA_CODE
and the output is:
AREA_CODE E_SALES
1 22208347.35
2 14453051.45
My question is, how can I join this codes to get this output:
AREA_CODE OUTLET_NAME E_SALES NETSALES
1 MAKATI BU CAFE 2 22208347.35 226202.52
2 MAKATI BU CART 14453051.45 170305.01
Thank you.

The easiest way is to use your two queries as subqueries. That said, I think you are missing a join condition on the second subquery:
select ab.area_code. ab.outlet_name, ab.netsales, ac.e_sales
from (select A.AREA_CODE, A.OUTLET_NAME, SUM(B.AMOUNT) as NETSALES
from M_OUTLET A join
T_SALES_DETAIL B
on A.OUTLET_NO = B.OUTLET_NO
where A.OUTLET_TYPE_DESC not like '%head%' and
A.OUTLET_TYPE_DESC not like '%prod%' and
A.OUTLET_TYPE_DESC not like '%stor%' and
B.SYSTEM_DATE between CONVERT(datetime, '3/1/2014') and CONVERT(datetime, '3/31/2014') and
B.VOID = 'N'
group by A.AREA_CODE, A.OUTLET_NAME
) ab join
(SELECT A.AREA_CODE, SUM(C.AMOUNT) AS E_SALES
FROM E_SALES_DETAIL C join
M_OUTLET A
ON ????
WHERE C.SYSTEM_DATE between CONVERT(datetime, '3/1/2014') and CONVERT(datetime, '3/31/2014') and
C.VOID = 'N'
GROUP BY A.AREA_CODE
) ac
on ab.area_code ac.area_code;

Related

Issue With GROUP BY In Query

First of all, thanks in advance for any help anyone can offer!
I'm fairly new to Oracle, so I apologize if this question is stupid...
I'm having an issue getting a query to run properly, and the issue is with the GROUP BY clause in the query.
When running the query, I get the error stating that it's not a GROUP BY statement?? Not really sure what that means.
Here is the query:
with SUB_GRP_LVL_1 as
(
select sg.HCC_ID,sge.SUB_GROUP_NAME
from XXXX_ODS.GROUP_T g
Join XXXX_ODS.SUB_GROUP sg
on g.GROUP_KEY=sg.GROUP_KEY
and g.HCC_ID NOT IN('A0002','A0003','A0004','A0005','A0021','A0022','A0041','A0042','A0081','A0121','A0061')
and sg.REC_SRC_CD='HS1'
and sg.LEVEL_NBR=1
join XXXX_ODS.SUB_GROUP_EFF sge
on sge.SUB_GROUP_KEY=sg.SUB_GROUP_KEY
---and sge.CURRENT_INDC=1
and sge.ACTIVE_INDC=1
)
,
SUB_GRP_LVL_2 as
(
select sg.HCC_ID,sge.SUB_GROUP_NAME
from XXXX_ODS.GROUP_T g
Join XXXX_ODS.SUB_GROUP sg
on g.GROUP_KEY=sg.GROUP_KEY
and g.HCC_ID NOT IN('A0002','A0003','A0004','A0005','A0021','A0022','A0041','A0042','A0081','A0121','A0061')
and sg.REC_SRC_CD='HS1'
and sg.LEVEL_NBR=2
join XXXX_ODS.SUB_GROUP_EFF sge
on sge.SUB_GROUP_KEY=sg.SUB_GROUP_KEY
and sge.ACTIVE_INDC=1
)
,
GRP_BEN_PL_ID as
(
select sg.SUB_GROUP_KEY,bpe.HCC_BENEFIT_PLAN_ID , sgbp.EFFECTIVE_DT, sgbp.EXPIRATION_DT, pe.FUNDING_TYPE, pe.HCC_PROD_ID, pe.STATE_CD, pe.SUB_COMPANY
from XXXX_ODS.GROUP_T g
Join XXXX_ODS.SUB_GROUP sg
on g.GROUP_KEY=sg.GROUP_KEY
and g.HCC_ID NOT IN('A0002','A0003','A0004','A0005','A0021','A0022','A0041','A0042','A0081','A0121','A0061')
and g.REC_SRC_CD='HS1'
JOIN XXXX_ODS.SUB_GROUP_BENEFIT_PLAN sgbp
on sg.SUB_GROUP_KEY=sgbp.SUB_GROUP_KEY
---and sgbp.CURRENT_INDC=1
and sgbp.ACTIVE_INDC=1
join XXXX_ODS.BENEFIT_PLAN_EFF bpe
on sgbp.BENEFIT_PLAN_KEY = bpe.BENEFIT_PLAN_KEY
and bpe.ACTIVE_INDC=1
join XXXX_ods.product_eff pe
on bpe.PRODUCT_KEY=pe.product_key
and pe.ACTIVE_INDC=1
)
select
BLK_1.SUB_GROUP_KEY,ACCT_NBR AS GROUPID,
SUB_GRP_ID,
DIVISION_ID
,GRP_BEN_PL_ID.HCC_BENEFIT_PLAN_ID
,INVOICE_NBR
,'CMD' BUSTYPE,
GRP_BEN_PL_ID.FUNDING_TYPE AS BUSSUBTYPE,
GRP_BEN_PL_ID.HCC_PROD_ID AS PROCODE,
'' AS PREMIUMTYPE,
INV.CURRENT_INVOICED_AMT AS PREMIUMBILLED,
INV.CURRENT_INVOICED_AMT AS PREMIUMDUEDATE,
'' AS PREMIUMPAID,
'' AS PREMIUMAPPLYDATE,
COUNT(invl.INVOICE_LINE_KEY) AS CONTRACTSXXXXL,
'' AS MEMBERSXXXXL
--ACCOUNT_TYPE
,DECODE (UPPER(GRP_BEN_PL_ID.STATE_CD),'NEBRASKA','NE','IOWA','IA') STATE,
GRP_BEN_PL_ID.SUB_COMPANY LGL_ENTITY,
LEVEL_NBR
from
(
select
g.GROUP_KEY, sg.SUB_GROUP_KEY, g.HCC_ID ACCT_NBR,ge.GROUP_NAME ACCT_NAME,substr(sg.HCC_ID, 1,instr(sg.hcc_id,'-',1,2)-1) SUB_GRP_ID,sg.HCC_ID DIVISION_ID,ce.FIRST_EFFECTIVE_DT,
add_months(
decode(ce.EFFECTIVE_DT,TO_DATE('01/01/1800','MM/DD/YYYY'),ce.FIRST_EFFECTIVE_DT,ce.EFFECTIVE_DT),ce.RENEWAL_INTERVAL) RENVL_DT, ce.ACCOUNT_TYPE
, sg.LEVEL_NBR
from
XXXX_ODS.CUSTOMER_EFF ce
join XXXX_ODS.GROUP_T g
on g.CUSTOMER_KEY=ce.CUSTOMER_KEY
Join XXXX_ODS.SUB_GROUP sg
on g.GROUP_KEY=sg.GROUP_KEY
and g.HCC_ID NOT IN('A0002','A0003','A0004','A0005','A0021','A0022','A0041','A0042','A0081','A0121','A0061')
and g.REC_SRC_CD='HS1'
and sg.LEVEL_NBR=2
and g.HCC_ID not like 'TEST%'
join XXXX_ODS.GROUP_EFF ge
on ge.GROUP_KEY = g.GROUP_KEY
and ge.CURRENT_INDC=1
union
select
gp.GROUP_KEY,
sgp.SUB_GROUP_KEY, gp.HCC_ID ACCT,gep.GROUP_NAME, sgp.HCC_ID SUB_GRP, NULL DIVISION,cep.FIRST_EFFECTIVE_DT,
add_months(decode(cep.EFFECTIVE_DT,TO_DATE('01/01/1800','MM/DD/YYYY'),cep.FIRST_EFFECTIVE_DT,cep.EFFECTIVE_DT),cep.RENEWAL_INTERVAL) RENVL_DT,
cep.ACCOUNT_TYPE, sgp.LEVEL_NBR
from
XXXX_ODS.CUSTOMER_EFF cep
join XXXX_ODS.GROUP_T gp
on gp.CUSTOMER_KEY=cep.CUSTOMER_KEY
Join XXXX_ODS.SUB_GROUP sgp
on gp.GROUP_KEY=sgp.GROUP_KEY
and gp.HCC_ID NOT IN('A0002','A0003','A0004','A0005','A0021','A0022','A0041','A0042','A0081','A0121','A0061')
and gp.REC_SRC_CD='HS1'
and sgp.LEVEL_NBR=1
and gp.HCC_ID not like 'TEST%'
join XXXX_ODS.GROUP_EFF gep
on gep.GROUP_KEY = gp.GROUP_KEY
and gep.CURRENT_INDC=1
and not exists
(
select 'X'
from
XXXX_ODS.GROUP_T gi
Join XXXX_ODS.SUB_GROUP sgi
on gi.GROUP_KEY=sgi.GROUP_KEY
and gi.HCC_ID NOT IN('A0002','A0003','A0004','A0005','A0021','A0022','A0041','A0042','A0081','A0121','A0061')
and gi.REC_SRC_CD='HS1'
and sgi.LEVEL_NBR=2
and gi.HCC_ID not like 'TEST%'
where
gp.HCC_ID=gi.HCC_ID
and sgp.HCC_ID=substr(sgi.HCC_ID, 1,instr(sgi.hcc_id,'-',1,2)-1)
)
) BLK_1
join SUB_GRP_LVL_1
on SUB_GRP_LVL_1.HCC_ID=BLK_1.SUB_GRP_ID
left join SUB_GRP_LVL_2
on SUB_GRP_LVL_2.HCC_ID=BLK_1.DIVISION_ID
left join GRP_BEN_PL_ID
on GRP_BEN_PL_ID.SUB_GROUP_KEY=BLK_1.SUB_GROUP_KEY
LEFT Join XXXX_ODS.INVOICE inv
on inv.BILL_TO_GROUP_KEY = BLK_1.GROUP_KEY
LEFT JOIN XXXX_ODS.INVOICE_BILLING_CATEGORY invbc
on invbc.INVOICE_KEY = inv.INVOICE_KEY
LEFT JOIN XXXX_ODS.INVOICE_LINE invl
on invl.INVOICE_BILLING_CATEGORY_KEY = invbc.INVOICE_BILLING_CATEGORY_KEY
GROUP BY
GRP_BEN_PL_ID.SUB_GROUP_KEY,
ACCT_NBR, --AS GROUPID,
SUB_GRP_ID,
DIVISION_ID
,GRP_BEN_PL_ID.HCC_BENEFIT_PLAN_ID
,INVOICE_NBR
,BUSTYPE
,GRP_BEN_PL_ID.FUNDING_TYPE,
,GRP_BEN_PL_ID.HCC_PROD_ID --AS PROCODE,
,PREMIUMTYPE
,INV.CURRENT_INVOICED_AMT
PREMIUMBILLED,
INV.CURRENT_INVOICED_AMT --AS PREMIUMDUEDATE,
,PREMIUMPAID
,PREMIUMAPPLYDATE
,MEMBERSXXXXL
ACCOUNT_TYPE
,STATE_CD,
GRP_BEN_PL_ID.SUB_COMPANY,
,LEVEL_NBR
order by ACCT_NBR,SUB_GRP_ID,LEVEL_NBR,DIVISION_ID
;
Thanks!
NEW ANSWER BASED ON COMMENT
Sorry I should have looked a bit closer I read the statement 'I get the error stating that it's not a GROUP BY statement' saw the count and made an assumption with my original answer.
With the further information provided in the comments I think the issue is actually that you are using the column alias in the group by.
When grouping you actually have to use the field names not the alias, if you have a static value (in this case '' AS MEMBERSXXXXL) you do not need to group by it SO
incorrect select using alias in the group by
select column1, column2 || column 3 as test2, '' as test, count(1) as cnt
from table
group by column1, test2, test
correct select omitting static value and putting full field in the group by
select column1, column2 || column 3 as test2, '' as test, count(1) as cnt
from table
group by column1, column2 || column 3
ORIGINAL ANSWER BELOW
where you have a count, min, max, sum ect within your select statement you need to tell it how to group the other columns....
you have
COUNT(invl.INVOICE_LINE_KEY) AS CONTRACTSXXXXL,
This means you will need a group by clause on your select.
Good resource for basics of this is
https://www.oracletutorial.com/oracle-basics/oracle-group-by/

Only want to return rows that have duplicate unitid within group by unitid

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;

Oracle SUM function not working in Select clause

I have the attached code below in which the sum(slice) column is giving me same results even if I don't use the SUM function. Can somebody help me fix the code?
Also need the following:
Currently, it is giving output as:
Task Name Resource ID Slice Date Slice Hours
abc 123 4/1 8.50
abc 123 4/2 7.50
abc 123 4/3 8.50
I need output as:
Task Name Resource ID Slice Date Slice Hours
abc 123 4/1 – 4/30 <Total for the month>
xyz 123 4/1 – 4/30 <Total for the month>
select distinct PCF.project_code as project_id,
PCF.project_name as project_name,
RCF.resource_id,
RCF.last_name || ' ' || RCF.first_name as resource_name,
Tsh.slice_date as fact_date,
sum(tsh.slice) "Total Slice",
T.prname as TaskName,
task_info.phase_code as phase_code,
task_info.phase_name as phase_name,
task_info.task_sequence as task_outline
from prj_blb_slicerequests tsr
JOIN prj_blb_slices tsh ON tsr.id = tsh.slice_request_id
JOIN prtimeentry TE ON tsh.prj_object_id = te.prid
JOIN prtimesheet TS ON TS.prid = TE.prtimesheetid
JOIN prAssignment A ON TE.prassignmentid = A.prid
JOIN prtask T ON A.prtaskid = T.prid
JOIN nbi_resource_current_facts RCF ON TS.prresourceid = RCF.resource_id
JOIN nbi_project_current_facts PCF ON T.prprojectid = PCF.project_id
JOIN prj_projects PP ON PCF.project_id = PP.prid
JOIN (select task_id, phase_code, phase_name, task_name, task_sequence from cust_phase_rollup_v) task_info ON T.prid = task_info.task_id
where tsr.request_name = 'Daily Timeentry Actuals'
and tsh.slice > 0
and ts.prstatus < 5
and tsh.slice_date >= to_date('1-MAY-14')
and tsh.slice_date <= to_date('31-MAY-14')
group by project_code, project_name, resource_id, RCF.last_name || ' ' || RCF.first_name, Tsh.slice_date, T.prname, task_info.phase_code,
task_info.phase_name, task_info.task_sequence
order by project_id
2 problems:
You want results per month, not per day, so instead of just selecting the slice_date, you should convert that into month/year format.
To use an aggregate function, you need a corresponding group by in your query. You should group by all of the other columns you're selecting.
This should work (untested, though):
select distinct PCF.project_code as project_id,
PCF.project_name as project_name,
RCF.resource_id,
RCF.last_name || ' ' || RCF.first_name as resource_name,
to_char(Tsh.slice_date, 'mm/yyyy') as fact_date,
sum(tsh.slice) "Total Slice",
T.prname as TaskName,
task_info.phase_code as phase_code,
task_info.phase_name as phase_name,
task_info.task_sequence as task_outline
from prj_blb_slicerequests tsr
JOIN prj_blb_slices tsh ON tsr.id = tsh.slice_request_id
JOIN prtimeentry TE ON tsh.prj_object_id = te.prid
JOIN prtimesheet TS ON TS.prid = TE.prtimesheetid
JOIN prAssignment A ON TE.prassignmentid = A.prid
JOIN prtask T ON A.prtaskid = T.prid
JOIN nbi_resource_current_facts RCF ON TS.prresourceid = RCF.resource_id
JOIN nbi_project_current_facts PCF ON T.prprojectid = PCF.project_id
JOIN prj_projects PP ON PCF.project_id = PP.prid
JOIN (select task_id, phase_code, phase_name, task_name, task_sequence from cust_phase_rollup_v) task_info ON T.prid = task_info.task_id
where tsr.request_name = 'Daily Timeentry Actuals'
and tsh.slice > 0
and ts.prstatus < 5
and tsh.slice_date >= to_date('1-MAY-14')
and tsh.slice_date <= to_date('31-MAY-14')
group by PCF.project_code,
PCF.project_name,
RCF.resource_id,
RCF.last_name || ' ' || RCF.first_name,
to_char(Tsh.slice_date, 'mm/yyyy'),
T.prname,
task_info.phase_code,
task_info.phase_name,
task_info.task_sequence
order by PCF.project_code

Print different sets of data from different tables in one SQR

I have a requirement where in if a PO_ID range is given then I need to do the following:
If a PO_ID from the PO_ID range is present in PS_DISTRIB_LINE then print PO_ID and Voucher ID and if not then print PI_ID and PO_DT from PS_PO_HDR.
How to achieve this.
Union is not working and I am not able to use break logic because if data is present in PS_DISTRIB_LINE then I am printing Sum of PO_AMT Total below the PO_IDs but in other case i am not.
UNION is possible in SQR, but the syntax is difficult.
You will need to put the UNION in parenthesis and make sure to put commas after the selected items. In the example below, I put commas after a.name, a.first_name, and a.last_name. Repeat this for b.name, b.first_name, and b.last_name.
I've run this example and this works for me:
begin-SELECT
employee_name.name
employee_name.first_name
employee_name.last_name
Show 'employee_name.name: ' &employee_name.name
Show 'employee_name.first_name: ' &employee_name.first_name
Show 'employee_name.last_name: ' &employee_name.last_name
from
(
select
a.name,
a.first_name,
a.last_name
from ps_names a
where name_type = 'PRI'
and emplid = '000000001'
union
select
b.name,
b.first_name,
b.last_name
from ps_personal_data b
where emplid = '000000001'
) employee_name
end-SELECT

Counting the total number of rows depending on a column value

I have a query that should count the total number of rows returned depending on a column value. For example:
As you can see, the M field should display the total number of rows returned which should be 5 because the FT_LOT are all the same value. Here is the query that I have so far:
SELECT DISTINCT
VBATCH_ID, MAXIM_PN, BAGNUMBER, FT_LOT
, m
, level as n
FROM
(
SELECT
VBATCH_ID, MAXIM_PN, BAGNUMBER, FT_LOT, QTY, DC, PRINTDATE, WS_GREEN, WS_PNR, WS_PCN, MSL, BAKETIME, EXPTIME
, una
, dulo
, (dulo - una) + 1 AS m
FROM
(
SELECT c.containername VBATCH_ID
,pb.productname MAXIM_PN
,bn.wipdatavalue BAGNUMBER
,ln.wipdatavalue FT_LOT
,aw.wipdatavalue QTY
,DECODE(ln.wipdatavalue,la.attr_081,la.attr_083
,la.attr_085,la.attr_087
,la.attr_089,la.attr_091
,la.attr_093,la.attr_095
,la.attr_097,la.attr_099
,la.attr_101,la.attr_103
,la.attr_105,la.attr_107
,la.attr_109,la.attr_111
,la.attr_113,la.attr_116
,la.attr_117,la.attr_119
) DC
,TO_CHAR(SYSDATE,'MM/DD/YYYY HH:MI:SS PM') PRINTDATE
,DECODE(UPPER(la.attr_158),'GREEN','HF',NULL) WS_GREEN
,DECODE(la.attr_140,NULL,NULL,'PNR') WS_PNR
,DECODE(la.Attr_080,NULL,NULL,'PCN') WS_PCN
,p.attr_011 MSL
,P.attr_013 BAKETIME
,p.attr_014 EXPTIME
, CASE
WHEN INSTR(bn.wipdatavalue, '-') = 0 THEN
bn.wipdatavalue
ELSE
SUBSTR(bn.wipdatavalue, 1, INSTR(bn.wipdatavalue, '-')-1)
END AS una
, CASE
WHEN INSTR(bn.wipdatavalue, '-') = 0 THEN
bn.wipdatavalue
ELSE
SUBSTR(bn.wipdatavalue, INSTR(bn.wipdatavalue, '-') + 1)
END AS dulo
FROM Container C
JOIN a_lotattributes la ON c.lotattributesid = la.lotattributesid
JOIN product p ON c.productid=p.productid
JOIN productbase pb ON p.productbaseid=pb.productbaseid
JOIN a_adhocwipdatarecord a ON a.objectrefid=c.containerid
JOIN a_adhocwipdatarecorddetails bn ON a.adhocwipdatarecordid=bn.adhocwipdatarecordid AND bn.wipdatanamename ='TR_BAG_NUMBER'
LEFT JOIN a_adhocwipdatarecorddetails ln ON a.adhocwipdatarecordid=ln.adhocwipdatarecordid AND ln.wipdatanamename ='TR_FT_LOT NUMBER'
LEFT JOIN a_adhocwipdatarecorddetails aw ON A.adhocwipdatarecordid=aw.adhocwipdatarecordid AND aw.wipdatanamename ='TR_FT LOT QTY'
WHERE ln.wipdatavalue = :ftlot AND bn.wipdatavalue LIKE :wip
)
) WHERE level LIKE :n
CONNECT BY LEVEL <= m
ORDER BY BAGNUMBER
Thanks for helping out guys.
Actually GROUP BY is not the solution. Having looked again at your desired output I have realised that what you want is an analytic count.
Your posted query is a bit of a mess and, sorry ,but I'm not prepared to invest time in it. This is the sort of structure you need:
select vbatch_id, maxim_pn, bagnumber, ft_lot
, count(*) over (partition by ft_lot) m
from whatever ...
Find out more.
Not sure why you need the DISTINCT. DISTINCT almost always indicates a failure to get the WHERE clause right.

Resources