Can anybody please help me with this particular query? I'm having ORA-00979 when trying to run this:
select t0.title, count (1) as count0, (select count (1)
from contract c1, se se1
where c1.c_id = se1.c_id
and se1.svc_id = 3
and se1.deleted = 0
and c1.deleted = 0
and c1.c_date between to_date ('07.10.2000', 'dd.mm.yyyy')
and to_date ('22.11.2010', 'dd.mm.yyyy')
and c1.company = 0
and c1.tdata.tariff = c0.tdata.tariff
) as count1
from contract c0, se se0, tariff t0
where c0.c_id = se0.c_id
and se0.svc_id = 3
and se0.deleted = 0
and c0.deleted = 0
and c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy')
and to_date ('06.01.2011', 'dd.mm.yyyy')
and c0.company = 0
and t0.tariff_id = c0.tdata.tariff
group by t0.title
The problem is your subquery with the select count(1) part. Just because it's got a count in it doesn't actually make it an aggregate. It's still a subquery that will be applied to every row and as you can see it uses the value c0.tdata.tariff which is not part of the group.
Looks like that scalar subquery is causing the problem -- it is neither a group function, nor is it in the GROUP BY list.
Probably you could workaround it with something like:
select t0.title, count (1) as count0, SUM(select count (1) ...) AS count1
...
Considering this seems to be two instances of the same query just over different dates (I might be wrong here...it's been a long day), you could probably just simplify it and rewrite like this:
select
t0.title,
count (case when c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy')
and to_date ('06.01.2011', 'dd.mm.yyyy') then 1 end) as count0,
count (case when c0.c_date between to_date ('07.10.2000', 'dd.mm.yyyy')
and to_date ('22.11.2011', 'dd.mm.yyyy') then 1 end) as count1
from
contract c0,
se se0,
tariff t0
where
c0.c_id = se0.c_id
and se0.svc_id = 3
and se0.deleted = 0
and c0.deleted = 0
and (c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy')
and to_date ('06.01.2011', 'dd.mm.yyyy')
or c0.c_date between to_date ('07.10.2000', 'dd.mm.yyyy')
and to_date ('22.11.2010', 'dd.mm.yyyy'))
and c0.company = 0
and t0.tariff_id = c0.tdata.tariff
group by t0.title
Your group by needs to include all of the non-aggregate columns from your select list. In this case, the group by is missing the count1 returned by your subquery.
Related
I need help with a query where a need to count de consecutive days like this
select
a.numcad, a.datapu , f.datapu , nvl(to_char(f.datapu, 'DD'),0)dia,
row_number() over (partition by a.numcad, f.datapu order by f.datapu)particao
from
ronda.r066apu a
left join (select t.numcad, t.numemp, t.datacc, t.datapu
from ronda.r070acc t
where t.datacc >= '21/01/2022'
and t.datacc <= trunc(sysdate)
group by t.numcad, t.numemp, t.datacc, t.datapu)f
on a.numemp = f.numemp
and a.numcad = f.numcad
and a.datapu = f.datapu
where a.numcad = 2675
and A.DATAPU >= '21/01/2022'
and A.DATAPU <= trunc(sysdate)
group by a.numcad, a.datapu, f.datapu, f.datacc
order by a.datapu
result is
between 24/01/2022 and 04/02/2022
is 12 days i need know this count , but i will ways get the '21/mes/year'
You can try:
SELECT TO_DATE('2022-01-24', 'YYYY-MM-DD') -
TO_DATE('2022-02-04', 'YYYY-MM-DD')
FROM dual
This returns 21, for example...
I wrote query to find sum of money in a given period with one filial and it is working fast:
SELECT FILIAL_CODE,
sum(sum_eqv)/100 AS summa
FROM table
WHERE substr(acc,1,5) = '65434'
and cast(substr(acc,18,3) as integer) >= 600
and cast(substr(account_co,18,3) as integer)<=607
AND o_day >= to_date('01.12.2019', 'DD.MM.YYYY')
and oday < to_date('08.12.2019', 'DD.MM.YYYY')+ INTERVAL '1' DAY
AND FILIAL_CODE = '001234'
Above query is working fine. But when I want to use it multiple filials it is becoming more complex.
Below query is needs to be fixed.
SELECT FILIAL_CODE,
sum(sum_eqv)/100 AS summa
FROM table
WHERE substr(acc,1,5) = '65434'
and cast(substr(acc,18,3) as integer) >= 600
and cast(substr(account_co,18,3) as integer)<=607
AND o_day >= to_date('01.12.2019', 'DD.MM.YYYY')
and oday < to_date('08.12.2019', 'DD.MM.YYYY')+ INTERVAL '1' DAY
AND FILIAL_CODE in (select code from city where region = '26')
group by FILIAL_CODE;
This query runs long. How can I maximize this statement. Any Help is appreciated!
Try JOIN instead of IN, such as:
SELECT t.filial_code, SUM (sum_eqv) / 100 AS summa
FROM your_table t JOIN city c ON c.code = t.filial_code
WHERE SUBSTR (t.acc, 1, 5) = '65434'
AND CAST (SUBSTR (t.acc, 18, 3) AS INTEGER) >= 600
AND CAST (SUBSTR (t.account_co, 18, 3) AS INTEGER) <= 607
AND t.o_day >= TO_DATE ('01.12.2019', 'DD.MM.YYYY')
AND t.oday < TO_DATE ('08.12.2019', 'DD.MM.YYYY') + INTERVAL '1' DAY
AND c.region = '26'
GROUP BY t.filial_code;
It would probably help if city.code and table.filial_code were indexed.
I'm into troubles with this code:
SELECT
s.codcli,
s.consfin consistenza_iniziale,
s.periodo,
ss.consfin consistenza_finale,
ss.periodo,
(SELECT sum(quanti) somma_bonifici_ingresso
FROM mov WHERE tipope IN ('VE1', 'VE5', 'VE9') AND (datope BETWEEN to_date('01-01-2016', 'DD-MM-YYYY') AND to_date('30-07-2017', 'DD-MM-YYYY')) AND mov.codcli = s.codcli
GROUP BY codcli, tipope) as somma_bonifici_ingresso,
(SELECT sum(quanti) somma_bonifici_ingresso
FROM mov WHERE tipope IN ('PR1', 'PR5', 'PR9') AND (datope BETWEEN to_date('01-01-2016', 'DD-MM-YYYY') AND to_date('30-07-2017', 'DD-MM-YYYY')) AND mov.codcli = s.codcli
GROUP BY codcli, tipope) as somma_bonifici_uscita,
(ss.consfin - (somma_bonifici_ingresso - somma_bonifici_uscita) - s.consfin)/s.consfin as variazione
FROM sre s
LEFT JOIN sre ss on s.codcli = ss.codcli
WHERE s.periodo=to_date('01-01-2016', 'DD-MM-YYYY')
AND ss.periodo=to_date('30-06-2017', 'DD-MM-YYYY')
AND s.consfin>0
ORDER BY s.codcli
I'm getting an 00904 error not a valid identifier. I need to insert a new column arranging the columns somma_bonifici_ingresso and somma_bonifici_uscita. I have to do some calculations but it seems like it isn't able to find those columns I guess for the aliases...
You cannot re-use a column alias in the same select, so use a subquery:
SELECT s.*,
(consistenza_iniziale - (somma_bonifici_ingresso - somma_bonifici_uscita) - consistenza_iniziale) / consistenza_iniziale as variazione
FROM (SELECT s.codcli, s.consfin as consistenza_iniziale, s.periodo,
ss.consfin as consistenza_finale, ss.periodo,
(SELECT sum(quanti)
FROM mov
WHERE tipope IN ('VE1', 'VE5', 'VE9') AND
(datope BETWEEN DATE '2016-01-01' AND DATE '2017-07-30') AND
mov.codcli = s.codcli
) as somma_bonifici_ingresso,
(SELECT sum(quanti)
FROM mov
WHERE tipope IN ('PR1', 'PR5', 'PR9') AND
(datope BETWEEN '2016-01-01' AND DATE '2017-07-30') AND
mov.codcli = s.codcli
) as somma_bonifici_uscita,
FROM sre s LEFT JOIN
sre ss
ON s.codcli = ss.codcli
WHERE s.periodo = DATE '2016-01-01' AND
ss.periodo = DATE '2017-07-30' AND
s.consfin > 0
) s
ORDER BY codcli;
Other notes:
Use date constants instead of literals. DATE for dates and TIMESTAMPS if there is a time.
Check your dates. There are differences between the dates in the subqueries and outer WHERE.
The GROUP BY is unnecessary -- and bad -- in the subqueries. You could end up with a "too many rows returned" error.
I have this query
SELECT *
FROM (SELECT mi.visit_id, mi.event_id, mi.patient_id, mi.mrn, mi.reg_date,
mi.d_date, mi.bml_count, mi.TYPE, mblp.baby_patient_id,
mblp.baby_birthdate
FROM ajmid.km0076_motherinfo_test mi LEFT JOIN alfayezb2.mbl_patients mblp
ON mblp.mother_patient_id = mi.patient_id
--works here
AND ( TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date - 1, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date + 1, 'mm/dd/YYYY')
)
) bml
LEFT OUTER JOIN --doesn't work here
(SELECT ROW_NUMBER () OVER (PARTITION BY vis.patient_id ORDER BY vis.admission_date_time)
num,
vis.admission_date_time, vis.visit_id, vis.patient_id,
vis.facility_id
FROM visit vis) v ON bml.baby_patient_id = v.patient_id
WHERE v.num = 1
ORDER BY bml.reg_date
bml by itself returns 118 rows while the whole query returns 117, the reason is bml returns 1 row with baby_patient_id as null, so I used left outer join to show it, but it's still not showing !!
what can I do to show all rows of bml ?
I'm using Toad 9.6
Thank you
check the query:
SELECT ROW_NUMBER () OVER (PARTITION BY vis.patient_id ORDER BY vis.admission_date_time)
num,
vis.admission_date_time, vis.visit_id, vis.patient_id,
vis.facility_id
FROM visit vis
does it return 118 not null patient_id's?
if it returns 117, that might be the reason.(LEFT OUTER JOIN doesnot pick the records which are null on both tables)
Also, are you sure the null value of baby_patient_id in bml table is actually a NULL value and not a empty charater?(' ').
The probable cause is your filter / criteria (where clause) is eliminating a row with a null value for v.num. The WHERE filters the results after the join.
WHERE v.num = 1 -- Are all v.num equal to 1 ?
The mere action of using a criteria against a field, by definition of what NULL means, eliminates that row from consideration because NULL cannot be evaluated. You can say "WHERE id != 1" and expect to get rows where id is null because null != 1 right? Wrong. id != NULL is not defined logically. It is why we say "IS or IS NOT NULL" when dealing with NULL.
it's working finally !
I added
OR bml.baby_patient_id IS NULL
to the where clause, so the final script is
SELECT *
FROM (SELECT mi.visit_id, mi.event_id, mi.patient_id, mi.mrn, mi.reg_date,
mi.d_date, mi.bml_count, mi.TYPE, mblp.baby_patient_id,
mblp.baby_birthdate
FROM ajmid.km0076_motherinfo_test mi LEFT JOIN alfayezb2.mbl_patients mblp
ON mblp.mother_patient_id = mi.patient_id
AND ( TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date - 1, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date + 1, 'mm/dd/YYYY')
)
) bml
LEFT OUTER JOIN
(SELECT ROW_NUMBER () OVER (PARTITION BY vis.patient_id ORDER BY vis.admission_date_time)
num,
vis.admission_date_time, vis.visit_id, vis.patient_id,
vis.facility_id
FROM visit vis) v ON bml.baby_patient_id = v.patient_id
WHERE v.num = 1
OR bml.baby_patient_id IS NULL
ORDER BY bml.reg_date
I don't know how this was helpful, I wish someone would explain for me !
Thanks all
I have a SELECT statement that counts the number of instances and then saves in a variable. It has a HAVING clause that does a SUM and a COUNT. However since you have to have a GROUP BY in order to use having, the select statement returns 4 lines that are 1 instead of the total being 4. This then doesn't save the count into the variable as 4 but as 1 which obviously is not what I need so I am looking for an alternative work around.
select count(distinct p1.community)
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
group By
p1.community
having
sum(p1.status_1) / count(p1.control_code) >= .16;
This is a reasonable alternative:
select count(*)
from (
select p1.community , sum(p1.status_1) / count(p1.control_code) SomeColumn
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
Group By
p1.community
) A
where A.SomeColumn >= .16;