Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have table with columns date and price
Can I find rows where the price was more than X percent from the previous price?
My table is
CREATE TABLE IF NOT EXISTS `mydb`.`prices` (
`id` INT NOT NULL,
`date` DATE NULL,
`price` FLOAT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
you want the lead() and lag() functions (one looks to the row above, and the other to the row below). (Here's an example)
For instance:
select * from (
select [date], [price], lag([price],1,0) over (order by date) as lagged_price
from your_table
) sub
where lagged_price / price > X -- here x would be 1.2 if you want it to be at least 20% greater
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Here is the statement I have now:
INSERT INTO jobs
SELECT *
FROM proposals
JOIN commissions
ON proposals.commission_id = commissions.job_id
WHERE proposals.proposal_id = '123'
It's working fine but is creating some primary key conflicts. So, what I would like to do when the new row is entered, is this:
new primary key + commissions table data + proposals table data
Any ideas?
then if you have auto increment pk , do this :
INSERT INTO jobs ( [list of columns except the auto increment column])
SELECT ( [list of columns accordingly])
FROM proposals
JOIN commissions
ON proposals.commission_id = commissions.job_id
WHERE proposals.proposal_id = '123'
Like p.Salmon said in the comments, I would add a new column to the jobs table
ALTER TABLE Jobs add autoid INT(10)AUTO_INCREMENT PRIMARY KEY;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
lets say I have two MySQL tables, table A and table B. Each table has a primary key called id and a name column. I also have a third table, table C, that contains relationships between table A and table B. Each row contains two foreign keys called a_id and b_id, which, as you might expect, correspond to ids in tables A and B.
What I want to do is select a random set of 10 table A rows, but only select rows that have a relationship with specific entries in table B. I don't know which entries I'm looking for ahead of time, and I will start with their names. The names will be provided via query parameters.
I understand I should probably start with this:
SELECT * FROM `A`
ORDER BY RAND()
LIMIT 10
But I don't know how to structure the where clause.
You need something like this:
SELECT *
FROM `A` a
INNER JOIN `C` c ON
a.ID = c.a_id AND
c.b_id in (1,2,3,4) -- your entries here
-- order and limit as you wish
ORDER BY RAND()
LIMIT 10
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have 3 tables
TABLE #1 "users"
3 columns
User-ID int (11)
Location varchar (250)
Age int (11)
Primary-key (User-ID)
TABLE #2 "books"
5 columns
ISBN varchar (13)
Book-title varchar(255)
Book-author varchar (255)
Year-of-publication int(10)
Publisher varchar (255)
TABLE #3 "Book-raitings"
3 columns
User-ID int (11)
ISBN varchar (13)
Book-rating int (11)
Primary-key (user-id, ISBN)
The Location column varchar will be of format (city, state, country)
I have to generate top 10 books per each country into a separate table. Generate SQL dump.
Thank you for your help in advance.
Some initial recommendation:
Do not use - in MySql variable name. It will force to use this syntax: 'Book-ratings'.'Book-rating' instead of: Book_ratings.Book_rating the sign minus is an operator in MySql.
It is a pity you have location as a sequence of comma separated values. It would have been much better to have 3 fields: city, state, country. This force complex string treatment and is a source of limitations.
You can create a table with the country this way:
CREATE TABLE books_by_country
SELECT
`Book-ratings`.`Book-rating`,
`books`.`Book-title`,
REPLACE(`users`.`Location`,CONCAT(SUBSTRING_INDEX(`users`.`Location`, ',', 2),', '),'') as Country
FROM `Book-ratings`
LEFT JOIN `books` ON `books`.`ISBN` = `Book-ratings`.`ISBN`
LEFT JOIN `users` ON `Book-ratings`.`User-ID` = `Book-ratings`.`User-ID`;
From that you can pick the top 10 books for each Country this way:
SELECT
`books_by_country`.`Country`,
`books_by_country`.`Book-title`,
SUM(`books_by_country`.`Book-rating`) as Book_ranking
FROM `Test`.`books_by_country`
WHERE `books_by_country`.`Country`= 'USA'
Group BY `books_by_country`.`Country`,`books_by_country`.`Book-title`
ORDER BY Book_ranking DESC
LIMIT 10;
Regards
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say i have two tables.
Table client
ID - primary key ( 1,2,3,4,5,6)
Table Orders
OrderID - primary key
ClientID (1,2,4,5)
I need to get ROWS of table CLIENT of clients 3 and 6 (which don't have orders / are not in orders table)
This is basic query. Try your self.
select * from client where id not in (select ClientID from orders)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I change the column position to first using ALTER query in MySQL table?
If you want your column at first position, you should use
ALTER TABLE tableName MODIFY COLUMN yourColumnName varchar(50) FIRST
For example, if your table is
Employee{FirstName,LastName,EmpId}
And if you want to move EmpId to first position
ALTER TABLE Employee MODIFY COLUMN EmpId varchar(15) FIRST;
For example :
Below query will bring empname column after department :
ALTER TABLE Employees MODIFY COLUMN empName VARCHAR(50) AFTER department;