Select query with multiple sub queries for counts (Netezza SQL) - sql

I am trying to create a report that gets counts at 3 periods of time: the previous month, that month last year, and year to date.
I previously used 3 separate queries like below while toggling the where clauses, but I want be able to combine all 3 into one query.
I've tried with case statements but couldn't seem to get that to work. FYI app_date is YYYY-MM-DD
Select count(application_id)
from application_data a
where to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-1
--where to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-101
--where to_char(app_date, 'YYYY') = to_char(current_date, 'YYYY') and to_char(app_date, 'YYYYMM') <> to_char(current_date, 'YYYYMM')
Sample data:
App_ID App_date
123519 2018-02-17
123521 2018-02-18
123522 2018-02-19
123523 2018-02-23
123518 2019-01-15
123546 2019-02-21
123547 2019-02-22
123548 2019-02-15
123542 2019-02-02
Desired Result:
LastMonth YTD YoY
4 5 4

I think you want conditional aggregation:
Select sum(case when to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-1 then 1 else 0 end),
sum(case to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-101 when then 1 else 0 end),
sum(case when to_char(app_date, 'YYYY') = to_char(current_date, 'YYYY') and to_char(app_date, 'YYYYMM') <> to_char(current_date, 'YYYYMM') then 1 else 0 end)
from application_data a

Related

Writing a sql query that captures the 2022 YTD data and compare to same period from 2021.. ex"Jan 1 to Jun 10 2022" vs "Jan 1 to Jun 10 2021"

SUM(case when TO_DATE(WO.W_MEASURE_CLAIMS_SAVING_DATE,'YYYY-MM-DD') between (sysdate -365) and sysdate then PM.M_GROSS_ANNUAL_KWH_SVG else 0 end) as CURRENT_KWH_12,
SUM(case when TO_DATE(WO.W_MEASURE_CLAIMS_SAVING_DATE,'YYYY-MM-DD') between sysdate -730 and (sysdate -365) then PM.M_GROSS_ANNUAL_KWH_SVG else 0 end) as PREVIOUS_LAST_KWH_12,
SUM(case when TO_DATE(WO.W_MEASURE_CLAIMS_SAVING_DATE,'YYYY-MM-DD') between (sysdate -365) and sysdate then PM.M_GROSS_ANNUAL_THERM_SVG else 0 end) as CURRENT_THERMS_12,
sum(case when TO_DATE(WO.W_MEASURE_CLAIMS_SAVING_DATE,'YYYY-MM-DD') between sysdate -730 and (sysdate -365) then PM.M_GROSS_ANNUAL_THERM_SVG else 0 end) as PREVIOUS_LAST_THERMS_12,
... i think the "current" data needs to be W_MEASURE_CLAIMS_SAVING_DATE >= 2022-01-01
and the [2:09 PM] Siegel, James R
and the "previous" data needs to be W_MEASURE_CLAIMS_SAVING_DATE between 2021-01-01 and today's date -365 days
but i just need help writing it
You appear to want to compare values between the start of this year until now to between the start of last year and the same date as now last year:
SUM(
CASE
WHEN WO.W_MEASURE_CLAIMS_SAVING_DATE BETWEEN TRUNC(SYSDATE, 'YY')
AND SYSDATE
THEN PM.M_GROSS_ANNUAL_KWH_SVG
END
) AS CURRENT_KWH,
SUM(
CASE
WHEN WO.W_MEASURE_CLAIMS_SAVING_DATE BETWEEN ADD_MONTHS(TRUNC(SYSDATE, 'YY'), -12)
AND ADD_MONTHS(SYSDATE, -12)
THEN PM.M_GROSS_ANNUAL_KWH_SVG
END
) as PREVIOUS_KWH

Sql Group by on subquery rows

I have this table :
ArretProductionJournee(Id, DateArret,HeureDebut,HeureFin,EnumArret)
Example :
DateArret ||HeureDebut ||HeureFin ||EnumArret
2020-11-30 ||2020-11-30 14:00:00.000 ||2020-11-30 15:00:00.000 ||PS
2020-11-30 ||2020-11-30 16:00:00.000 ||2020-11-30 17:00:00.000 ||HI
i want to sum the datediff(HeureDebut,HeureFin) in columns for each EnumArret
so i run this query :
SELECT ArretProductionJournee.DateArret,
(select
sum (datediff(minute,ArretProductionJournee.HeureDebut,
ArretProductionJournee.HeureFin))
where ArretProductionJournee.EnumArret Like 'HI')as HI,
(select
sum (datediff(minute,ArretProductionJournee.HeureDebut,
ArretProductionJournee.HeureFin))
where ArretProductionJournee.EnumArret Like 'PS') as PS
FROM dbo.ArretProductionJournee
where ArretProductionJournee.EnumArret Like 'HI'OR
ArretProductionJournee.EnumArret Like 'PS'
group by ArretProductionJournee.EnumArret, dbo.ArretProductionJournee.DateArret
Result :
DateArret ||HI || PS
2020-10-30 ||12 || NULL
2020-11-30 ||60 || NULL
2020-11-30 ||NULL || 60
The result i want is Grouping the sum by the date:
DateArret ||HI || PS
2020-10-30 ||12 || 0
2020-11-30 ||60 || 60
I think you want conditional aggregation:
select datearret,
sum(case when enumarret = 'PS' then datediff(minute, heuredebut, heurefin) else 0 end) ps,
sum(case when enumarret = 'HI' then datediff(minute, heuredebut, heurefin) else 0 end) hi
from dbo.arretproductionjournee
where enumarret in ('PS', 'HI')
group by datearret

how to optimize my oracle sql?

I need count in range two date,this sql is work,bug not better,can you help me?
select dmc.doctor_id,
(
select count(*)
from hele_dct_member_config dmc
WHERE (EXTRACT(YEAR FROM dmc.start_time) = 2016 OR EXTRACT(YEAR FROM dmc.end_time) = 2016) AND dmc.status=1
AND TO_DATE('2016-01-31', 'yyyy-mm-dd') BETWEEN start_time AND end_time
) Jan,
(
select count(*)
from hele_dct_member_config dmc
WHERE (EXTRACT(YEAR FROM dmc.start_time) = 2016 OR EXTRACT(YEAR FROM dmc.end_time) = 2016) AND dmc.status=1
AND TO_DATE('2016-02-28', 'yyyy-mm-dd') BETWEEN start_time AND end_time
) Feb,
.
.
.
from hele_dct_member_config dmc
enter code here
WHERE (EXTRACT(YEAR FROM dmc.start_time) = 2016 OR EXTRACT(YEAR FROM dmc.end_time) = 2016) AND dmc.status=1
grouy by dmc.doctor_id
I need count in range two date,this sql is work,bug not better,can you help me?
Use conditional aggregation:
select dmc.doctor_id,
sum(case when date '2016-01-31' BETWEEN start_time AND end_time then 1 else 0
end) as Jan,
sum(case when date '2016-02-31' BETWEEN start_time AND end_time then 1 else 0
end) as Feb,
.
.
.
from hele_dct_member_config dmc
where (extract(year from dmc.start_time) = 2016 or
extract(year from dmc.end_time) = 2016) AND
dmc.status = 1
group by dmc.doctor_id;
if i want get quarter count ? this is one way
SUM(case when date '2016-01-31' BETWEEN start_time AND end_time then 1 else 0 end) +
SUM(case when date '2016-02-28' BETWEEN start_time AND end_time then 1 else 0 end) +
SUM(case when date '2016-03-31' BETWEEN start_time AND end_time then 1 else 0 end) one

How to change row value into column header

select
extract(year from datetimestamp ) Yr,extract(month from datetimestamp) Mn,
c.weekday_of_month wk, a.aircraft_type,count( a.aircraft_type) from fcm_bv.Flights b
join fcm_bv.Fleet a on b.aircraftid=a.tail
join SYS_CALENDAR.CALENDAR c
on cast(b.datetimestamp AS DATE FORMAT 'YYYY-MM-DD') = cast(c.calendar_date AS DATE FORMAT 'YYYY-MM-DD')
where cast(datetimestamp as date) >= '2011-09-01'
and cast(datetimestamp as date) <= '2011-09-30' order by wk
group by Yr,Mn,wk,a.fleet,a.aircraft_type
While Running above Query I am getting out put like this
Yr Mn wk AIRCRAFT_TYPE Count(AIRCRAFT_TYPE)
2011 9 1 B737-700 1744
2011 9 1 B737-800 131
2011 9 1 B737-800W 2711
2011 9 1 B737-8BK 180
2011 9 1 B737-700W 329
But I need output in below format
Yr Mn wk B737-700 B737-800 B737-800W B737-8BK B737-700W
2011 9 1 1744 131 2711 180 329
Could any one help me
In the past when I have needed to do this the pivot was against a discrete, managable volume of categories and the following SQL has served me well:
SELECT EXTRACT(YEAR FROM b.datetimestamp) AS Yr
, EXTRACT(MONTH FROM b.datetimestamp) AS Mn
, C.weekday_of_month
, COUNT(CASE WHEN a.aircraft_type = 'B737-700' THEN a.aircraft_type ELSE NULL END) AS B737-700
, COUNT(CASE WHEN a.aircraft_type = 'B737-800' THEN a.aircraft_type ELSE NULL END) AS B737-800
, /* Other Known Aircrafts */
, COUNT(CASE WHEN a.aircrat_type NOT IN ('<list of known aircraft types>') THEN a.aircraft_type ELSE NULL END) AS Uncategorized_Aircraft
FROM fcm_bv.Flights b
join fcm_bv.Fleet a on b.aircraftid=a.tail
join SYS_CALENDAR.CALENDAR c
on cast(b.datetimestamp AS DATE FORMAT 'YYYY-MM-DD') = cast(c.calendar_date AS DATE FORMAT 'YYYY-MM-DD')
WHERE cast(datetimestamp as date) >= '2011-09-01'
AND cast(datetimestamp as date) <= '2011-09-30' order by wk
GROUP BY Yr,Mn,wk,a.fleet
If you have to pivot against a constantly changing category it may be best to leave the pivoting to MS Excel or BI tool of choice.
Hope this helps.

SQL Results group by month

I'm trying to return some results spread over a rolling 12 month period eg:
MONTH IN OUT
January 210 191
February 200 111
March 132 141
April 112 141
May 191 188
etc...
How do I spread the results over a date range, populating the first column with the month name?
IN MSSQL it would be something like:
SELECT COUNT(problem.problem_type = 'IN') AS IN,
COUNT(problem.problem_type = 'OUT') AS OUT,
DATEPART(year, DateTime) as Year,
DATEPART(month, DateTime) as Month
FROM problem
WHERE (DateTime >= dbo.FormatDateTime('2010-01-01'))
AND
(DateTime < dbo.FormatDateTime('2010-01-31'))
GROUP BY DATEPART(year, DateTime),
DATEPART(month, DateTime);
But this is against an Oracle database so DATEPART and DateTime are not available.
My Problem table is roughly:
problem_ID Problem_type IN_Date OUT_Date
1 IN 2010-01-23 16:34:29.0 2010-02-29 13:06:28.0
2 IN 2010-01-27 12:34:29.0 2010-01-29 12:01:28.0
3 OUT 2010-02-13 13:24:29.0 2010-09-29 15:04:28.0
4 OUT 2010-02-15 16:31:29.0 2010-07-29 11:03:28.0
Use:
SELECT SUM(CASE WHEN p.problem_type = 'IN' THEN 1 ELSE 0 END) AS IN,
SUM(CASE WHEN p.problem_type = 'OUT' THEN 1 ELSE 0 END) AS OUT,
TO_CHAR(datetime, 'YYYY') AS year,
TO_CHAR(datetime, 'MM') AS month
FROM PROBLEM p
WHERE p.DateTime >= TO_DATE('2010-01-01', 'YYYY-MM-DD')
AND p.DateTime < TO_DATE('2010-01-31', 'YYYY-MM-DD')
GROUP BY TO_CHAR(datetime, 'YYYY'), TO_CHAR(datetime, 'MM')
You could also use:
SELECT SUM(CASE WHEN p.problem_type = 'IN' THEN 1 ELSE 0 END) AS IN,
SUM(CASE WHEN p.problem_type = 'OUT' THEN 1 ELSE 0 END) AS OUT,
TO_CHAR(datetime, 'MM-YYYY') AS mon_year
FROM PROBLEM p
WHERE p.DateTime >= TO_DATE('2010-01-01', 'YYYY-MM-DD')
AND p.DateTime < TO_DATE('2010-01-31', 'YYYY-MM-DD')
GROUP BY TO_CHAR(datetime, 'MM-YYYY')
Reference:
TO_CHAR
TO_DATE
You probably want something like
SELECT SUM( (CASE WHEN problem_type = 'IN' THEN 1 ELSE 0 END) ) in,
SUM( (CASE WHEN problem_type = 'OUT' THEN 1 ELSE 0 END) ) out,
EXTRACT( year FROM DateTime ) year,
EXTRACT( month FROM DateTime ) month
FROM problem
WHERE DateTime >= date '2010-01-01'
AND DateTime < date '2010-01-31'
GROUP BY EXTRACT( year FROM DateTime ),
EXTRACT( month FROM DateTime )