I have to get “suggested” groupings of records based off of existing data.
Table A has a composite key of Akey+Bkey. Table B has a primary key of Bkey.
Akey is generated from a SQL 2012 Sequence object and there is a one to many relationship between table A and table and Table B on Bkey.
The structure and sample data are listed below.
Table A
Akey Bkey ItemSequence
---- ---- ------------
1 1 1
1 5 2
1 7 3
2 7 1
3 2 1
3 3 2
Table B
Bkey GroupValue Data HashString
---- --------------- -------------- ----------
1 Ford Festiva AIR BAG HASH1
2 Ford Festiva RADIATOR CAP HASH2
3 Ford Festiva FUEL PUMP HASH3
4 Ford Mustang AIR FILTER HASH4
5 Ford Explorer AIR FILTER HASH5
6 Ford Edge RADIATOR CAP HASH2
7 Ford Edge FUEL PUMP HASH3
The query has to insert new groups into Table A that match existing sets of Bkeys where the HASH value matches in Table B. To depict the point I use the example of a car model. The hash values of the items within the model of the car could match exactly ( but they might not ). So if all items exist within of OVER the GroupValue then I want to insert a new set of records into Table A with the values where the exact matches were found within that group.
In the sample data you can see that AKey 3 has BKeys 2 and 3 and Bkeys 6 and 7 are a match so these 2 records would be inserted into Table A and a new sequence # generated for the AKey.
I am not sure I understand the part about the groups and GroupValue field.
According to what I understand, you want to insert into Table A any rows from Table B that match the HashString for the Bkey of each AKey and don't already exist in Table A?
If I understand correctly: for Akey1, row TableB.Bkey 7 should be added to Table A. Is that right?
If the logic is good, the following query should select those rows.
SELECT
a.Akey, a.Bkey, b3.*
FROM b
INNER JOIN a ON a.Bkey=b.Bkey
INNER JOIN b b2 ON b2.HashString=b.HashString and b2.Bkey=a.Bkey
INNER JOIN b b3 ON b3.HashString=b2.HashString AND b3.Bkey <> b.Bkey
WHERE a.Akey=3 --Comment this line for the full matches
Related
I have this data:
Table A
pk name
1 Peter
2 Jon
Table B
pk trait fk_A
1 funny 1
2 generous 1
3 rude 2
I want to get this result set:
pk name traits
1 Peter funny,generous
2 Jon rude
How can I aggregate the trait column of table B and attach it as a CSV field 'traits' to table A?
I have a table filled with cars, another table that has the car IDs and the feature IDs and another table with the feature IDs and feature names.
A car can have multiple features in that table. The result of the query is, that i get multiple instances of the same car, each having a different feature.
I would like to have only one instance of the car (by ID) and combine all features into one column (list or array). I could do it in code after the query but i would like to do it in a query instead.
Cars table Car property table Property table
ID Name Car_ID property_ID property_id Property_name
1 Audi 1 1 1 Aircon
2 BMW 1 2 2 Autopilot
3 Mercedes 2 1
3 2
Result is:
1 Audi Aircon
1 Audi Autopilot
2 BMW Autopilot
3 Mercedes None
Result should be:
1 Audi [Aircon, Autopilot]
2 BMW Autopilot
3 Mercedes None
This seems like a basic aggregation query with joins:
select c.name, group_concat(Property_name) as properties
from cars c left join
car_properties cp
on c.car_id = cp.car_id left join
properties p
on p.property_id = cp.property_id
group by c.name
I have the table PERSONAL
ID_PERS NAME
---------------
11 azerty
22 uiop
and the table TOURNE_LABEL
ID_TOUR NAME
--------------
1 w
2 p
3 v
I want to loop over all of person and then join it with tourne and insert to a new table.
I have created empty table LS_PDA
ID_PERS ID_TOURN
-------------------
11 1
11 2
11 3
22 1
22 2
22 3
how can I do that ?
SQL is all about set based operations. If you're thinking about a loop, chances are you're heading in the wrong direction. For this problem, you could cross-join the tables, thus producing all the possible combinations, and use the insert-select syntax:
INSERT INTO ls_pda
SELECT id_pres, id_tour
FROM personal
CROSS JOIN tourne_label
I want to count the number of HOLD entry between the Two ACTIVE entries.
S.NO DATA STATUS
-----------------------------
1 B active
2 D hold
3 C active
4 H hold
5 j hold
6 k hold
7 l hold
8 y active
The output should be
COUNT OF HOLD
-------------
2
When HOLD entry comes between two ACTIVE entries we take it as a batch of HOLD entry.
Then we Count the whole Batch of HOLD entry in the table. In the above Example, there is one hold entry between two active, this is counted as 1.
Then another 4 HOLD entries between another two ACTIVE, This is counted to be 1.
So there are two counts. 1+1. So the output is 2.
You can make use of LEAD function to look what is the status in the next row. Then count the occurrences where current row is active and next is hold.
Subtract 1 from it, and you have your output.
SQL Fiddle
Oracle 11g R2 Schema Setup:
create table sam(
sno_ number,
data_ varchar2(5),
status_ varchar2(10)
);
insert into sam values(1,'a','hold');
insert into sam values(2,'b','active');
insert into sam values(3,'d','active');
insert into sam values(4,'s','hold');
insert into sam values(5,'c','active');
insert into sam values(6,'r','hold');
insert into sam values(7,'t','hold');
insert into sam values(8,'m','active');
insert into sam values(9,'y','hold');
Query:
select count(1) - 1 count_of_hold
from (
select status_, lead(status_,1,'hold') over (order by sno_) next_status_
from sam
)
where status_ = 'active' and next_status_ = 'hold';
Results:
| COUNT_OF_HOLD |
|---------------|
| 2 |
I have a table A that contains the following columns
ID(PK) id_1
1 4
2 10
3 15
4 4
Now Im trying to create a table B with columns such that
ID(PK) Description id_1_a_id (composite foreign key(ID,id_1))
1 Apple (1,4)
2 Orange (2,10)
3 Banana (3,15)
4 dog (4,4)
5
Does this design make sense? Or is there a better way to do this? (SQL rookie)
Composite foreign keys are common and useful, but you don't have one. If you did, it would look like this.
ID(PK) Description A_id id_1
--
1 Apple 1 4
2 Orange 2 10
3 Banana 3 15
4 dog 4 4
But you wouldn't ordinarily do that, either. Ordinarily, you'd reference a unique set of columns in table A. The unique set of columns is just the single column A.ID. So your table would usually look like this.
ID(PK) Description A_id
--
1 Apple 1
2 Orange 2
3 Banana 3
4 dog 4
You wouldn't usually duplicate the values of A.id_1 in table B. If you need the values from A.id_1, write a query with a JOIN.
select B.ID, B.Description, A.ID, A.id_1
from B
inner join A on A.ID = B.A_id;
If the only unique constraint you have in your tables is on the ID numbers, you're doing it wrong. But that's a different question.