How to solve set theory in wolfram alpha? - wolframalpha

A={1,2,3} B={1,2,3,4,5}, C={0,2,4,6} is 0 in (B union C)
How can I solve that does 0 is part of B union C?

This
intersection[{0},union[{1,2,3,4,5},{0,2,4,6}]]
gives you
{0}
and so zero is in that union while this
intersection[{8},union[{1,2,3,4,5},{0,2,4,6}]]
gives you
{}
and so 8 is not in that union.
WA link
There may be other ways of accomplishing this with WA, but as always the question is how can you find a way to coax WA into understanding what you want it to do.

Related

New column based on list of values SQL

I am new to SQL and working on a database that needs a binary indicator based on the presence of string values in a column. I'm trying to make a new table as follows:
Original:
Indicator
a, b, c
c, d, e
Desired:
Indicator
type
a, b, c
1
c, d, e
0
SQL code:
SELECT
ID,
Contract,
Indicator,
CASE
WHEN Indicator IN ('a', 'b')
THEN 1
ELSE 0
END as Type
INTO new_table
FROM old_table
The table I keep creating reports every type as 0.
I also have 200+ distinct indicators, so it will be really time-consuming to write each as:
CASE
WHEN Indicator = 'a' THEN '1'
WHEN Indicator = 'b' THEN '1'
Is there a more streamlined way to think about this?
Thanks!
I think the first step is to understand why your code doesn’t work right now.
If your examples of what’s Indicator column are literally the strings you noted (a, b, c in one string and c, d, e in another) you should understand that your case statement is saying “I am looking for an exact match on the full value of Indicator against the following list -
The letter A or
The letter B
Essentially- you are saying “hey SQL, does ‘a,b,c’ match to ‘a’? Or does ‘a,b,c’ match to ‘b’. ?”
Obviously SQL’s answer is “these don’t match” which is why you get all 0s.
You can try wildcard matching with the LIKE syntax.
Case when Indicator like ‘%a%’ or Indicator like ‘%b%’ then 1 else 0 end as Type
Now, if the abc and cde strings aren’t REALLY what’s in your database then this approach may not work well for you.
Example, let’s say your real values are words that are all slapped together in a single string.
Let’s say that your strings are 3 words each.
Cat, Dog, Man
Catalog, Stick, Shoe
Hair, Hellcat, Belt
And let’s say that Cat is a value that should cause Type to be 1.
If you write: case when Indicator like ‘%cat%’ then 1 else 0 end as Type - all 3 rows will get a 1 because the wildcard will match Cat in Catalog and cat in Hellcat.
I think the bottom line is that unless your Indicator values really are 3 letters and your match criteria is a single letter, you very well could be better off writing a 200 line long case statement if you need this done any time soon.
A better approach to consider (depending on things like are you going to have 300 different combinations a week or month or year from now?)
If yes, wouldn’t it be nice if you had a table with a total of 6 rows - like so?
Indicator | Indictor_Parsed
a,b,c | a
a,b,c | b
a,b,c | c
c,d,e | c
c,d,e | d
c,d,e | e
Then you could write the query as you have it case when Indicator_Parsed in (‘a’, ‘b’) then 1 else 0 end as Type - as a piece of a more verbose solution.
If this approach seems useful to you, here’s a link to the page that lets you parse those comma-separated-values into additional rows. Turning a Comma Separated string into individual rows
ON mysql/sql server You can do it as follows :
insert into table2
select Indicator,
CASE WHEN Indicator like '%a%' or Indicator like '%b%' THEN 1 ELSE 0 END As type
from table1;
demo here
You can use the REGEXP operator to check for presence of either a, b or both.
SELECT Indicator,
Indicator REGEXP '.*[ab].*'
FROM tab
If you need that into a table, you either create it from scratch
CREATE your_table AS
SELECT Indicator,
Indicator REGEXP '.*[ab].*'
FROM tab
or you insert values in it:
INSERT INTO your_table
SELECT Indicator,
Indicator REGEXP '.*[ab].*'
FROM tab
Check the demo here.

How to get char from regular expression matching list

I want to get all the characters from regular expression matching list with SQL ORACLE
Example: regular expression matching list is : '[a-c2-5]'
Result is table include rows:
a
b
c
2
3
4
5
This sounds very much like an XY problem (google the phrase to learn what it means). What is the real problem you are trying to solve this way?
In any case - here is one way to solve this. I assume you are only interested in ASCII characters (with ASCII codes between 1 and 255); you may generalize this if needed.
select chr(level) as matched_character
from dual
where regexp_like(chr(level), '[a-c2-5]')
connect by level <= 255
;
MATCHED_CHARACTER
-----------------
2
3
4
5
a
b
c

Create an expression in ACCESS 2010

Thank you in advance! I'm a novice, so plain and simple explanations would greatly be appreciated. I really don't know what I'm doing, but I have grand concepts w/o any idea how to execute them.
I am creating an append query in Access 2010 and I want to identify Col_C as yes, if the word "red" appears in Col_B and everything else as no.
Desired Result
#Col A Col B Col C
1 red y
2 blue n
3 red y
4 green n
5 red blue y
I don't know how to write the if then statement.
INSERT into TABLE1
SELECT [TABLE2].[Col_A], [TABLE2].[CoL_B]
IF [TABLE2].[Col_B] like "*red`*`" then add y to Col_C else n
FROM [TABLE2]
I know it's a bad attempt, but I've been researching and the explanations aren't clear enough for me to proceed.
It could be:
INSERT INTO
TABLE1
([Col_A], [CoL_B], [Col_C])
SELECT
[TABLE2].[Col_A],
[TABLE2].[CoL_B],
IIF([TABLE2].[Col_B] = "red", "y", "n")
FROM
[TABLE2]

SQL Query returning records that have always the same value

I am learning SQL and doing some practice. I cannot give you the exact scenario because the website I'm practicing on don't want any solution to be found directly on the web so I'll explain the situation in other words. The question I am stuck is using a table XYZ with columns X, Y, and Z. Column X can have duplicates and column Z also. what I need to find is the X's that always have the same value in Z. So
X Y Z
1 ? a
1 ? a
2 ? b
2 ? c
3 ? c
3 ? a
would return me 1 because when X is 1 Z is always a.
My real problem is that I feel I am missing some SQL knowledge in order to achiev this. I would appreciate it if anyone can give me a hint, not a solution but maybe a link to the the SQL knowledge im missing or and brief explanation of the SQL statement that could make me do this.
Otherwise have a nice day.
David.
edit: SELECT X FROM XYZ GROUP BY X HAVING COUNT(DISTINCT Z) = 1 worked and I understand it well. Now what I cannot understand is how to add the Z column to the resultset.
select x, min(z)
from tab
group by x
having min(z) = max(z)
-- or
having count(distinct z) = 1
There are several SQL simple functions that could be used to accomplish this.
DISTINCT MSDN http://technet.microsoft.com/en-us/library/ms187831(v=SQL.105).aspx
GROUP BY http://technet.microsoft.com/en-us/library/ms177673.aspx

A simple SQL Select query to crawl all connected people in a social graph?

What is the shortest or fastest SQL select query or SQL procedure to crawl a social graph. Imagine we have this table:
UId FriendId
1 2
2 1
2 4
1 3
5 7
7 5
7 8
5 9
9 7
We have two subset of people here, i'm talking about a sql query or procedure which if we pass:
Uid = 4 return the result set rows with uid : {1, 2, 3}
or if
Uid = 9 return the result set rows with uid : {5, 7, 8}
Sorry for my poor english.
So you want get all friends of someone, including n-th degree friends? I don't think it is possible without recursion.
How you can do that is explained here:
https://inviqa.com/blog/graphs-database-sql-meets-social-network
If you are storing your values in an adjacency list, the easiest way I've found to crawl it is to translate it into a graphing language and query that. For example, if you were working in PHP, you could use the Image_GraphViz package. Or, if you want to use AJAX, you might consider cytoscapeweb. Both work well.
In either case, you'd SELECT * FROM mytable and feed all the records into the graph package as nodes. This means outputting them in dot or GraphML (or other graphing language). Then you can easily query them.
If you don't wish to translate the dataset, consider storing it as nested sets. Nested sets, though a bit of a pain to maintain, are much better than adjacency lists for the kind of queries you are looking to do.
If you are storing your values in an adjacency list, and you want n-th degree you can simply recursively INNER JOIN the UID's. For example:
Select t1.uid, t2.uid, t3.uid FROM t1 INNER JOIN t2 ON t1.uid=t2.uid INNER JOIN t3 ON t2.uid=t3.uid
This query is like a DFS with a fixed depth.