I have two tables, "keywords" and "stats" and want to know per keyword how many results each merchant has. So one row per keyword.
Desired result e.g.:
KWD | RESULTS Amazon | RESULTS eBay
test 3 5
second 6 2
The tables:
create table keywords
(
ID mediumint unsigned auto_increment
primary key,
KEYWORD varchar(255) null
);
create table stats
(
MERCHANT_ID tinyint unsigned not null,
TYPE_ID mediumint unsigned not null comment 'the ID of the coresponding type. E.g. kw_id from keywords',
RESULTS smallint unsigned null,
DATE date not null,
primary key (DATE, MERCHANT_ID, TYPE_ID)
)
comment 'How many results does each merchant have per search?';
Sample data:
-- keywords
insert into test.keywords (ID, KEYWORD) values (1, 'testing');
insert into test.keywords (ID, KEYWORD) values (2, 'blablub');
-- stats
insert into test.stats (MERCHANT_ID, TYPE_ID, RESULTS, DATE) values (1, 1, 33, '2021-07-06');
insert into test.stats (MERCHANT_ID, TYPE_ID, RESULTS, DATE) values (1, 2, 3, '2021-07-06');
insert into test.stats (MERCHANT_ID, TYPE_ID, RESULTS, DATE) values (2, 1, 22, '2021-07-06');
insert into test.stats (MERCHANT_ID, TYPE_ID, RESULTS, DATE) values (2, 2, 6, '2021-07-06');
The query:
select
kwd.KEYWORD,
mss.MERCHANT_ID,
mss.RESULTS
from keywords kwd
LEFT JOIN stats mss ON mss.TYPE_ID = kwd.ID
where
date = 20210705
group by kwd.ID
There are about 10 merchants. Is it possible to get one row per keyword and have the number of results per merchant in seperate colunns?
Try something like this:
select
kwd.KEYWORD,
SUM(IF(mss.MERCHANT_ID = 'amazon', mss.RESULTS, 0)) as `amazon_sum`,
SUM(IF(mss.MERCHANT_ID = 'eBay', mss.RESULTS, 0)) as `eBay_sum`
from keywords kwd
LEFT JOIN stats mss ON mss.TYPE_ID = kwd.ID
where
date = 20210705
group by kwd.ID
# Product table contains 2 columns - id, name
create table Product
(id INT
,name varchar(100)
);
insert into Product(id, name) values
(1, 'Pen'),
(2, 'Paper'),
(3, 'Printer'),
(4, 'Sharpner'),
(5, 'Eraser'),
(6, 'Clip');
OrderInfo table contains 4 columns - id, customer_id, product_id, amount
customer_id is foreignkey to table Customer
product_id is foreignkey to table Product
amount is the value of order placed by a customer for a particular product
create table OrderInfo
(id INT
, customer_id INT
, product_id INT
, amount decimal);
insert into OrderInfo(id, customer_id, product_id, amount) values
(1, 3, 4, 565),
(2, 5, 4, 346),
(3, 1, 1, 365),
(4, 6, 3, 765),
(5, 1, 4, 245),
(6, 6, 2, 876);
The output should look like this
name amount
Pen 365
Paper 876
Printer 765
Shapner 1156
Fast and easy to understand. Just join the two tables by the id of the product and group by the product. The result cann be summarized with SUM()
Select
P.name
, SUM(O.amount)
FROM Product P
Inner Join OrderInfo O on O.product_id = P.id
Group by P.name
There are three related tables:
operations (id, name)
pricelists (id, operations_id (link to operations table), cost)
accounting (id, pricelists_id (link to pricelists table), quantity)
How to get table, like
NAME SUMMARY_COST SUMMARY_QUANTITY
milling result of 2*750 2
threading result of 1*444 1
... ... ...
overall 2*750+1*444+... 2+1+...
I trying to group for two tables at a start:
select operations.name, sum(pricelists.cost) total
from operations
left join pricelists on pricelists.operations_id*accounting_quantity = operations.id*accounting.quantity
group by operations.id
but it is not worked yet
A little database:
CREATE TABLE operations (id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (100));
CREATE TABLE pricelists (id INT PRIMARY KEY, operations_id INT NOT NULL, cost DECIMAL(10,2),
FOREIGN KEY (operations_id) REFERENCES operations (id));
CREATE TABLE accounting (id INT PRIMARY KEY, pricelists_id INT NOT NULL, quantity INT,
FOREIGN KEY (pricelists_id) REFERENCES pricelists (id));
INSERT INTO operations (name) VALUES
('milling'),
('threading'),
('grinding'),
('welding'),
('brazing'),
('soldering'),
('riveting');
INSERT INTO pricelists (id, operations_id, cost) VALUES
(1, 2, 750),
(2, 1, 444),
(3, 3, 123),
(4, 4, 450),
(5, 5, 375),
(6, 6, 250),
(7, 7, 232);
INSERT INTO accounting (id, pricelists_id, quantity) VALUES
(1, 7, 2),
(2, 2, 5),
(3, 4, 2),
(4, 1, 1),
(5, 3, 4);
Consder:
select o.name, sum(a.quantity * pl.cost) total, sum(a.quantity) quantity
from operations o
left join pricelists pl on pl.operations_id = o.id
left join accounting a on a.pricelists_id = pl.id
group by o.name
You can generate the summary row by just adding with rollup at the very end of the query.
I have the following tables:
Payment Table
CREATE TABLE Payment (
PayID INTEGER,
PayName CHAR (55),
PaymentDescription VARCHAR (100),
Primary Key (PayID)
);
Users Table
CREATE TABLE Users (
UserID INTEGER,
UserFirst CHAR (40),
UserLast CHAR (40),
UserName VARCHAR (40),
UserAddress VARCHAR (35),
UserBirthdate DATE,
UserGender CHAR (1),
UserEmail VARCHAR (35),
PRIMARY KEY (UserID)
);
Genre Table
CREATE TABLE GENRE (
GenreID Integer,
Name CHAR(45),
Description VARCHAR (250),
PRIMARY KEY (GenreID)
);
Song Table
CREATE TABLE SONG (
SongID Integer,
GenreID Integer,
Title VARCHAR (50),
Album VARCHAR (40),
Duration TIME,
ReleaseYear DATE,
PRIMARY KEY (SongID),
CONSTRAINT foreignkey_holds_GenreID FOREIGN KEY (GenreID)
REFERENCES Genre(GenreID)
);
Song_Users Table
CREATE TABLE Song_Users (
UserID INTEGER,
SongID Integer,
PRIMARY KEY (UserID,SongID),
CONSTRAINT fk_users_song_user FOREIGN KEY (UserID)
REFERENCES Users(UserID),
CONSTRAINT fk_users_song_song FOREIGN KEY (SongID)
REFERENCES SONG(SongID)
);
Payment_Users Table
CREATE TABLE Payment_Users (
UserID INTEGER,
PayID INTEGER,
PRIMARY KEY (UserID,PayID),
CONSTRAINT fk_users_payment_user FOREIGN KEY (UserID)
REFERENCES Users(UserID),
CONSTRAINT fk_users_payment_pay FOREIGN KEY (PayID)
REFERENCES Payment(PayID)
);
And I have also populated the tables:
Payments
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (1, 'Visa', 'Applies to VISA Debit, Electron, Credit')
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (2, 'Mastercard', 'Applies across all types')
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (3, 'PayPal', 'Payment made using PayPal')
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (4, 'ApplePay', 'Payment method for IOS users')
Users
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (1, 'Jane', 'Johnson', 'jjsweet135', '10 House Wood Street',19970321, 'F', 'jj1997#gmail.com')
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (2, 'Joe', 'Watson', 'joewatson98', '21 House Red Street',19980414, 'M', 'joewatson1998#gmail.com')
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (3, 'Katie', 'Davies', 'katiethebest425', '17 House Fun Street',20000318, 'F', 'katiebest452#hotmail.com')
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (4, 'Tom', 'Branson', 'tommylad100', '27 House Church Street',20010719, 'M', 'tommylad35#orange.com')
Genre Table
INSERT INTO Genre (GenreID, Name, Description) VALUES
(1, 'Electro Swing', 'A musical journey set in the 90s')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(2, 'House', 'When the beat shakes your house')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(3, 'Classical', 'To stimulate neural activity in your brain')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(4, 'Rap', 'Experience the streets from the comfort of your own home')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(5, 'Pop', 'Follow the status quo')
Song Table
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (1, 1, 'Swing the Electro', 'Electro Swing Vol 1', 11537, 19970316)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (2, 2, 'This is my House', 'UK House Anthem', 11454, 19990419)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (3, 3, 'Candy Shop', '50 Cent Classics', 2254, 20010324)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (4, 4, 'Mozart', 'Classical strokes', 44524, 18000225)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (5, 5, 'Katy Perry', 'Baby you are a Firework', 13524, 20050324)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (6, 5, 'One Direction', 'We are the one', 013204, 20060211)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (7, 5, 'Chainsmokers', 'In the back seat of your rover', 33544, 20160123)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (8, 5, 'Justin Bieber', 'Teenage dream', 22222, 20150325)
Song_Users Table
INSERT INTO Song_Users (UserID, SongID) VALUES (1,1)
INSERT INTO Song_Users (UserID, SongID) VALUES (2,2)
INSERT INTO Song_Users (UserID, SongID) VALUES (3,3)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,4)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,1)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,2)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,3)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,5)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,6)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,7)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,8)
Payment_Users Table
INSERT INTO Payment_Users VALUES (1,1)
INSERT INTO Payment_Users VALUES (2,2)
INSERT INTO Payment_Users VALUES (3,3)
INSERT INTO Payment_Users VALUES (4,3)
I need to find the most popular genre of music for Females (F). I can't seem to retrieve this information, because there is no corresponding Foreign Key for Genre(GenreID).
How could I create the query to get the most popular genre amongst females?
EDIT
SELECT Users.UserID, UserGender, SongID, GenreID FROM Users, Song_Users, Song
WHERE Users.UserID = Song_Users.UserID
AND
WHERE Genre.GenreID = Song.GenreID;
I am trying to link GenreID with SongID, but I am not sure how to do it. There is a link with a foreign key, but not in the Genre table, but only in the Song Table. My logic was to select the user, their gender, their songs and then the corresponding genre. Then I could use something like a HAVING CLAUSE to select stricly females and using a COUNT (Genre) and the appropriate GROUPBY function
As per the comments above, there is a huge amount of unnecessary information in this question. A set of sample data and evidence of effort on your part should be included, but the commands used to populate the tables are irrelevant.
I am assuming the following definition of popularity: The greatest number of users that have purchased songs in a given genre.
From what you have above, then you can accomplish what you are looking for with inner joins across four tables:
SELECT
count(u.UserID) as 'popularity',
g.Name as 'genre'
FROM
Users u INNER JOIN
Song_Users su INNER JOIN
SONG s INNER JOIN
GENRE g
ON
u.UserID = su.UserID,
su.SongID = s.SongID,
s.GenreID = g.GenreID
WHERE
u.UserGender = 'F'
GROUP BY
g.Name
ORDER BY
count(u.UserID) DESC
You will Inner join In MYSQL,
Select Field names from Songs Innerjoin users.user_id ON songs.user_id
Innerjoin Genre.Genre_id ON songs.Genre_id
You see type your table names and columns names correctly . You will get the correct result.
SELECT COUNT(g.`GenreID`) AS popularity, g.`Name` FROM genre g, song_users su, song s, users u WHERE
u.`UserGender` = 'F'
AND su.`UserID` = u.`UserID`
AND s.`SongID` = su.`SongID`
AND g.`GenreID` = s.`GenreID`
GROUP BY g.`GenreID`
The popularity would give you the genres name and their count as popularity, the highest count is the most famous.
I have the following DDLs...
CREATE TABLE IF NOT EXISTS `product` (
`id_product` int(10),
`id_manufacturer` int(10)
);
INSERT INTO `product` (`id_product`, `id_manufacturer`) VALUES
(1,1),
(2,1),
(3,2),
(4,1),
(5,2);
CREATE TABLE IF NOT EXISTS `feature_product` (
`id_feature` int(10),
`id_product` int(10),
`id_feature_value` int(10)
);
INSERT INTO `feature_product` (`id_feature`, `id_product`, `id_feature_value`) VALUES
(5, 1, 9),
(5, 2, 9),
(5, 3, 10),
(5, 4, 10),
(7, 5, 10);
http://sqlfiddle.com/#!2/cbe05/1/0
Can you explain me please, how I can get - all Products with the same Manufacturer and the same Feature_value?
Now (in project) I do it with 2 additional SELECT's (for getting id_manufacturer and id_feature_value), but maybe there are more correct (and fast) way?
Thanks for your time and sorry for my English)
I need too see result like this:
id_product |
-----------|
1 |
2 |
only this 2 products have same manufacturer and (at the same time) same feature value
Just use GROUP_CONCAT:
SELECT GROUP_CONCAT(p.id_product SEPARATOR '\n') AS Products
FROM product p
INNER JOIN feature_product fp ON (p.id_product = fp.id_product AND fp.id_feature = 5)
GROUP BY p.id_manufacturer, fp.id_feature_value
HAVING COUNT(p.id_manufacturer) > 1
AND COUNT(fp.id_feature_value)>1;
This will give you the list of Products having multiple Manufacturer Id and Feature Value in a single line, separated by a newline character. You can change the separator as your requirement.
Here is the SQL Fiddle link:
http://sqlfiddle.com/#!2/cbe05/70