The first one below works, but, just wanted to see if there's a better way...
If I'm trying to find all records that start with 'C' with either a flag of 2 or a status of 9, do I need to incorporate the 'C' criteria twice?
i.e.,
"SELECT * FROM mytable WHERE name like 'C%' AND flag = 2 OR name like 'C%' AND status = 9"
Or, is there a way quicker way to write it so that I only need to set 'C%' once?
Logically, AND and OR are distributive to each other, i.e.
a & (b | c) = (a & b) | (a & c)
that means your condition can be rewritten as
name LIKE 'C%' AND (flag = 2 OR status = 9)
OR has lower priority than AND so you need parentheses.
WHERE name LIKE "C%" AND (flag = 2 OR flag = 9)
you can also check for membership of a set of values
... AND flag IN (1, 9)
Related
Try to learn minizinc but after going through examples, may I just confirm that I actually have to write some procedural language if I want to get multiple output or there is a more "natural to minizinc" way to get it.
For example, suppose I want to have all distinct digits add up to 3 the answers should be 0+3 1+2 2+1 3+0 ...
My mininzinc here:
% how to generate more than one result meeting the constraints
int: n=3;
var 0..9: a;
var 0..9: b;
include "alldifferent.mzn";
constraint all_different([a, b]);
constraint a + b = n;
solve satisfy;
output [
"a + b = n \t\n",
show(a), " + ",
show(b), " = ",
show(n)];
produce only 3+0. How to get to the other answers? Thanks for any advice in advance.
I looked at a post for minizinc 1.6 and it seemed to say left out the output statement would produce all the output (Easy way to print full solution (all decision variables) in minizinc). It does not work. Only one is output.
First of all, the default is to print all variables and their values for a solution, not all solutions.
Use the option -a to get all solutions. mzn-gecode --help to see all options. In your case mzn-gecode -a test.mzn which gives:
a + b = n
3 + 0 = 3
----------
a + b = n
0 + 3 = 3
----------
a + b = n
2 + 1 = 3
----------
a + b = n
1 + 2 = 3
----------
==========
Under configuration there is an option to change the default from printing the first solution after satisfaction. Change it to user-defined-behaviour: print all solutions ... You can have output statement, btw, as well.
I'm working with vr_ad package for e. My question is: What is the difference between 2 following macros for modifying registers (suppose foo register consists of 2 fields: field1 and field2):
1)
write_reg foo {.field1 == 1;};
2)
write_reg_fields foo {.field1 = 1};
I really appreciate any help
There is a very important difference between these forms.
In the first, the register value will be generated, using all defined constraints, + the constraint you wrote in this action (field1 == 1). The new generated value will be written to the DUT.
In the second code, what you state is that you want to modify only one field of the register - field1.
What will happen is that vr_ad will get the current value of the register from the e model (the shadow model), change field1 - and will write the new value the the register in the DUT. None of the other register's fields will be changed. Also - there is no check that the value you assign to field1 complies with the constraints defined on this register.
Lets use it like an example:
foo = 0xA8;
field1 = 0x8;
field2 = 0xA;
will write a totally new value in foo and in RTL, and that value(using your example above where field1 == 1) will be 0x01;
will change only that filed you constrained and value of foo in this case will be 0xA1;
But except that I wanted to say that in fist example you could also get the same result by doing following:
var tem_read_val: uint(bits: 8);
read_reg foo TO tem_read_val;
tem_read_val[3:0] = field1;
write_reg foo read_reg;
=> foo is 0xA1;
From the beginning I was not aware of write reg filds, so I had to use something like this.
In PostgreSQL this is a valid query:
SELECT case 2+2 when 1 then 2 else 3 end
If I put a complex subquery instead of '2+2' it still works well. But how can I change this query if i want to know if the result is smaller than a specific number?
For example this one doesn't work:
SELECT case 2+2 when > 1 then 2 else 3 end
There are two forms of the CASE statement in SQL, described in the PostgreSQL manual here.
One, which you are using, compares a particular expression to a series of values, like a switch statement in C or PHP:
CASE something WHEN 1 THEN 'hello' ELSE 'goodbye' END
The other is a more general set of branching conditions, like an if-elseif-else sequence, or PHP's switch(true). The above can also be written like this:
CASE WHEN something = 1 THEN 'hello' ELSE 'goodbye' END
So to use any comparison other than =, you need the "if-like" version. In your example:
SELECT CASE WHEN 2+2 > 1 THEN 2 ELSE 3 END
select case when 2+2 > 1 then this else that end
Hi is there a possibility to make a distinct select request to mnesia ?
I could copy the content of one table to ets and since ets is a hash table it could work. But i thought there is maybe a more elegant solution to this problem.
Thank you.
I'm not sure if this is what you had in mind, but you could use QLC's {unique, true} option (See QLC documentation for more info).
I created a mnesia table, called test, with bag semantics. Each row consists of the table name, a Key and a Value, so my rows looked like:
1. test, 1, 1
2. test, 2, 1
3. test, 2, 2
4. test, 3, 1
5. test, 3, 2
6. test, 3, 3
... etc.
Then this simple module illustrates my approach. Notice that you have to include the qlc library and that, in my example, I am selecting distinct Keys.
-module(test).
-export([select_distinct/0]).
-include_lib("stdlib/include/qlc.hrl").
select_distinct()->
QH = qlc:q( [K || {_TName, K, _V} <- mnesia:table(test)], {unique, true}),
F = fun() -> qlc:eval(QH) end,
{atomic, Result} = mnesia:transaction(F),
Result.
Compiling and running
> c("/home/jim/test", [{outdir, "/home/jim/"}]).
> test:select_distinct().
> [4,1,2,3,5]
If you wanted sorted output then use the following version of the QH = ... line above
QH = qlc:sort(qlc:q( [K || {_TName, K, _V} <- mnesia:table(test)], {unique, true})),
If you wanted to select distinct values, the following would work:
QH = qlc:sort(qlc:q( [V || {_TName, _K, V} <- mnesia:table(test)], {unique, true})),
Again, the code is just to illustrate an approach
For keys you can get a list of unique keys using:
mnesia:all_keys(Table).
From my tests, for bags it yields a list of unique keys.
I need to code up a query for something like this:
Select [something]
Where
condition in
case
when (if another_condition = A and 3rd Condition = B) then (C,D)
when (if another_condition = N and 3rd Condition = E) then (F,G)
else (J,K)
end
essentially, what I want is if A and B are met, condition could be set to either C or D, if N or E are met, then condition could be set to F or G, else condition set to J or K.
However, when I run this, I kept getting
Incorrect syntax near the keyword 'Case'.
Please help! Thanks!
Maybe this:
Where (Another_Condition = 'A' And Third_Condition = 'B' And Condition in ('C','D'))
Or
(Another_Condition = 'N' and Third_Condition = 'E' And Condition in ('F','G'))
Or
Condition In ('J','K')
Be very careful about mixing and's and or's in a where clause. Parenthesis are important.
How about this - the UNION subquery will give you the full result set within the subquery. Then you can say 'WHERE condition IN ' (subquery). Like this:
SELECT [something]
WHERE
condition IN
(SELECT CASE WHEN (another_condition = A AND 3rd Condition = B) THEN C
WHEN (another_condition = N AND 3rd Condition = E) THEN F
ELSE J
END AS Value
UNION
SELECT CASE WHEN (another_condition = A AND 3rd Condition = B) THEN D
WHEN (another_condition = N AND 3rd Condition = E) THEN G
ELSE K
END AS Value
)
I'd probably go with G Mastro's approach of expanding the query as a Boolean expression. While the nested query approach will work, the intent of the code is less obvious IMO.
Having said that, if there are a lot of cases in your CASE statement, you may want to consider reshaping your data, because no matter how you write the query, it boils down to a big Boolean expression.