Oracle Rows into Columns - oracle

Can someone help me design the query to get below o/p.
Data I have:
Table X:
Count Name Amount Days
5 ABC 500 Day1
10 ABC 1000 Day2
3 BCD 100 Day1
4 BDC 200 Day2
Result I need:
Name Count AmountDay1 Count AmountDay2
ABC 5 500 10 1000
BCD 3 100 4 200
etc
Is this Possible?
I tried something with the below query, but not getting the desired o/p
select * from X
pivot
(sum(amount) for days in ('Day1', 'Day2'))
Please help
I'm using Oracle 11 G

Since you want to pivot on two columns Count and Amount, in might be easier to use an aggregate function with a CASE expression to get the result:
select name,
sum(case when days = 'Day1' then "count" else 0 end) CountDay1,
sum(case when days = 'Day1' then amount else 0 end) AmountDay1,
sum(case when days = 'Day2' then "count" else 0 end) CountDay2,
sum(case when days = 'Day2' then amount else 0 end) AmountDay2
from tableX
group by name;
See SQL Fiddle with Demo
If you want to use the PIVOT function, then you will want to unpivot the two columns Amount and Count, first:
select name, CountDay1, AmountDay1, CountDay2, AmountDay2
from
(
select name, col||days as col, value
from tableX
unpivot
(
value
for col in ("Count", Amount)
) u
) d
pivot
(
sum(value)
for col in ('CountDay1' as CountDay1, 'AMOUNTDay1' as AmountDay1,
'CountDay2' as CountDay2, 'AMOUNTDay2' as AmountDay2)
) piv;
See SQL Fiddle with Demo

you need to pivot on two columns count and amount:
select * from x
PIVOT
( min(count) as count ,sum(amount)for days in ('Day1', 'Day2'))

Related

Efficiently get array of all previous dates per id per date limited to past 6 months in BigQuery

I have a very big table 'DATES_EVENTS' (20 T) that looks like this:
ID DATE
1 '2022-04-01'
1 '2022-03-02'
1 '2022-03-01'
2 '2022-05-01'
3 '2021-12-01'
3 '2021-11-11'
3 '2020-11-11'
3 '2020-10-01'
I want per each row to get all past dates (per user) limited to up to 6 months.
My desired table:
ID DATE DATE_list
1 '2022-04-01' ['2022-04-01','2022-03-02','2022-03-01']
1 '2022-03-02' ['2022-03-02','2022-03-01']
1 '2022-03-01' ['2022-03-01']
2 '2022-05-01' ['2022-05-01']
3 '2021-12-01' ['2021-12-01','2021-11-11']
3 '2021-11-11' ['2021-11-11']
3 '2020-11-11' ['2020-11-11','2020-10-01']
3 '2020-10-01' ['2020-10-01']
I have a solution for all dates not limited:
SELECT
ID, DATE, ARRAY_AGG(DATE) OVER (PARTITION BY ID ORDER BY DATE) as DATE_list
FROM
DATES_EVENTS
But for a limited up to 6 months I don't have an efficient solution:
SELECT
distinct A.ID, A.DATE, ARRAY_AGG(B.DATE) OVER (PARTITION BY B.ID ORDER BY B.DATE) as DATE_list
FROM
DATES_EVENTS A
INNER JOIN
DATES_EVENTS B
ON
A.ID=B.ID
AND B.DATE BETWEEN DATE_SUB(A.DATE, INTERVAL 180 DAY) AND A.DATE
** ruffly a solution
Anyone know of a good and efficient way to do what I need?
Consider below approach
select id, date, array(
select day
from t.date_list day
where day <= date
order by day desc
) as date_list
from (
select *, array_agg(date) over win as date_list
from dates_events
window win as (
partition by id
order by extract(year from date) * 12 + extract(month from date)
range between 5 preceding and current row
)
) t
if applied to sample data in your question - output is
In case if (as I noticed in your question) 180 days is appropriate substitution for 6 months for you - you can use below simpler version
select *, array_agg(date) over win as date_list
from dates_events
window win as (
partition by id
order by unix_date(date)
range between current row and 179 following
)

Oracle count with group by returning empty rows

I am trying to build a query where it returns the count for each month (passing a start and an end date) of a certain value, the output should be like this
Month Qtn
---------
|July|0|
|Augu|0|
|Sept|0|
but I get no rows, the query is the following:
SELECT TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') AS XMONTH, COUNT(TT_CUSTOMERS.NAMES) AS QTY FROM TT_CUSTOMERS
WHERE COMPANY = 700
AND TT_CUSTOMERS.C1_LOOKUP_ID = 100
AND TT_CUSTOMERS.C2_LOOKUP_ID = 1
AND TT_CUSTOMERS.C3_LOOKUP_ID IN (70, 80)
AND TT_CUSTOMERS.ST_LOOKUP_ID = 90
AND TO_NUMBER(TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMMDD')) >= TO_NUMBER('20170701')
AND TO_NUMBER(TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMMDD')) <= TO_NUMBER('20170930')
GROUP BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM')
ORDER BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') ASC;
I know it's because the query is returning no rows, so the XMONTH is emtpy and so also the qty is empty, but I can't figure out a way to get the output that I've shown.
Assuming closed_date is of a date datatype...
and assuming all your other fields values are int and matches exist in the database...
I'd assume it's the numeric comparison of date values. Compare a date as a date not as numbers.
SELECT TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') AS XMONTH
, COUNT(TT_CUSTOMERS.NAMES) AS QTY
FROM TT_CUSTOMERS
WHERE COMPANY = 700
AND TT_CUSTOMERS.C1_LOOKUP_ID = 100
AND TT_CUSTOMERS.C2_LOOKUP_ID = 1
AND TT_CUSTOMERS.C3_LOOKUP_ID IN (70, 80)
AND TT_CUSTOMERS.ST_LOOKUP_ID = 90
AND TT_CUSTOMERS.CLOSED_DATE >= TO_DATE('20170701','YYYYMMDD')
AND TT_CUSTOMERS.CLOSED_DATE <= TO_DATE('20170930','YYYYMMDD')
GROUP BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM')
ORDER BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') ASC;

PL/SQL: Need help creating calculated columns based on conditions. to be done in select query

I am trying to count all the distinct ids based on conditions. But I am unable to figure out where I am going wrong with the syntax. The logic is
COUNTD(IF ([column_name1] = 1) THEN [DATAPAGEID] END)
This is the formula I used in Tableau. However when writing it in a PL/SQL query as
Select FT.NAME, COUNT(DISTINCT FT.pageID IF FT."column_name" = 1 )
as total_expected
FROM
( Sub Query) FT
Group by FT.Name
Order by FT.Name
Needless to say its throwing errors. Now I can write separate queries which can give me each number using a where condition. For example, if I wanted a count of distinct pageid where column_name1 = 1, I would write something like this
Select FT.SITENAME, COUNT(DISTINCT DATAPAGEID) as Datapage
from
(sub query)
WHERE FT."column_name" = 1
but the problem with that is that I have other calculated columns in the query which will all need to be part of the same row. To illustrate here's what the table would look like
name Calculated_Column1 Calculated_Column2 Calculated_column3
abc 781 811 96.54%
pqr 600 800 75.00%
where calculated_column3 is the result of 781/811. Therefore I can't have a new query for each column. I thought using an if condition when calculating columns will solve this, but I can't get the syntax right somehow.
Therefore, I need to know how can I create conditional calculated columns within the select query. If I have not explained this well, please let me know and I will try to clarify further.
You can use a CASE block inside the count (DISTINCT ) as shown.
SELECT FT.NAME,
COUNT(DISTINCT
CASE
WHEN DATAPAGEID = 1
THEN 1
ELSE 0
END ) Calculated_Column1,
COUNT(DISTINCT
CASE
WHEN DATAPAGEID = 2
THEN 1
ELSE 0
END ) Calculated_Column2,
( COUNT(DISTINCT
CASE
WHEN DATAPAGEID = 1
THEN 1
ELSE 0
END ) / COUNT(DISTINCT
CASE
WHEN DATAPAGEID = 2
THEN 1
ELSE 0
END ) ) * 100||'%' Calculated_Column3
FROM
( SELECT 'abc' name, 1 DATAPAGEID FROM dual
UNION ALL
SELECT 'abc' name, 1 DATAPAGEID FROM dual
UNION ALL
SELECT 'pqr' name, 2 DATAPAGEID FROM dual
UNION ALL
SELECT 'pqr' name, 2 DATAPAGEID FROM dual
UNION ALL
SELECT 'pqr' name, 3 DATAPAGEID FROM dual
) FT
GROUP BY FT.Name
ORDER BY FT.Name;
Output is
abc 1 1 100%
pqr 1 2 50%

Calculating values in date ranges in Oracle (possibly with recursive CTE)

I have a problem which can be handled by a recursive CTE, but not within an acceptable period of time. Can anyone point me at ways to improve the performance and/or get the same result a different way?
Here's my scenario!
I have : A large table which contains in each row an id, a start date, an end date, and a ranking number. There are multiple rows for each id and the date ranges often overlap. Dates are from 2010 onward.
I want: A table which contains a row for each combination of id + date which falls inside any date range for that id from the previous table. Each row should have the lowest ranking number for that id and day.
Eg:
ID Rank Range
1 1 1/1/2010-1/4/2010
1 2 1/2/2010-1/5/2010
2 1 1/1/2010-1/2/2010
becomes
ID Rank Day
1 1 1/1/2010
1 1 1/2/2010
1 1 1/3/2010
1 1 1/4/2010
1 2 1/5/2010
2 1 1/1/2010
2 1 1/2/2010
I can do this with a recursive CTE, but the performance is terrible (20-25 minutes for a relatively small data set which produces a final table with 31 million rows):
with enc(PersonID, EncounterDate, EndDate, Type_Rank) as (
select PersonID, EncounterDate, EndDate, Type_Rank
from Big_Base_Table
union all
select PersonID, EncounterDate + 1, EndDate, Type_Rank
from enc
where EncounterDate + 1 <= EndDate
)
select PersonID, EncounterDate, min(Type_Rank) Type_Rank
from enc
group by PersonID, EncounterDate
;
You could extract all possible dates from the table once in a CTE, and then join that back to the table:
with all_dates (day) as (
select start_date + level - 1
from (
select min(start_date) as start_date, max(end_date) as end_date
from big_base_table
)
connect by level <= end_date - start_date + 1
)
select bbt.id, min(bbt.type_rank) as type_rank, to_char(ad.day, 'YYYY-MM-DD') as day
from all_dates ad
join big_base_table bbt
on bbt.start_date <= ad.day
and bbt.end_date >= ad.day
group by bbt.id, ad.day
order by bbt.id, ad.day;
ID TYPE_RANK DAY
---------- ---------- ----------
1 1 2010-01-01
1 1 2010-01-02
1 1 2010-01-03
1 1 2010-01-04
1 2 2010-01-05
2 1 2010-01-01
2 1 2010-01-02
7 rows selected.
The CTE gets all dates from the lowest for any ID, up to the highest for any ID. You could also use a static calendar table for that if you have one, to save hitting the table twice (and getting min/max at the same time is slow in some versions at least).
You could also write it the other way round, as:
...
from big_base_table bbt
join all_dates ad
on ad.day >= bbt.start_date
and ad.day <= bbt.end_date
...
but I think the optimisier will probably end up treating them the same, with a single full scan of your base table; worth checking the plan it actually comes up with for both though, and if one is more efficnet that the other.

oracle sql, counting working hours

I have been trying to find something related but couldn't.
I have an issue that i need to produce an availability percentage of something. I have a table that includes events that are happening, which i managed to count them by the day they are happening, but i am finding issues to count the total number of working hours in a quarter or a year.
when each day of the week has a different weight.
Basically my question is: can i do it without making a table with all dates in that month/year?
An example of the data:
ID DATE duration Environment
1 23/10/15 25 a
2 15/01/15 50 b
3 01/01/15 43 c
8 05/06/14 7 b
It can work for me by a calculated field or just a general query to get the information.
sorry I don't really understand the question but if you want to generate dates using connect by level is an easy way to do it (you could also use the model clause or recursive with) I did it here for just 10 days but you get the idea. I put in your dates as t1 and generated a list of dates (t) and then did a left outer join to put them together.
WITH t AS
(SELECT to_date('01-01-2015', 'mm-dd-yyyy') + level - 1 AS dt,
NULL AS duration
FROM dual
CONNECT BY level < = 10
),
t1 AS
(
SELECT to_date('10/01/15', 'dd-mm-yy') as dt, 50 as duration FROM dual
UNION ALL
SELECT to_date('01/01/15', 'dd-mm-yy'), 43 FROM dual
UNION ALL
SELECT to_date('06/01/15', 'dd-mm-yy'), 43 FROM dual
)
SELECT t.dt,
NVL(NVL(t1.duration, t.duration),0) duration
FROM t,
t1
WHERE t.dt = t1.dt(+)
ORDER BY dt
results
DT Duration
01-JAN-15 43
02-JAN-15 0
03-JAN-15 0
04-JAN-15 0
05-JAN-15 0
06-JAN-15 43
07-JAN-15 0
08-JAN-15 0
09-JAN-15 0
10-JAN-15 50
This was my intention, and the full answer below.
WITH t AS
(SELECT to_date('01-01-2015', 'mm-dd-yyyy') + level - 1 AS dt
FROM dual
CONNECT BY level < =365
),
t1 as
(
SELECT dt,
CASE
WHEN (to_char(TO_DATE( t.dt,'YYYY-MM-DD HH24:MI:SS'),'DY') in ('MON', 'TUE', 'WED', 'THU', 'FRI'))
THEN 14*60
WHEN (to_char(TO_DATE( t.dt,'YYYY-MM-DD HH24:MI:SS'),'DY') in ('SAT'))
THEN 8*60
WHEN (to_char(TO_DATE( t.dt,'YYYY-MM-DD HH24:MI:SS'),'DY') in ('SUN'))
THEN 10*60
ELSE 0 END duration ,
to_char(t.dt,'Q') as quarter
FROM t
)
select to_char(t1.dt,'yyyy'), to_char(t1.dt,'Q'),sum(t1.duration)
from t1
group by
to_char(t1.dt,'yyyy'), to_char(t1.dt,'Q');

Resources