I've a view and a table. I'm inserting data into table on the basis of that view. I want that view to eliminate same data that is already inserted into table.
This is the view which is named view_1.
CREATE VIEW view_1 AS
SELECT
y.employee_registration_id,
x.employee_first_name,
x.employee_last_name,
x.employee_email,
y.employee_current_salary_amount,
(SELECT
x.salary_of_year_month_YearMonth
FROM
salary_of_years_months x
LEFT JOIN
status_salary_of_years_months y ON x.salary_of_year_month_id = y.salary_of_year_month_id
WHERE
y.year_month_status = 'Ongoing') AS salary_of_year_month_YearMonth,
(SELECT
CONCAT(MONTHNAME(x.salary_of_year_month_YearMonth),
'-',
YEAR(x.salary_of_year_month_YearMonth))
FROM
salary_of_years_months x
LEFT JOIN
status_salary_of_years_months y ON x.salary_of_year_month_id = y.salary_of_year_month_id
WHERE
y.year_month_status = 'Ongoing') AS salary_of_year_month_YearMonth_MonthYear
FROM
basic_info_employees x
LEFT JOIN
current_salary_employees y ON x.employee_registration_id = y.employee_registration_id
WHERE
y.employee_current_salary_amount > 0;
This is the table.
CREATE TABLE `salary_tracker_employees` (
serial_no INT(4) NOT NULL AUTO_INCREMENT,
employee_registration_id INT(4),
employee_first_name VARCHAR(30),
employee_last_name VARCHAR(30),
employee_email VARCHAR(30),
salary_of_year_month_YearMonth VARCHAR(50),
employee_current_salary_amount DOUBLE(8 , 2 ),
PRIMARY KEY (serial_no),
UNIQUE (employee_registration_id , salary_of_year_month_YearMonth)
);
I want to get a view that will intersect the between view_1 and salary_tracker_employees.
Related
CREATE TABLE Application
(
ID_Application INT,
ID_Owner1 INT,
ID_Owner2 INT,
ID_Owner3 INT,
Name varchar(200) NOT NULL
);
CREATE TABLE `Person`
(
ID_Person INT,
Name varchar(200) NOT NULL
);
CREATE TABLE `PersonInOrg`
(
ID_Person INT,
ID_Org INT
);
CREATE TABLE `Org`
(
ID_Org INT,
ID_Owner1 INT,
ID_Owner2 INT,
ID_Owner3 INT,
Name varchar(200) NOT NULL
);
INSERT INTO Application
(ID_Application, ID_Owner1, ID_Owner2, ID_Owner3, Name)
VALUES ( 123,11,21,null,"A"),
( 124,11,null,32,"B"),
( 125,11, null,null,"C");
INSERT INTO Person
(ID_Person, Name)
VALUES ( 12345,"Peter"),
( 12346,"Rafi"),
( 12347,"Rafael");
INSERT INTO PersonInOrg
(ID_Person, ID_Org)
VALUES ( 12345,1234),
( 12346,1111),
( 12347,1212);
INSERT INTO Org
(ID_Org, ID_Owner1, ID_Owner2, ID_Owner3, Name)
VALUES ( 1234,11,21,null,"Org1"),
( 1111,12,null,32,"Org2"),
( 1212,13, null,null,"Org3");
Fiddle Link
I want to see the list of owners for all applications. The below picture explains the relations between the lists of tables.
------>Application
ID_Application; ID_Owner1; ID_Owner2; ID_Owner3;Name
------>Person
ID_Person; Name
------->PersonInOrg
ID_Person; ID_Org
------->Org
ID_Org; ID_Owner1; ID_Owner2; ID_Owner3; Name
Expected result -->List of applications with names for each owner as below:
ID_Application;ID_Owner1; ID_Owner2; ID_Owner3; ID_Person_1; ID_Person_2; ID_Person_3
SELECT a.id_application,
a.id_owner1,
a.id_owner2,
a.id_owner3,
p.id_person AS [ID_Person1],
p.id_person AS [ID_Person2],
p.id_person AS [ID_Person3]
FROM application AS a
JOIN org AS o
ON a.id_owner1 = o.id_owner1
OR a.id_owner2 = o.id_owner2
OR a.id_owner3 = o.id_owner3
JOIN pesoninorg AS po
ON o.id_org = po.id_org
JOIN person AS p
ON po.id_org = p.id_person
Sample data
Consider:
Query1: AppUNION
SELECT ID_Application, ID_Owner1 AS Owner, 1 AS Seq FROM Application
UNION SELECT ID_Application, ID_Owner2, 2 FROM Application
UNION SELECT ID_Application, ID_Owner3, 3 FROM Application;
Query2: OrgPIO_UNION
SELECT Org.ID_Org, ID_Owner1 AS OwnerID, ID_Person, "ID_Person1" AS Seq FROM Org
INNER JOIN PersonInOrg ON Org.ID_Org = PersonInOrg.ID_Org
UNION SELECT Org.ID_Org, ID_Owner2, ID_Person, "ID_Person2" FROM Org
INNER JOIN PersonInOrg ON Org.ID_Org = PersonInOrg.ID_Org
UNION SELECT Org.ID_Org, ID_Owner3, ID_Person, "ID_Person3" FROM Org
INNER JOIN PersonInOrg ON Org.ID_Org = PersonInOrg.ID_Org;
Query3: AppPersonCROSS
TRANSFORM First(OrgPIO_UNION.ID_Person) AS FirstOfID_Person
SELECT AppUNION.ID_Application
FROM AppUNION INNER JOIN OrgPIO_UNION ON AppUNION.Owner = OrgPIO_UNION.OwnerID
GROUP BY AppUNION.ID_Application
PIVOT OrgPIO_UNION.Seq;
Query4:
SELECT Application.*, ID_Person1, ID_Person2, ID_Person3
FROM AppPersonCROSS
INNER JOIN Application ON AppPersonCROSS.ID_Application = Application.ID_Application;
I have these Tables :
Livestream
----------
id_livestream int primary key,
name_match varchar(255),
date_match varchar(255),
time_match varchar(255),
league_type int,
tour varchar(255),
stadium int,
id_team1 int,
id_team2 int,
live_video varchar(255),
Team
------
id_team int primary key,
name_team varchar(255),
image_team varchar(255)
League
------
id_league int primary key,
name_league varchar(255)
staduim
-------
id_stadium int primary key,
name_stadium varchar(255)
I am using this sql query to get the data from the tables as below :
select id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
live_video
from Livestream,League,staduim,IsLive
where Livestream.league_type=League.id_league and Livestream.stadium=staduim.id_stadium
What i am getting :
id_livestream|name_match|date_match|time_match|name_league|tour|stadium
65 BarcaMatch 9/5/2017 20:45 League1 22 CampNou
This query is going well but i don t know how to alter this query to :
select the name team,image_team from Team for both id_team1,id_team2 from Livestream
UPDATED : i have searched and found this query to get what i want but i can t add it to my first query :
SELECT
lm.id_team1,
t1.name_team AS name_team_1,
t1.image_team AS image_team_1,
lm.id_team2,
t2.name_team AS name_team_2,
t2.image_team AS image_team_2
FROM Livestream lm
INNER JOIN Team t1
ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
ON lm.id_team2 = t2.id_team
UPDATE 2 :
Please i need help
Simply rewrite the whole query in explizite join Format:
SELECT
lm.id_team1,
t1.name_team AS name_team_1,
t1.image_team AS image_team_1,
lm.id_team2,
t2.name_team AS name_team_2,
t2.image_team AS image_team_2,
id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
live_video
FROM Livestream lm
INNER JOIN Team t1
ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
ON lm.id_team2 = t2.id_team
JOIN League on lm.league_type=League.league
JOIN staduim on lm.stadium=staduim.id_stadium
I am working in LAMP environment.
In MySQL I have 3 tables & I want to create the desired report as mentioned in below attached image.
How I can achieve this.
Because of table ProductMaster allowing non-ID integers in its FK_ProductTag_# fields, a special join is needed for each Attribute (assuming each FK_ProductTag_# can have a "0" value. Here is the requested query:
select a.ProductName as 'Product Name',
a.Attr1 as 'Atrribute-1',
b.Attr2 as 'Attribute-2',
c.Attr3 as 'Atrribute-3' from
(select m.ProductName as 'ProductName',
concat_ws(': ', tagtype.Description, tag1.Description) as 'Attr1'
from ProductMaster m
left join ProductTag tag1 on m.FK_ProductTag_1 = tag1.ID
left join ProductTagType tagtype on tag1.FK_ProductTagType = tagtype.ID) as a
join
(select m.ProductName as 'ProductName',
concat_ws(': ', tagtype.Description, tag2.Description) as 'Attr2'
from ProductMaster m
left join ProductTag tag2 on m.FK_ProductTag_2 = tag2.ID
left join ProductTagType tagtype on tag2.FK_ProductTagType = tagtype.ID) as b
on a.ProductName = b.ProductName
join
(select m.ProductName as 'ProductName',
concat_ws(': ', tagtype.Description, tag3.Description) as 'Attr3'
from ProductMaster m
left join ProductTag tag3 on m.FK_ProductTag_3 = tag3.ID
left join ProductTagType tagtype on tag3.FK_ProductTagType = tagtype.ID) as c
on a.ProductName = c.ProductName
order by a.ProductName asc
See this SQLFiddle for a demo.
SQLFiddle was acting up during testing, so copy the above query and the below table schema into SQLTest for a demo:
create table ProductTagType (ID int not null auto_increment, Description varchar(20), primary key (ID));
create table ProductTag (ID int not null auto_increment, Description varchar(20), FK_ProductTagType int(1), primary key (ID));
create table ProductMaster (ID int not null auto_increment, ProductName varchar(20), FK_ProductTag_1 int(1), FK_ProductTag_2 int(1), FK_ProductTag_3 int(1), primary key (ID));
insert into ProductTagType (Description)
values ('Imported'), ('Local'), ('HomeMade');
insert into ProductTag (Description, FK_ProductTagType)
values ('Wood', 2), ('Plastic', 2), ('Steel', 1), ('Aluminum', 3);
insert into ProductMaster (ProductName, FK_ProductTag_1, FK_ProductTag_2, FK_ProductTag_3)
values ('Chair', 1, 2, 3), ('Table', 0, 3, 4);
I cannot seem to get this MySQL query right. My table contains yearly inventory data for retail stores. Here's the table schema:
CREATE TABLE IF NOT EXISTS `inventory_data` (
inventory_id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
store_id smallint unsigned NOT NULL,
inventory_year smallint unsigned NOT NULL,
shortage_dollars decimal(10,2) unsigned NOT NULL
)
engine=INNODB;
Every store is assigned to a district which in this table (some non-relevant fields removed):
CREATE TABLE IF NOT EXISTS `stores` (
store_id smallint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
district_id smallint unsigned not null
)
engine=INNODB;
I want to be able to retrieve the shortage dollar amounts for two given years for all the stores within a given district. Inventory data for each store is only added to the inventory_data table when the inventory is completed, so not all stores within a district will all be represented all the time.
This query works to return inventory data for all stores within a given district for a given year (ex: stores in district 1 for 2012):
SELECT stores.store_id, inventory_data.shortage_dollars
FROM stores
LEFT JOIN inventory_data ON (stores.store_id = inventory_data.store_id)
AND inventory_data.inventory_year = 2012
WHERE stores.district_id = 1
But, I need to be able to get data for stores within a district for two years, such that the data looks something close to this:
store_id | yr2011 | yr2012
For the specific result format that you need, you may try the following query:
SELECT `s`.`store_id`, `i`.`shortage_dollars` AS `yr2011`, `i1`.`shortage_dollars` AS `yr2012`
FROM `stores` `s`
LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
AND `i`.`inventory_year` = 2011
LEFT JOIN `inventory_data` `i1` ON `s`.`store_id` = `i1`.`store_id`
AND `i1`.`inventory_year` = 2012
WHERE `s`.`district_id` = 1
Alternatively, you may as well try the next simpler query.
SELECT `s`.`store_id`, `i`.`inventory_year`, `i`.`shortage_dollars`
FROM `stores` `s`
LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
WHERE `s`.`district_id` = 1
AND `i`.`inventory_year` IN (2011, 2012)
ORDER BY `s`.`store_id`, `i`.`inventory_year`
Hope it helps!
SELECT
stores.store_id,
inventory_data.inventory_year
inventory_data.shortage_dollars
FROM
(SELECT * FROM stores district_id = 1) stores
LEFT JOIN
(SELECT * FROM inventory_data
WHERE inventory_year IN (2011,2012)) inventory_data
USING (store_id)
;
or
SELECT
stores.store_id,
GROUP_CONCAT(inventory_data.shortage_dollars) dollars_per_year
FROM
(SELECT * FROM stores district_id = 1) stores
LEFT JOIN
(SELECT * FROM inventory_data
WHERE inventory_year IN (2011,2012)) inventory_data
USING (store_id)
GROUP BY stores.id,inventory_year;
UPDATED:
I am using MySQL statement to create a view:
I need to show Editors First Name, Last Name and the City if they shipped more than 50 books. The three tables I have are:
create table editors (
ed_id char(11),
ed_lname varchar(20),
ed_fname varchar(20),
ed_pos varchar(12),
phone varchar(10),
address varchar(30),
city varchar(20),
state char(2),
zip char(5),
ed_boss char(11));
create table titleditors (
ed_id char(11),
title_id char(6),
ed_ord integer);
create table salesdetails (
sonum integer,
qty_ordered integer,
qty_shipped integer,
title_id char(6),
date_shipped date);
Can anyone tell me what code would be to create this result?
I didn't make the tables, I just have to work with what I was given.
Antiquated syntax (note the intermixing of join conditions and filter conditions):
CREATE VIEW qtyorderedview AS
SELECT
salesdetails.title_id, salesdetails.qty_shipped,
editors.ed_id, editors.ed_lname, editors.ed_fname, editors.city
FROM
titleditors, salesdetails, editors
WHERE
titleditors.title_id = salesdetails.title_id
AND editors.ed_id = titleditors.ed_id
AND salesdetails.qty_ordered > 50
Modern syntax (join conditions and filter conditions are separate):
CREATE VIEW qtyorderedview AS
SELECT
salesdetails.title_id, salesdetails.qty_shipped,
editors.ed_id, editors.ed_lname, editors.ed_fname, editors.city
FROM
titleditors
INNER JOIN salesdetails ON titleditors.title_id = salesdetails.title_id
INNER JOIN editors ON editors.ed_id = titleditors.ed_id
WHERE
salesdetails.qty_ordered > 50
Joins against views work exactly like joins against tables. Just use the view name in place of a regular table name.
SELECT e.*
FROM (
SELECT DISTINCT te.ed_id
FROM (
SELECT title_id
FROM sales_details
GROUP BY
title_id
HAVING SUM(qty_shipped) > 50
) t
JOIN titleditors te
ON te.title_id = t.title_id
) te
JOIN editors e
ON e.ed_id = te.ed_id