In SQLite3 how to group by those having no certain values? - sqlite

A table named "example" is like this:
enter image description here
I would like to SELECT Name FROM example GROUP BY NAME. But at the same time, if anyone's value contains X, then that one should be excluded from the result. In the "example" table, A has three values and one of them is X so A should be excluded from the result. So does B.
The result produced by SQL clauses should be:
C
D
Could anyone help me write corresponding SQL clauses so that I can get the result I want?
Thank you!!
I tried to write something like this:
SELECT Name FROM example WHERE Value <> X GROUP BY NAME.
It didn't work because A and B still have other values which prevent them from being excluded. I just have no idea of what else I can do. I'm very new in SQL.

A comparison results in either 0 or 1.
So taking the largest result in a group shows whether there is a match in the group:
SELECT Name
FROM Example
GROUP BY Name
HAVING MAX(Value = 'X') = 0;

Related

Align Text To Row Identified by Number and to ID Matching that Embedded in String

I need to align text in an ALERT STRING column with the row identified by number in an ID ROW column.
Additionally, I need to also align the same ALERT STRING text with the same ID ROW number AND with the ID matching that embedded in a string in the TEXT WITH ID column. (This double-check will sometimes be necessary with the real-world data.)
So far, I've only figured out how to align the ALERT STRING with the ID matching that embedded in the TEXT WITH ID column:
=LOOKUP(2,1/SEARCH(A2,$F$2:$F$11),$G$2:$G$11)
I appreciate any help folks can offer. You can find an editable copy of the workbook here:
https://1drv.ms/x/s!ArQ7Kw6ayNMY2zktTW3pDCbMmJZ_
UPDATE: Nayan provided a solution to the first part of this question (please see answer below). I'm still trying to work out a formula for the column D part of this question, in which the row reference shown in column E is combined with a match of the ID shown in column A with its corresponding value in one of the text strings in column F.
The best I've been able to come up with so far is a formula with a high failure rate:
=INDEX($G$2:$G$11,MATCH(ROW(D2),$E$2:$E$11,MATCH("*"&A2&"*",$F$2:$F$11,0)))
Any help with this part of the question will be greatly appreciated.
ROW([reference])
Returns the row number of a reference
E.g.: Row(B2) returns 2. If nothing provided like ROW() will also
return row number based on position of cell where it is called.
VLOOKUP(loolup_value, table_array, col_index_num, [range_lookup])
Looks for a value in the leftmost column of a table, and then returns a value in the same row from a column you specify (col_index_num)
By default - the table must be sorted in an ascending order.
Try this:
=VLOOKUP(ROW(B2),$E$2:$G$11,3,FALSE)
INDEX(array, row_num, [column_num]) INDEX(reference, row_num,
[column_num], [area_num])
Returns a value or reference of the cell at the intersection of a particular row and column, in a given range.
In this case, you have to get row_num with MATCH function.
MATCH(lookup_value, lookup_array, [match_type])
Returns a relative position of an item in an array that matches a specified value in a specified order.
match_type: 1 (Less than), 0 (Exact match), -1 (Greater than)
Try this:
=INDEX($G$2:$G$11,MATCH(ROW(B2),$E$2:$E$11,0))
Identity Data with Multiple Criteria Condition using MATCH()
=INDEX($G$2:$G$11,MATCH(1, (ROW(D2) = $E$2:$E$11) * (ISNUMBER(SEARCH(A2, $F$2:$F$11))),0))
References:
https://exceljet.net/excel-functions/excel-vlookup-function
https://exceljet.net/excel-functions/excel-index-function
https://exceljet.net/formula/index-and-match-with-multiple-criteria
This is the formula I was looking for in column D:
=INDEX($G$2:$G$11,MATCH(ROW(D2)&"*"&A2&"*",INDEX($E$2:$E$11&$F$2:$F$11,),0))
You can see it working here.
Nayan provided a great deal of help with answering this question, so I will mark his answer as the accepted solution.
Syeda Fahima Nazreen provided the example I referenced to figure out the formula shown above.
Reference:
Nested Excel Formula with Two INDEX Functions and a MATCH Function with Multiple Criteria

R Merge 2 tables/dataframes by partial match

I have two tables/dataframes.
The first Table (ID) one looks like this:
The second table (Names) looks like this:
I want to match the "IDTag" variable to the first few letters of the "Name" variable. In other programming languages I would do a foreach and run through each of the IDTags for each of the rows of the second table (matching the IDTag to the first n characters of the "Name" variable where n is the number of characters of the IDTag in question.
In R it seems like there should be a method for doing this and I have looked at pmatch and a few others but those either don't appear to make the match at all or when I try to use them come up with several NAs in places where I wouldn't have expected them (Sample code using the table data above:
NameMatches <- Names[pmatch(
ID$IDTag,
Names$Name,
duplicates.ok = TRUE
),]
I have the feeling I am going about this with the wrong theory or concept so I am looking to see if someone can guide on the simplest/clearest way to do this accurately.
Editing original question to reply to comments...
The expected output would look something like this (i.e. - all of the columns of the Names table with the addition of the Group column from the ID table. Multiple matches are expected - one to many relationship between ID and Names tables):
Thanks,
If you are open to using the sqldf package, then one option would be to just write a join using the logic you gave us:
library(sqldf)
sql <- "SELECT * FROM ID t1 INNER JOIN Names t2
ON t2.Name LIKE t1.IDTag || '%'"
output <- sqldf(sql)
Note: If you want to keep all rows from the ID data frame, regardless of whether or not they match to anything in Names, then use a left join instead.

Select one row in R, result is a new table

I try to select just one row in my data but the result come out in new data and I can't compute the mean of this, because the header is showing. But when I select the column, there is no problem and I can see the result as a value. You can see the result of the code in the photo that y is in data and X is in the value.
y=mydata[1,]
X=mydata[,4]
enter image description here
you should use rowMeans.
When you select a row you are selecting multiple variables and that results in giving you a dataframe.

Inserting and combining data between 2 table

We have a table students with ID,NAME,SURNAME. We want have another table (created) students_2 with ID1,NAME1,SURNAME1.
Starting from table students, i want to fill data in the second table in the following way : I want to have in the second table combinations of names ( example : NAME,SURNAME1; NAME1,SURNAME1). Moreover, i want to generate combination of names.
How can I do that ? I tried something like :
INSERT INTO students_2 (ID1,NAME1,SURNAME1) SELECT ID,NAME,NAME from students;
But it's not correct cause I don't generate combinations, just inserting . A solution is appreciated, but mainly i need ideas.
You could write something like
INSERT INTO students2(NAME, VALUE) FROM
SELECT s1.name, s2.value from students1 s1 cross join students1 s2
This will do Cartesian product , and will get a NxN rows with combinations

What is the difference between these two Postgres query

Select sum(a),sum(b)
from table
where c like '234%'
and time > '2013-12-31'
and d = 'hello';
Then I explicitly calculate the value of sum(a)-sum(b).
Select sum(a-b)
from table
where c like '234%'
and time > '2013-12-31'
and d = 'hello';
As both of these queries are giving different results.
By Doing an EXPLAIN ANALYSE on both of these queries, I see that second query has removed more rows by Filter conditions.
I am not able to find an explanation for this.
The difference is likely due to nulls existing within the data. If you had "a" as 3 and "b" as null then you'd receive a null result on something like:
select 3 - null;
Doing the sums individually will allow each to increment to the actual sums skipping nulls only for that individual column not the entire row and likely be more of what you're looking for.

Resources