I have a doubt regarding how to implement this statement with constraints:
Only if A or B or both, then C or D or both.
I want to implement it with constraints that take binary values.
This is an example:
If A then B, means that A=B;
A or B or both, means that A+B>=1.
Thank you so much.
Let me try.
First A => B is not the same as A=B. It is however the same as B >= A. If we want: A <=> B then indeed A=B.
The real question seems to be: A+B>=1 <=> C+D>=1 or
A+B>=1 => C+D>=1
A+B=0 => C+D=0
We can write this as a system of inequalities:
C+D >= A
C+D >= B
C+D <= 2(A+B)
All variables are assumed to be binary.
Related
I am explaining here I have table name TB which contains three columns and data like this
Cl1. Cl2 Cl3
0. X. A
0. Y. B
0. Z. C
1. X. a
1. X. b
1. X. c
1. Z. d
And I output like this
Cl1. Cl2. Cl3. No
0. X. A. 3
0. Y. B. 0
0. Z. C. 1
Here no column shows repetition of Cl2. Value when Cl1.=1 with respective value of Cl2. When Cl1. = 0 means for Cl1. = 1 here we can see for Cl.=1 and Cl2.=X it's value is repeated twice for Cl1.=1 similar when no match found for Cl1.=1 and Cl2.=Y no gives 0
I hope i have described my problem
I did many attempts but I didn't get any solution. My problem goes out of mind.
My few attempts are like
Select Cl1. ,Cl2. ,Cl3. ,IF(Cl2.=(Select Cl2. From TB where Cl1.=1),Cl2. ,0) as no.
From TB
where Cl2.=0
I also try join type statement for the same table but it also didn't help me.
Please guys help me. I am new on stack overflow if I did any mistake in describing my question then sorry
Try this, #Rajesh
SELECT
t1.cl1,
t1.cl2,
t1.cl3,
IFNULL(cnt, 0) as No
FROM
(
SELECT cl1, cl2, cl3 FROM tb WHERE cl1=0
) AS t1
LEFT OUTER JOIN
(
SELECT cl2, COUNT(cl2) AS cnt FROM tb WHERE cl1 <> 0 GROUP BY cl2
) AS t2
ON t1.cl2=t2.cl2
What difference it makes if I use, winner IN ('Subject1','Subject2'); & winner='Subject1' OR winner='Subject2';
Queries for the table 17 in the below link:
https://www.w3resource.com/sql-exercises/sql-retrieve-from-table.php#SQLEDITOR
For lists with two elements it doesn't make a difference.
However, MySQL optimizes IN when the list consists of constant expressions. It basically sorts them and does a binary search through the list. This can be a considerable savings with longer lists. As the documentation explains:
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.
In general, IN is safer and does a better job of capturing the column you want. It is very easy to take conditions like this:
where winner = 'Subject1' OR winner = 'Subject2'
and add another condition:
where winner = 'Subject1' or winner = 'Subject2' and
foo = 'bar'
and this logic is probably not longer what you really want -- because it really means:
where winner = 'Subject1' or
(winner = 'Subject2' and foo = 'bar')
This doesn't happen with IN:
where winner in ('Subject1', 'Subject2') and
foo = 'bar'
If there's an index on the column in question, IN vastly out-performs OR. Experience has shown me that the db consistently doesn't use the index when there's an OR on the column.
If there's no index on the column in question, IN out-performs OR if the list is longer than about 5 (it's faster to do a few serial comparisons than traverse a small BTree of values, which is what the DB turns the list into for execution).
IN is also preferred for readability and avoiding SQL's operator precedence trap if brackets are omitted, ie x = a or x = b and c = d is parsed as x = a or (x = b and c = d) instead of the (perhaps) expected (x = a or x = b) and c = d.
Careful when using NOT:
select col1 from
(
select 1 as col1
union all
select 2 as col1
union all
select 3 as col1
union all
select 4 as col1
)x
where x.col1 NOT IN (2,3,4) ;
----------
col1
1
However
select col1 from
(
select 1 as col1
union all
select 2 as col1
union all
select 3 as col1
union all
select 4 as col1
)x
where x.col1 != 2 OR x.col1 != 3 OR x.col1 != 4 ;
---
col1
1
2
3
4
I've forgotten whatever I used to know about pivots, but this seems to me the reverse. Suppose I have a set of items A, B, C, D, … and a list of attributes W, X, Y, Z. I have in a spreadsheet something like
A B C D
W 1 P 3 Q
X 5 R 7 S
Y T 2 U 4
Z D 6 F 7
where the value of attribute X for item B is 'P'. In order to do some statistics on comparisons, I'd like to change it from table to list, i.e.,
W A 1
X A 5
Y A T
Z A D
W B P
X B R
Y C U
Z C F
W D Q
X D S
Y B 2
Z B 6
Etc.
I can easily write a nested loop macro in the spreadsheet to do it, but is there an easy way to import it into mySQL in the desired format? Queries to get the statistics needed are simple in SQL (and formulas not very hard in a spreadsheet) if the data is in the second format.
Since there apparently isn't a "spreadsheet" tag, I used "excel." :-)
There are a lot of questions that looked similar at first glance, but the five I looked at all wanted to discard one of the indices (A-D or W-Z), i.e. creating something like
W 1
W P
X 5
X R
EDITED
You can use PowerQuery to unpivot tables. See the answer by teylyn for the following question. I have Office 365 and didn't need to install the plugin first. The functionality was already available.
Convert matrix to 3-column table ('reverse pivot', 'unpivot', 'flatten', 'normalize')
Another way to unpivot data without using VBA is with PowerQuery, a free add-in for Excel 2010 and higher, available here: http://www.microsoft.com/en-us/download/details.aspx?id=39379
...
Click the column header of the first column to select it. Then, on the Transform ribbon, click the Unpivot Columns drop-down and select Unpivot other columns.
...
OLD ANSWER
If you import the spreadsheet as is, you can run a query to output the correct format.
For a fixed, small number of items, you can use UNION for each of the columns.
SELECT attr, 'A' AS 'item', A AS 'value'
FROM sheet
UNION
SELECT attr, 'B' AS 'item', B AS 'value'
FROM sheet
UNION
SELECT attr, 'C' AS 'item', C AS 'value'
FROM sheet
UNION
SELECT attr, 'D' AS 'item', D AS 'value'
FROM sheet;
Working example: http://sqlfiddle.com/#!9/c274e7/7
I am fairly new to SQL, Big Query
I have a dataset and I want to retrieve values in column 2 corresponding to the values in column 1 if they satisfy certain conditions. I want to know how to do that. I am using Big Query Platform
Example Dataset D :
Col 1 ; Col 2
A ; 1
B ; 2
C ; 3
D ; 4
E ; 5
Query to retrieve values of col1, col2 such that col2 >2
Expected Output :
C ; 3
D ; 4
E ; 5
I am using big query platform.
According to me,
SELECT col1,col2
FROM [D]
WHERE col2>2
will give col1 and col2 as outputs where col2>2 but the values in col2 may or may not be the ones corresponding to col1.
Am I wrong ? If so, please suggest a query to get necessary output.
If you don't have a row A;5, it won't ever exist in your return. The only time you need to worry about the mismatch is if you're doing a join between one data set of {A, B, C, D, E} and another of {1, 2, 3, 4, 5}. Then every possible combination from A;1, A;2... to ...E;4, E;5 would be output, and filtering on col2 > 2 would produce A;3, B;3, C;3, ..., etc. But that isn't how your data is set up in your question, so don't worry. If you wonder how a select query will work, it's usually okay to just run it, unless it will take hours and consume tons of resources and you have a budget... but it seems more like you're doing homework.
Also don't ask for homework help on stack overflow.
I have two tables, Table A and Table B. I have two attributes L1 and L2 for each table. I am trying output all the rows for both tables where L1 and L2 are equal for both tables. The problem is that L1 an L2 may differ my some small quantity. So when I run:
SELECT * FROM TableA l1 join TableB l2 on l1.L1 =l2.L1 and l1.L2 = l2.L2
I get an empty set even though there are records that do match. How do I resolve this problem?
Example:
L1 = 118.4363 for Table A but for Table B L1 = 118.445428
Instead of checking for equality, check that the difference is below some threshold (e.g., 0.1, as in the example below).
SELECT * FROM
TableA l1, TableB l2
WHERE
ABS(l1.L1-l2.L1) < 0.1
AND
ABS(l1.L2-l2.L2) < 0.1
You will need to devise some tolerance, like say a difference of 0.01. Then compute the absolute value of the two when subtracted and see if it's within your tolerance
SET #tolerance_value = 0.01;
SELECT *
FROM
TableA l1 join
TableB l2
on ABS(l1.L1 - l2.L1) < #tolerance_value and ABS(l1.L2 - l2.L2) < #tolerance_value;
You cannot ask the engine to return the ones which differ in "some small quantity".
You can choose the rows which difference "abs(a - b)" is between two fixed values.
Like rows where a-b > 5 or a - b > x and a - b < x+10. for example
Try using the round function in Sybase or SQL Server so 118 matches 118. For other DBMS find a round equivalent.
mike
In order to #cheeken's answer to work, you must put a semicolon at the last query, otherwise it won't work:
SELECT * FROM
TableA l1, TableB l2
WHERE
ABS(l1.L1-l2.L1) < 0.1
AND
ABS(l1.L2-l2.L2) < 0.1;