I created two tables having following records.
CREATE TABLE ForgeRock
(`id` int,`sta` int, `productName` varchar(7), `description` varchar(55));
INSERT INTO ForgeRock
(`id`,`sta`, `productName`, `description`)
VALUES
(1, 0,'OpenIDM', 'Platform for building enterprise provisioning solutions'),
(2,0, 'OpenAM', 'Full-featured access management'),
(3,0, 'OpenDJ', 'Robust LDAP server for Java');
CREATE TABLE ForgeRock1
(`id` int,`sta` int, `productName` varchar(7), `description` varchar(55));
INSERT INTO ForgeRock1
(`id`,`sta`, `productName`, `description`)
VALUES
(1, 2,'hii', 'Platform for building enterprise provisioning solutions'),
(2,0, 'OpenAM', 'Full-featured access management'),
(3,0, 'OpenDJ', 'Robust LDAP server for Java');
I want to Union both tables but want records of duplicate id having sta=2
SELECT id,sta
FROM ForgeRock
GROUP BY id
UNION
SELECT id, sta
FROM ForgeRock1
GROUP BY id
Result:
id sta
1 0
2 0
3 0
1 2
Expected Result:
id sta
1 2
2 0
3 0
Do the grouping after the UNION, and use MAX() to select the row to output for each group:
SELECT id, MAX(sta) AS sta
FROM (SELECT id, sta
FROM ForgeRock
UNION
SELECT id, sta
FROM ForgeRock1)
GROUP BY id
ORDER BY id
Related
I have an application that I noticed suddenly decreased in speed when copying from the live database to a test database. Both databases are on the same server, and the test database was created from a mysqldump of the live data. So they are identical, on the same instance.
When I explain a slow query on the two databases, one is using indexes and the other is not. This is happening on more than one query type, but I will show one example:
Here is the query I'm running:
SELECT * FROM product
INNER JOIN product_category pc
ON product.id = pc.product_id
INNER JOIN category c
ON c.id = pc.category_id
WHERE
(c.discount_amount > 0 OR c.discount_percent > 0)
AND (c.`discount_start_date` <= NOW() OR c.`discount_start_date` IS NULL)
AND (c.`discount_end_date` >= NOW() OR c.`discount_end_date` IS NULL)
Here is the EXPLAIN result from the live database:
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
SIMPLE
c
index_merge
PRIMARY,category_discount_start_date,category_discount_end_date,category_discount_amount,category_discount_percent
category_discount_amount,category_discount_percent
8,8
NULL
10
Using sort_union(category_discount_amount,category_discount_percent); Using where
1
SIMPLE
pc
ref
category_id,product_id
category_id
4
lollipop_site.c.id
19
1
SIMPLE
product
eq_ref
PRIMARY
PRIMARY
4
lollipop_site.pc.product_id
1
and here is the EXPLAIN result from the test database:
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
SIMPLE
product
ALL
PRIMARY
NULL
NULL
NULL
1
1
SIMPLE
pc
ALL
category_id,product_id
NULL
NULL
NULL
1
Using where; Using join buffer (flat, BNL join)
1
SIMPLE
c
eq_ref
PRIMARY,category_discount_start_date,category_discount_end_date,category_discount_amount,category_discount_percent
PRIMARY
4
lollipop_sandbox.pc.category_id
1
Using where
I am using MariaDB inside docker version is 10.7.3-MariaDB-1:10.7.3+maria~focal.
I'm hoping someone can shed some light onto why the server is using a different query plan for the same query on the same data just being in different databases.
Note this query was previously using WHERE id IN (SELECT product_id FROM... style query and I converted it as recommended by other stackoverflow answers. This install has a number of those queries that are also having this problem.
Having a similar issue with query not using primary keys, in my case on two different mariadb server. Maridb is on exactly same version, also the configuration is the same. Here is my query:
select .....
from model_number mn
inner join manufacturer m on (mn.manufacturer_id = m.id)
inner join product_type pt on (mn.product_type_id = pt.id)
inner join user cu on cu.id = mn.created_by
inner join user uu on uu.id = mn.updated_by
inner join replacement r on (mn.id = r.model_number_by_id)
inner join mapping ma on r.model_number_by_id = ma.model_number_id and r.physical_item_type_id = ma.physical_item_type_id
where r.model_number_id = 1355
and r.physical_item_type_id = 4
indexes (primary keys) are not using for m, pt, cu and uu.
also the order in query plan is different:
server using primary keys: r,mn,uu,pt,m,cu,ma
server not using primary keys: r,m,pt,cu,uu,mn,ma
I have no glue what is wrong
Here is query plan from the server where all works as expected:
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
SIMPLE
r
ref
PRIMARY,fk_replacement_model_number_by,fk_replacement_physical_item_type
fk_replacement_physical_item_type
8
const,const
3
Using index
1
SIMPLE
mn
eq_ref
PRIMARY,model_number_unq_idx2,fk_model_number_user_c,fk_model_number_user_u,fk_model_number_product_type
PRIMARY
4
vat_warehouse.r.model_number_by_id
1
Using where
1
SIMPLE
uu
eq_ref
PRIMARY
PRIMARY
1
vat_warehouse.mn.updated_by
1
1
SIMPLE
pt
eq_ref
PRIMARY
PRIMARY
4
vat_warehouse.mn.product_type_id
1
1
SIMPLE
m
eq_ref
PRIMARY
PRIMARY
4
vat_warehouse.mn.manufacturer_id
1
1
SIMPLE
cu
eq_ref
PRIMARY
PRIMARY
1
vat_warehouse.mn.created_by
1
1
SIMPLE
ma
ref
old_mapping_uniq_idx,fk_old_mampping_physical_item_type
old_mapping_uniq_idx
8
vat_warehouse.r.model_number_by_id,const
1
Using index
and here is a query plan from server where indexes are not used:
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
SIMPLE
r
ref
PRIMARY,fk_replacement_model_number_by,fk_replacement_physical_item_type
fk_replacement_physical_item_type
8
const,const
1
Using index
1
SIMPLE
m
ALL
PRIMARY
NULL
NULL
NULL
1
Using join buffer (flat, BNL join)
1
SIMPLE
pt
ALL
PRIMARY
NULL
NULL
NULL
1
Using join buffer (incremental, BNL join)
1
SIMPLE
cu
ALL
PRIMARY
NULL
NULL
NULL
1
Using join buffer (incremental, BNL join)
1
SIMPLE
uu
ALL
PRIMARY
NULL
NULL
NULL
1
Using join buffer (incremental, BNL join)
1
SIMPLE
mn
eq_ref
PRIMARY,model_number_unq_idx2,fk_model_number_user_c,fk_model_number_user_u,fk_model_number_product_type
PRIMARY
4
vat_warehouse.r.model_number_by_id
1
Using where
1
SIMPLE
ma
ref
old_mapping_uniq_idx,fk_old_mampping_physical_item_type
old_mapping_uniq_idx
8
vat_warehouse.r.model_number_by_id,const
1
Using index
I want to update single row in target table if target has two duplicate rows.
target table
Employee_Id Join_date Status Eartned_points used_vocher_cnt
123 02/01/2021 A 50 null
123 02/01/2021 A 40 null
source table
Employee_Id Join_date Status used_vocher_cnt
123 02/01/2021 A 4
There are two rows in target table and only one row has to be update with used_vocher_cnt 4
(either of the row) i.e total used vochers used by employee is 4.
UPDATE TGT
FROM (
SELECT EMPLOYEE_ID,
JOIN_DATE,
STATUS,
USED_VOCHER_CNT
FROM TARGET_TABLE
QUALIFY ROW_NUMBER() OVER( PARTITION BY Employee_id,Join_date,Status ORDER BY Eartned_points DESC)=1
)TGT,
SOURCE_TABLE SRC
SET Used_vocher_cnt=SRC.used_vocher_cnt
WHERE SRC.Join_date=TGT.Join_date
AND SRC.EMPLOYEE_ID=TGT.EMPLOYEE_ID
AND SRC.Status=TGT.Status;
Error: derived table not allowed for update
I have an SQLite3 database from which I want to remove rows that have two fields of the same value.
It seems that I am able to select such values with this query:
SELECT * FROM mydb GROUP BY user_id, num HAVING COUNT(*) > 1
However I am not able to delete them.
DELETE FROM mydb WHERE user_id IN (SELECT * FROM mydb GROUP BY user_id, num HAVING COUNT(*) > 1)
returns a syntax error.
This is what I expect:
Example:
id user_id num
1 1 1
2 1 1
3 2 1
4 1 2
5 2 2
In this example id 1 and 2 have both columns (user_id and num) of the same value so they should be removed. Preferably, but not necessarily I would like to have a solution that would leave only one such row (doesn't matter which one).
Result:
id user_id num
2 1 1
3 2 1
4 1 2
5 2 2
Note: id is a primary key. user_id is a foreign key. num is an INTEGER.
You were having a syntax error because your IN operator has a single value on the left (user_id) but a table of non-single-value rows in the right side (SELECT *). Compare like with like; WHERE user_id IN (SELECT user_id ...) to avoid it.
Anyway, here's a query to delete all-but-newest:
DELETE FROM mydb
WHERE id NOT IN (
SELECT MAX(id) FROM mydb
GROUP BY user_id, num
);
The subquery will return the highest id for every unique (user_id, num) combination. Then we just delete all the other rows. I.e. in your example, the subquery would return 2, 3, 4, 5 as "correct", which would result in deletion of row 1.
I'm intended to develop a database model for my department. I found it difficult to establish the relationship between student, courses and staffs considering that any number of students can elect any number of courses and any number of staffs can handle any number of courses. How will I be able to represent this data in an oracle database?
What did you manage to do so far? What kind of difficulties did you meet?
Anyway: here's a suggestion, see whether it helps. An example is based on your STUDENT and COURSES tables. Idea is to include additional "cross" table which maps courses and students, i.e. contains columns that make primary keys of both tables, they are constrained by foreign key constraints and both of them make the primary key of the new, cross table.
Here's the code:
Create tables:
SQL> -- Students
SQL> create table t_student
2 (id_student number constraint pk_stu primary key,
3 student_name varchar2(20) not null
4 );
Table created.
SQL> -- Courses
SQL> create table t_course
2 (id_course number constraint pk_cou primary key,
3 course_name varchar2(20) not null
4 );
Table created.
SQL> -- Additional "cross" table
SQL> create table t_stu_x_cou
2 (id_student number constraint fk_sxc_stu
3 references t_student (id_student),
4 id_course number constraint fk_sxc_cou
5 references t_course (id_course),
6 constraint pk_sxc primary key (id_student, id_course)
7 );
Table created.
Insert sample data:
SQL> insert into t_student (id_student, student_name)
2 select 1, 'Little' from dual union
3 select 2, 'Foot' from dual;
2 rows created.
SQL> insert into t_course (id_course, course_name)
2 select 100, 'Mathematics' from dual union
3 select 200, 'Physics' from dual union
4 select 300, 'Chemistry' from dual;
3 rows created.
SQL> -- Mapping students and courses:
SQL> -- - student 1 takes 2 courses (100 and 300)
SQL> -- - student 2 takes 3 courses (100, 200 and 300)
SQL> insert into t_stu_x_cou (id_student, id_course)
2 select 1, 100 from dual union
3 select 1, 300 from dual union
4 --
5 select 2, 100 from dual union
6 select 2, 200 from dual union
7 select 2, 300 from dual;
5 rows created.
Select that shows courses taken by student 1:
SQL> select s.student_name, c.course_name
2 from t_stu_x_cou x
3 join t_student s on s.id_student = x.id_student
4 join t_course c on c.id_course = x.id_course
5 where s.id_student = 1;
STUDENT_NAME COURSE_NAME
-------------------- --------------------
Little Mathematics
Little Chemistry
SQL>
Now, try to add the STAFF table yourself, using the same principle (you'd add a new "cross" table between STAFF and COURSES).
I have a table with 5 columns: ID, Session1, Session2, Session3, Session4. What I want to do is to display the number of same items from each column. Same item can't appear in more than one column.
ID Session1 Session2 Session3 Session4
-- -------- -------- -------- --------
1 Music Web Tech Future
2 Art Articles Search Introduction
3 Music Books Camera Phone
4 Music Glass Cup Future
5 Art Books Tech Future
6 Music Glass Cup Phone
I want to display it like this on an asp.net page.
Music: 4
Art: 2
Web: 1
Articles: 1
Books: 2
Glass: 2
Tech: 2
Search: 1
Camera: 1
Cup: 2
Future: 3
introduction: 1
Phone: 2
how would I construct a sql query and display them on asp.net?
You could simply use:
SELECT
session1,
COUNT(*)
FROM
My_Table
GROUP BY
session1
UNION ALL
SELECT
session2,
COUNT(*)
FROM
My_Table
GROUP BY
session2
...
With SQL Server you could do something like this:
SELECT Session, COUNT(*) FROM (
SELECT Session1 AS Session FROM TableName
UNION ALL
SELECT Session2 AS Session FROM TableName
UNION ALL
SELECT Session3 AS Session FROM TableName
UNION ALL
SELECT Session4 AS Session FROM TableName) T1
GROUP BY Session
It's not pretty, but it should work.
try:
Select z.Val, Count(*) ValueCount
From (Select Distinct session1 val From Table Union
Select Distinct session2 val From Table Union
Select Distinct session3 val From Table Union
Select Distinct session4 val From Table) Z
Join Table t on Z.Val In (t.Session1, t.Session2, t.Session3, t.Session4)
Group By z.Val