I want to create sub query in stored proc in oracle the following query giving error too many values - oracle11g

I want to add this query in stored procedure but it gives an error: ORA-00913: too many values
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
( SELECT COUNT(CASE WHEN SRM_DISCON_STATUS_ID = 3 THEN 1 END) AS ACTIVE_CONNECTIONS,
COUNT(CASE WHEN SRM_DISCON_STATUS_ID = 2 THEN 1 END) AS DISC_CONNECTIONS
from Connection_master)
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;

The problem is that you are selecting multiple values in the sub-query. You can only select a single value in a sub-query in the SELECT clause.
However, you do not need the sub-query and appear to want:
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
COUNT(CASE SRM_DISCON_STATUS_ID WHEN 3 THEN 1 END) AS ACTIVE_CONNECTIONS,
COUNT(CASE SRM_DISCON_STATUS_ID WHEN 2 THEN 1 END) AS DISC_CONNECTIONS
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;
If you did want the sub-queries then:
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
( SELECT COUNT(CASE SRM_DISCON_STATUS_ID WHEN 3 THEN 1 END)
from Connection_master) AS ACTIVE_CONNECTIONS,
( SELECT COUNT(CASE SRM_DISCON_STATUS_ID WHEN 2 THEN 1 END)
from Connection_master) AS DISC_CONNECTIONS
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;

Related

HANA: Want to include offices that is having 0 value also

I have 4 offices in the table OFFICE_DETAILS which are a,b,c,d.
The below query outputs offices which are having any lab and rad count, so it is including all offices except for c
office_name lab_count rad_count
a 5 0
b 1 2
d 3 1
I want the output to be as follows:
office_name lab_count rad_count
a 5 0
b 1 2
c 0 0
d 3 1
what is the change required in the following code
SELECT d.OFFICE_name AS "OFFICE_NAME" ,
count(CASE
WHEN c.LAB_TYPE LIKE 'L' THEN 1
END) AS "LAB_TEST_COUNT",
count(CASE
WHEN c.LAB_TYPE in ('X','O') THEN 1
END) AS "RAD_TEST_COUNT"
FROM
DOCTOR_CONSULT a
INNER JOIN consult_labtest b
on(a.CONSULT_ID=b.CONSULT_ID)
INNER JOIN test_setup c
on(b.LABTEST_ID=c.TEST_ID)
INNER JOIN OFFICE_DETAILS d
on(a.OFFICE_ID=D.OFFICE_ID)
INNER JOIN USER_SETUP e
on(a.DOCTORS_ID = e.USER_ID)
INNER JOIN DEPARTMENT_SETUP f
ON(a.DEPARTMENT_ID = f.DEPARTMENT_ID)
INNER JOIN TEST_CATEGORY g
ON (c.CATEGORY_ID=g.CATEGORY_ID)
WHERE
c.LAB_TYPE IN ('L','X','O') --'L'-> Laboratory, 'X'-> Radiology
AND c.ACTIVE_STATUS='Y'
AND d.ACTIVE_STATUS='Y'
AND g.ACTIVE_STATUS='Y'
AND
a.CONSULT_DATE BETWEEN CURRENT_DATE AND CURRENT_DATE
AND
d.ACTIVE_STATUS='Y'
AND
a.EMPLOYEE_ID NOT IN ('NEW RECRUITMENT 380', '0000', 'army', 'undefined')
GROUP BY d.OFFICE_NAME
ORDER BY d.OFFICE_NAME ASC;
To retrieve result rows from joined tables that do not have matching rows (e.g. no matching entries in OFFICE_DETAILS table) SQL provides OUTER JOINS.
Join the details tables as outer joins and handle the resulting NULLs in the projection.
This is the most common approach to address this requirement.
Alternatively, the details tables could contain a special “no match” record that is used to match in an OR branch of the join condition. Some data warehouses do this to avoid NULLs.
I did by using LEFT JOIN. I am getting the required output
CREATE VIEW ECLINIC_KNG.VIEW_LABRADTESTS_OFFICE_COUNT_TODAY AS
SELECT ROW_NUMBER() OVER() AS row_num,
v1.OFFICE_NAME,
v1.LAB_TEST_COUNT,
v1.RAD_TEST_COUNT
FROM
(
SELECT od.OFFICE_NAME as "OFFICE_NAME",
(CASE WHEN v.LAB_TEST_COUNT IS NULL THEN 0 ELSE v.LAB_TEST_COUNT END) AS
"LAB_TEST_COUNT",
(CASE WHEN v.RAD_TEST_COUNT IS NULL THEN 0 ELSE v.RAD_TEST_COUNT END) AS
"RAD_TEST_COUNT"
FROM
OFFICE_DETAILS od
LEFT JOIN
(
SELECT d.OFFICE_NAME ,
count(CASE WHEN c.LAB_TYPE LIKE 'L' THEN 1 END) AS "LAB_TEST_COUNT",
count(CASE WHEN c.LAB_TYPE in ('X','O') THEN 1 END) AS "RAD_TEST_COUNT"
FROM
ECLINIC_KNG.DOCTOR_CONSULT a
INNER JOIN ECLINIC_KNG.consult_labtest b
on(a.CONSULT_ID=b.CONSULT_ID)
INNER JOIN ECLINIC_KNG.test_setup c
on(b.LABTEST_ID=c.TEST_ID)
INNER JOIN ECLINIC_KNG.OFFICE_DETAILS d
on(a.OFFICE_ID=D.OFFICE_ID)
INNER JOIN ECLINIC_KNG.USER_SETUP e
on(a.DOCTORS_ID = e.USER_ID)
INNER JOIN ECLINIC_KNG.DEPARTMENT_SETUP f
ON(a.DEPARTMENT_ID = f.DEPARTMENT_ID)
INNER JOIN ECLINIC_KNG.TEST_CATEGORY g
ON (c.CATEGORY_ID=g.CATEGORY_ID)
WHERE
c.LAB_TYPE IN ('L','X','O') --'L'-> Laboratory, 'X'-> Radiology
AND c.ACTIVE_STATUS='Y'
AND d.ACTIVE_STATUS='Y'
AND g.ACTIVE_STATUS='Y'
AND a.CONSULT_DATE BETWEEN CURRENT_DATE AND CURRENT_DATE
AND d.ACTIVE_STATUS='Y'
AND a.EMPLOYEE_ID NOT IN ('NEW RECRUITMENT 380', '0000', 'army', 'undefined')
GROUP BY d.OFFICE_NAME
ORDER BY d.OFFICE_NAME ASC
)AS v
on(od.OFFICE_NAME=v.OFFICE_NAME)
WHERE od.ACTIVE_STATUS='Y'
ORDER BY od.OFFICE_NAME
)AS v1

derive 2 fields SPEED_UPGRADE and TV_PACKAGE from 3 tables

I need to derive 2 fields SPEED_UPGRADE and TV_PACKAGE from 3 tables. I have written code below. I am not getting correct count of 'SPEED' and 'TV' which are falling between start_dt and END_DT. rather it is giving me count of all 'SPEED' and 'TV'. Kindly optimise my query .
select <few columns>,
count (case WHEN Cast('2021-11-09 00:00:00' AS TIMESTAMP(0))
BETWEEN a.START_DT AND Coalesce(a.END_DT,Cast('9999-12-31 00:00:00' AS TIMESTAMP FORMAT 'Y4-MM-DDBHH:MI:SS')) AND b.VOUCHER_TYPE_CD='SPEED'
then 1
ELSE 0
end) SPEED_UPGRADE,
count(case WHEN Cast('2021-11-09 00:00:00' AS TIMESTAMP(0))
BETWEEN a.START_DT AND Coalesce(a.END_DT,Cast('9999-12-31 00:00:00' AS TIMESTAMP FORMAT 'Y4-MM-DDBHH:MI:SS')) AND b.VOUCHER_TYPE_CD='TV'
then 1
ELSE 0
end) TV_PACKAGE
FROM
(sel * from P0_view.edw_v_fct_subscriber_household_base where subscriber_status_cd ='Active' and billed_customer_id ='-1' and household_id >0 and household_base_dt='2021-11-09' ) f
right join P0_VIEW.EDW_V_FCT_FIXED_VOUCHER_REDEEMED a on f.CUSTOMER_ID=a.CUSTOMER_ID
left join P0_VIEW.EDW_V_DIM_FIXED_VOUCHER b on a.FIXED_VOUCHER_ID = b.FIXED_VOUCHER_ID
group by 1,2,3,4,5,6,7,8,9 ) q ;

update single record with results from multiple case query

Found related type solutions, but nothing to match my need;
I want to update an existing table record (fields pu_cnt, pu_ot, pu_ltc etc.) with the result from the query (pu_cnt, pu_ot, pu_ltc etc.) etc.
Here's my query:
SELECT
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('C','D','H','F') THEN 1 END) AS
pu_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE '' THEN 1 END) AS
pu_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'C' THEN 1 END)
AS pu_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'U' THEN 1 END)
AS pu_ltu,
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('D','H','F') THEN 1 END) AS del_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE '' THEN 1 END) AS
del_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'C' THEN 1 END)
AS del_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'U' THEN 1 END)
AS del_ltu
FROM vuWO_CHC_ALL WHERE
RTRIM(vuWO_CHC_ALL.SHIPPER) LIKE #pVendor AND
RTRIM(vuWO_CHC_ALL.ORIG) LIKE #pOrigin AND
RTRIM(vuWO_CHC_ALL.Dest) LIKE #pDestin
..
Figured it out using #variables.

Is there any way to accomplish this in IBM DB2 enviroment

In DB2 is there a way to basically say:
case when sku (select * from table1 where tb1field = 'SMOMD') then 'True' end
Okay so this is my query so far, I've been going at this for at least a month now so any help would be great.
select tb4.customer, tb4.sku, tb4.qty, tb4.retqty, tb4.stipqty, tb4.lastdate, tb4.firstdate, tb4.stipdate
from(
--Table 4
select tb3.Customer as Customer, tb3.sku as SKU, tb3.qty as Qty, tb3.retqty as RetQty, tb3.stipqty as STIPQty,
case when tb3.lastdate is null then '00/0000' else substr(tb3.lastdate,5,2)||'/'||substr(tb3.lastdate,1,4) end as LastDate,
case when tb3.firstdate is null then '00/0000' else substr(tb3.firstdate,5,2)||'/'||substr(tb3.firstdate,1,4) end as FirstDate,
case when tb3.stipdate is null then '00/0000' else substr(tb3.stipdate,5,2)||'/'||substr(tb3.stipdate,1,4) end as STIPDate
from(
--Table 3
select tb2.Customer as Customer, tb2.SKU as SKU, tb2.Qty as Qty, tb2.RetQty as RetQty, tb2.STIPQty as STIPQty,
max(case when tb2.TranID in ('010','100') then tb2.datenum end) as LastDate,
min(case when tb2.TranID in ('010','100') then tb2.datenum end) as FirstDate,
case when tb2.RC = '4M' then tb2.datenum end as STIPDate
from(
--Table 2
select tb1.Customer as Customer, tb1.SKU as SKU,
sum(case when tb1.TranID in ('010','100') then abs(tb1.OrdNet) else '0' end) as Qty,
sum(case when tb1.TranID = '500' and tb1.rc != '4M' then abs(tb1.OrdNet) else '0' end) as RetQty,
count(case when tb1.rc = '4M' then tb1.sku end) as STIPQty,
tb1.datenum as datenum, tb1.TranID as tranid, tb1.RC as rc
from(
--Table 1
select distinct stkund as Customer, sthptg||space(1)||stmodl||space(1)||stvari||space(1)||stfarb||space(1)||stgroe as SKU,
stvorg as TranID, stggru as RC, stprg09 as PG9, stprg08 as PG8, stperi as datenum, ormne1 as OrdNet
from st_usus.s_stati_pv
join caspdtau.cospf440 on stadrn = jadr40
where trim(stvert) in ('111S','122S')
and sthptg != 'V'
and aktv40 = 'A'
and stprg01 in ('01','04')
and stprg02 = '01'
and stvorg in ('500','010','100')
and stperi >= '20160100'
) as tb1
group by tb1.Customer, tb1.SKU, tb1.datenum, tb1.tranid, tb1.rc
) as tb2
group by tb2.customer, tb2.sku, tb2.qty, tb2.retqty, tb2.stipqty, tb2.tranid, tb2.rc, tb2.datenum
) as tb3
group by tb3.customer, tb3.sku, tb3.qty, tb3.retqty, tb3.stipqty, tb3.lastdate, tb3.firstdate, tb3.stipdate
) as tb4
order by tb4.Customer, tb4.sku
I'm not going to try to decipher exactly what you're trying to do...
Some general advice, rather than using Nested Table Expressions (NTE)
select <..> from (select <...>from mytable)
Consider Common Table Expressions (CTE)
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
, table2 as (select <...> from table1)
, table3 as (select <...> from table2)
, table4 as (select <....> from table3)
select <...> from table4;
Each CTE (ie. tableX) can refer to a prior CTE or a physical table/view as needed. The final select can refer to one or more CTE's along with one or more physical tables or views.
Nice thing about building with CTE's, is that you can check your results after each step..
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
select * from table1;

using vb6 and oracle

I have the records using the query
SELECT trunc(createdon,'hh') CREATEDON,count(*)
FROM WHERE LABSTATUS=1 AND
CREATEDON >=TO_DATE('01/07/2010 10','DD/MM/YYYY hh')
GROUP BY trunc(createdon,'hh')
in an hourly basis.. I need to place the count value in corresponding time column in the grid.
How can I do?? any idea
You can do it like this:
Hours are in columns
SELECT TRUNC(createdon,'hh'), SUM(CASE WHEN hh=1 THEN 1 ELSE 0) h1,
SUM(CASE WHEN hh=2 THEN 1 ELSE 0) h2 .....
FROM TABLle1,
(SELECT 1 AS hh FROM dual
UNION
SELECT 2 AS hh FROM dual
UNION
SELECT 3 AS hh FROM dual
....
) hours
WHERE LABSTATUS=1 AND CREATEDON >=TO_DATE('01/07/2010 10','DD/MM/YYYY hh') AND
TRUNC(createdon,'hh')= hours.hh
GROUP BY TRUNC(createdon,'hh')
Hours are in rows:
SELECT hours.hh, SUM(CASE WHEN TRUNC(createdon,'hh')= hours.hh THEN 1 ELSE 0 END) hh,
FROM TABLle1,
(SELECT 1 AS hh FROM dual
UNION
SELECT 2 AS hh FROM dual
UNION
SELECT 3 AS hh FROM dual
....
) hours
WHERE LABSTATUS=1 AND CREATEDON >=TO_DATE('01/07/2010 10','DD/MM/YYYY hh')
GROUP BY hours.hh
This query will result like this
row 1 12
row 2 0
row 3 4
...

Resources