Can someone explain the purpose of using bitwise operators(like BIT_OR) in MySQL queries. For example, if have a table such as following:
What is the purpose of aggregate operation like:
SELECT name FROM table GROUP BY name HAVING BIT_OR(value) = 0;
What exactly does the BIT_OR do? I understand the actual operation of converting two integers to binary and determining if each pair of corresponding digits are either 0 or 1(if at least one of them is a 1), but what happens with varchar or other non-number columns columns? I know for example, SUM aggregate function can give me the sum of a column of each group. Likewise, what does BIT_OR tell me for each group?
**NOTE:**I randomly created the above table and query - it doesn't illustrate any specific problem
Related
Why use union in Sql at all when there must be same order of columns and it's names in the select statements. Couldn't we just update or alter the table to add more rows ?
The reason that you would not just keep adding rows to a single table is that they don't belong in that table.
For the same reason that if you're doing arithmetic between two integer variables like x + y, you don't permanently add the value of y to x. You want to preserve x's value as it's own thing, even though sometimes you also need the sum.
A book like SQL and Relational Theory: How to Write Accurate SQL Code makes clear that there's a difference between a relation and a relvar. A relvar is like a table. It's a persistent storage of a specific set of rows.
A relation is the result of a SQL expression like SELECT or VALUES. That relation may not be stored in any relvar; it is ephemeral. Perhaps it's the result of a more complex query that uses expressions, joins, and so on.
By analogy, the a number like 42 is an integer value. But int x that stores an integer value 42 is an integer variable. They can both be used as an operand for + but they're not the same kind of thing.
You can UNION two relations, if their columns are compatible in number and data type. Those relations aren't necessarily just relvars, they could be the result of other subqueries.
Just like in arithmetic, you can add x and a whole other integer expression.
Is tableau has a opportunity to create calculated column for table? For example here are 2 column with some number and I want to make third column with formula like this
If(column1>column2;column1;column2. I see there is option to create calculated field with summarize columns but I just want to compare them.
If you only want to create a new column storing the max between 2 original column, you just need to create a calcultaed field like the following:
if column1 > column2
then column1
else column2
end
EDIT
Or even simpler
MAX([column1], [column2])
There are two (overloaded) functions named MAX(). With one argument, MAX() is an aggregate function that operates on a column of values and returns the maximum - just like other aggregate functions like SUM(), AVG() etc. With two arguments, MAX() is a record level function that operates on an individual data row and returns the larger of its two arguments. MIN() behaves similarly.
I have a MySQL column which contains a string of scores separated by a semi-colon eg: "5;21;24;25;26;28;117".
This column was created not by design, but by collecting the values from multiple rows in a table using GROUP_CONCAT and GROUP BY. The original data arrived as a spreadsheet with multiple rows with the ID value.
I can use a select clause with REPLACE function to replace the ; with a +.
SELECT values, REPLACE(values,";","+") AS score FROM [table_name] WHERE 1
values score
5;21;24;25;26;28;117 5+21+24+25+26+28+117
However what I need is the sum of: 5+21+24+25+26+28+117 to get a total of 246.
Is there any way to do this in MySQL without using some other scripting language?
The SELECT clause shows me a string of numbers joined with the + symbol.
Am looking for a way to evaluate that string to give me the result: 246
UPDATE:
As I was framing my question, I did more research and came up with this link which solves my problem:
(https://dba.stackexchange.com/questions/120747/evaluate-a-string-value-as-a-computed-expression-in-an-sql-statement-sthg-like).
Am keeping this question and the link to the answer here in case it could help other people searching for the same.
Is there a way to add all the marks under total Column?
This is my school project work.
The Question is to create a query and show ROll Number and Name and total marks
obtained by each student using both the table while adding fields in query.
There is no intrinsic function to sum multiple fields. If your table structure were normalized could do a CROSSTAB query. Otherwise, build a simple SELECT query that constructs the Total field with an expression:
[Bengali]+[English]+[Maths]+[Physics]+[Chemistry].
Be aware that if any field is Null the expression will return Null. If needed, deal with possible Null in each field: Nz([Bengali],0) or some recommend Iif([Bengali] Is Null, 0, [Bengali]).
There is no need to have Total field in table. Calculate the value when needed. However, starting with Access2010 could use a Calculated type field in table. Some developers despise their use but they do work. They do have limitations, for instance the Nz() will not work nor will Is Null. IIf(IsNull([Bengali]),0,[Bengali]).
What would be the difference between doing:
SELECT person FROM population WHERE id = 1 or id = 2 or id = 3
and -
SELECT person FROM population WHERE id IN (1,2,3)
Are they executed the exact same way? What difference is there? Would there ever be a reason where one would you IN rather than multiple ='s?
No, they perform the same thing. The IN minimizes the query string. That's all. Such statements help in query optimization.
One difference in these two comparison operators would be that IN uses a SET of values to compare, unlike the "=" or "<>" which takes a single value.
According to the manual:
if expr is equal to any of the values in the IN list, else returns 0.
If all values are constants, they are evaluated according to the type
of expr and sorted. The search for the item then is done using a
binary search. This means IN is very quick if the IN value list
consists entirely of constants.