Oracle not returning any data for yesterdays hour - sql

I have the following query:
SELECT d_dtm,
BTS_ID,
CASE WHEN D_DTM = (D_DTM-24/24)
THEN sum(V_ATT_CNT)
END AS "LASTATT",
sum(V_ATT_CNT) as "V_ATT_CNT",
CASE WHEN D_DTM = D_DTM
THEN sum(V_ATT_CNT)
END AS "ATT"
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI
WHERE to_date(D_DTM, 'DD/MM/yyyy') >=
(SELECT TO_DATE(max(D_DTM),'DD/MM/YYYY')
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)-2
GROUP BY d_dtm,
BTS_ID
HAVING CASE WHEN D_DTM = (D_DTM-24/24)
THEN sum(V_ATT_CNT)
END > 0
But it is not returning any results because of the "having" clause. I know it should return results because all I want it to do is in one column have the V_ATT for the current time period, and in the 2nd column, have the V_ATT 24 hours ago. I've checked the data and I should get results back but can't seem to figure out why this is not working...

If you subtotal (group by) D_DTM, you get one row per date. If you want to compare two dates with each other in the same row you need to do one of two things: either a subquery, or a window function like lag/lead.
I think this is what you're looking for:
SELECT d_dtm,
BTS_ID,
lag(sum(V_ATT_CNT), 1) over (partition by bts_id order by d_dtm)
AS "LASTATT",
sum(V_ATT_CNT) as "V_ATT_CNT"
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI
WHERE to_date(D_DTM, 'DD/MM/yyyy') >=
(SELECT TO_DATE(max(D_DTM),'DD/MM/YYYY')
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)-2
GROUP BY d_dtm,
BTS_ID
Note that the partition by columns in the lag/over is a subset of the group by columns.

I re-wrote the query using a with statement and two queries. Works pretty fast and gives me the correct results.
WITH FRST
AS ( SELECT D_DTM,
BSM_NM,
SUM (V_ATT_CNT) AS "V_ATT_CNT",
SUM (V_CUST_BLK_CNT) AS "V_CUST_BLK_CNT",
SUM (V_DRP_CALL_CNT) AS "V_DRP_CALL_CNT",
SUM (D_ATT_CNT) AS "D_ATT_CNT",
SUM (D_CUST_BLK_CNT) AS "D_CUST_BLK_CNT",
SUM (D_DRP_CALL_CNT) AS "D_DRP_CALL_CNT"
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI
WHERE D_DTM >=
(SELECT MAX (D_DTM) - NUMTODSINTERVAL (12, 'HOUR')
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)
GROUP BY D_DTM, BSM_NM),
LST
AS ( SELECT D_DTM,
BSM_NM,
SUM (V_ATT_CNT) AS "V_ATT_CNT",
SUM (V_CUST_BLK_CNT) AS "V_CUST_BLK_CNT",
SUM (V_DRP_CALL_CNT) AS "V_DRP_CALL_CNT",
SUM (D_ATT_CNT) AS "D_ATT_CNT",
SUM (D_CUST_BLK_CNT) AS "D_CUST_BLK_CNT",
SUM (D_DRP_CALL_CNT) AS "D_DRP_CALL_CNT"
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI
WHERE D_DTM >=
(SELECT MAX (D_DTM) - NUMTODSINTERVAL (48, 'HOUR')
FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)
GROUP BY D_DTM, BSM_NM),
EVDOLST
AS ( SELECT D_DTM,
BSM_NM,
SUM (ATT_CNT) AS "ATT_CNT",
SUM (CUST_BLK_CNT) AS "CUST_BLK_CNT",
SUM (DRP_CALL_CNT) AS "DRP_CALL_CNT"
FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI
WHERE D_DTM >=
(SELECT MAX (D_DTM) - NUMTODSINTERVAL (48, 'HOUR')
FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI)
GROUP BY D_DTM, BSM_NM),
EVDOFRST
AS ( SELECT D_DTM,
BSM_NM,
SUM (ATT_CNT) AS "ATT_CNT",
SUM (CUST_BLK_CNT) AS "CUST_BLK_CNT",
SUM (DRP_CALL_CNT) AS "DRP_CALL_CNT"
FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI
WHERE D_DTM >=
(SELECT MAX (D_DTM) - NUMTODSINTERVAL (12, 'HOUR')
FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI)
GROUP BY D_DTM, BSM_NM)
SELECT t1.D_DTM,
t1.BSM_NM,
t2.V_ATT_CNT AS "LASTV_ATTCNT",
t2.V_CUST_BLK_CNT AS "LASTV_BLKCNT",
t2.V_DRP_CALL_CNT AS "LASTV_DRPCNT",
t1.V_ATT_CNT AS "V_ATT_CNT",
t1.V_CUST_BLK_CNT AS "V_CUST_BLK_CNT",
t1.V_DRP_CALL_CNT AS "V_DRP_CALL_CNT",
t2.D_ATT_CNT AS "LASTD_ATTCNT",
t2.D_CUST_BLK_CNT AS "LASTD_BLKCNT",
t2.D_DRP_CALL_CNT AS "LASTD_DRPCNT",
t1.D_ATT_CNT AS "D_ATT_CNT",
t1.D_CUST_BLK_CNT AS "D_CUST_BLK_CNT",
t1.D_DRP_CALL_CNT AS "D_DRP_CALL_CNT",
t3.ATT_CNT AS "EVDO_ATTCNT",
t3.CUST_BLK_CNT AS "EVDO_BLKCNT",
t3.DRP_CALL_CNT AS "EVDO_DRPCNT",
t4.ATT_CNT AS "EVDO_LASTATTCNT",
t4.CUST_BLK_CNT AS "EVDO_LASTBLKCNT",
t4.DRP_CALL_CNT AS "EVDO_LASTDRPCNT"
FROM FRST t1
INNER JOIN LST t2
ON t1.BSM_NM = t2.BSM_NM
AND t1.D_DTM - NUMTODSINTERVAL (24, 'HOUR') = t2.D_DTM
LEFT OUTER JOIN EVDOLST t4
ON t1.BSM_NM = t4.BSM_NM
AND t1.D_DTM - NUMTODSINTERVAL (24, 'HOUR') = t4.D_DTM
LEFT OUTER JOIN EVDOFRST t3
ON t1.BSM_NM = t3.BSM_NM AND t1.D_DTM = t3.D_DTM

Related

Count of consecutive days ORACLE SQL

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...

How to use CASE WHEN in group by

I want to use group by for the table NRW_MONTH_DATA.
SELECT [OBJECT_ID]
,[YEAR_MONTH]
,[SELLING_AMOUNT]
,[DEFAULT_SELLING_DATA]
,[LOCK_SELLING_AMOUNT]
,[RGCB]
,[ICKZ]
,[YCKZ]
FROM [dbo].[NRW_MONTH_DATA]
IF LOCK_SELLING_AMOUNT is 0 then group by OBJECT_ID and calculate the sum of [RGCB],[ICKZ] and [YCKZ]
SELECT #SELLING_AMOUNT=(ISNULL(SUM(YCKZ),0)+ISNULL(SUM(RGCB),0)+ ISNULL(SUM(ICKZ),0))
FROM [dbo].[NRW_MONTH_DATA]
WHERE OBJECT_ID=#OBJECT_ID
AND YEAR_MONTH >=#SELLING_CENSUS_START_YM
AND YEAR_MONTH <=#SELLING_CENSUS_END_YM
GROUP BY OBJECT_ID
Now I want to add a condition that if LOCK_SELLING_AMOUNT is 1 , I need to
SELECT #SELLING_AMOUNT=ISNULL(SUM(DEFAULT_SELLING_DATA),0)
ELSE use original result to calculate the sum of the 3 columns.
I use CASE WHEN but is seems that I could not use it in group by
SELECT #SELLING_AMOUNT=
CASE LOCK_SELLING_AMOUNT WHEN 1 THEN SELLING_AMOUNT
ELSE (ISNULL(SUM(YCKZ),0)+ISNULL(SUM(RGCB),0)+ ISNULL(SUM(ICKZ),0))
END
The error is like
The column'dbo.NRW_MONTH_DATA.LOCK_SELLING_AMOUNT' in the select list is invalid because the column is not included in the aggregate function or GROUP BY clause.
Thank you in advance.
I need the group by to calculate the sum of them. Each row has an object_id and a LOCK_SELLING_AMOUNT and other columns for one month, I want to use group to calculate the sum during month span.
It works well when I do not consider the LOCK_SELLING_AMOUNT
First, you don't want GROUP BY. So just use:
SELECT #SELLING_WATER = (COALESCE(SUM(YCKZ), 0) + COALESCE(SUM(RGCB), 0)+ COALESCE(SUM(ICKZ), 0))
FROM [dbo].[NRW_MONTH_DATA]
WHERE OBJECT_ID=#OBJECT_ID AND
YEAR_MONTH >= #SELLING_CENSUS_START_YM
YEAR_MONTH <= #SELLING_CENSUS_END_YM;
Now, the problem is that a column can change values on different rows. So, what row does LOCK_SELLING_AMOUNT come from? We could assume it is the same on all rows. Or perhaps you want an aggregation function:
SELECT #SELLING_WATER = (CASE WHEN MAX(LOCK_SELLING_AMOUNT) = 1
THEN MAX(CASE WHEN LOCK_SELLING_AMOUNT = 1 THEN SELLING_AMOUNT END)
ELSE (COALESCE(SUM(YCKZ), 0) + COALESCE(SUM(RGCB), 0)+ COALESCE(SUM(ICKZ), 0))
END)
FROM [dbo].[NRW_MONTH_DATA]
WHERE OBJECT_ID=#OBJECT_ID AND
YEAR_MONTH >= #SELLING_CENSUS_START_YM
YEAR_MONTH <= #SELLING_CENSUS_END_YM;

Group by in columns and rows, counts and percentages per day

I have a table that has data like following.
attr |time
----------------|--------------------------
abc |2018-08-06 10:17:25.282546
def |2018-08-06 10:17:25.325676
pqr |2018-08-05 10:17:25.366823
abc |2018-08-06 10:17:25.407941
def |2018-08-05 10:17:25.449249
I want to group them and count by attr column row wise and also create additional columns in to show their counts per day and percentages as shown below.
attr |day1_count| day1_%| day2_count| day2_%
----------------|----------|-------|-----------|-------
abc |2 |66.6% | 0 | 0.0%
def |1 |33.3% | 1 | 50.0%
pqr |0 |0.0% | 1 | 50.0%
I'm able to display one count by using group by but unable to find out how to even seperate them to multiple columns. I tried to generate day1 percentage with
SELECT attr, count(attr), count(attr) / sum(sub.day1_count) * 100 as percentage from (
SELECT attr, count(*) as day1_count FROM my_table WHERE DATEPART(week, time) = DATEPART(day, GETDate()) GROUP BY attr) as sub
GROUP BY attr;
But this also is not giving me correct answer, I'm getting all zeroes for percentage and count as 1. Any help is appreciated. I'm trying to do this in Redshift which follows postgresql syntax.
Let's nail the logic before presenting:
with CTE1 as
(
select attr, DATEPART(day, time) as theday, count(*) as thecount
from MyTable
)
, CTE2 as
(
select theday, sum(thecount) as daytotal
from CTE1
group by theday
)
select t1.attr, t1.theday, t1.thecount, t1.thecount/t2.daytotal as percentofday
from CTE1 t1
inner join CTE2 t2
on t1.theday = t2.theday
From here you can pivot to create a day by day if you feel the need
I am trying to enhance the query #johnHC btw if you needs for 7days then you have to those days in case when
with CTE1 as
(
select attr, time::date as theday, count(*) as thecount
from t group by attr,time::date
)
, CTE2 as
(
select theday, sum(thecount) as daytotal
from CTE1
group by theday
)
,
CTE3 as
(
select t1.attr, EXTRACT(DOW FROM t1.theday) as day_nmbr,t1.theday, t1.thecount, t1.thecount/t2.daytotal as percentofday
from CTE1 t1
inner join CTE2 t2
on t1.theday = t2.theday
)
select CTE3.attr,
max(case when day_nmbr=0 then CTE3.thecount end) as day1Cnt,
max(case when day_nmbr=0 then percentofday end) as day1,
max(case when day_nmbr=1 then CTE3.thecount end) as day2Cnt,
max( case when day_nmbr=1 then percentofday end) day2
from CTE3 group by CTE3.attr
http://sqlfiddle.com/#!17/54ace/20
In case that you have only 2 days:
http://sqlfiddle.com/#!17/3bdad/3 (days descending as in your example from left to right)
http://sqlfiddle.com/#!17/3bdad/5 (days ascending)
The main idea is already mentioned in the other answers. Instead of joining the CTEs for calculating the values I am using window functions which is a bit shorter and more readable I think. The pivot is done the same way.
SELECT
attr,
COALESCE(max(count) FILTER (WHERE day_number = 0), 0) as day1_count, -- D
COALESCE(max(percent) FILTER (WHERE day_number = 0), 0) as day1_percent,
COALESCE(max(count) FILTER (WHERE day_number = 1), 0) as day2_count,
COALESCE(max(percent) FILTER (WHERE day_number = 1), 0) as day2_percent
/*
Add more days here
*/
FROM(
SELECT *, (count::float/count_per_day)::decimal(5, 2) as percent -- C
FROM (
SELECT DISTINCT
attr,
MAX(time::date) OVER () - time::date as day_number, -- B
count(*) OVER (partition by time::date, attr) as count, -- A
count(*) OVER (partition by time::date) as count_per_day
FROM test_table
)s
)s
GROUP BY attr
ORDER BY attr
A counting the rows per day and counting the rows per day AND attr
B for more readability I convert the date into numbers. Here I take the difference between current date of the row and the maximum date available in the table. So I get a counter from 0 (first day) up to n - 1 (last day)
C calculating the percentage and rounding
D pivot by filter the day numbers. The COALESCE avoids the NULL values and switched them into 0. To add more days you can multiply these columns.
Edit: Made the day counter more flexible for more days; new SQL Fiddle
Basically, I see this as conditional aggregation. But you need to get an enumerator for the date for the pivoting. So:
SELECT attr,
COUNT(*) FILTER (WHERE day_number = 1) as day1_count,
COUNT(*) FILTER (WHERE day_number = 1) / cnt as day1_percent,
COUNT(*) FILTER (WHERE day_number = 2) as day2_count,
COUNT(*) FILTER (WHERE day_number = 2) / cnt as day2_percent
FROM (SELECT attr,
DENSE_RANK() OVER (ORDER BY time::date DESC) as day_number,
1.0 * COUNT(*) OVER (PARTITION BY attr) as cnt
FROM test_table
) s
GROUP BY attr, cnt
ORDER BY attr;
Here is a SQL Fiddle.

SQL Sum() returning postive and negative values

I'm trying to get SUM() to return the sum of a column summing the positive and negative values in the column. Instead its currently returning one positive value and one negative value, can anyone help?
SELECT
LedgerAP.Period, LedgerAP.Account, SUM(LedgerAP.Amount) Amount
FROM
LedgerAP
WHERE
LedgerAP.Period >= 201500 AND LedgerAP.Account = N'105.71'
GROUP BY LedgerAP.Period, LedgerAP.Account
HAVING SUM(Amount) <> 0
UNION ALL
SELECT
LedgerAR.Period, LedgerAR.Account, SUM(LedgerAR.Amount)
FROM
LedgerAR
WHERE
LedgerAR.Period >= 201500 AND LedgerAR.Account = N'105.71'
GROUP BY LedgerAR.Period, LedgerAR.Account
UNION ALL
SELECT
LedgerEx.Period, LedgerEx.Account, SUM(LedgerEx.Amount)
FROM
LedgerEx
WHERE
LedgerEx.Period >= 201500 AND LedgerEx.Account = N'105.71'
GROUP BY LedgerEx.Period, LedgerEx.Account
UNION ALL
SELECT
LedgerMisc.Period, LedgerMisc.Account, SUM(LedgerMisc.Amount)
FROM
LedgerMisc
WHERE
LedgerMisc.Period >= 201500 AND LedgerMisc.Account = N'105.71'
GROUP BY LedgerMisc.Period, LedgerMisc.Account
I think you need to re-aggregate your results:
with l as (
<your query here>
)
select period, account, sum(amount)
from l
group by period, account;
You can do the same thing with a subquery instead of a CTE.

TSQL : Sum of count of distinct rows per day within a specified time period

I am trying to get the customer count per day,
The bill number is identical in POS machines.
TSQL query
SELECT sum(tot) as Customer_Count
FROM
(select count(distinct (slip_no)) as tot
from rem_upload r
where (r.location_id like '001') and (r.tran_type <>'Z' and r.tran_type <> 'X') and (r.tran_date >= '2014-01-02' and r.tran_date <= '2014-01-03')
group by mech_no)
as C_count
OutPut:
Customer_Count
379
I need to get the total customer count per day for a specified time period.
Now what I get in my output is the total customer count of a given period.
Thank you,
Something along those lines:
SELECT C_count.tran_date
,SUM(C_count.tot) as Customer_Count
FROM
(SELECT r.tran_date
,COUNT(distinct (slip_no)) as tot
FROM rem_upload r
WHERE (r.location_id like '001') and (r.tran_type <>'Z' and r.tran_type <> 'X') and (r.tran_date >= '2014-01-02' and r.tran_date <= '2014-01-03')
GROUP BY r.mech_no
,r.tran_date)as C_count
GROUP BY tran_date
Untested..but the idea is something like this.
Try replace the following:
<your_specified_date> by your custom date
r.<timestamp_column> if you have a timestamp column type
<your_specified_time_range> by your custom time.
SELECT sum(tot) as Customer_Count
FROM
(select count(distinct (slip_no)) as tot
from rem_upload r
where (r.location_id like '001')
and (r.tran_type <>'Z' and r.tran_type <> 'X')
and (r.tran_date >= '2014-01-02' and r.tran_date <= '2014-01-03')
and r.tran_date = <your_specified_date>
and r.<timestamp_column> = <your_specified_time_range>
group by mech_no)
as C_count