let
G=DiMultigraph(4)
add_edge!(G,2,4)
add_edge!(G,4,1)
add_edge!(G,4,1)
add_edge!(G,1,3)
add_edge!(G,1,3)
add_edge!(G,2,3)
When I run
e=edges(G)
e1=collect(e)
I got
Multiple edge 1 => 3 with multiplicity 2
Multiple edge 2 => 3 with multiplicity 1
Multiple edge 2 => 4 with multiplicity 1
Multiple edge 4 => 1 with multiplicity 2
What I want to have is the non sorted list
Multiple edge 2 => 4 with multiplicity 1
Multiple edge 4 => 1 with multiplicity 2
Multiple edge 1 => 3 with multiplicity 1
Multiple edge 2 => 3 with multiplicity 1
it seems like there is a code for that here but I don't really understand it.
I think you want the edges in the order you inserted them. To do that, you need to have a key to sort by. Here is one way:
edgevector = [[2,4], [4,1], [4,1],[1,3],[1,3],[2,3]]
G = DiMultigraph(4)
for edg in edgevector
add_edge!(G, edg[1], edg[2])
end
e = edges(G)
e1 = sort!(collect(e), by = edg -> findfirst(==([edg.src, edg.dst]), edgevector))
Related
could someone explain this!?
range i from 1 to 3 step 1
| scan with_match_id=matchId declare (s: long, n: long=0) with
(
step s1: true => s=1, n=s1.n + 1;
step s2: true => s=2, n=s2.n + 1;
)
Output
i
s
n
matchId
1
1
1
0
2
2
1
0
2
1
1
1
3
2
1
1
3
1
1
2
Why is the first row (i=1) not matched by s2?
Why is n always 1?
That SCAN Operator makes me totally crazy. With one step it's more or less understandable, but with more than one step, I don't get it...
Thanks a lot!
Steps are always executed in their definition order.
We always start with the 1st step (s1) and then move to the 2nd, the 3rs etc. (note that a step might repeat itself multiple times).
By this definition, the 1st input line might match the 1st step, however, it cannot match any other step.
2.
n is always 1 because each step is matched only once.
In step s1 you are refering to s1.n.
s1.n is the previous value of s1.
It has a value only if you execute this step multiple time.
Since you don't, the value take is the default value you have defined when you declared n, which is 0 (n: long=0).
Same thing for step s2.
I have a table with 3 columns :
ID
productId
customerId
1
5
1
2
4
1
3
5
1
4
4
1
I want to add a new column called ID_MOD and its value by default will be ID%X (X is a number).
Expected result for X=3 :
ID
productId
customerId
ID_MOD
1
5
1
1
2
4
1
2
3
5
1
0
4
4
1
1
I have X instances of my app and I want each instance to query specific ID_MOD values(0/1/2.../X-1).
Is it possible to use default values for columns? If it is, can the default value be calculated based on other columns ?
what you can do is create a stored function that receives x as an input parameter, and extends your table with a calculated column (at query time).
For example:
.create-or-alter function FunctionName(x:int)
{
TableName
| extend ID_MOD = ID % x
}
If you decide x always has the same value and shouldn't be parameter, you can name the function using the same name as the table, and it will 'hide' the original table.
If the logic of calculating the extended column is well-defined in advance, you can invoke it at ingestion time, using an update policy
Incorrect query:
$qb = $this->createQueryBuilder('product')
->join('product.specifications', 'specification')
->andWhere("specification.id = :specificationId_1") // this
->andWhere("specification.id = :specificationId_2") // and this
->setParameter("specificationId_1", 1)
->setParameter("specificationId_2", 2)
->getQuery()
->getResult()
;
Tables (product related to specifications by ManyToMany):
product:
id name
1 Product1
2 Product2
3 Product3
specifcation:
id name
1 Specification1
2 Specification2
3 Specification3
product_specification:
product_id specification_id
1 1
1 2
1 3
2 1
2 2
3 3
Expected result:
Products with ids 1 and 2.
What's needed:
Need to get products by ALL specification_id entries.
So, in example, I need to get products 1 and 2 by specifications 1 and 2, because both products has this specifications.
IN operator doesn't help, because select result by specifications 1, 2 and 3 must be empty, because we don't have products which has ALL three specifications relation.
P.S. I understand why it's not working, but don't understand how to do it. And can't find something like in stackoverflow.
Thank you!
Solution:
$qb = $this->createQueryBuilder("product")
->join("product.specifications", "specification_1")
->join("product.specifications", "specification_2")
->andWhere("specification_1.id = :specificationId_1")
->andWhere("specification_2.id = :specificationId_2")
->setParameter("specificationId_1", 1)
->setParameter("specificationId_2", 2)
->getQuery()
->getResult()
;
For each where need to join table again with new alias.
Still learning SQL :)
This time I'd like to a 'linked list' walk from a table I guess using CTE.
Despite all the example on the web I could not find one simple example I could start from then peek and poke from there.
Here is my table
create table yo (id integer, nx integer)
select * from yo
id nx
---------- ----------
1 5
2 4
3 7
4 9
5 3
6 0
7 0
8 6
9 8
I'd like to get a list of 'id','nx' from yo following the next link 'nx' given a start 'id'
So a start 'id' of 1 would produce
id nx
---------- ----------
1 5
3 7
5 3
7 0
Note that 0 is a end marker.
I can't find the magic SQL for doing this
Thanx in advance
Cheers,
Phi
The first row of the list is easy:
SELECT id, nx
FROM yo
WHERE id = 1
If the nx column of the previous entry is available as list.nx, the next entry can be returned with this query:
SELECT yo.id, yo.nx
FROM yo
JOIN list ON yo.id = list.nx
Then just plug these together:
WITH RECURSIVE list(id, nx) AS (
SELECT id, nx
FROM yo
WHERE id = 1
UNION ALL
SELECT yo.id, yo.nx
FROM yo
JOIN list ON yo.id = list.nx
)
SELECT * FROM list
(This stops automatically because there is no row where id is zero; otherwise, you could add a WHERE list.nx != 0.)
how can i generate 6 numbers between 1 and 2 where 4 of the numbers will be 1 and the other 2 will be 2 in a random order i.e.
results
1
2
1
1
1
2
and also in a different ratio i.e. 3:2:1 for numbers between 1 and 3 for 12 numbers
i.e.
results
1
1
2
3
1
2
1
3
1
1
3
3
results don't have to be in this order but in the ratios as above in oracle SQL or PL/SQL
To get the ratios perfect you could do something like this - generate all the numbers, then sort in random order:
SELECT r
FROM (SELECT CASE
WHEN ROWNUM <=4 THEN 1
ELSE 2
END AS r
FROM DUAL
CONNECT BY LEVEL <= 6)
ORDER BY DBMS_RANDOM.value;
R
----------------------
2
1
1
2
1
1
I think this will work in straight SQL; it's horrifically inefficient, and a PL/SQL one might be less so. It's also completely static; differing ratios call for a different number of values selected.
select value
from (
select mod(value, 2) + 1 as value,
row_number() over (partition by
case mod(value, 2) = 1
then 1
else 0
end) as twos_row,
row_number() over (partition by
case mod(value, 2) = 0
then 1
else 0
end) as ones_row
from (select dbms_crypto.randominteger as value
from dba_objects
order by object_id
)
)
where twos_rows <= 2
or ones_rows <= 4
The inner-most select grabs a big stack of random numbers. The next query out determines whether that random value would be a 2 or a 1 by mod'ing the earlier random value. The last level of nesting just filters out all the rows after the correct number of that type of row has been returned.
This is untested and fragile. If you need a solution that's reliable and performance, I'd recommend PL/SQL, where you
loop
pick off random numbers
determine what partition in your set of values they'd fit into
keep them if that partition hasn't been satisfied
exit when all partitions have been satisfied.