COUNT returning NULL, should return 0 - oracle

I have a simple query for the table:
Person id Organization id employee_nam age busines_group_id
123 Zuyo 10 John 30 81
2457 Zuyo 10 Geet 69 81
56 Ghiya 12 paul 20 81
frei 13 81
SELECT
COUNT(DISTINCT ped.person_id)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
SELECT
NVL(COUNT(DISTINCT ped.person_id), 0)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
p_org_id is the parameter which I am passing which can be 10, 12, or 13.
COUNT returns 2 for id 10. 1 for id 12. but is returning NULL for id 13.
I want 0 to be returned in this case.
NVL and CASE are also not working.

try MAX:
nvl(max(count(DISTINCT ped.person_id)),0)

Related

How to clone data that has self relational/ self reference in laravel

How to replicate/clone all data in a table that has self-relation in Laravel?
I have this table:
members table
id
member_id
company_id
name
1
null
23
John
2
1
23
James
3
1
23
Ken
4
null
23
Test
5
3
23
Max
EXISTING CODE
And I want to replicate all data that has company_id of 23 using
$members = Members::where['company_id' => 23]->get();
foreach ($members as $member) {
$duplicate = $member->replicate()->fill(['company_id' => 24);
$duplicate->save();
}
Results of the code
members table
id
member_id
company_id
name
1
null
23
John
2
1
23
James
3
1
23
Ken
4
null
23
Test
5
3
23
Max
6
null
24
John
7
1
24
James
8
1
24
Ken
9
null
24
Test
10
3
24
Max
But in that code, the replication of self-relation is wrong. How can I do it?
Expected result
members table
id
member_id
company_id
name
1
null
23
John
2
1
23
James
3
1
23
Ken
4
null
23
Test
5
3
23
Max
6
null
24
John
7
6
24
James
8
6
24
Ken
9
null
24
Test
10
8
24
Max
I'm sorry if my English is messed up, but I hope you get the idea of what I'm trying to do. Thank you!
Try this .You have two options to replicate
$members = Members::where(['company_id' => 23])->get();
foreach ($members as $member) {
$duplicate = $member->replicate();
$duplicate->company_id=24;
$duplicate->save();
}
});
or
$members = Members::where(['company_id' => 23])->get();
foreach ($members as $member) {
$duplicate = $member->replicate()->fill(['company_id'=>24]);
$duplicate->save();
}
});
Updated
$members = Members::where(['company_id' => 23])->get();
$tempIdMap=[];
foreach ($members as $key=>$member) {
if($key==0){
$tempIdMap[$member->id]=$member->member_id;
}
$duplicate = $member->replicate()->fill(['company_id'=>24,'member_id'=>isset( $tempIdMap[$member->member_id])? $tempIdMap[$member->member_id]:null]);
$duplicate->save();
$tempIdMap[$member->id]=$duplicate->id;
}

Get Total count

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

After Sort in Python how to create new Index field

After I sort my data, I am trying to create a new index field.
Here is my code:
mydata_1=[[38,125,56],[98,23,150],[11,46,15],[23,87,81]]
cols= ['Col_A','Col_B','Col_C']
mydata_2= pd.DataFrame(mydata_1, columns=cols)
mydata_3= mydata_2.sort('Col_A')
So with my code above the output looks like this:
Col_A Col_B Col_C
2 11 46 15
3 23 87 81
0 38 125 56
1 98 23 150
I need to create a new column called "Col_D" as seen below:
Col_A Col_B Col_C Col_D
2 11 46 15 1
3 23 87 81 2
0 38 125 56 3
1 98 23 150 4
Also, I am going to be using this on many data sets. Is there a way to do this without specifying a range?
Here you go
import pandas as pd
mydata_1=[[38,125,56],[98,23,150],[11,46,15],[23,87,81]]
cols= ['Col_A','Col_B','Col_C']
mydata_2= pd.DataFrame(mydata_1, columns=cols)
mydata_3= mydata_2.sort('Col_A')
mydata_3["Col_D"] = range(len(mydata_3.index))
print(mydata_3)

Transform row into column and vice-versa using sql - oracle

I have this table:
create table history (
date_check DATE,
type VARCHAR2(30),
id_type NUMBER,
total NUMBER
)
Selecting.....
select * from history order by 1
DATE_CHECK TYPE ID_TYPE TOTAL
14/02/2016 abc 1 14
14/02/2016 abc33 1 14
14/02/2016 bbb 1 40
14/02/2016 bbb33 3 43
14/02/2016 ddd 2 61
14/02/2016 ddd33 2 62
15/02/2016 abc 1 33
15/02/2016 abc33 1 44
15/02/2016 bbb 1 55
15/02/2016 bbb33 3 66
15/02/2016 ddd 2 77
15/02/2016 ddd33 2 88
Type its always this 6 values:
abc
abc33
bbb
bbb33
ddd
ddd33
And I cross this data with "id_type" so there is a decode like this:
select type || decode(id_type, 1, '- new', 2, '- old', 3, '- xpto') as type from history order by 1
In the end I need something like this:
DATE_CHECK abc - new abc33 - old bbb - new bbb33 - old ....
14/02/2016 14 14 40 43
15/02/2016 33 44 55 66
What is the easiest way to do it? Using pivot?
try this:
with data as(
select date_check, type, total from (
select date_check, type || ' ' || decode(id_type, 1, '- new', 2, '- old', 3, '- xpto') as type, total from history
))
select * from data
pivot(
max(total) for type in ('abc - new', 'abc33 - new', 'bbb - new',
'bbb33 - xpto', 'ddd - old', 'ddd33 - old')
)
order by date_check;
And for the "vice versa" use UNPIVOT
You can reference multiple columns in a pivot statement to get your desired output. In your case you have a single analytic column (TOTAL) but multiple columns forming composite columns on which to perform the analytic function, you can use a pivot query like the following:
select *
from history
PIVOT ( max(TOTAL)
for (TYPE, ID_TYPE) in ( ('abc',1) abc_new
, ('abc',2) abc_old
, ('abc',3) abc_xpto
, ('abc33',1) abc33_new
, ('abc33',2) abc33_old
, ('abc33',3) abc33_xpto
, ('bbb',1) bbb_new
, ('bbb',2) bbb_old
, ('bbb',3) bbb_xpto
, ('bbb33',1) bbb33_new
, ('bbb33',2) bbb33_old
, ('bbb33',3) bbb33_xpto
, ('ddd',1) ddd_new
, ('ddd',2) ddd_old
, ('ddd',3) ddd_xpto
, ('ddd33',1) ddd33_new
, ('ddd33',2) ddd33_old
, ('ddd33',3) ddd33_xpto
)
)
You can adjust the output column headings to suite if desired by changing them similar to the following:
...
PIVOT ( max(TOTAL)
for (TYPE, ID_TYPE) in ( ('abc',1) "abc - new"
, ('abc',2) "abc - old"
, ('abc',3) "abc - xpto"
, ('abc33',1) "abc33 - new"
, ...

Sorting Matrix Report Columns in Specified Order

I've written the following TSQL to use in a report:
SELECT patcnty, CASE WHEN CAST(age_yrs AS int) <= '5' THEN 'Ages 5 and Younger' WHEN CAST(age_yrs AS int) BETWEEN '6' AND
'17' THEN 'Ages 6 to 17' WHEN CAST(age_yrs AS int) BETWEEN '18' AND '44' THEN 'Ages 18 to 44' WHEN CAST(age_yrs AS int) BETWEEN '45' AND
'64' THEN 'Ages 45 to 64' WHEN CAST(age_yrs AS int) >= '65' THEN 'Ages 65 and Older' END AS AgeGroup,
CASE WHEN patcnty = '54' THEN 'Tulare' WHEN patcnty = '16' THEN 'Kings' WHEN patcnty = '15' THEN 'Kern' WHEN patcnty = '10' THEN 'Fresno' END
AS County, oshpd_id, age_yrs
FROM OSHPD2009
WHERE (patcnty IN ('10', '15', '16', '54')) AND (age_yrs <> ' ')
I've created a SSRS 2005 Matrix report and have placed AgeGroup as my column and County as my row. When the report is displayed, the order of the columns are: Ages 18 to 44, Ages 45 to 64, Ages 5 and Younger, Ages 6 to 17 and Ages 65 and Older. This makes sense in that I set the sort order for the group to ascending.
How can I change the group sort order for the matrix column to sort in this manner:
Age 5 and Younger, Ages 6 to 17, Ages 18 to 44, Ages 45 to 64 and Ages 65 and Older?
Thanks much for your help!
After a little digging, I decided that SSRS needed to be told in what order to sort the groups. So, in the column grouping sort screen, I added an expression of
=iif(Fields!AgeGroup.Value="Ages 5 and Younger","1",
iif(Fields!AgeGroup.Value="Ages 6 to 17","2",
iif(Fields!AgeGroup.Value="Ages 18 to 44","3",
iif(Fields!AgeGroup.Value="Ages 45 to 64","4",
iif(Fields!AgeGroup.Value="Ages 65 and Older","5","Other")))))
This basically forces SSRS to sort the groups in the order I specify by assigning Ages 5 and Younger a value of 1, so that it's the first sort group etc. My report now works like a charm!
Or you can modify your source table to include a new column for AGe_Group order. And on the SSRS you can do the sorting based on the new column order. To clarify:
Newcolumn int
Set NewColumn = 1
where AgeGroup = "Ages 5 and Younger"
Set NewColumn = 2
where AgeGroup = ="Ages 6 to 17"
Set NewColumn = 3
where AgeGroup = Ages 18 to 44"

Resources