PROC SQL SAS PROGRAMMING - sql

I have the following dataset:
Name Address Bank_Account Ph_NO IP_Address Chargeoff
AJ 12 ABC Street 1234 369 12.12.34 0
CK 12 ABC Street 1234 450 12.12.34 1
DN 15 JMP Street 3431 569 13.8.09 1
MO 39 link street 8421 450 05.67.89 1
LN 12 ABC Street 1234 340 14.75.06 1
ST 15 JMP Street 8421 569 13.8.09 0`
Using this dataset I want to create the below view in SAS:
Name CountOFAddr CountBankacct CountofPhone CountOfIP CountCharegeoff
AJ 3 3 1 2 2
CK 3 3 2 2 3
DN 2 1 2 2 1
MO 1 2 2 1 2
LN 3 3 1 1 2
ST 2 2 2 2 2
The output variables indicates as follows :
-CountOfAddr : For AJ countOFAddr is 3 which means that AJ Shares its address with itself, CK and LN
-CountBankAcct : For MO count of BankAcct is 2 which means that MO Shares its bank account number with itself and ST.Similarly for variables CountofPhone and CountOfIP.
-CountChargeoff: This one is a little tricky it basically implies that AJ is Linked to CK And LN through address...and both CK and LN have been charged off so the countChargeoff for AJ is 2.
For CK the countChargeOff is 3 because it is linked with itself, MO through Bank Account, and LN/AJ through street address...so total chargeoff in CK's Network is 3(CO count of AJ+CO count of CK+CO Count of MO+CO count of LN)
I currently work as a Risk Analyst in a Financial Service Firm and the code for this problem may help us to significantly reduce funding of fraudulent accounts.
Thanks.

SQL Fiddle Demo
SELECT
Name,
(SELECT Count(Address)
FROM dataset d2
WHERE d1.Address = d2.Address
) CountOFAddr,
(SELECT Count(Bank_Account)
FROM dataset d2
WHERE d1.Bank_Account = d2.Bank_Account
) CountBankacct,
(SELECT Count(Ph_NO)
FROM dataset d2
WHERE d1.Ph_NO = d2.Ph_NO
) CountofPhone,
(SELECT Count(IP_Address)
FROM dataset d2
WHERE d1.IP_Address = d2.IP_Address
) CountOfIP,
(SELECT count(d2.Chargeoff)
FROM dataset d2
WHERE d1.name <> d2.name
and ( d1.Address = d2.Address
or d1.Bank_Account = d2.Bank_Account
or d1.Ph_NO = d2.Ph_NO
or d1.IP_Address = d2.IP_Address
)
) CountCharegeoff
FROM dataset d1
I Include the charge off calculation.
Bring all d2 <> d1.name where have any field in common. Then count that.

Related

Is there a Teradata equivalent to R’s rbind?

I’m trying to combine table1 (with columns “id”, “state”, “cost”) and table2 (with same columns “id”, “state”, “cost”) such that all rows are combined into a single table3. There may be duplicates for “id” and/or “state” but all duplicates should be retained.
Table1:
Id
State
Cost
1
IL
50
3
CA
10
2
WY
70
Table2:
Id
State
Cost
4
NY
100
3
PA
15
6
FL
5
Goal table3:
Id
State
Cost
1
IL
50
3
CA
10
2
WY
70
4
NY
100
3
PA
15
6
FL
5
This would be a simple rbind using R but I’m probably overthinking it in Teradata.
Thanks!

Aggregate from two tables

I have two tables
Table 'Sales Line' (SL)
Date "Entry No" Item Qty
(PK)
01/01/2018 1 ABC 1
01/02/2018 2 ABC 2
03/02/2018 3 DEF 1
04/06/2018 4 DEF 3
01/01/2019 5 DEF 1
06/06/2019 6 ABC 2
Table 'Cost Breakdown' (CB)
"SL Entry No" Cost
(FK)
1 10
1 15
2 5
3 25
4 10
4 10
5 5
6 5
6 10
Expected result:
Item Tot_Qty Tot_Cost
ABC 3 30
DEF 4 45
Note that I'm only interested on transaction in 2018 only.
How do I aggregate Tot_Qty and Tot_Cost ? Thank you
With query suggested by #GMB, the result is :
Item Tot_Qty Tot_Cost
ABC 4 30
DEF 7 45
so, line from SL will be repeated as many as correponding number of lines in CB.
You can join both tables, filter on the date, and aggregate by item:
select sl.item, sum(sl.qty) tot_qty, sum(sl.qty * cb.cost) tot_cost
from sales_line sl
inner join cost_breakdown cb on cb.sl_entry_no = sl.entry_no
where sl.date >= '20180101' and sl.date < '20190101'
group by sl.item

SQL Basket Analysis grouping query

I am trying to build a query to create a basket analysis of products based on their type grouping. They have two levels of grouping beyond the product ids.
Dept level
1
2
3
4
and buying group level
MA
M
MC
WA
W
WC
KA
KC
K
the hierarchy is
1 > W, WC
2 > M, MC
3 > K, KC
4 > MA, KA, WA
right now the query I have
Select
i.buying_group,
Sum(d.sales),
Sum(d.units),
count(distinct d.trans_nbr) transaction_count
From
sales_details d, item_data i, (select trans_nbr from sales_details where item_dept = 1 group by trans_nbr) main_group
Where
d.trans_nbr = main_group.trans_nbr
d.item_nbr = i.item_nbr
group by i.buying_group;
Right now I get the data that I need for most of the buying groups but because this is being run at the dept level it does not give me the correct basket information for W and WC. Is there a way to do this at the dept level that would show if the customer bought something from either of these groups and had the other in their basket without double counting it?
the results at the moment are something like this
buyyin_group Sum(sales) Sum(units) transaction_count
MA 100 5 4
M 75 3 3
MC 56 1 1
WA 48 3 2
W 250 6 6
WC 200 9 9
KA 164 7 5
KC 400 12 7
K 521 14 12 `

MS Access 2010 joining with sum

I am trying to combine two tables but my results are showing incorrect data from one table.
One example is my first table has the following:
PositionCode AcctUnit BudgetFTE
JDFF HRT 2
my second table does not contain any record for PositionCode=JDFF and AcctUnit=HRT however I get the following results with running the query below:
PositionCode AcctUnit BudgetFTE ActualFTE VarianceFTE
JDFF HRT 2 1 -1
sql code:
SELECT
b.PositionCode,
b.AcctUnit,
e.POSITION,
e.HM_ACCT_UNIT,
b.FTE AS BudgetFTE,
sum(e.FTE) AS ActualFTE,
sum(e.FTE) - b.FTE AS VarianceFTE
FROM
QryBudgetRollUp AS b
LEFT JOIN ActiveEmployees AS e ON
( b.PositionCode = e.POSITION ) AND
( e.HM_ACCT_UNIT = b.AcctUnit )
GROUP BY
b.PositionCode,
b.AcctUnit,
e.POSITION,
e.HM_ACCT_UNIT,
b.FTE,
e.FTE - b.FTE
sample data:
QryBudgetRollup
PositionCode AcctUnit FTE
JDFF HRT 2
VIPP HRT 1
HROPSA CMP 1
ActiveEmployees
C E CE LAST_NAME FIRST_NAME EMP PROCESS HM_ACCT_UNIT D POSITION D;P' Date SC Expr1013 AP PG HC FTE PE
2 2343 22343 Doe John FT CHRE CMP CMP HROPSA CMP;HROPSA 2/15/1999 H $20.00 $40,000.00 4 1 1 $4,000.00
2 2515 22515 Jetson George PT CHRE CMP CMP HROPSA CMP;HROPSA 4/22/2014 H $10.00 $20,000.00 2 1 0.5 $2,000.00
4 18 418 Doe Jane FT CSIS HRT HRT VIPP HRT;VIPP 11/1/2002 S $40.00 $80,000.00 7 1 1 $8,000.00
Desired Query Results
PositionCode AcctUnit BudgetFTE ActualFTE VarianceFTE
JDFF HRT 2 0 2
VIPP HRT 1 1 0
HROPSA CMP 1.5 2 -0.5

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3
table 1
ID name: status : age
1 john F 28
2 peter G 20
3 Roger K 67
Table 2:
ID name: status : age
1 john Y 28
2 peter J 20
3 Roger K 67
4 trump U 120
5 Donald F 450
Table 3 should contain the updated values
1 john Y 28
2 peter J 20
3 Roger K 67
I need to get the updated status of IDs present in table 1 in table 3 how can I do that.
Note: I am using exacttarget SQL activity and update and many more functionalities does not work so I need some work around> I have tried this but this does not work.
UPDATE
1C-C1-MatchStatus_72hoursSubscribers
SET
1C-C1-MatchStatus_72hoursSubscribers.current_status = B.current_status
FROM
1C-C1-MatchStatus_72hoursSubscribers A
INNER JOIN
a_query B
ON
A.current_status = B.current_status