I am new to sql and need help with a problem. Can anyone please help?
For each customer with orders in the last two quarters of the previous year, display their total extended cost for each of the last two quarters of the previous year.
A quarter of a year is 3 months- the first quarter is months 1, 2, 3; the second quarter is months 4, 5, 6; the third quarter is months 7, 8, 9; the fourth quarter is months 10, 11, 12. The result set should display one row per customer; the first column is the customer id, the second is the total for quarter 3 and the third column is the total for quarter 4. Display 0 if there are no sales for that customer in that quarter.
Select cust_id
, ifnull(Quarter1, 0) as Quarter1
, ifnull(Quarter2, 0) as Quarter2
FROM a_bkorders.customers
a_bkorders.order_headers using (cust_id)
Join a_bkorders.order_details using (order_id)
where order_id IN
(
Select order_id
FROM a_bkorders.order_details
GROUP BY order_id
Having QUARTER(orders)=
(
Select QUARTER(orders)=1 as Quarter1
, QUARTER(orders)=2 as Quarter2
From(
Select order_id
, sum(order_price * quantity) as orders
FROM a_bkorders.order_details
)tbl
)
);
HERE ARE THE TABLES:
-- create customers
create table a_bkorders.customers (
cust_id integer not null
, cust_name_last varchar(20) not null
, cust_name_first varchar(20) null
, cust_state char(2) not null
, cust_postal_code char(10) not null
, cust_acct_opened date not null
, constraint bk_cust_pk primary key (cust_id)
, constraint bk_cust_id_range check (cust_id > 1000)
, constraint bk_cust_acct_opened_ck check (cust_acct_opened >= '1975-01-01' )
)engine = INNODB;
-- create order_headers
create table a_bkorders.order_headers (
order_id integer not null
, order_date date not null
, cust_id integer not null
, constraint bk_orders_pk primary key (order_id)
, constraint bk_orders_cust_fk foreign key(cust_id)
references a_bkorders.customers(cust_id)
, constraint bk_order_id_range check (order_id > 100)
, constraint bk_order_date_ck check (order_date >= '2000-01-01')
)engine = INNODB;
-- create order_details
create table a_bkorders.order_details (
order_id integer not null
, order_line integer not null
, book_id integer not null
, quantity integer not null
, order_price numeric(6,2) not null
, constraint bk_orderline_pk primary key (order_id, order_line)
, constraint bk_orderline_order_fk foreign key (order_id)
references a_bkorders.order_headers(order_id) on delete cascade
, constraint bk_orderline_book_fk foreign key (book_id )
references a_bkinfo.books(book_id)
, constraint bk_quantity_ck check (quantity > 0)
, constraint bk_ordprice_ck check (order_price >= 0)
)engine = INNODB;
HERE ARE SOME INSERTS:
-- customers
insert into a_bkorders.customers values (208950, 'Adams', 'Samuel', 'MA', '02106', '1996-04-15' );
insert into a_bkorders.customers values (200368, 'Blake', 'William', 'CA', '95959', '1997-07-15' );
insert into a_bkorders.customers values (258595, 'Jobs', 'Peter', 'MA', '02575', '1997-01-09' );
insert into a_bkorders.customers values (263119, 'Jones', null, 'IL', '62979', '1997-03-02' );
insert into a_bkorders.customers values (224038, 'Austin', 'Pat', 'CA', '95900', '1997-08-02' );
insert into a_bkorders.customers values (255919, 'Milton', 'John', 'NJ', '08235', '2011-05-31' );
insert into a_bkorders.customers values (211483, 'Carroll', 'Lewis', 'CA', '94203', '1998-08-08' );
insert into a_bkorders.customers values (221297, 'Dodgson', 'Charles', 'MI', '49327', '2001-05-06' );
insert into a_bkorders.customers values (261502, 'Hawthorne', 'Nathaniel', 'MA', '02297', '2001-10-12' );
insert into a_bkorders.customers values (212921, 'Books on Tap', NULL, 'CA', '94112', '2002-01-06' );
insert into a_bkorders.customers values (260368, 'Muller', 'Jonathan', 'IL', '62885', '2005-12-15' );
insert into a_bkorders.customers values (259969, 'Carlsen', 'Benny', 'NJ', '08505', '2011-07-12' );
insert into a_bkorders.customers values (239427, 'Marksa', 'Anna', 'NJ', '08495', '2011-02-28' );
insert into a_bkorders.customers values (296598, 'Collins', 'Douglas', 'MO', '65836', '2005-04-25' );
insert into a_bkorders.customers values (276381, 'Collins', 'Douglas', 'OH', '22451', '2005-02-08' );
insert into a_bkorders.customers values (234138, 'Keats', 'John', 'IL', '61500', '2006-04-30' );
insert into a_bkorders.customers values (267780, 'Shelly', 'Mary', 'CA', '94100', '2010-10-02' );
insert into a_bkorders.customers values (290298, 'Swift', 'Jonathan', 'MI', '49201', '2010-10-12' );
insert into a_bkorders.customers values (226656, 'Randall', 'Randell', 'NJ', '08251', '2011-08-08' );
insert into a_bkorders.customers values (222477, 'Rossetti', 'Christina', 'MI', '49742', '2011-07-11' );
insert into a_bkorders.customers values (227105, 'Kafka', 'Franz', 'MA', '02297', '2010-12-31' );
insert into a_bkorders.customers values (202958, 'Denver', null, 'IL', '60405', '2011-01-15' );
insert into a_bkorders.customers values (218709, 'Bonnard', 'Paul', 'MA', '02558', '2005-11-15' );
insert into a_bkorders.customers values (217796, 'Anders', null, 'IL', '62505', '2011-03-30' );
insert into a_bkorders.customers values (272787, 'Carlson', 'Ben', 'IL', '62505', '2011-05-05' );
insert into a_bkorders.customers values (234709, 'Brahms', 'Johnnie', 'MA', '02558', '2012-01-15' );
insert into a_bkorders.customers values (217002, 'Grieg', 'Edvard', 'IL', '62329', '2012-02-29' );
insert into a_bkorders.customers values (272611, 'Jarrett', 'Keith', 'IL', '62329', '2011-11-11' );
insert into a_bkorders.customers values (299099, 'Sam', 'Dave', 'CA', '94141', '2011-01-01' );
insert into a_bkorders.customers values (259906, 'Capybara', 'Wile E.', 'CA', '94132', '2012-01-05' );
insert into a_bkorders.customers values (259907, 'Hedge', 'Mr.', 'CA', '94132', '2011-09-05' );
insert into a_bkorders.customers values (282716, 'Biederbecke','Dwight', 'PA', '18106', '2013-01-01' );
insert into a_bkorders.customers values (287261, 'Biederbecke','Bix', 'PA', '18106', '2012-01-01' );
insert into a_bkorders.customers values (226275, 'Dalrymple','Jack', 'SD', '57216', '2013-01-01' );
insert into a_bkorders.customers values (228175, 'Cardin','Benjamin', 'MD', '20609', '2013-04-02' );
insert into a_bkorders.customers values (228275, 'Mikulski','Barbara', 'MD', '21203', '2013-04-04' );
insert into a_bkorders.customers values (228352, 'Edwards','Donna', 'MD', '21205', '2013-06-08' );
-- orders and order_details
/* July 2011 */
Insert into a_bkorders.order_headers values(21841, '2011-07-02', 267780);
Insert into a_bkorders.order_details values(21841, 1, 1448, 50, 25.00);
Insert into a_bkorders.order_headers values(21850, '2011-07-02', 261502);
Insert into a_bkorders.order_details values(21850, 1, 1162, 1, 30.49);
Insert into a_bkorders.order_details values(21850, 2, 1109, 1, 25.00);
Insert into a_bkorders.order_headers values(2045, '2011-07-18', 267780);
Insert into a_bkorders.order_details values(2045, 1, 1894, 1, 35.99);
Insert into a_bkorders.order_headers values(2200, '2011-07-18', 261502);
Insert into a_bkorders.order_details values(2200, 1, 1200, 5, 16.33);
Insert into a_bkorders.order_details values(2200, 2, 1180, 5, 45.99);
Insert into a_bkorders.order_details values(2200, 3, 1128, 5, 46.20);
/* Sep 2011 */
Insert into a_bkorders.order_headers values(22909, '2011-09-25', 239427);
Insert into a_bkorders.order_details values(22909, 1, 1104, 5, 45.00);
Insert into a_bkorders.order_headers values(22910, '2011-09-25', 218709);
Insert into a_bkorders.order_details values(22910, 1, 1678, 5, 49.99);
Insert into a_bkorders.order_details values(22910, 2, 1162, 5, 35.00);
Insert into a_bkorders.order_headers values(32997, '2011-09-22', 239427);
Insert into a_bkorders.order_details values(32997, 1, 1948, 5, 40.94);
Insert into a_bkorders.order_details values(32997, 2, 1199, 5, 18.39);
Insert into a_bkorders.order_details values(32997, 3, 1457, 5, 53.99);
Insert into a_bkorders.order_details values(32997, 4, 1133, 5, 18.15);
Insert into a_bkorders.order_details values(32997, 5, 1894, 5, 36.79);
Insert into a_bkorders.order_headers values(32998, '2011-09-22', 261502);
Insert into a_bkorders.order_details values(32998, 1, 2006, 3, 20.00);
Insert into a_bkorders.order_headers values(41005, '2011-09-28', 290298);
Insert into a_bkorders.order_details values(41005, 1, 1142, 2, 42.45);
Insert into a_bkorders.order_details values(41005, 2, 1107, 4, 21.50);
Insert into a_bkorders.order_headers values(41006, '2011-09-28', 267780);
Insert into a_bkorders.order_details values(41006, 1, 1142, 10, 42.95);
Insert into a_bkorders.order_headers values(42899, '2011-09-29', 261502);
Insert into a_bkorders.order_details values(42899, 1, 1128, 5, 25.00);
Insert into a_bkorders.order_details values(42899, 2, 1103, 1 , 10.95);
/* Oct 2011 */
Insert into a_bkorders.order_headers values(21254, '2011-10-23', 263119);
Insert into a_bkorders.order_details values(21254, 2, 2008, 10, 46.95);
Insert into a_bkorders.order_details values(21254, 3, 2007, 10, 39.00);
Insert into a_bkorders.order_headers values(21255, '2011-10-28', 267780);
Insert into a_bkorders.order_details values(21255, 1, 1101, 5, 59.99);
Insert into a_bkorders.order_details values(21255, 2, 1142, 5, 39.00);
Insert into a_bkorders.order_details values(21255, 3, 1162, 2, 35.00);
Insert into a_bkorders.order_headers values(21261, '2011-10-28', 200368);
Insert into a_bkorders.order_details values(21261, 1, 1142, 100, 34.95);
Insert into a_bkorders.order_details values(21261, 2, 1128, 50, 46.95);
Insert into a_bkorders.order_details values(21261, 3, 2001, 100, 39.00);
Insert into a_bkorders.order_headers values(32905, '2011-10-02', 259906);
Insert into a_bkorders.order_details values(32905, 1, 2028, 1, 58.00);
Try this:
SELECT c.cust_id, SUM(IF(QUARTER(od.orders) = 1, od.order_price * od.quantity, 0)) Quarter1,
SUM(IF(QUARTER(od.orders) = 2, od.order_price * od.quantity, 0)) Quarter2,
SUM(IF(QUARTER(od.orders) = 3, od.order_price * od.quantity, 0)) Quarter3,
SUM(IF(QUARTER(od.orders) = 4, od.order_price * od.quantity, 0)) Quarter4
FROM a_bkorders.customers c
INNER JOIN a_bkorders.order_headers oh ON c.cust_id = oh.cust_id
INNER JOIN a_bkorders.order_details od ON oh.order_id = od.order_id
WHERE YEAR(od.orders) = YEAR(CURRENT_DATE()) - 1
GROUP BY c.cust_id
Related
I am trying to sum multiple rows of the same athlete so it will return the total amount of medals that they have won overall. I have the following code:
CREATE TABLE athlete (
athlete_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name TINYTEXT NOT NULL,
country TINYTEXT NOT NULL,
birthdate DATE NOT NULL,
age INT UNSIGNED,
height_inch INT UNSIGNED,
PRIMARY KEY (athlete_id)
);
INSERT INTO athlete (name, country, birthdate, age, height_inch) VALUES ('Simone Biles', 'United States', '1997-03-14', 24, 56);
INSERT INTO athlete (name, country, birthdate, age, height_inch) VALUES ('Michael Phelps', 'United States', '1985-06-30', 36, 76);
CREATE TABLE sport (
sport_id INT UNSIGNED NOT NULL,
sport TINYTEXT NOT NULL,
PRIMARY KEY (sport_id)
);
INSERT INTO sport VALUES (101, 'Skiing');
INSERT INTO sport VALUES (102, 'Biathlon');
INSERT INTO sport VALUES (103, 'Curling');
INSERT INTO sport VALUES (104, 'Skating');
INSERT INTO sport VALUES (105, 'Ice Hockey');
INSERT INTO sport VALUES (106, 'Luge');
INSERT INTO sport VALUES (107, 'Snowboard');
INSERT INTO sport VALUES (108, 'Basketball');
INSERT INTO sport VALUES (109, 'Gymnastics');
INSERT INTO sport VALUES (110, 'Swimming');
INSERT INTO sport VALUES (111, 'Diving');
INSERT INTO sport VALUES (112, 'Track and Field');
INSERT INTO sport VALUES (113, 'Badminton');
INSERT INTO sport VALUES (114, 'Tennis');
INSERT INTO sport VALUES (115, 'Volleyball');
INSERT INTO sport VALUES (116, 'Skateboard');
INSERT INTO sport VALUES (117, 'Soccer');
INSERT INTO sport VALUES (118, 'Golf');
INSERT INTO sport VALUES (119, 'Cycling');
INSERT INTO sport VALUES (120, 'Climbing');
INSERT INTO sport VALUES (121, 'Surfing');
INSERT INTO sport VALUES (122, 'Water Polo');
INSERT INTO sport VALUES (123, 'Karate');
CREATE TABLE olympics (
olympics_id INT UNSIGNED NOT NULL,
season TINYTEXT NOT NULL,
year YEAR NOT NULL,
city TINYTEXT NOT NULL,
PRIMARY KEY (olympics_id)
);
INSERT INTO olympics VALUES (1001, 'Summer', 1936, 'Berlin');
INSERT INTO olympics VALUES (1002, 'Summer', 1956, 'Melbourne');
INSERT INTO olympics VALUES (1003, 'Summer', 1960, 'Rome');
INSERT INTO olympics VALUES (1004, 'Summer', 1964, 'Tokyo');
INSERT INTO olympics VALUES (1005, 'Summer', 1976, 'Montreal');
INSERT INTO olympics VALUES (1006, 'Summer', 1984, 'Los Angelos');
INSERT INTO olympics VALUES (1007, 'Summer', 1996, 'Atlanta');
INSERT INTO olympics VALUES (1008, 'Summer', 2000, 'Sydney');
INSERT INTO olympics VALUES (1009, 'Summer', 2004, 'Athens');
INSERT INTO olympics VALUES (1010, 'Summer', 2008, 'Beijing');
INSERT INTO olympics VALUES (1011, 'Summer', 2012, 'London');
INSERT INTO olympics VALUES (1012, 'Summer', 2016, 'Rio de Janeiro');
INSERT INTO olympics VALUES (1013, 'Summer', 2020, 'Tokyo');
CREATE TABLE sport_events (
sport_id INT UNSIGNED NOT NULL,
event_id INT UNSIGNED NOT NULL,
event TINYTEXT NOT NULL,
PRIMARY KEY (event_id),
FOREIGN KEY (sport_id) REFERENCES sport (sport_id)
);
INSERT INTO sport_events VALUES (101, 501, 'Alpine Skiing');
INSERT INTO sport_events VALUES (101, 502, 'Cross-Country Skiing');
INSERT INTO sport_events VALUES (104, 503, 'Figure Skating');
INSERT INTO sport_events VALUES (101, 504, 'Freestyle Skiing');
INSERT INTO sport_events VALUES (104, 505, 'Short Track Speed Skating');
INSERT INTO sport_events VALUES (101, 506, 'Ski Jumping');
INSERT INTO sport_events VALUES (107, 507, 'Half-pipe');
INSERT INTO sport_events VALUES (101, 508, 'Half-pipe');
INSERT INTO sport_events VALUES (104, 509, 'Speed Skating');
INSERT INTO sport_events VALUES (109, 510, 'Artistic Gymnastics');
INSERT INTO sport_events VALUES (109, 511, 'Rhythmic Gymnastics');
INSERT INTO sport_events VALUES (115, 512, 'Beach Volleyball');
INSERT INTO sport_events VALUES (112, 513, 'High Jump');
INSERT INTO sport_events VALUES (112, 514, '100m');
INSERT INTO sport_events VALUES (112, 515, '200m');
INSERT INTO sport_events VALUES (112, 516, '400m');
INSERT INTO sport_events VALUES (112, 517, '800m');
INSERT INTO sport_events VALUES (112, 518, '4x100m relay');
INSERT INTO sport_events VALUES (112, 519, 'Triple Jump');
CREATE TABLE athlete_sport (
athlete_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sport_id INT UNSIGNED NOT NULL,
PRIMARY KEY (athlete_id),
FOREIGN KEY (athlete_id) REFERENCES athlete (athlete_id),
FOREIGN KEY (sport_id) REFERENCES sport (sport_id)
);
INSERT INTO athlete_sport (sport_id) VALUES (109);
INSERT INTO athlete_sport (sport_id) VALUES (110);
CREATE TABLE compete (
athlete_id INT UNSIGNED NOT NULL,
olympics_id INT UNSIGNED NOT NULL,
sport_id INT UNSIGNED NOT NULL,
event_id INT UNSIGNED,
gold INT UNSIGNED,
silver INT UNSIGNED,
bronze INT UNSIGNED,
FOREIGN KEY (olympics_id) REFERENCES olympics (olympics_id),
FOREIGN KEY (athlete_id) REFERENCES athlete (athlete_id),
FOREIGN KEY (event_id) REFERENCES sport_events (event_id)
);
INSERT INTO compete VALUES (1, 1012, 109, 510, 4, 0, 1);
INSERT INTO compete VALUES (1, 1013, 109, 510, 0, 1, 1);
INSERT INTO compete VALUES (2, 1009, 110, NULL, 6, 0, 2);
INSERT INTO compete VALUES (2, 1010, 110, NULL, 8, 0, 0);
INSERT INTO compete VALUES (2, 1011, 110, NULL, 4, 2, 0);
INSERT INTO compete VALUES (2, 1012, 110, NULL, 5, 1, 0);
I have looked at other answers people have posted and most of them are just saying to use group by, but when I use that it just orders the either names or number of medals together just in a different order. I'm trying to get it so it says that the total number of medals simone biles has is 7 and michael phelps is 28 just in a single table.
This is the query I have that returns the sum of the medals for each olympic games they've been in, but again if I use group by it just orders them.
select a.name, gold+silver+bronze as medalTotal from athlete a join compete c using (athlete_id) group by medalTotal;
+----------------+------------+
| name | medalTotal |
+----------------+------------+
| Simone Biles | 2 |
| Simone Biles | 5 |
| Michael Phelps | 6 |
| Michael Phelps | 8 |
+----------------+------------+
you need to do the following:
group by athlete
sum each medal type
add the sum of each medal type
You need to group by athlete, not total.
You have to use an aggregation function to combine values from all the rows in a group. Use SUM() to add them together.
select a.name, SUM(b.gold+b.silver+b.bronze) as medalTotal
from athlete a
join compete c using (athlete_id)
group by a.athlete_id;
You should apply group by using something unique like athelet id.
This get all atheletes with all medals number including with zero medals
if you want just athelet with medals only use join only.
select
a.name, sum(gold+silver+bronze) as medalTotal
from
athlete a
left join
compete c on c.athlete_id = a.athlete_id
group by
a.athlete_id
I have these three main tables Microdisenos, competencias and resultados.
My problem is this: I want to know what are the resultados that belong to the competencias of a microdisenos.
I know how to do it when relationships are one to many, but in this case it is many to many and I do not know how to handle those intermediate tables.
Thanks for your help.
The only way is to join throught all the columns:
SELECT * (or whatever you need)
FROM resultados r
INNER JOIN competencia_resultado cr
ON r.id = cr.resultado_id
INNER JOIN cometencias c
ON c.id = cr.cometencia_id
INNER JOIN competencia_microdisendo cm
ON c.id = cm.competencia_id
INNER JOIN microdisendos m
ON m.id = cm.microdisendo_id
And if you want to select just the once that belong to one specific microdesendo add the WHERE clause with m.id
CREATE DATABASE testDB;
USE testDB;
CREATE TABLE microdisenos (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
description VARCHAR(255),
PRIMARY KEY (id)
);
CREATE TABLE competencias (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
modulo VARCHAR(128),
PRIMARY KEY (id)
);
CREATE TABLE competencia_microdiseno (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
microdiseno_id INTEGER UNSIGNED,
competencia_id INTEGER UNSIGNED,
PRIMARY KEY (id),
FOREIGN KEY (microdiseno_id) REFERENCES microdisenos (id),
FOREIGN KEY (competencia_id) REFERENCES competencias (id)
);
CREATE TABLE resultados (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
description VARCHAR(256),
PRIMARY KEY (id)
);
CREATE TABLE competencia_resultado(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
resultado_id INTEGER UNSIGNED,
competencia_id INTEGER UNSIGNED,
PRIMARY KEY (id),
FOREIGN KEY (resultado_id) REFERENCES resultados (id),
FOREIGN KEY (competencia_id) REFERENCES competencias (id)
);
INSERT INTO competencias VALUES (0, 'Compentencia AA');
INSERT INTO competencias VALUES (0, 'Compentencia BB');
INSERT INTO competencias VALUES (0, 'Compentencia CC');
INSERT INTO competencias VALUES (0, 'Compentencia DD');
INSERT INTO competencias VALUES (0, 'Compentencia EE');
INSERT INTO microdisenos VALUES (0, 'Microdisenos 101');
INSERT INTO microdisenos VALUES (0, 'Microdisenos 202');
INSERT INTO microdisenos VALUES (0, 'Microdisenos 303');
INSERT INTO microdisenos VALUES (0, 'Microdisenos 404');
INSERT INTO microdisenos VALUES (0, 'Microdisenos 505');
INSERT INTO resultados VALUES (0, 'Resultados 11');
INSERT INTO resultados VALUES (0, 'Resultados 22');
INSERT INTO resultados VALUES (0, 'Resultados 33');
INSERT INTO resultados VALUES (0, 'Resultados 44');
INSERT INTO resultados VALUES (0, 'Resultados 55');
INSERT INTO competencia_microdiseno VALUES(0, 1, 1);
INSERT INTO competencia_microdiseno VALUES(0, 1, 2);
INSERT INTO competencia_microdiseno VALUES(0, 1, 3);
INSERT INTO competencia_microdiseno VALUES(0, 2, 4);
INSERT INTO competencia_microdiseno VALUES(0, 2, 5);
INSERT INTO competencia_microdiseno VALUES(0, 3, 1);
INSERT INTO competencia_microdiseno VALUES(0, 3, 2);
INSERT INTO competencia_microdiseno VALUES(0, 4, 3);
INSERT INTO competencia_microdiseno VALUES(0, 4, 4);
INSERT INTO competencia_microdiseno VALUES(0, 4, 5);
INSERT INTO competencia_microdiseno VALUES(0, 5, 1);
INSERT INTO competencia_microdiseno VALUES(0, 5, 2);
INSERT INTO competencia_microdiseno VALUES(0, 5, 3);
INSERT INTO competencia_resultado VALUES(0, 1, 1);
INSERT INTO competencia_resultado VALUES(0, 2, 2);
INSERT INTO competencia_resultado VALUES(0, 3, 3);
INSERT INTO competencia_resultado VALUES(0, 4, 4);
INSERT INTO competencia_resultado VALUES(0, 5, 5);
INSERT INTO competencia_resultado VALUES(0, 1, 1);
INSERT INTO competencia_resultado VALUES(0, 2, 2);
INSERT INTO competencia_resultado VALUES(0, 3, 3);
INSERT INTO competencia_resultado VALUES(0, 4, 4);
INSERT INTO competencia_resultado VALUES(0, 5, 5);
INSERT INTO competencia_resultado VALUES(0, 1, 1);
INSERT INTO competencia_resultado VALUES(0, 2, 2);
INSERT INTO competencia_resultado VALUES(0, 3, 3);
-- Give me all (unique) Resultados for Compentencias for given Microdisenos
SELECT DISTINCT r.*
FROM resultados r, competencia_resultado cr, competencias c, competencia_microdiseno cm, microdisenos m
WHERE r.id = cr.resultado_id
AND c.id = cr.competencia_id
AND c.id = cm.competencia_id
AND m.id = cm.microdiseno_id
AND m.description = "Microdisenos 303";
The result would be:
# id, description
---- ---------------
1, Resultados 11
2, Resultados 22
Option 2:
Subquery
SELECT DISTINCT r.*
FROM resultados r
WHERE r.id IN ( SELECT DISTINCT cr.id
FROM competencia_resultado cr
WHERE cr.competencia_id IN (
SELECT DISTINCT c.id
FROM competencias c
WHERE c.id IN (
SELECT DISTINCT cm.competencia_id
FROM competencia_microdiseno cm, microdisenos m
WHERE m.id = cm.microdiseno_id
AND m.description = "Microdisenos 303" )
)
);
Which customer(s) ordered the most different books? Display the customer id and include ties. This is not talking about the quantity of books ordered. For example, suppose my only orders are Customer 1 ordered 400 copies of book_id 34 Customer 2 ordered 2 copies of book_id 62 and 3 copies of book_id 29 Customer 1 ordered a larger quantity of books but customer 2 ordered more different books. The query should - in this case- return customer 2:
Here is my attempted solution. Not sure how to solve this one:
SELECT cust_id
FROM a_bkorders.customers
WHERE cust_id IN
(
SELECT cust_id
FROM a_bkorders.order_headers
Join a_bkorders.order_details using (order_id)
GROUP BY cust_id
HAVING count(book_id) <=
(
SELECT MAX(numBooks)
FROM (
SELECT cust_id
, count(book_id) AS numBooks
Join a_bkorders.order_details using (order_id)
FROM a_bkorders.order_headers
GROUP BY cust_id
) t
)
)ORDER BY cust_id;
Here are the tables:
-- create customers
create table a_bkorders.customers (
cust_id integer not null
, cust_name_last varchar(20) not null
, cust_name_first varchar(20) null
, cust_state char(2) not null
, cust_postal_code char(10) not null
, cust_acct_opened date not null
, constraint bk_cust_pk primary key (cust_id)
, constraint bk_cust_id_range check (cust_id > 1000)
, constraint bk_cust_acct_opened_ck check (cust_acct_opened >= '1975-01-01' )
)engine = INNODB;
-- create order_headers
create table a_bkorders.order_headers (
order_id integer not null
, order_date date not null
, cust_id integer not null
, constraint bk_orders_pk primary key (order_id)
, constraint bk_orders_cust_fk foreign key(cust_id)
references a_bkorders.customers(cust_id)
, constraint bk_order_id_range check (order_id > 100)
, constraint bk_order_date_ck check (order_date >= '2000-01-01')
)engine = INNODB;
-- create order_details
create table a_bkorders.order_details (
order_id integer not null
, order_line integer not null
, book_id integer not null
, quantity integer not null
, order_price numeric(6,2) not null
, constraint bk_orderline_pk primary key (order_id, order_line)
, constraint bk_orderline_order_fk foreign key (order_id)
references a_bkorders.order_headers(order_id) on delete cascade
, constraint bk_orderline_book_fk foreign key (book_id )
references a_bkinfo.books(book_id)
, constraint bk_quantity_ck check (quantity > 0)
, constraint bk_ordprice_ck check (order_price >= 0)
)engine = INNODB;
Here are the inserts:
-- customers
insert into a_bkorders.customers values (208950, 'Adams', 'Samuel', 'MA', '02106', '1996-04-15' );
insert into a_bkorders.customers values (200368, 'Blake', 'William', 'CA', '95959', '1997-07-15' );
insert into a_bkorders.customers values (258595, 'Jobs', 'Peter', 'MA', '02575', '1997-01-09' );
insert into a_bkorders.customers values (263119, 'Jones', null, 'IL', '62979', '1997-03-02' );
insert into a_bkorders.customers values (224038, 'Austin', 'Pat', 'CA', '95900', '1997-08-02' );
insert into a_bkorders.customers values (255919, 'Milton', 'John', 'NJ', '08235', '2011-05-31' );
insert into a_bkorders.customers values (211483, 'Carroll', 'Lewis', 'CA', '94203', '1998-08-08' );
insert into a_bkorders.customers values (221297, 'Dodgson', 'Charles', 'MI', '49327', '2001-05-06' );
insert into a_bkorders.customers values (261502, 'Hawthorne', 'Nathaniel', 'MA', '02297', '2001-10-12' );
insert into a_bkorders.customers values (212921, 'Books on Tap', NULL, 'CA', '94112', '2002-01-06' );
insert into a_bkorders.customers values (260368, 'Muller', 'Jonathan', 'IL', '62885', '2005-12-15' );
insert into a_bkorders.customers values (259969, 'Carlsen', 'Benny', 'NJ', '08505', '2011-07-12' );
insert into a_bkorders.customers values (239427, 'Marksa', 'Anna', 'NJ', '08495', '2011-02-28' );
insert into a_bkorders.customers values (296598, 'Collins', 'Douglas', 'MO', '65836', '2005-04-25' );
insert into a_bkorders.customers values (276381, 'Collins', 'Douglas', 'OH', '22451', '2005-02-08' );
insert into a_bkorders.customers values (234138, 'Keats', 'John', 'IL', '61500', '2006-04-30' );
insert into a_bkorders.customers values (267780, 'Shelly', 'Mary', 'CA', '94100', '2010-10-02' );
insert into a_bkorders.customers values (290298, 'Swift', 'Jonathan', 'MI', '49201', '2010-10-12' );
insert into a_bkorders.customers values (226656, 'Randall', 'Randell', 'NJ', '08251', '2011-08-08' );
insert into a_bkorders.customers values (222477, 'Rossetti', 'Christina', 'MI', '49742', '2011-07-11' );
insert into a_bkorders.customers values (227105, 'Kafka', 'Franz', 'MA', '02297', '2010-12-31' );
insert into a_bkorders.customers values (202958, 'Denver', null, 'IL', '60405', '2011-01-15' );
insert into a_bkorders.customers values (218709, 'Bonnard', 'Paul', 'MA', '02558', '2005-11-15' );
insert into a_bkorders.customers values (217796, 'Anders', null, 'IL', '62505', '2011-03-30' );
insert into a_bkorders.customers values (272787, 'Carlson', 'Ben', 'IL', '62505', '2011-05-05' );
insert into a_bkorders.customers values (234709, 'Brahms', 'Johnnie', 'MA', '02558', '2012-01-15' );
insert into a_bkorders.customers values (217002, 'Grieg', 'Edvard', 'IL', '62329', '2012-02-29' );
insert into a_bkorders.customers values (272611, 'Jarrett', 'Keith', 'IL', '62329', '2011-11-11' );
insert into a_bkorders.customers values (299099, 'Sam', 'Dave', 'CA', '94141', '2011-01-01' );
insert into a_bkorders.customers values (259906, 'Capybara', 'Wile E.', 'CA', '94132', '2012-01-05' );
insert into a_bkorders.customers values (259907, 'Hedge', 'Mr.', 'CA', '94132', '2011-09-05' );
insert into a_bkorders.customers values (282716, 'Biederbecke','Dwight', 'PA', '18106', '2013-01-01' );
insert into a_bkorders.customers values (287261, 'Biederbecke','Bix', 'PA', '18106', '2012-01-01' );
insert into a_bkorders.customers values (226275, 'Dalrymple','Jack', 'SD', '57216', '2013-01-01' );
insert into a_bkorders.customers values (228175, 'Cardin','Benjamin', 'MD', '20609', '2013-04-02' );
insert into a_bkorders.customers values (228275, 'Mikulski','Barbara', 'MD', '21203', '2013-04-04' );
insert into a_bkorders.customers values (228352, 'Edwards','Donna', 'MD', '21205', '2013-06-08' );
-- orders and order_details
/* July 2011 */
Insert into a_bkorders.order_headers values(21841, '2011-07-02', 267780);
Insert into a_bkorders.order_details values(21841, 1, 1448, 50, 25.00);
Insert into a_bkorders.order_headers values(21850, '2011-07-02', 261502);
Insert into a_bkorders.order_details values(21850, 1, 1162, 1, 30.49);
Insert into a_bkorders.order_details values(21850, 2, 1109, 1, 25.00);
Insert into a_bkorders.order_headers values(2045, '2011-07-18', 267780);
Insert into a_bkorders.order_details values(2045, 1, 1894, 1, 35.99);
Insert into a_bkorders.order_headers values(2200, '2011-07-18', 261502);
Insert into a_bkorders.order_details values(2200, 1, 1200, 5, 16.33);
Insert into a_bkorders.order_details values(2200, 2, 1180, 5, 45.99);
Insert into a_bkorders.order_details values(2200, 3, 1128, 5, 46.20);
/* Sep 2011 */
Insert into a_bkorders.order_headers values(22909, '2011-09-25', 239427);
Insert into a_bkorders.order_details values(22909, 1, 1104, 5, 45.00);
Insert into a_bkorders.order_headers values(22910, '2011-09-25', 218709);
Insert into a_bkorders.order_details values(22910, 1, 1678, 5, 49.99);
Insert into a_bkorders.order_details values(22910, 2, 1162, 5, 35.00);
Insert into a_bkorders.order_headers values(32997, '2011-09-22', 239427);
Insert into a_bkorders.order_details values(32997, 1, 1948, 5, 40.94);
Insert into a_bkorders.order_details values(32997, 2, 1199, 5, 18.39);
Insert into a_bkorders.order_details values(32997, 3, 1457, 5, 53.99);
Insert into a_bkorders.order_details values(32997, 4, 1133, 5, 18.15);
Insert into a_bkorders.order_details values(32997, 5, 1894, 5, 36.79);
Insert into a_bkorders.order_headers values(32998, '2011-09-22', 261502);
Insert into a_bkorders.order_details values(32998, 1, 2006, 3, 20.00);
Insert into a_bkorders.order_headers values(41005, '2011-09-28', 290298);
Insert into a_bkorders.order_details values(41005, 1, 1142, 2, 42.45);
Insert into a_bkorders.order_details values(41005, 2, 1107, 4, 21.50);
Insert into a_bkorders.order_headers values(41006, '2011-09-28', 267780);
Insert into a_bkorders.order_details values(41006, 1, 1142, 10, 42.95);
Insert into a_bkorders.order_headers values(42899, '2011-09-29', 261502);
Insert into a_bkorders.order_details values(42899, 1, 1128, 5, 25.00);
Insert into a_bkorders.order_details values(42899, 2, 1103, 1 , 10.95);
/* Oct 2011 */
Insert into a_bkorders.order_headers values(21254, '2011-10-23', 263119);
Insert into a_bkorders.order_details values(21254, 2, 2008, 10, 46.95);
Insert into a_bkorders.order_details values(21254, 3, 2007, 10, 39.00);
Insert into a_bkorders.order_headers values(21255, '2011-10-28', 267780);
Insert into a_bkorders.order_details values(21255, 1, 1101, 5, 59.99);
Insert into a_bkorders.order_details values(21255, 2, 1142, 5, 39.00);
Insert into a_bkorders.order_details values(21255, 3, 1162, 2, 35.00);
Insert into a_bkorders.order_headers values(21261, '2011-10-28', 200368);
Insert into a_bkorders.order_details values(21261, 1, 1142, 100, 34.95);
Insert into a_bkorders.order_details values(21261, 2, 1128, 50, 46.95);
Insert into a_bkorders.order_details values(21261, 3, 2001, 100, 39.00);
Insert into a_bkorders.order_headers values(32905, '2011-10-02', 259906);
Insert into a_bkorders.order_details values(32905, 1, 2028, 1, 58.00);
Try this:
SELECT c.cust_id, COUNT(DISTINCT od.book_id) bookCnt
FROM a_bkorders.customers c
INNER JOIN a_bkorders.order_headers oh ON c.cust_id = oh.cust_id
INNER JOIN a_bkorders.order_details od ON oh.order_id = od.order_id
GROUP BY c.cust_id
ORDER BY bookCnt DESC
Display the publisher id and name of the publishers who supply both books with a topic id of MySQL and books with a topic id of DB. The books must have a publication year of 2005 or later. These do not have to be the same book. Display only one row per publisher who meets the test.
My Solution:
select DISTINCT publ_id, publ_name
from a_bkinfo.publishers
join a_bkinfo.books using (publ_id)
join a_bkinfo.book_topics using (book_id)
join a_bkorders.order_details using (book_id)
join a_bkorders.order_headers USING (order_id)
where book_id in (
Select book_id
From a_bkinfo.book_topics
Where topic_id = 'SQL'
)
And book_id in (
Select book_id
From a_bkinfo.book_topics
Where topic_id = 'DB'
)
And book_id in
Select book_id
From a_bkinfo.book_topics
Where year(order_date) > 2005
);
Here are the tables:
-- create publishers
create table a_bkinfo.publishers (
publ_id integer not null
, publ_name varchar(25) not null
, constraint bk_publishers_pk primary key(publ_id)
, constraint publ_id_range check (publ_id >1000)
)engine = INNODB;
-- create books
create table a_bkinfo.books (
book_id integer not null
, title varchar(75) not null
, publ_id integer null
, year_publd integer not null
, isbn varchar(17) null
, page_count integer null
, list_price numeric(6,2) null
, constraint bk_books_pk primary key (book_id)
, constraint bk_books_publ_fk foreign key(publ_id)
references a_bkinfo.publishers (publ_id)
, constraint book_id_range check (book_id > 1000)
, constraint bk_page_count_ck check (page_count >= 0)
, constraint bk_price_ck check (list_price >= 0)
, constraint bk_books_year_ck check (year_publd >= 1850)
)engine = INNODB;
-- create book_topics
create table a_bkinfo.book_topics (
book_id integer not null
, topic_id varchar(5) not null
, constraint bk_book_topics_pk primary key (book_id, topic_id)
, constraint bk_books_topics_fk foreign key(topic_id)
references a_bkinfo.topics(topic_id)
, constraint bk_books_id_fk foreign key(book_id)
references a_bkinfo.books(book_id)
)engine = INNODB;
-- create order_headers
create table a_bkorders.order_headers (
order_id integer not null
, order_date date not null
, cust_id integer not null
, constraint bk_orders_pk primary key (order_id)
, constraint bk_orders_cust_fk foreign key(cust_id)
references a_bkorders.customers(cust_id)
, constraint bk_order_id_range check (order_id > 100)
, constraint bk_order_date_ck check (order_date >= '2000-01-01')
)engine = INNODB;
-- create order_details
create table a_bkorders.order_details (
order_id integer not null
, order_line integer not null
, book_id integer not null
, quantity integer not null
, order_price numeric(6,2) not null
, constraint bk_orderline_pk primary key (order_id, order_line)
, constraint bk_orderline_order_fk foreign key (order_id)
references a_bkorders.order_headers(order_id) on delete cascade
, constraint bk_orderline_book_fk foreign key (book_id )
references a_bkinfo.books(book_id)
, constraint bk_quantity_ck check (quantity > 0)
, constraint bk_ordprice_ck check (order_price >= 0)
)engine = INNODB;
Here are some inserts:
-- publishers
Insert into a_bkinfo.publishers values (9000, 'Microsoft Press') ;
Insert into a_bkinfo.publishers values (9456, 'New Directions') ;
Insert into a_bkinfo.publishers values (9102, 'Alfred A. Knopf') ;
Insert into a_bkinfo.publishers values (9325, 'Addison Wesley') ;
Insert into a_bkinfo.publishers values (9745, 'Morgan Kaufmann') ;
Insert into a_bkinfo.publishers values (9521, 'Benjamin/Cummings') ;
Insert into a_bkinfo.publishers values (9822, 'O''Reilly') ;
Insert into a_bkinfo.publishers values (9030, 'McGraw Hill') ;
Insert into a_bkinfo.publishers values (9444, 'APress') ;
Insert into a_bkinfo.publishers values (9528, 'Manning');
Insert into a_bkinfo.publishers values (9020, 'Princeton Univer Press') ;
Insert into a_bkinfo.publishers values (9021, 'Yale University Press') ;
Insert into a_bkinfo.publishers values (9022, 'Havard University Press') ;
Insert into a_bkinfo.publishers values (9507, 'J.Q. Vanderbildt');
Insert into a_bkinfo.publishers values (9664, 'WROX') ;
Insert into a_bkinfo.publishers values (9825, 'MySQL Press') ;
Insert into a_bkinfo.publishers values (9623, 'Prentice Hall') ;
Insert into a_bkinfo.publishers values (9725, 'Springer') ;
Insert into a_bkinfo.publishers values (9561, 'Houghton Mifflin');
Insert into a_bkinfo.publishers values (9902, 'W.W. Norton ') ;
Insert into a_bkinfo.publishers values (9023, 'Holt Paperbacks') ;
Insert into a_bkinfo.publishers values (9024, 'Univ of California Press') ;
Insert into a_bkinfo.publishers values (9776, 'Simon and Schuster') ;
-- topics
Insert into a_bkinfo.topics values ('ADO', 'ADO');
Insert into a_bkinfo.topics values ('CMP', 'Computer Science');
Insert into a_bkinfo.topics values ('DB', 'Database Systems');
Insert into a_bkinfo.topics values ('FCT', 'Fiction');
Insert into a_bkinfo.topics values ('HIST', 'History');
Insert into a_bkinfo.topics values ('MySQL', 'MySQL Database');
Insert into a_bkinfo.topics values ('NET', '.NET Technologies');
Insert into a_bkinfo.topics values ('NOSQL', 'Alternate Data Storage');
Insert into a_bkinfo.topics values ('ORA', 'Oracle Database');
Insert into a_bkinfo.topics values ('POE', 'Poetry');
Insert into a_bkinfo.topics values ('PGM', 'General Programming');
Insert into a_bkinfo.topics values ('SCI', 'Science');
Insert into a_bkinfo.topics values ('SQL', 'SQL');
Insert into a_bkinfo.topics values ('SSRV', 'SQL Server Database');
Insert into a_bkinfo.topics values ('VB', 'Visual Basic');
Insert into a_bkinfo.topics values ('XML', 'XML Techniques');
Insert into a_bkinfo.topics values ('ART', 'Arts, Photography');
-- books
insert into a_bkinfo.books values (1101, 'Programming SQL Server with VB.NET', 9000, 2002, '0735615357', 300, 59.99);
insert into a_bkinfo.books values (1102, 'Practical Standards for VB.NET', 9000, 2003, '0735613568', 250, 49.99);
insert into a_bkinfo.books values (1103, 'Selected Poems', 9456, 1949, null, 125, 12.00);
insert into a_bkinfo.books values (1104, 'Sibley Guide to Bird Life and Behavior', 9102, 2001, '0679451234', 604, 45.00);
insert into a_bkinfo.books values (1105, 'SQL:1999 Relational Language Concepts', 9745, 2002, '1558604561', 450, 59.95);
insert into a_bkinfo.books values (1106, 'SQL for Smarties', 9745, 1995, '1558603239', 250, 29.00);
insert into a_bkinfo.books values (1107, 'SQL Puzzles and Answers', 9745, 1997, '1558604537', 325, 25.00);
insert into a_bkinfo.books values (1108, 'Database Systems', 9325, 1996, null, 680, 39.95);
insert into a_bkinfo.books values (1109, 'Intro to DB Systems-7th Ed', 9325, 2000, '0201385902', 650, 80.00);
insert into a_bkinfo.books values (1110, 'Adv SQL:1999 Object_Relational Features', 9745, 2002, '1558606077', 520, 59.95);
insert into a_bkinfo.books values (1128, 'Temporal Data and the Relational Model', 9325, 2003, 'na', 275, 49.95);
insert into a_bkinfo.books values (1133, 'Leaves of Grass', 9623, 1902, null, 125, 19.95);
insert into a_bkinfo.books values (1142, 'Relational Database Theory', 9521, 1993, null, 879, 95.00);
insert into a_bkinfo.books values (1161, 'SQL Programming Style', 9745, 2005, '0120887975', 780, 35.00);
insert into a_bkinfo.books values (1162, 'Trees and Hierarchies', 9745, 2004, '1558609202', 350, 35.00);
insert into a_bkinfo.books values (1180, 'MySQL Database Design and Tuning', 9825, 2005, '9780672234650', 400, 49.99);
insert into a_bkinfo.books values (1175, 'MySQL in a Nutshell', 9822, 2008, '9780596514331', 538, 34.99);
insert into a_bkinfo.books values (1182, 'MySQL Cookbook', 9822, 2007, '9780596527082', 918, 49.99);
insert into a_bkinfo.books values (1185, 'MySQL Stored Procedures', 9822, 2007, '9780596100896', 595, 49.99);
insert into a_bkinfo.books values (1184, 'MySQL Developer''s Library', 9325, 2009, '9780672329388', 650, 49.99);
insert into a_bkinfo.books values (1301, 'ADO and Oracle Workbook', 9000, 2002, '0265615357', 0, 59.99);
insert into a_bkinfo.books values (1302, 'ADO: the ebook', 9000, 2002, '0852515358', null, 49.99);
insert into a_bkinfo.books values (1303, 'Rainbows and Rainbows', 9521, 2002, '0657895157', null, 59.99);
insert into a_bkinfo.books values (1304, 'Stories of Discoveries', 9325, 2002, '0777788887', 300, 59.99);
insert into a_bkinfo.books values (1305, 'Journeys Through Flatland', 9325, 1958, '0387515357', 100, 9.99);
insert into a_bkinfo.books values (1306, 'Myths of SQL',
9664, 2000, '0454615027', 2895,259.99);
Insert into a_bkorders.order_headers values(30822, '2012-03-12', 211483);
Insert into a_bkorders.order_details values(30822, 1, 1128, 10, 49.95);
/* Apr 2012 */
Insert into a_bkorders.order_headers values(30824, '2012-04-05', 222477);
Insert into a_bkorders.order_details values(30824, 1, 1670, 10, 40.00);
Insert into a_bkorders.order_details values(30824, 4, 2005, 20, 45.00);
Insert into a_bkorders.order_headers values(2001, '2012-04-02', 272787);
Insert into a_bkorders.order_details values(2001, 1, 1448, 50, 25.00);
Insert into a_bkorders.order_headers values(2002, '2012-04-12', 272787);
Insert into a_bkorders.order_details values(2002, 1, 1103, 20, 10.95);
Insert into a_bkorders.order_headers values(2003, '2012-04-12', 272787);
Insert into a_bkorders.order_details values(2003, 1, 1103, 2, 12.00);
Insert into a_bkorders.order_headers values(1564, '2012-04-18', 227105);
Insert into a_bkorders.order_details values(1564, 1, 1106, 50, 34.95);
Insert into a_bkorders.order_details values(1564, 2, 1107, 50, 20.95);
Insert into a_bkorders.order_details values(1564, 3, 2001, 50, 39.00);
Insert into a_bkorders.order_headers values(1800, '2012-04-12', 217796);
Insert into a_bkorders.order_details values(1800, 1, 2009, 5, 34.95);
Insert into a_bkorders.order_details values(1800, 2, 2008, 1, 46.95);
Insert into a_bkorders.order_headers values(1801, '2012-04-13', 217796);
Insert into a_bkorders.order_details values(1801, 1, 1103, 2, 10.95);
Insert into a_bkorders.order_details values(1801, 2, 1106, 1, 29.00);
Insert into a_bkorders.order_headers values(30825, '2012-04-21', 221297);
Insert into a_bkorders.order_details values(30825, 1, 1776, 4, 45.49);
Insert into a_bkorders.order_headers values(30826, '2012-04-24', 211483);
Insert into a_bkorders.order_details values(30826, 2, 1161, 16, 35.00);
Insert into a_bkorders.order_headers values(30833, '2012-04-14', 211483);
Insert into a_bkorders.order_details values(30833, 1, 1448, 50, 25.00);
Insert into a_bkorders.order_headers values(30834, '2012-04-17', 211483);
Insert into a_bkorders.order_details values(30834, 1, 1128, 1, 49.95);
/* May 2012 */
Insert into a_bkorders.order_headers values(30835, '2012-05-17', 211483);
Insert into a_bkorders.order_details values(30835, 1, 1103, 25, 10.95);
Insert into a_bkorders.order_headers values(30836, '2012-05-20', 258595);
Insert into a_bkorders.order_details values(30836, 1, 2008, 2, 12.50);
Insert into a_bkorders.order_headers values(1811, '2012-05-12', 221297);
Insert into a_bkorders.order_details values(1811, 1, 2007, 1, 40.49);
Insert into a_bkorders.order_details values(1811, 2, 1357, 2, 23.40);
Insert into a_bkorders.order_details values(1811, 3, 1537, 3, 28.19);
Insert into a_bkorders.order_headers values(1812, '2012-05-12', 227105);
Insert into a_bkorders.order_details values(1812, 1, 2009, 1, 26.99);
Insert into a_bkorders.order_headers values(1814, '2012-05-15', 290298);
Insert into a_bkorders.order_details values(1814, 1, 1258, 1, 45.99);
Insert into a_bkorders.order_headers values(1818, '2012-05-16', 212921);
Insert into a_bkorders.order_details values(1818, 1, 1106, 30, 20.00);
Insert into a_bkorders.order_details values(1818, 2, 1537, 2, 25.00);
Insert into a_bkorders.order_details values(1818, 3, 1180, 1, 46.99);
Insert into a_bkorders.order_details values(1818, 4, 1979, 1, 53.99);
Insert into a_bkorders.order_headers values(1710, '2012-05-08', 261502);
Insert into a_bkorders.order_details values(1710, 1, 1776, 99, 45.49);
Insert into a_bkorders.order_headers values(1712, '2012-05-09', 290298);
Insert into a_bkorders.order_details values(1712, 1, 1835, 1, 45.99);
Insert into a_bkorders.order_details values(1712, 2, 1162, 99, 30.00);
Insert into a_bkorders.order_headers values(2004, '2012-05-22', 272787);
Insert into a_bkorders.order_details values(2004, 2, 1161, 1, 35.00);
Insert into a_bkorders.order_headers values(2005, '2012-05-30', 272787);
Insert into a_bkorders.order_details values(2005, 1, 1448, 50, 25.00);
/* June 2012 */
Insert into a_bkorders.order_headers values(2012, '2012-06-22', 272787);
Insert into a_bkorders.order_details values(2012, 1, 1448, 50, 25.00);
Insert into a_bkorders.order_headers values(2013, '2012-06-22', 272787);
Insert into a_bkorders.order_details values(2013, 1, 2009, 2, 12.50);
Insert into a_bkorders.order_headers values(30847, '2012-06-20', 296598);
Insert into a_bkorders.order_details values(30847, 1, 1103, 2, 12.00);
Insert into a_bkorders.order_headers values(30848, '2012-06-21', 263119);
Insert into a_bkorders.order_details values(30848, 1, 2007, 2, 12.50);
Insert into a_bkorders.order_headers values(30849, '2012-06-22', 217796);
Insert into a_bkorders.order_details values(30849, 1, 1448, 50, 25.00);
/* July 2012 */
Insert into a_bkorders.order_headers values(31840, '2012-07-01', 267780);
Insert into a_bkorders.order_details values(31840, 1, 1103, 2, 12.00);
Insert into a_bkorders.order_headers values(31841, '2012-07-02', 272787);
Insert into a_bkorders.order_details values(31841, 1, 1448, 50, 25.00);
Insert into a_bkorders.order_headers values(31850, '2012-07-02', 234138);
Insert into a_bkorders.order_details values(31850, 1, 1279, 1, 40.49);
Insert into a_bkorders.order_headers values(1045, '2012-07-18', 222477);
Insert into a_bkorders.order_details values(1045, 1, 1894, 1, 35.99);
Insert into a_bkorders.order_headers values(1200, '2012-07-18', 212921);
Insert into a_bkorders.order_details values(1200, 1, 1200, 5, 16.33);
Insert into a_bkorders.order_details values(1200, 2, 1199, 5, 18.39);
Insert into a_bkorders.order_details values(1200, 3, 1457, 5, 53.99);
Insert into a_bkorders.order_details values(1200, 4, 1133, 5, 18.15);
Insert into a_bkorders.order_details values(1200, 5, 1894, 5, 36.79);
Insert into a_bkorders.order_details values(1200, 6, 1948, 5, 40.94);
Insert into a_bkorders.order_details values(1200, 7, 1180, 5, 45.99);
Insert into a_bkorders.order_details values(1200, 8, 1128, 5, 46.20);
Insert into a_bkorders.order_headers values(1205, '2012-07-20', 212921);
Insert into a_bkorders.order_details values(1205, 1, 1448, 1, 27.29);
Insert into a_bkorders.order_headers values(1212, '2012-07-20', 290298);
Insert into a_bkorders.order_details values(1212, 1, 1894, 1, 37.59);
Insert into a_bkorders.order_details values(1212, 2, 1894, 1, 18.75);
First, a warning. I think this is the first MySQL query I have ever written! So I may not have the syntax right. The way you are joining is strange to me. In my experience, the join is a little more explicit as to what is joined to what and on what fields. I'm also unsure of including publ_name and order_date in the SELECT clause but not in the GROUP BY. Would this produce multiple results per publisher regardless of the GROUP BY clause?
Saharsh's approach is better, but an alternative might look something like this:
SELECT
p.publ_id,
max(p.publ_name) as publ_name,
max(oh.order_date) as order_date
FROM
a_bkinfo.publishers p INNER JOIN a_bkinfo.books b
ON p.publ_id = b.publ_id
INNER JOIN a_bkinfo.book_topics bt
ON b.book_id = bt.book_id
INNER JOIN a_bkinfo.book_topics bt2
ON b.book_id = bt2.book_id
INNER JOIN a_bkorders.order_details od
ON b.book_id = od.book_id
INNER JOIN a_bkorders.order_headers oh
ON od.order_id = oh.order_id
WHERE
bt.topic_id = 'SQL' AND
bt2.topic_id = 'DB' AND
YEAR(oh.order_date) >= 2005
GROUP BY
p.publ_id
Try this:
SELECT p.publ_id, p.publ_name, oh.order_date
FROM a_bkinfo.publishers p
JOIN a_bkinfo.books b USING (publ_id)
JOIN a_bkinfo.book_topics bt USING (book_id)
JOIN a_bkorders.order_details od USING (book_id)
JOIN a_bkorders.order_headers oh USING (order_id)
WHERE bt.topic_id IN ('SQL', 'DB') AND YEAR(oh.order_date) >= 2005
GROUP BY p.publ_id HAVING COUNT(DISTINCT bt.topic_id) = 2;
I need to update some rows in my table, for simplicity called "three".
I select the columns to update with this query:
SELECT one.id
FROM one
JOIN `two` ON ( one.id = two.page )
JOIN `three` ON ( one.id = three.item )
WHERE two.level = 1
AND two.item = (SELECT item FROM two WHERE page = 5 AND level = 1 )
AND three.position > (SELECT position FROM three WHERE item = 5 )
ORDER BY three.position
Now I call an update query with id's I get.
Is there any chance to eliminate the subqueries?
Edit (after Melanie's comment):
Table "one":
|id|text|
Table "two":
|id|item|page|level|
Table "three":
|item|position|
So when I run the query
SELECT item FROM two WHERE page = 5 AND level = 1
It will return f.ex 1 and the final WHERE clause will be:
two.item = 1 AND two.level = 1
Which is not the same as:
two.level = 1 and two.page = 5
I have the table one - some text with some one.id. I need to update all items from table three which has higher position than my item (f.ex. id = 5) have. But those items should also have the same two.item in table two, where two.page = one.id and level = 1
I am sorry for a poor description.
You should be able to replace those subqueries by joins:
SELECT one.id
FROM one
JOIN `two2` ON (two2.page = 5 AND two2.level = 1)
JOIN `two` ON ( one.id = two.page AND two.item = two2.item )
JOIN `three2` ON ( three.item = 5)
JOIN `three` ON ( one.id = three.item AND three.position > three2.position)
WHERE two.level = 1
ORDER BY three.position
#TheVedge is interesting solution but does not produce the same result as your query
I suggest to avoid duplicate the same table also with a view so a little correction
Another correction is three2.item=5
I suggest to use in subquery limit 0,1 so never return more then one element
SELECT one.id
FROM one
JOIN `two` AS TWO2 ON (two2.page = 5 AND two2.level = 1)
JOIN `two` ON ( one.id = two.page AND two.item = two2.item )
JOIN `three` AS THREE2 ON ( three2.item = 5)
JOIN `three` ON ( one.id = three.item AND three.position > three2.position)
WHERE two.level = 1
ORDER BY three.position
Remember that you are not doing the same thing with this query.
Try this
CREATE TABLE `one` (
`id` INT(10) NULL DEFAULT NULL,
`text` VARCHAR(50) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
_
CREATE TABLE `three` (
`item` INT(10) NULL DEFAULT NULL,
`position` INT(10) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
_
CREATE TABLE `two` (
`id` INT(10) NULL DEFAULT NULL,
`item` INT(10) NULL DEFAULT NULL,
`page` INT(10) NULL DEFAULT NULL,
`level` INT(10) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
_
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (1, 1, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (3, 3, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (4, 4, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (5, 5, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (6, 6, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (7, 7, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (8, 8, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (9, 9, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (2, 2, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (10, 2, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (11, 1, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (13, 3, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (14, 4, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (15, 5, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (16, 6, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (17, 7, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (18, 8, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (19, 9, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (20, 2, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (21, 1, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (23, 3, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (24, 4, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (25, 5, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (26, 6, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (27, 7, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (28, 8, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (29, 9, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (30, 2, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (31, 1, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (33, 3, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (34, 4, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (35, 5, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (36, 6, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (37, 7, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (38, 8, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (39, 9, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (40, 2, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (41, 1, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (42, 3, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (43, 4, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (44, 5, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (45, 6, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (46, 7, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (47, 8, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (48, 9, 4, 1);
-
INSERT INTO `three` (`item`, `position`) VALUES (1, 1);
INSERT INTO `three` (`item`, `position`) VALUES (2, 1);
INSERT INTO `three` (`item`, `position`) VALUES (3, 1);
INSERT INTO `three` (`item`, `position`) VALUES (4, 1);
INSERT INTO `three` (`item`, `position`) VALUES (5, 0);
INSERT INTO `three` (`item`, `position`) VALUES (6, 1);
INSERT INTO `three` (`item`, `position`) VALUES (7, 1);
INSERT INTO `three` (`item`, `position`) VALUES (8, 1);
INSERT INTO `three` (`item`, `position`) VALUES (9, 1);
INSERT INTO `three` (`item`, `position`) VALUES (10, 1);
INSERT INTO `three` (`item`, `position`) VALUES (11, 1);
INSERT INTO `three` (`item`, `position`) VALUES (12, 1);
_
INSERT INTO `one` (`id`, `text`) VALUES (1, 'A');
INSERT INTO `one` (`id`, `text`) VALUES (2, 'B');
INSERT INTO `one` (`id`, `text`) VALUES (3, 'C');
INSERT INTO `one` (`id`, `text`) VALUES (4, 'D');
INSERT INTO `one` (`id`, `text`) VALUES (5, 'E');
INSERT INTO `one` (`id`, `text`) VALUES (6, 'F');
INSERT INTO `one` (`id`, `text`) VALUES (7, 'G');
_
SELECT
one.id, one.text
,two.id,two.item,two.page,two.level
,three.item,three.position
FROM one
JOIN `two` ON ( one.id = two.page )
JOIN `three` ON ( one.id = three.item )
WHERE two.level = 1
AND two.item = (SELECT item FROM two WHERE page = 5 AND level = 1 limit 0,1 )
AND three.position > (SELECT position FROM three WHERE item = 5 limit 0,1 )
ORDER BY three.position
SELECT
one.id, one.text
,two.id,two.item,two.page,two.level
,three.item,three.position
FROM one
JOIN `two` AS TWO2 ON (two2.page = 5 AND two2.level = 1)
JOIN `two` ON ( one.id = two.page AND two.item = two2.item )
JOIN `three` AS THREE2 ON ( three2.item = 5)
JOIN `three` ON ( one.id = three.item AND three.position > three2.position)
WHERE two.level = 1
ORDER BY three.position
With original query you made a select of specific element in TheVedge solution you are joining more data
So result depend on what you select
Another useful analysis is http://dev.mysql.com/doc/refman/5.0/en/show-profile.html and Explain
Show Profile show that at the first run
your original query does
Status Duration
starting 0.000039
checking query cache for query 0.000144
Opening tables 0.000032
System lock 0.000007
Table lock 0.000061
init 0.000054
optimizing 0.000314
statistics 0.000021
preparing 0.000051
Creating tmp table 0.000084
executing 0.000004
Copying to tmp table 0.000063
optimizing 0.000008
statistics 0.000019
preparing 0.000009
executing 0.000004
Sending data 0.000054
optimizing 0.000008
statistics 0.000007
preparing 0.000009
executing 0.000003
Sending data 0.000126
Sorting result 0.000030
Sending data 0.000025
end 0.000004
removing tmp table 0.000011
end 0.000005
query end 0.000004
freeing items 0.000101
storing result in query cache 0.000008
logging slow query 0.000003
cleaning up 0.000006
Proposed query does
Status Duration
starting 0.000036
checking query cache for query 0.000122
Opening tables 0.000030
System lock 0.000008
Table lock 0.000064
init 0.000046
optimizing 0.000028
statistics 0.000026
preparing 0.000072
Creating tmp table 0.000086
executing 0.000005
Copying to tmp table 0.001081
Sorting result 0.000040
Sending data 0.000056
end 0.000005
removing tmp table 0.000010
end 0.000005
query end 0.000004
freeing items 0.000108
storing result in query cache 0.000007
logging slow query 0.000003
cleaning up 0.000005
So when you have full data you can try to evalute better the response of both query