SQL Basket Analysis grouping query - sql

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 `

Related

how can I create recursion with all previous lines in t-sql

Initial data: there are conditionally 100 people who eat nuts, sweets, apples ... in total, the maximum amount of apples, sweets, nuts, 1000 pieces for all. These people eat 2 meals a day and each eats a different amount of sweets, apples and nuts. How many nuts, sweets, apples they ate for the first time is known.
We need to calculate how much they can eat in the second meal, if each person can eat up to 10 sweets, apples and nuts per day.
They take turns eating and I need to calculate how much each of them can still eat these sweets, apples and nuts today. Thus, I need to calculate how much of everything is left from the original 1000 pieces of nuts, sweets, apples after each person. And how much more each of these people can eat nuts, sweets, apples, taking into account the last meal and taking into account that they can eat up to 10 pieces of nuts, sweets, apples per day
I think this should be solved by recursion and in each iteration use the previous records and the current record. Or can you offer me some other way to solve it?
My wrong desicion
There is a table:
select 1 num, 1 s, 50 ss, 11 b, 20 bs
union
select 2 num, 1 s, 101 ss, 11 b, 50 bs
union
select 3 num, 2 s, 103 ss, 12 b, 50 bs
I need to recursively connect each of these lines sequentially in each iteration by num with myself and with the previous lines.
1 iteration:
[n num s ss b bs num_ s_ ss_ b_ bs_
1 1 1 50 11 20 1 1 50 11 20][1]
2 iteration:
[n num s ss b bs num_ s_ ss_ b_ bs_
2 2 1 101 11 50 2 1 50 11 20
2 2 1 101 11 50 2 1 101 11 50][2]
3 iteration:
[n num s ss b bs num_ s_ ss_ b_ bs_
3 3 2 103 12 50 3 1 50 11 20
3 3 2 103 12 50 3 1 101 11 50
3 3 2 103 12 50 3 2 103 12 50][3]
Total:
enter image description here
At each iteration, I will do some more calculations, so I'm looking for a way to output just such a recursion with one computational part.
Below is my attempt, but it fails to add the current anchor part to the iteration:
with a as
(
select 1 num, 1 s, 50 ss, 11 b, 20 bs
union
select 2 num, 1 s, 101 ss, 11 b, 50 bs
union
select 3 num, 2 s, 103 ss, 12 b, 50 bs
)
--select * from res
,
rec as (
select 1 n, a.num, a.s, a.ss, a.b, a.bs, a.num num_, a.s s_, a.ss ss_, a.b b_, a.bs bs_ from a where num =1
union all
select n+1, a.num, a.s, a.ss, a.b, a.bs, rec.num num_ , rec.s s_, rec.ss ss_, rec.b b_, rec.bs bs_ from rec,a
where a.num=rec.num+1-- and a.num<>rec.num_
)
select top 20 * from rec order by n,num, num_
I find it hard to understand what your need. You mention recursion, iterations, and calculations, but I see such things nowhere. The result set you indicate can be had with this simple self-join:
with a as
(
select 1 num, 1 s, 50 ss, 11 b, 20 bs
union
select 2 num, 1 s, 101 ss, 11 b, 50 bs
union
select 3 num, 2 s, 103 ss, 12 b, 50 bs
)
select *
from
a
inner join a a_prev on a_prev.num<=a.num
order by a.num,a_prev.num

Group By and get top N in Simple SQL

I have following table in SQLite
BANK:
user-id sender-name receiver-name amount
----------------------------------------
1 A B 200
2 A C 250
3 A B 400
4 A B 520
4 A D 120
4 A D 130
4 A B 110
4 A B 300
4 A B 190
4 A C 230
4 A B 110
4 A C 40
4 A C 80
I need to find out top 3 transaction from each receiver. There are multiple solutions provided for several other database which is not compatible with SQLite cause of the use of certain functions like PARTITION and RANK and even user-defined variables.
I need the solution in simple SQL queries to allow use with SQLite.
Expected result:
receiver-name amount
--------------------
B 560
C 1220
D 250
I managed to do it with using only simple function with self-join.
Now you can just update N with your preferred value, for my case top 3, it would be LIMIT 3.
SELECT receiver-name ,(
SELECT SUM(amount) as sum_amount
FROM (
SELECT amount
FROM bank as b2
WHERE b2.receiver-name = b.receiver-name
ORDER BY b2.amount DESC
LIMIT 3
)
) as sum_amount
FROM bank as b
GROUP BY receiver-name

Select row referring to another row conditionally sqlite

I have a table in which I want to calculate some row values based on another row value conditionally.
The table looks like this
LevelID StepID Amt Type BItem PItem
-------------------------------------------------
6 3 18000 Fixed BS
6 3 10 Percent BS UA
6 3 10 Percent BS TA
6 3 3.5 Percent BS Tx
7 3 24000 Fixed BS
7 3 10 Percent BS UA
7 3 10 Percent BS TA
7 3 3.5 Percent BS Tx
The aim is to calculate the Amt value of rows with Type = 'Percent' Where Bitem = PItem and LevelID and StepID are the same for both BItem and PItem
So a sample output would be
LevelID StepID Amt Type BItem PItem Total
----------------------------------------------------------
6 3 18000 Fixed BS 18,000
6 3 10 Percent BS UA 1,800
6 3 10 Percent BS TA 1,800
6 3 3.5 Percent BS Tx 630
7 3 24000 Fixed BS 24,000
7 3 10 Percent BS UA 2,400
7 3 10 Percent BS TA 2,400
7 3 3.5 Percent BS Tx 840
I have been trying different SQL statements, but haven't got any reasonable output yet
To look up values, use a correlated subquery:
SELECT *,
CASE Type
WHEN 'Fixed' THEN Amt
ELSE (SELECT B.Amt * ATable.Amt / 100
FROM ATable AS B
WHERE B.Type = 'Fixed'
AND B.LevelID = ATable.LevelID
AND B.StepID = ATable.StepID
AND B.PItem = ATable.BItem)
END AS Total
FROM ATable;

PROC SQL SAS PROGRAMMING

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.

Grouping query into group and subgroup

I want to group my data using SQL or R so that I can get top or bottom 10 Subarea_codes for each Company and Area_code. In essence: the Subarea_codes within the Area_codes where each Company has its largest or smallest result.
data.csv
Area_code Subarea_code Company Result
10 101 A 15
10 101 P 10
10 101 C 4
10 102 A 10
10 102 P 8
10 102 C 5
11 111 A 15
11 111 P 20
11 111 C 5
11 112 A 10
11 112 P 5
11 112 C 10
result.csv should be like this
Company Area_code Largest_subarea_code Result Smallest_subarea_code Result
A 10 101 15 102 10
P 10 101 10 102 8
C 10 102 5 101 4
A 11 111 15 112 10
P 11 111 20 112 5
C 11 112 10 111 5
Within each Area_code there can be hundreds of Subarea_codes but I only want the top and bottom 10 for each Company.
Also this doesn't have to be resolved in one query, but can be divided into two queries, meaning smallest is presented in results_10_smallest and largest in result_10_largest. But I'm hoping I can accomplish this with one query for each result.
What I've tried:
SELECT Company, Area_code, Subarea_code MAX(Result)
AS Max_result
FROM data
GROUP BY Subarea_code
ORDER BY Company
;
This gives me all the Companies with the highest results within each Subarea_code. Which would mean: A, A, P, A-C for the data above.
Using sqldf package:
df <- read.table(text="Area_code Subarea_code Company Result
10 101 A 15
10 101 P 10
10 101 C 4
10 102 A 10
10 102 P 8
10 102 C 5
11 111 A 15
11 111 P 20
11 111 C 5
11 112 A 10
11 112 P 5
11 112 C 10", header=TRUE)
library(sqldf)
mymax <- sqldf("select Company,
Area_code,
max(Subarea_code) Largest_subarea_code
from df
group by Company,Area_code")
mymaxres <- sqldf("select d.Company,
d.Area_code,
m.Largest_subarea_code,
d.Result
from df d, mymax m
where d.Company=m.Company and
d.Subarea_code=m.Largest_subarea_code")
mymin <- sqldf("select Company,
Area_code,
min(Subarea_code) Smallest_subarea_code
from df
group by Company,Area_code")
myminres <- sqldf("select d.Company,
d.Area_code,
m.Smallest_subarea_code,
d.Result
from df d, mymin m
where d.Company=m.Company and
d.Subarea_code=m.Smallest_subarea_code")
result <- sqldf("select a.*, b.Smallest_subarea_code,b.Result
from mymaxres a, myminres b
where a.Company=b.Company and
a.Area_code=b.Area_code")
If you already doing it in R, why not use the much more efficient data.table instead of sqldf using SQL syntax? Assuming data is your data set, simply:
library(data.table)
setDT(data)[, list(Largest_subarea_code = Subarea_code[which.max(Result)],
Resultmax = max(Result),
Smallest_subarea_code = Subarea_code[which.min(Result)],
Resultmin = min(Result)), by = list(Company, Area_code)]
# Company Area_code Largest_subarea_code Resultmax Smallest_subarea_code Resultmin
# 1: A 10 101 15 102 10
# 2: P 10 101 10 102 8
# 3: C 10 102 5 101 4
# 4: A 11 111 15 112 10
# 5: P 11 111 20 112 5
# 6: C 11 112 10 111 5
There seems to be a discrepancy between the output shown and the description. The description asks for the top 10 and bottom 10 results for each Area code/Company but the sample output shows only the top 1 and the bottom 1. For example, for area code 10 and company A subarea 101 is top with a result of 15 and and subarea 102 is 2nd largest with a result of 10 so according to the description there should be two rows for that company/area code combination. (If there were more data there would be up to 10 rows for that company/area code combination.)
We give two answers. The first assumes the top 10 and bottom 10 are wanted for each company and area code as in the question's description and the second assumes only the top and bottom for each company and area code as in the question's sample output.
1) Top/Bottom 10
Here we assume that the top 10 and bottom 10 results for each Company/Area code are wanted. If its just the top and bottom one then see (2) later on (or replace 10 with 1 in the code here). Bottom10 is all rows for which there are 10 or fewer subareas for the same area code and company with equal or smaller results. Top10 is similar.
library(sqldf)
Bottom10 <- sqldf("select a.Company,
a.Area_code,
a.Subarea_code Bottom_Subarea,
a.Result Bottom_Result,
count(*) Bottom_Rank
from df a join df b
on a.Company = b.Company and
a.Area_code = B.Area_code and
b.Result <= a.Result
group by a.Company, a.Area_code, a.Subarea_code
having count(*) <= 10")
Top10 <- sqldf("select a.Company,
a.Area_code,
a.Subarea_code Top_Subarea,
a.Result Top_Result,
count(*) Top_Rank
from df a join df b
on a.Company = b.Company and
a.Area_code = B.Area_code and
b.Result >= a.Result
group by a.Company, a.Area_code, a.Subarea_code
having count(*) <= 10")
The description indicated you wanted the top 10 OR the bottom 10 for each company/area code in which case just use one of the results above. If you want to combine them we show a merge below. We have added a Rank column to indicate the smallest/largest (Rank is 1), second smallest/largest (Rank is 2), etc.
sqldf("select t.Area_code,
t.Company,
t.Top_Rank Rank,
t.Top_Subarea,
t.Top_Result,
b.Bottom_Subarea,
b.Bottom_Result
from Bottom10 b join Top10 t
on t.Area_code = b.Area_code and
t.Company = b.Company and
t.Top_Rank = b.Bottom_Rank
order by t.Area_code, t.Company, t.Top_Rank")
giving:
Area_code Company Rank Top_Subarea Top_Result Bottom_Subarea Bottom_Result
1 10 A 1 101 15 102 10
2 10 A 2 102 10 101 15
3 10 C 1 102 5 101 4
4 10 C 2 101 4 102 5
5 10 P 1 101 10 102 8
6 10 P 2 102 8 101 10
7 11 A 1 111 15 112 10
8 11 A 2 112 10 111 15
9 11 C 1 112 10 111 5
10 11 C 2 111 5 112 10
11 11 P 1 111 20 112 5
12 11 P 2 112 5 111 20
Note that this format makes less sense if there are ties and, in fact, could generate more than 10 rows for a Company/Area code so you might just want to use the individual Top10 and Bottom10 in that case. You could also consider jittering df$Result if this a problem:
df$Result <- jitter(df$Result)
# now perform SQL statements
2) Top/Bottom Only
Here we give only the top and bottom results and the corresponding subareas for each company/area code. Note that this uses an extension to SQL supported by sqlite and the SQL code is substantially simpler:
Bottom1 <- sqldf("select Company,
Area_code,
Subarea_code Bottom_Subarea,
min(Result) Bottom_Result
from df
group by Company, Area_code")
Top1 <- sqldf("select Company,
Area_code,
Subarea_code Top_Subarea,
max(Result) Top_Result
from df
group by Company, Area_code")
sqldf("select a.Company,
a.Area_code,
Top_Subarea,
Top_Result,
Bottom_Subarea
Bottom_Result
from Top1 a join Bottom1 b
on a.Company = b.Company and
a.Area_code = b.Area_code
order by a.Area_code, a.Company")
This gives:
Company Area_code Top_Subarea Top_Result Bottom_Result
1 A 10 101 15 102
2 C 10 102 5 101
3 P 10 101 10 102
4 A 11 111 15 112
5 C 11 112 10 111
6 P 11 111 20 112
Update Correction and added (2).
Above answers are fine to fetch max result.
This solves the top10 issue:
data.top <- data[ave(-data$Result, data$Company, data$Area_code, FUN = rank) <= 10, ]
In this script the user declares the company. The script then indicates the max top 10 results (idem for min values).
Result=NULL
A <- read.table(/your-file.txt",header=T,sep="\t",na.string="NA")
Company<-A$Company=="A" #can be A, C, P or other values
Subarea<-unique(A$Subarea)
for (i in 1:length(unique(A$Subarea)))
{Result[i]<-max(A$Result[Company & A$Subarea_code==Subarea[i]])}
Res1<-t((rbind(Subarea,Result)))
Res2<-Res1[order(-Res1[,2]),]
Res2[1:10,]