sqlite update a column with itself - sqlite

I got a table like this
a b c
-- -- --
1 1 10
2 1 0
3 1 0
4 4 20
5 4 0
6 4 0
The b column 'points' to 'a', a bit like if a is the parent.
c was computed. Now I need to propagate the parent c value to their children.
The result would be
a b c
-- -- --
1 1 10
2 1 10
3 1 10
4 4 20
5 4 20
6 4 20
I can't make an UPDATE/SELECT combo that works
So far I got a SELECT that procuce the c column I'd like to get
select t1.c from t t1 join t t2 on t1.a=t2.b;
c
----------
10
10
10
20
20
20
But I dunno how to stuff that into c
Thanx in advance
Cheers, phi

You have to look up the value with a correlated subquery:
UPDATE t
SET c = (SELECT c
FROM t AS parent
WHERE parent.a = t.b)
WHERE c = 0;

I finnally found a way to copy back my initial 'temp' SELECT JOIN to table 't'. Something like this
create temp table u as select t1.c from t t1 join t t2 on t1.a=t2.b;
update t set c=(select * from u where rowid=t.rowid);
I'd like to know how the 2 solutions, yours with 1 query UPDATE correlated SELECT, and mine that is 2 queries and 1 correlated query each, compare perf wise. Mine seems more heavier, and less aesthetic, yet regarding perf I wonder.
On the Algo side, yours take care not to copy the parent data, only copy child data, mine copy parent on itself, but that's a nop, yet consuming some cycles :)
Cheers, Phi

Related

Join one table with multiple tables in R

I have around 800 data frames(a1,a2,a3...a800) in R and all of them have the same number of columns and column names.I want to a left join table a1 with rest of the 799 tables and store it in an object. Similarly, left join table a2 with the rest of them and store it another object and so on. I am unable to proceed with this! If anyone could help me will be great.
Here is an example
Table a1:
Names ID Time
X 1 2
Y 2 6
Z 3 5
K 4 8
Table a2;
Names ID Time
P 11 8
Q 12 9
R 10 7
Y 2 6
and so on.. I want to join by ID Column. And I have 800 tables!
u can use data.table::rbindlist
dataframe_name_list = list(a1,a2,a3,...a800)
data.table::rbindlist(dataframe_name_list, use.names=TRUE)

Which tool should I use in Alteryx to find values and add new column

I got stuck at this for a long time and couldn't find answer elsewhere.
Below is my data:
Market Start Type(0 or 1)
A 1
A 2
A 4
A 6
A 10
A 2
B 2
B 4
B 6
B 8
B 4
B 9
C 1
C 4
C 7
C 3
C 9
C 11
C 12
And I want to complete the Type column based on following conditions:
If Market is A and Start is 1,2,3, then Type is 1, otherwise 0
If Market is B and Start is 2,4,5, then Type is 1, otherwise 0
If Market is C and Start is 4,6,9, then Type is 1, otherwise 0
In Alteryx, I tried using the formula tool three times:
IIF ( [Market]="A" && ([Start] in (1,2,3),"1","0")
IIF ( [Market]="B" && ([Start] in (2,4,5),"1","0")
IIF ( [Market]="C" && ([Start] in (4,6,9),"1","0")
But the third IIF function overwrites the previous two. Is there any other tools in Alteryx that can do what I want to do? Or is there something wrong with my code?
Thanks in advance. Really appreciate it.
It evaluates to False and places a zero for any market <> "C"... try a single Formula tool with:
IF [Market]="A" THEN
IIF([Start] in (1,2,3),"1","0")
ELSEIF [Market]="B" THEN
IIF([Start] in (2,4,5),"1","0")
ELSEIF [Market]="C" THEN
IIF([Start] in (4,6,9),"1","0")
ENDIF
This should eliminate overlap.

Create a calculated field in tableau

I have a set of data in the following format:
Items Shipped | Month
A 1
B 1
C 1
D 2
E 2
F 3
G 3
H 3
I would like to show the count of items shipped each month using a calculated field in Tableau.
Item_Count | Month
3 1
2 2
3 3
Any Suggestions?
You should probably have a look on the Tableau page for their basic tutorials:
https://www.tableau.com/learn/training
Drag the [month] pill to row (if it's an actual date, change it to discrete month, otherwise leave it like it is)
Drag the [item_count] to columns, click on it and change it to COUNT or COUNTD depending whether you want the total count or only the distinct elements.

Tabulating association frequency counts

I have data which is in this format:
User Item
1 A
1 B
1 C
1 D
2 A
2 C
2 E
What I want to get is a frequency count for each pair. Order is not important so I don't want to count the inverse. I want to end up with a result similar to this, where the frequency counts are partitioned by user.
Pair Frequency
AB 1
AC 2
AD 1
AE 1
BC 1
BD 1
BE 0
CD 1
CE 1
What tool can I use to formulate this kind of table? I'd prefer some open source solution if possible.
Edit- Added example for my comment below
I'm reading in data from a CSV file using the following two lines and removing the factors with these two steps in code.
xa<-read.csv("C:/Direcotry/MyData.csv")
xa<-data.frame(lapply(xa, as.character), stringsAsFactors=FALSE)
User Item
1 394324 Item A
2 124209 Item B
3 212457 Item C
4 427052 Item A
5 118281 Item D
6 156831 Item A
7 212442 Item E
8 156831 Item B
9 212442 Item A
10 177734 Item C
When I try running suggested answer, I get an error with this result:
Error in combn(x, 2) : n < m
Well R is open source.
Here's an example based on your tiny sample of data:
Here I just read your data in by copypasting it straight from your post:
> xa=read.table(stdin(),header=TRUE,as.is=TRUE)
0: User Item
1: 1 A
2: 1 B
3: 1 C
4: 1 D
5: 2 A
6: 2 C
7: 2 E
8:
So that's the data in. Then with a couple of lines of code:
> f=function(x) apply(combn(x,2),2,paste0,collapse="")
> table(unlist(tapply(xa$Item,xa$User,f)))
AB AC AD AE BC BD CD CE
1 2 1 1 1 1 1 1
If you need all the empty combinations explicitly as zeroes it takes another line or two (you need to generate all the possible combinations as a factor, rather than just the observed ones and tell table to include the empty ones).
After some research and suggestions by Glen, I came up with the following code which gets me a 3 column CSV file with the pair combination plus frequency count. If anyone sees a better way, let me know! But this seems to work.
The errors I was referring to in my follow up comments were caused by users having purchased only at one location.
library(reshape2)
xa<-read.csv("C:/Input.csv",as.is=TRUE)
xa=xa[!duplicated(xa),]
xa<-data.table(xa)
setkey(xa,ContactId,PurchaseLocation)
tab=table(xa$ContactId)
xa=xa[xa$ContactId %in% names(tab[tab>1]),]
f=function(x) apply(combn(x,2),2,paste0,collapse="--")
xb<-as.data.frame(table(unlist(tapply(xa$PurchaseLocation,xa$ContactId,f))))
xc=with(xb, cbind(Freq, colsplit(xb$Var1, pattern = "--", names = c('a', 'b'))))
xc=subset(xc,a!=b & a!="" & b!="" & Freq>1)
write.csv(xc,file="C:/Output.csv")
Edit- I made a small change to make it order independent by sorting the data table on a key.

Bill of Materials Query

I currently have 2 tables as follows within my database:
Table: SampleProducts
SampleProductsId (PK) Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
Table: SampleProductsBoms
SampleProductsBomId (PK) ParentId (FK) ChildId (FK) Quantity
1 1 2 3
2 2 3 4
3 4 6 2
ParentId and ChildId both reference SampleProductsId
In English so I can ensure that we are all on the same page:
Product A is made up of 3 of B
Product B is made up of 4 of C
Product D is made up of 2 of F
I would like to create a Stored Procedure / LinQ statement or something which I can use in my MVC 3 c# Web Application which will give me the following table structure / object to use...
Example:
Recursive Query to find the components of B
ProductId Name Quantity
3 C 4
6 F 2
This could go quite deep, so I really do need recursion!
CTE is helpfull for recursing as require in your problem statement check the link
Common Table Expression
or i think following query may also solve your purpose
select components.SampleProductId as productid,components.Name as Name,Quantity
from SampleProductsBOM bom
inner join SampleProducts products
on products.ParentId=bom.ParentId
inner join SampleProducts components
on components.SampleProductId=bom.ChildId
where products.Name='B'

Resources