sql import file
--sources (id, name)---
1, "source one"
2, "source two"
3, "source three"
--first_related_items (value, source_id) ---
0, 1
--second_related_items (value, source_id) ---
0, 2
2, 3
--third_related_items (value, source_id) ---
1, 3
I have source rows and related items in multiple other tables. How do I get a computed column based off the other tables? The computed column would be for each row in source table. Like
SELECT
sources.name as name,
COMPUTED_VALUE(
first_related_items,
second_related_items,
third_related_items) as result
FROM sources
The computed column would be derived something like this
if(value for source row in first table == 0){
return 0
}
else if(value for source row in second table == 0){
if(value for source row in third table == 1){
return 1
} else {
return 0
}
} else if(value for source row in third table != NULL){
return value for source row in third table
} else {
return -1
}
You can join all four tables and compute this column as a case expression:
SELECT s.id,
CASE WHEN fri.value = 0 THEN 0
WHEN sri.value = 0 THEN CASE WHEN tri = 1 THEN 1 ELSE 0 END
WHEN tri IS NOT NULL THEN tri
ELSE -1
END AS computed_value
FROM sources s
LEFT JOIN first_related_items fri ON fri.source_id = s.id
LEFT JOIN second_related_items sri ON sri.source_id = s.id
LEFT JOIN third_related_items tri ON tri.source_id = s.id
Related
A B C
1 2 3
4 2 3
1 2 3
I want to compare row1 with row2 and row2 with row3 so on rown with row1.
If they are same I want to print it is as "same" or else "different" in another data frame
output for above table:
A B C
Different same same
Different same same
same same same
For the below code I'm getting
True or false as the output. I want to replace that with Different and same.
compare = t(combn(nrow(Data.matrix),2,FUN=function(x)we2009[x[1],]==Data.matrix[x[2],]))
rownames(compare) = combn(nrow(Data.matrix),2,FUN=function(x)paste0("seq",x[1],"_seq",x[2]))
View(compare)
there are plenty of options how to do that ,
since you add MySQL tag the easiest way is to do that with sql in case you have limited number of columns , you can also use package SQL in r
library(sqldf)
sqldf('select
case when a=b then 'same' else 'different' as a
case when b=c then 'same' else 'different' as b
case when c=a then 'same' else 'different' as c
from my_dataset'
Does this deliver the desired results?
data_test = data.frame(A = c(1,4,1), B = c(2,2,2), C = c(3,3,3))
# create shifted helper-columns
data_test_help = cbind(data_test, data_test[c(2:NROW(data_test), 1),])
# apply comparision on each row
t(apply(data_test_help,1, function(f) f[1:3] == f[4:6]))
# for same, different notation instead of true false
t(apply(data_test_help,1, function(f) ifelse(f[1:3] == f[4:6], "same", "Different")))
I have a list of columns like a, b, c, d, e, f.
Ideally only one of those can have value.
How can I find out the rows that break this rule.
thanks.
Assuming that no value in a column means null (not empty string ''). We can utilize comparison operators IS NOT NULL. It returns 1 if the value is not null; else 1.
Sum of the above-mentioned comparison results for each of the columns should be equal to 1 only (if there is only one non-null column in a row).
SELECT *
FROM your_table_name
WHERE ((a IS NOT NULL) +
(b IS NOT NULL) +
(c IS NOT NULL) +
(d IS NOT NULL) +
(e IS NOT NULL) +
(f IS NOT NULL)) <> 1
DB Fiddle DEMO
You could use:
SELECT *
FROM tab
WHERE LENGTH(CONCAT(
IF(a IS NULL,'','.'),
IF(b IS NULL,'','.'),
IF(c IS NULL,'','.'),
IF(d IS NULL,'','.'),
IF(e IS NULL,'','.'),
IF(f IS NULL,'','.'))) <> 1;
db<>fiddle demo
Sounds like you should not have 6 columns for one value, but instead, 1 column, perhaps plus a second column to say which thing it represents (a..f).
I am trying to create a new column in my DataFrame.
I want the new column to be a*b if the sum of a few other columns is == 0, 1 if the sum is == 1, and 0 otherwise.
The number of columns that I am summing across is dynamic in that it may be 3 columns I am summing across or it could be 100. I have a list of those column names (list_to_check) which could be of any length.
df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['dd','ee','ff'], 'd1':[5,0,1], 'd2':[5,0,1], 'dn':[5,0,1]})
list_to_check = ['d1','d2','dn']
def func(a,b,c):
if sum(c) == 0:
a*b
elif sum(c) == 1:
1
else:
0
df['new_column'] = np.vectorize(func)(df.a,df.b,df[list_to_check])
vals = df[list_to_check].sum(1)
df['new_col'] = 0
df.loc[vals == 0, 'new_col'] = df.a * df.b
df.loc[vals == 1, 'new_col'] = 1
i need a my sql statement which selects something similar to this
SELECT present, wholeday, attendance
present and wholeday is given, while attendance is generated by a combination of present and wholeday
if present == 1 and wholeday == 1 then attendance = 1
if present == 1 and wholeday == 0 then attendance = .5
if present == 0 and wholeday == 0 then attendance = 0
Your query would be:
SELECT PRESENT, WHOLEDAY,
CASE
WHEN (PRESENT = 1 AND WHOLEDAY = 1) THEN 1
WHEN (PRESENT = 1 AND WHOLEDAY = 0) THEN 0.5
ELSE 0
END as ATTENDANCE
FROM MY_TABLE
Case Syntax is :
CASE
WHEN condition_1 THEN commands
WHEN condition_2 THEN commands
...
ELSE commands
END CASE;
Trouble with using Max in where clause of LINQ to SQL. Data below:
QID, Question, TypeID, Disable, VersionID, Sequence
1 Who's on 1st 1 False 1 1
2 Who's on 1st 1 False 2 1
3 What's on 2nd 1 False 1 2
4 What's on 2nd 1 False 2 2
5 I don't know 1 False 1 3
6 I don't know 1 False 2 3
I need to return a group of questions based on the Max of the VersionID as noted below. The result I expect from the data above would include rows 2, 4 & 6 ordered by Sequence.
IEnumerable<QUESTION> questions =
(from q in dataContext.QUESTIONs
where q.TypeID == Convert.ToInt16(ddlType.SelectedValue)
&& (q.Disable == null || q.bDisable == false)
&& (q.VersionID == dataContext.QUESTIONs.Max(q.nVersionID))
orderby q.Sequence ascending
select q);
Max() translates properly in linq-to-sql
Try
IEnumerable<QUESTION> questions = (from q in dataContext.QUESTIONs
let maxVersion = dataContext.QUESTIONs.Max(q.nVersionID)
where q.TypeID == Convert.ToInt16(ddlType.SelectedValue)
&& (q.Disable == null || q.bDisable == false)
&& (q.VersionID == maxVersion)
orderby q.Sequence ascending
select q);