Can i use alias in where clause? [closed] - mysql

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 5 years ago.
Improve this question
SELECT sum(st.stock_in) - sum(st.stock_out) as'stock_qty',
p.product_name,p.product_limit
FROM stock as st
join products as p
on p.product_id=st.product_id
where 'stock_qty' < 'p.product_limit'
GROUP BY st.product_id
I Have two Tables Products and Stock.
Products Table: Product_id, Product_name and Product_limit
Stock: stock_id, Product_id, stock_in, stock_out
From above query i get all the records, but I want to get only those records who product limit is reached or below.
if product 1 has 50 product_limit and stock reached the 50 limit or below the 50 so get me those records.
Suggest me best solution.

You cannot:
Use an alias in a where clause.
Use aggregation functions in the where clause.
Although comparing two strings is pretty much not ever recommended, it is allowed.
But, you want a having clause anyway, so the question in your title is irrelevant. Try this:
select sum(st.stock_in) - sum(st.stock_out) as stock_qty,
p.product_name, p.product_limit
from stock st join
products p
on p.product_id = st.product_id
group by p.product_name, p.product_limit
having stock_qty < p.product_limit ;
Note that I changed the group by to match the columns being selected in the select. That is also a good practice.

Related

MySQL: Show all data from one query and join data from another query where it matches [closed]

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 2 years ago.
Improve this question
Trying to understand SQL more as part of a POC I'm working on and have hit a snag. I have two select queries (shown below) and I want to combine these into a single query that:
Shows all of the results of query one
Joins the result of query two if the category column matches.
Provides a default of 0 if there is no match
Query one:
SELECT activityId, location, category, activityScore FROM activities WHERE location = "manchester";
Query two:
SELECT userId, category, userScore FROM userscore s WHERE userId = "32976428";
Expected Output:
The resulting query should show all activities in "manchester" along with the associated userScore if the specified use has one that matches that category. If there is no userscore then 0 should be returned instead.
Thanks for any help.
Carl
I think you need a LEFT JOIN on your userscore table
SELECT a.activityId, a.location, a.category, a.activityScore,
s.userId, ISNULL(s.userScore,0) as userScore
FROM activities a
LEFT JOIN userscore s ON s.category = a.category AND s.userId = "32976428"
WHERE a.location = "manchester";

How many products have multiple prices? Do not count the same product twice [closed]

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 3 years ago.
Improve this question
How many products have multiple prices?
Do not count the same product twice.
This question i am struggling with because it almost contradicts itself. If someone could point me in the right direction. I have tried everything.
i have attached an image because i was not sure how to copy and paste my code from mysql
By not counting the same product twice, I think that means only report each product once regardless of whether it has 2, 3, 5 or a million different prices, so don't report every difference, just a summary of which products have multiples. That's just a guess.
Anyway, something like this will list every product that has more than one price:
SELECT productid
FROM mytable
GROUP BY productid
HAVING COUNT(DISTINCT PRICE) > 1;
This will filter out all products with just a single price, leaving only those with more than one.
And if you want to count them, just jam the query above into a subquery:
SELECT COUNT(*) FROM (
SELECT productid
FROM mytable
GROUP BY productid
HAVING COUNT(DISTINCT PRICE) > 1
) multprod;
multprod above is just an alias for the subquery. I added it because I can't remember if MySQL requires one.

Simplify SQL query for me please [closed]

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 5 years ago.
Improve this question
I have a table of multiple employment records of an employee/s where i want to query for a certain range of date.
Parameters are Company.id,date_hired,date_end
Results must be on the range of the specified date and employee.id must only be one if the employee has multiple employment records result must get the latest employment record..
THIS IS WHAT I CURRENTLY HAVE.
SELECT *
FROM employmentrecords
WHERE employmentrecords.id IN(
SELECT MAX(employmentrecords.id)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22')
GROUP
BY employmentrecords.employee_id)
AND employmentrecords.company_id<>0`
Hope any one would suggest a better approach.Thank you
I am not sure if I got your question all clear, this query below will give you the latest employee record if there are multiple user records -
SELECT * FROM employmentrecords WHERE id IN(SELECT MAX(id) FROM employmentrecords
WHERE ((date_end >='2017-08-22'
OR date_end IS NULL
OR (date_end <='2017-08-22' AND date_end >='2017-08-08'))
AND date_hired <='2017-08-22')
GROUP BY employee_id)
AND company_id<>0
and rownum = 1
order by date_hired desc
JFYI, no need to use the table name as alias if there is only one table you are fetching the data from, it just makes it hard to read the query on go.
SELECT id, max(date_hired)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22') group by id

How to calculating SUM of (quantity*price) from two different tables? [closed]

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
I have this two following tables:
Order Table
Product Table
I'm trying to calculate the subtotal price for each product (quantity*price) then SUM the TOTAL value for the entire order.
Thanks, Any help would be very appreciated.
Check if table names are correct and try this:
SELECT o.order_number, o.item_quantity, p.price,(o.item_quantity * p.price) AS subtotal, SUM( o.item_quantity * p.price) AS total
FROM order AS o
LEFT JOIN product AS p ON product_number = idproduct_detail;
With this I am connecting each order with the relative product then selecting item_quantity and price and SUM the product of them.

Relational MySQL query to work out the total cost of an order [closed]

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 8 years ago.
Improve this question
I'm working on an assignment dealing with relational mysql databases and I'd like to be able to use my tables to do some math. However after creating my tables and attempting various queries I'm starting to think I've messed the relations up. I have four tables, customer, address, orders, and product. My schema is as follows:
I'm trying to user orders.o_qty to multiply the values in product.p_price and output the totals I have been trying for a while now and either my tables are linked up incorrectly or I'm just not getting it.
o_qty in DDR.orders is a numerical value, I have been trying to use this value in a query to multiply the value stored in DDI.product under p_price and output the total value.
Example rows can be found here:
First I have been trying to work out the total then I will progress to modifying this query to work out total per customer.
This is could be done with joining the tables.
case 1 : Show total amount per user i.e. total purchased per user without drilling down each product
select c.c_id,
c.c_fname,
c.c_lname,
sum(p.p_price) as total
from orders o
join customer c on c.cid = o.c_id
join product p on p.p_id = o.p_id group by c.c_id;
Case 2 : Break per product purchased per customer
select c.c_id,
c.c_fname,
c.c_lname,
p.p_id,
sum(p.p_price) as total
from orders o
join customer c on c.cid = o.c_id
join product p on p.p_id = o.p_id
group by c.c_id,p.p_id;