I have a table like this, A and B as columns:
A B
1 0
2 1
3 2
4 3
A record can be selected by defining a value for A. If the selected row has a value for B, the row whose A's value is equal to the record's B must also be selected, and if that selected record has a B it must again be selected and so on.
Example:
If the user queries for A = 3, the returned rows must be:
A B
3 2
2 1
1 0
This is the output because of this condition: A3 has a value for B; there is a record whose A is equal to the first row's B which is 2 and the second record's B is 1 which still has a matching record.
Is there a way this can be done without looping through the records?
You can use recursive CTE to achieve this:
http://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
;WITH RCTE AS
(
SELECT * FROM Table1 WHERE A = 3
UNION ALL
SELECT t.* FROM RCTE r
INNER JOIN Table1 t ON r.B = t.A
)
SELECT * FROM RCTE
SQLFiddle DEMO
Related
I am very confused how to define the problem statement but Let's say below is table History i want to find those rows which have a pair.
Pair I will defined like column a and b will have same value and c should have False and d should be different for both row.
If I am using Java i would have set row 3, C column as true when i hit a pair or would have saved both row 1 and row 3 into different list. So that row 2 can be excluded. But i don't know how to do the same functionality in SQL.
Table - History
col a, b, c(Boolean ), d
1 bb F d
1 bb F d
1 bb F c
Query ? ----
Result - rows 1 and 3.
Assuming the table is called test:
SELECT
*
FROM
test
WHERE id IN (
SELECT
MIN(id)
FROM
test
WHERE
!c
AND a = b
AND d != a
GROUP BY a, d
)
We get the smallest id of every where matching your conditions. Furthermore we group the results by a, d which means we get only unique pairs of "a and d". Then we use this ids to select the rows we want.
Working example.
Update: without existing id
# add PK afterwards
ALTER TABLE test ADD COLUMN id INT PRIMARY KEY AUTO_INCREMENT FIRST;
Working example.
All the rows match the conditioin you specified. A "pair" happens when:
column a and b will have same value, and
c should have False, and
d should be different for both rows.
1 and 3 will match that as well as 2 and 3. Also, 3 and 1 will match as well as 3 and 2. There are four solutions.
You don't say which database, so I'll assume PostgreSQL. The query that can search using your criteria is:
select *
from t x
where exists (
select null from t y
where y.a = x.a
and y.b = x.b
and not y.c
and y.d <> x.d
);
Result:
a b c d
-- --- ------ -
1 bb false d
1 bb false d
1 bb false c
That is... the whole table.
See running example at DB Fiddle.
I have a table with the following columns:
ID_group, ID_elements
For example with the following records:
1, 1
1, 2
2, 2
2, 4
2, 5
2, 6
3, 7
And I have sets of the elements, for example: 1,2,5; 1,5,2; 1,2,4; 2,7;
I need to check (true or false) that exist a common group for the pairs of adjacent elements.
For example elements:
1,2,5 -> true [i.e. elements 1,2 has common group 1 and elements 2,5 has common group 2]
1,5,2 -> false [i.e. 1,5 do not have a common group unlike 5,2 (but the result is false due to 1,5 - false)]
1,2,4 -> true
2,7 -> false
First, we need a list of pairs. We can get this by taking your set as an array, turning each element into a row with unnest and then making pairs by matching each row with its previous row using lag.
with nums as (
select *
from unnest(array[1,2,5]) i
)
select lag(i) over() a, i b
from nums
offset 1;
a | b
---+---
1 | 2
2 | 5
(2 rows)
Then we join each pair with each matching row. To avoid counting duplicate data rows twice, we count only the distinct rows.
with nums as (
select *
from unnest(array[1,2,5]) i
), pairs as (
select lag(i) over() a, i b
from nums
offset 1
)
select
count(distinct(id_group,id_elements)) = (select count(*) from pairs)
from pairs
join foo on foo.id_group = a and foo.id_elements = b;
This works on any size array.
dbfiddle
Your query to check if elements in a set evaluate to true or not can be done via procedures/function. Set representation can be taken as a string and then splitting it to substring then returning the required result can use a record for multiple entries. For sql query, below is a sample that can be used as a workaround, you can try changing the below query based on your requirement.
select case when ( Select count(*)
from ( SELECT
id_group, count(distinct id_elements)
from table where
id_group
in (1,2,5)
group by ID_group having
id_elements
in (1,2,5)) =3 ) then "true" else "false"
end) from table;
#Schwern, thank you, it helped. But I have changed the condition join foo on foo.id_group = a, because as I understand, a is element's ID, not group's. I have changed the following section:
join foo fA on fA.id_elements = a
join foo fB on fB.id_elements = b and fA.group_id = fB.group_id;
I have two tables A and B where I only want the parent A's rows when all the children (in Table B) meet the criteria. If one row from B does not meet the criteria, then I don't need parent A's row. I think I need to use exists here, but not show how.
Here are the data tables:
Table A
Primary Key Level
1 low
2 low
3 high
4 high
5 low
Table B
Primary Key Phase Parent Primary Key
1 open 1
2 open 1
3 close 1
4 close 2
5 close 2
6 close 3
7 open 4
8 open 4
9 open 5
10 close 5
And the query I was trying:
select *
from table_a, table_b
where table_a.level = 'low' and
table_b.phase = 'close' and
table_a.primary_key=table_b.parent_primary_key
but my query would also return rows where the table_a.primary_key = 5.
Basically the only rows I want returned is when table_A.primary_key = 2 beause the level is low, and both children rows have a phase equal to close.
Thank you!
Is this what you want
select a.*
from table_a a
where a.level = 'low' and
not exists (select 1
from table_b b
where b.parent_primary_key = a.primary_key and
b.phase <> 'close'
);
The not exists is a double negative. It checks that there are no children with a phase other than 'close' -- which is basically equivalent to saying that all children are 'close'. (If NULL values are allowed, the logic is not exactly equivalent.)
Alternatively:
select a.*
from table_a a
where a.level = 'low' and
'close' = all (select phase
from table_b b
where b.parent_primary_key = a.primary_key
);
I have a sql table :
Levels
LevelId Min Product
1 x 1
2 y 1
3 z 1
4 a 1
I need to duplicate the same data into the database by changing only the product Id from 1 2,3.... 40
example
LevelId Min Product
1 x 2
2 y 2
3 z 2
4 a 2
I could do something like
INSERT INTO dbo.Levels SELECT top 4 * fROM dbo.Levels
but that would just copy paste the data.
Is there a way I can copy the data and paste it changing only the Product value?
You're most of the way there - you just need to take one more logical step:
INSERT INTO dbo.Levels (LevelID, Min, Product)
SELECT LevelID, Min, 2 FROM dbo.Levels WHERE Product = 1
...will duplicate your rows with a different product ID.
Also consider that WHERE Product = 1 is going to be more reliable than TOP 4. Once you have more than four rows in the table, you will not be able to guarantee that TOP 4 will return the same four rows unless you also add an ORDER BY to the select, however WHERE Product = ... will always return the same rows, and will continue to work even if you add an extra row with a product ID of 1 (where as you'd have to consider changing TOP 4 to TOP 5, and so on if extra rows are added).
You can generate the product id's and then load them in:
with cte as (
select 2 as n
union all
select n + 1
from cte
where n < 40
)
INSERT INTO dbo.Levels(`min`, product)
SELECT `min`, cte.n as product
fROM dbo.Levels l cross join
cte
where l.productId = 1;
This assumes that the LevelId is an identity column, that auto-increments on insert. If not:
with cte as (
select 2 as n
union all
select n + 1
from cte
where n < 40
)
INSERT INTO dbo.Levels(levelid, `min`, product)
SELECT l.levelid+(cte.n-1)*4, `min`, cte.n as product
fROM dbo.Levels l cross join
cte
where l.productId = 1;
INSERT INTO dbo.Levels (LevelId, Min, Product)
SELECT TOP 4
LevelId,
Min,
2
FROM dbo.Levels
You can include expressions in the SELECT statement, either hard-coded values or something like Product + 1 or anything else.
I expect you probably wouldn't want to insert the LevelId though, but left that there to match your sample. If you don't want that just remove it from the INSERT and SELECT sections.
You could use a CROSS JOIN against a numbers table, for example.
WITH
L0 AS(SELECT 1 AS C UNION ALL SELECT 1 AS O), -- 2 rows
L1 AS(SELECT 1 AS C FROM L0 AS A CROSS JOIN L0 AS B), -- 4 rows
Nums AS(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS N FROM L1)
SELECT
lvl.[LevelID],
lvl.[Min],
num.[N]
FROM dbo.[Levels] lvl
CROSS JOIN Nums num
This would duplicate 4 times.
I want to write a query to achieve the following. I have a table xyz in which there are multiple row with same column value(1) in say column a.
I want to find in column b doesn't have a particular value for the set of rows with value 1 in column a.
Table xyz
---------
a b
1 te
1 we
1 re
2 te
2 re
3 ge
4 re
So basically I want to find if the column b does not have the value 'te' for a set of values from column a
when i do
Select a from xyz where b <> 'te'
group by a
I will get 1,2,3 and 4 both for the result.
But I want the result should only contain 1 and 2. Please help.
Select a from xyz where (b<>'te') and ((a=1) or (a=2))
or as variant
select a from xyz where (b<>'te') and (a in (1, 2))
select a from xyz
where b! = 'tz' and
a in (select a from xyz where b = 'tz')
Is this what you are looking for?
Try this for you:
Select a from xyz where b = 'te'
group by a
I just realized I didn't and still don't understand what you are asking. Could you try and restate it? The only non-trivial interpretation I can come up with that would return 1 and 2 based on this data would be:
What are the values of a such that there is a row with both a and
'te' and a row with both a and a value other than 'te'
in which case a query would be:
SELECT DISTINCT q1.a FROM (SELECT a FROM xyz WHERE b='te') q1
JOIN (SELECT a FROM xyz WHERE b!='te') q2 ON
q1.a=q2.a
The interpretation which corresponds with returning 3 and 4 in your example or with returning 1 and 2 in your geo example would be:
What are the values of a for which a te row does not exist?
in which case a query would be:
SELECT DISTINCT a FROM xyz WHERE a NOT IN (SELECT a FROM xyx WHERE b='te')
as shown here (sqlfiddle is acting up, so I used ideone)