Get Total count - oracle

I want to merge two columns(Sender and Receiver) and get the Transaction Type count then merge another table with using Sender_Receiver primary id.
Sender Receiver Type Amount Date
773787639 777611388 1 300 2/1/2019
773631898 776806843 4 450 8/20/2019
773761571 777019819 6 369 2/11/2019
774295511 777084440 34 1000 1/22/2019
774263079 776816905 45 678 6/27/2019
774386894 777202863 12 2678 2/10/2019
773671537 777545555 14 38934 9/29/2019
774288117 777035194 18 21 4/22/2019
774242382 777132939 21 1275 9/30/2019
774144715 777049859 30 6309 7/4/2019
773911674 776938987 10 3528 5/1/2019
773397863 777548054 15 35892 7/6/2019
776816905 772345091 6 1234 7/7/2019
777035194 775623065 4 453454 7/20/2019
Second Table
Mobile_number Age
773787639 34
773787632 23
774288117 65
I am try to get like this kind of table
Sender/Receiver Type_1 Type_4 Type_12...... Type_45 Age
773787639 3 2 0 0 23
773631898 1 0 1 2 56
773397863 2 2 0 0 65
772345091 1 1 0 3 32

Ok, I have seen your old question and you just need inner join in sub-query as following:
SELECT
SenderReceiver,
COUNT(CASE WHEN Type = 1 THEN 1 END) AS Type_1,
COUNT(CASE WHEN Type = 2 THEN 1 END) AS Type_2,
COUNT(CASE WHEN Type = 3 THEN 1 END) AS Type_3,
...
COUNT(CASE WHEN Type = 45 THEN 1 END) AS Type_45,
Age -- changes here
FROM
( SELECT sr.SenderReceiver, sr.Type, st.Age from -- changes here
(SELECT Sender AS SenderReceiver, Type FROM yourTable
UNION ALL
SELECT Receiver, Type FROM yourTable) sr
join <second_table> st on st.Mobile_number = sr.SenderReceiver -- changes here
) t
GROUP BY
SenderReceiver,
Age; -- changes here
Changes done in your previous query are marked with comments -- changes here.
Please replace the name of the <second_table> with the original name of the table.
Cheers!!

Related

How to pivot duplicate rows to distinct columns in excel

I have an excel data where I need to transpose the duplicate rows to columns suitable for analysis. Please let me know how to do this?
For example,
My excel data looks like
id metrics date value
1 A 20190812 100
1 A 20190813 100
1 A 20190814 100
1 B 20190812 200
1 B 20190813 130
2 A 20190812 100
2 B 20190813 106
2 C 20190814 104
The result I look forward to is
id A B C date
1 100 200 null 20190812
1 100 130 null 20190813
1 100 null null 20190814
2 100 null null 20190812
2 null 106 null 20190813
2 null null 104 20190814
Try below in Pivot:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
Pivot =
Table.Pivot(
Source,
List.Distinct(Source[metrics]),
"metrics",
"value",
List.Sum)
in
Pivot

Flag a newly added subid within a ID

Hi I have a dataframe as below with thousands of ID's. It has a list of ID's which have sub id's within them as shown. The subid's may get changed on daily basis, either a new sub id may be added, or an existing sub id maybe lost.
I need to create 2 new columns, which will flag whenever a sub id is added/lost.
So, in the below format you can see that on the 12th, a new sub id 'D' is added
and on the 13th, and existing sub id (c) is lost.
i want to create a new column/flag to track these sub ids. Can you please help me with this?
I am using Python 3.5. Thanks
Sample format for one ID:
ID Sub Id Date is_new
1 a 3/11/2016 0
1 b 3/11/2016 0
1 c 3/11/2016 0
1 a 3/12/2016 0
1 b 3/12/2016 0
1 c 3/12/2016 0
1 d 3/12/2016 1
1 a 3/13/2016 0
1 b 3/13/2016 0
1 d 3/13/2016 0
Below query will indicate when a sub-id is added or deleted. Hope this helps.
Get the max and min update date per id, I put it in a temp table name: min_max
If update date is same with min and max then mark them as 1
Lag and lead functions will get the previous and next sub id per ID, subid order by
Put everything on a subquery (table s)
If update date is not the start or end date per ID, then it can be added (is_mindte=0) or deleted (is_maxdte=0)
If is_added column is null, then it is added on that date (is_added is null); If is_deleted column is null, then it is deleted the next update date (is_added is null)
select s.id,
s.subid,
s.upddate,
(case when is_mindte=0 and is_added is null
then 1 else 0 end ) is_new,
(case when is_maxdte=0 and is_deleted is null
then 1 else 0 end) is_removed
from (
with min_max as
(select id,min(upddate) mindate,max(upddate) maxdate
from myTable
group by id)
select t.id,
t.subid,
t.upddate,
case when t.upddate=m.mindate
then 1 else 0 end is_mindte,
case when t.upddate=m.maxdate
then 1 else 0 end is_maxdte,
lag(t.subid) over (partition by t.id, t.subid order by t.upddate) is_added,
lead(t.subid) over (partition by t.id, t.subid order by t.upddate) is_deleted
from myTable t, min_max m
where t.id=m.id) s
order by s.id,
s.upddate,
s.subid
sample result:
ID SUBID UPDDATE IS_NEW IS_REMOVED
1 a 2016-03-11T00:00:00Z 0 0
1 b 2016-03-11T00:00:00Z 0 0
1 c 2016-03-11T00:00:00Z 0 0
1 a 2016-03-12T00:00:00Z 0 0
1 b 2016-03-12T00:00:00Z 0 0
1 c 2016-03-12T00:00:00Z 0 1
1 d 2016-03-12T00:00:00Z 1 0
1 a 2016-03-13T00:00:00Z 0 0
1 b 2016-03-13T00:00:00Z 0 0
1 d 2016-03-13T00:00:00Z 0 0
2 a 2016-03-11T00:00:00Z 0 0
2 b 2016-03-11T00:00:00Z 0 0
2 c 2016-03-11T00:00:00Z 0 0

Oracle reduce result set on field duplication

I have a result set of a select in Oracle (12c) as the following:
GROUP_ID NAME ORDERING
1 AA 0
1 AA 1
1 AB 2
1 AC 3
2 BA 1
2 BA 2
2 BB 3
2 BC 4
I do not know how I could reduce the result set to remove rows based on one column while keeping the other fields. The expected outcome looks like the following:
GROUP_ID NAME ORDERING
1 AA 1
1 AB 2
1 AC 3
2 BA 2
2 BB 3
2 BC 4
I tried to solve it using group by but it got rid of the required field ordering. I am not an expert on window functions but I think it could be a valid attempt to use one.
From your data, it seems that you only need:
select group_id, name, max(ordering)
from yourTable
group by group_id, name

Using loop to insert values from one table to another

I have the following table named screening_plan:
plan_id movie_id plan_start_day plan_end_day plan_min_start_hh24 plan_max_start_hh24 screenings
1 1 1/06/2015 28/06/2015 9 17 5
2 2 1/06/2015 28/06/2015 9 22 4
3 3 1/06/2015 28/06/2015 9 22 5
4 4 1/06/2015 28/06/2015 9 17 4
And another tables theatre:
THEATRE_ID THEATRE_DESCRIPTION THEATRE_TOTAL_ROWS
1 2
2 2
3 3
4 2
There is a total of 18 screenings per day. I have to insert the details in the screening table as follows:
screening_id plan_id theatre_id screening_date screening_start_hh24 screening_start_mm60
1 1 3 1/06/2015 9 0
2 1 3 1/06/2015 11 30
3 1 3 1/06/2015 14 0
4 1/06/2015
plan_id is a foreign key referring table screening and theatre_id is a foreign key referring table theatre.
Each movie should be screened as per the screening number is defined
in the table screening_plan.
There is a break of 30 minutes between 2 consecutive screenings in
the same theatre.
The screening_start_hh24 should be less than plan_max_start_hh24.
Please note that the number of screenings for the first movie won't
fit into the provided time interval,so the second screening should be done
in an alternate theatre(preferably in theatre_id=2 starting from 11:30).
Each movie has a lenght of 2 hours.
I am stuck with this since yesterday. Tried doing it using the If-Else block, but that requires defining every condition. How can I do this using a loop?Please help.
My code(I have skipped the declaration part here):
BEGIN
SELECT plan_id INTO s_plan_id FROM screening_plan WHERE plan_id=1;
SELECT theatre_id INTO s_theatre_id FROM theatre WHERE theatre_id=1;
SELECT PLAN_START_DATE INTO s_screening_date FROM screening_plan WHERE plan_id=1;
SELECT Count(*) INTO s_count_theatre_id FROM screening;
IF s_count_theatre_id = 0
THEN
s_screening_start_hh24:=9;
s_screening_start_mm60:=0;
ELSIF s_count_theatre_id >0 AND s_count_theatre_id <=4
THEN
s_screening_start_hh24:=11 ;
s_screening_start_mm60:=30 ;
ELSE
Dbms_Output.put_line('---');
END IF;
INSERT INTO screening (plan_id, theatre_id, screening_date, screening_start_hh24, screening_start_mm60)
VALUES( s_plan_id,
s_theatre_id,
s_screening_date,
s_screening_start_hh24,
s_screening_start_mm60);
END;
There should be a total of 18 record in the table screening.5 for movie_id=1, 4 for movie_id=2, 5 for movie_id=3 and 4 for movie_id=3.

not getting output in group by using linq query

My table is:
Student_answer_Master:
Stud_ans_id QuestionId OptionId iscorrect
1 25 1 1
2 26 1 0
3 27 2 1
4 27 3 1
5 27 4 1
6 28 1 1
8 29 1 0
9 29 2 1
10 29 3 1
(my options are single choice and multiple choice(checkbox)).
now what i want to get total count of only those question which are correct.
1-indicate correct
0-indicate incorrect.
i want output:3(for questionid 25,27,28)
this is my linq query:
var data = (from temp in context.Student_Answer_Master
where temp.isCorrect == '1'
group temp by temp.Question_Id into g
select new {g.Key}).Count();
but here count is coming 0.
can any one tell me what is wrong with my query???
Try this this should work.
var data = (from temp in s
group temp by temp.Question_Id into g
select new { questionid = g.Key, minIscorrect = g.Min(x => x.isCorrect) }).Where(y => y.minIscorrect != 0).Count();
There are 2 things that looks wrong to me in your Query.
comparing an int to a char temp.isCorrect == '1' if you make it temp.isCorrect == 1, you will still get 4 as the count because you are eliminating the row that has wrong answer for Question id 29.
once you get the group, you need to check if there are any wrong answers for the question
if yes eliminate that and then put a count

Resources