Here is the table:
itemcode
batch
subbatch
desc
itemcode
batch
subbatch
desc
A
1
1
red
B
1
1
red
C
9
1
red
C
9
2
blue
D
8
1
red
D
8
2
blue
E
2
1
red
F
3
1
red
G
4
1
red
H
1
1
red
I
9
1
red
I
9
2
blue
J
8
1
red
J
8
2
blue
There are two kinds of items, one item with just 1 batch and 1 subbatch, the other item has 1 batch and multiple subbatch.
I want to select all items with the 1 batch / 1 subbatch including the other items with 1 batch and a specific subbatch. In this case, items with batch 8 or 9 has multiple subbatch. I wanted to select all items that is not in batch 8 or 9 plus items with batch 8 or 9 with subbatch 2.
From what you describe you can use not exists with boolean logic:
select t.*
from t
where not exists (select 1
from t t2
where t2.itemcode = t.itemcode and
t2.subbatch <> t.subbatch
) or
subbatch = 2;
Related
I have a db table with summarized data, but I want to unpack it into a table of details.
Summary Table:
id speed color count
---------------------
1 50 red 2
2 50 blue 1
3 70 orange 2
Detail Table (Desired):
id speed color count
---------------------
1 50 red 1
1 50 red 1
2 50 blue 1
3 70 orange 1
3 70 orange 1
Is this possible to do with a query?
Use generate_series():
select id, speed, color, 1 as cnt
from summary t, lateral
generate_series(1, t.count) g
I have this table:
id sort_ord
0 6
1 7
2 2
3 3
4 4
5 5
6 8
Why does this query:
UPDATE table
SET sort_ord=(
SELECT count(*)
FROM table AS pq
WHERE sort_ord<table.sort_ord
ORDER BY sort_ord
)
WHERE(sort_ord>=0)
Produce:
id sort_ord
0 4
1 5
2 0
3 1
4 2
5 4
6 6
I was expecting all sort_ord fields to subtract by 2.
Here is defined: https://www.sqlite.org/isolation.html
About this link i can interpret, you has several instances for one query (update table and select count table) and independent of each other.
When you are in update sort_data(5) id 5, you have new data for read on every "SET sot_ord" (understanding what say about isolation), and now the result is 4.
Every select is a new instance and a new data reading
id sort_ord
0 4
1 5
2 0
3 1
4 2
5 5**
6 8**
From a given table I want to be able to sum values having the same number (should be easy, right?)
Problem: A given value can be assigned from 2 to n consecutive numbers.
For some reasons this information is stored in a single row describing the value, the starting number and the ending number as below.
TABLE A
id | starting_number | ending_number | value
----+-----------------+---------------+-------
1 2 5 8
2 0 3 5
3 4 6 6
4 7 8 10
For instance the first row means:
value '8' is assigned to numbers: 2, 3 and 4 (5 is excluded)
So, I would like the following intermediairy result table
TABLE B
id | number | value
----+--------+-------
1 2 8
1 3 8
1 4 8
2 0 5
2 1 5
2 2 5
3 4 6
3 5 6
4 7 10
So I can sum 'value' for elements having identical 'number'
SELECT number, sum(value)
FROM B
GROUP BY number
TABLE C
number | sum(value)
--------+------------
2 13
3 8
4 14
0 5
1 5
5 6
7 10
I don't know how to do this and didn't find any answer on the web (maybe not looking with appropriate key words...)
Any idea?
You can do what you want with generate_series(). So, TableB is basically:
select id, generate_series(starting_number, ending_number - 1, 1) as n, value
from tableA;
Your aggregation is then:
select n, sum(value)
from (select id, generate_series(starting_number, ending_number - 1, 1) as n, value
from tableA
) a
group by n;
I have a Person table.Persons are good at some language and i give them weight.My table are as follows:
Person
ID Name Description
1 Rodra Some..
2 Rakib Some..
3 Samsad Some..
4 Foysal Some..
Language
TypeID TypeName
1 C#
2 Asp.Net
3 Python
4 JSP
5 Java
6 Jquery
7 Android
PersonSkill
ID PersonID TypeID Weight
1 1 1 60
2 1 3 50
3 1 7 40
4 2 1 80
5 2 2 70
6 3 1 90
7 3 2 50
8 4 1 60
9 4 2 50
10 4 6 40
11 4 7 55
Now i want to query those person who know c#(TypeId 1)>65 and Asp.net(TypeID 2)>65.How to do it?Anyone helps me greatly appreciated.
Select *
FROM person
WHERE EXISTS(SELECT * FROM PersonSkill WHERE PersonId = Person.Id AND TypeID = 1 AND Weight>65)
AND EXISTS(SELECT * FROM PersonSkill WHERE PersonId = Person.Id AND TypeID = 2 AND Weight>65)
select p.* from
person p
JOIN personSkill ps ON p.ID=ps.PersonID
JOIN Language l ON l.TypeID=ps.TypeID
WHERE (l.TypeName = 'c#' AND ps.weight>65)
AND
(l.TypeName = 'Asp.net' AND ps.weight>65)
I've got a bit of a complicated query that I'm struggling with. You will notice that the schema isn't the easiest thing to work with but it's what I've been given and there isn't time to re-design (common story!).
I have rows like the ones below. Note: The 3 digit value numbers are just random numbers I made up.
id field_id value
1 5 999
1 6 888
1 7 777
1 8 foo <--- foo so we want the 3 values above
1 9 don't care
2 5 123
2 6 456
2 7 789
2 8 bar <--- bar so we DON'T want the 3 values above
2 9 don't care
3 5 623
3 6 971
3 7 481
3 8 foo <--- foo so we want the 3 values above
3 9 don't care
...
...
n 5 987
n 6 654
n 7 321
n 8 foo <--- foo so we want the 3 values above
n 9 don't care
I want this result:
id result
1 999*888*777
3 623*971*481
...
n 987*654*321
Is this clear? So we have a table with n*5 rows. For each of the sets of 5 rows: 3 of them have values we might want to multiply together, 1 of them tells us if we want to multiply and 1 of them we don't care about so we don't want the row in the query result.
Can we do this in Oracle? Preferably one query.. I guess you need to use a multiplication operator (somehow), and a grouping.
Any help would be great. Thank you.
something like this:
select m.id, exp(sum(ln(m.value)))
from mytab m
where m.field_id in (5, 6, 7)
and m.id in (select m2.id
from mytab m2
where m2.field_id = 8
and m2.value = 'foo')
group by m.id;
eg:
SQL> select * from mytab;
ID FIELD_ID VAL
---------- ---------- ---
1 5 999
1 6 888
1 7 777
1 8 foo
1 9 x
2 5 123
2 6 456
2 7 789
2 8 bar
2 9 x
3 5 623
3 6 971
3 7 481
3 8 foo
3 9 x
15 rows selected.
SQL> select m.id, exp(sum(ln(m.value))) result
2 from mytab m
3 where m.field_id in (5, 6, 7)
4 and m.id in (select m2.id
5 from mytab m2
6 where m2.field_id = 8
7 and m2.value = 'foo')
8 group by m.id;
ID RESULT
---------- ----------
1 689286024
3 290972773
Same logic; just removed the hard-coded values. posting this answer thinking might be helpful to some others.
SELECT a.id,
exp(sum(ln(a.val)))
FROM mytab a,
(SELECT DISTINCT id,
field_id
FROM mytab
WHERE val = 'foo') b
WHERE a.id = b.id
AND a.field_id < b.field_id
GROUP BY a.id;