This question already has answers here:
SQL - HAVING vs. WHERE
(9 answers)
Closed 4 years ago.
How to select rows where sum of a row is over 200?
I tried all kinds of combinations with grouping, setting AS something and using WHERE clause
Current attempt as follow
SELECT something.CustomerName, something.CustomerAge, cars.Prices,
SUM(cars.Price) AS Amount
FROM cars
INNER JOIN something ON something.CustomerNo=Cars.CustomerNo
GROUP BY AMOUNT
WHERE AMOUNT > '200'
I could not find a tutorial on how to do this
According to your current attempt where clause should go before group by clause
SELECT something.CustomerName, something.CustomerAge,
SUM(cars.Price) AS Amount
FROM cars
INNER JOIN something ON something.CustomerNo=Cars.CustomerNo
GROUP BY something.CustomerName, something.CustomerAge
HAVING SUM(cars.Price) > 200;
However, you actually need to apply your filter on Amount but, you can't do that via where clause for that you would need to apply having clause filter rather than where clause
My today advice is to use table alise that could be more readable and easy to use/implement
SELECT s.CustomerName, s.CustomerAge,
SUM(c.Price) AS Amount
FROM cars as c -- use of alise to make it more effective or readable
INNER JOIN something as s ON s.CustomerNo = c.CustomerNo -- and use that alise everywhere entire the query
GROUP BY s.CustomerName, s.CustomerAge
HAVING SUM(c.Price) > 200;
You could use a subquery to do your maths, then select the values you want
SELECT * FROM
(
SELECT (col1 + col2) AS SumOfCols, table.* FROM table
)
WHERE SumOfCols > 200
Or another similar approach is to join an ad-hoc table
SELECT table.* FROM table
INNER JOIN
(
SELECT ID, (col1 + col2) AS SumOfCols FROM table
) AS TableSums ON TableSums.ID = table. ID
WHERE TableSums.SumOfCols > 200
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 4 years ago.
this one is driving me to drink so I would love some help.
I've got a table with:
act_Address, act_OrderID, act_Date
I'm trying to get the first act_Date for each address we shipped to.
Here's what I've tried but it's been running now for well over an hour so I'm thinking this isn't going to work...
SELECT c.act_Address,
(SELECT o.act_OrderID
FROM tbl_Activity o
WHERE c.act_Address = o.act_Address
ORDER BY o.act_Date
LIMIT 1) AS order_id,
(SELECT d.act_Date
FROM tbl_Activity d
WHERE c.act_Address = d.act_Address
ORDER BY d.act_Date
LIMIT 1) as order_date
FROM tbl_Activity c
I've got to be doing something very wrong, doesn't seem like getting the first date for an address would be that hard, but I'm not that smart.
Your query uses two correlated subqueries to get act_Date and act_OrderID values. Each subquery is executed once for every record of tbl_Activity.
You can use:
SELECT act_Address, MIN(act_Date) AS fist_Date
FROM tbl_Activity
GROUP BY act_Address
to get the first date per address. Then you can use the above query as a derived table and join back to the original table to get the rest of the fields:
SELECT t1.act_Address, t1.act_OrderID, t1.act_date
FROM tbl_Activity AS t1
JOIN (
SELECT act_Address, MIN(act_Date) AS fist_Date
FROM tbl_Activity
GROUP BY act_Address
) AS t2 ON t1.act_Address = t2.act_Address AND t1.act_Date = t2.first_Date
I also propose placing a composite index on (act_Address, act_Date).
You can do this by GROUP BY in a subselect:
SELECT a.act_Address, a.act_OrderID, a.act_Date
FROM (
SELECT a2.act_Address addr, MIN(a2.act_Date) mindate FROM tbl_Activity a2
GROUP BY a2.act_Address
) g, tbl_Activity a
WHERE a.act_Address = g.addr AND a.act_Date = g.mindate;
I have one SQL query with INNER JOINS. I need to get all offers from table offers.
Table offers is empty now. But the following query returns one row with NULL field.
Why is it returned? How to fix that? I need to return 0 rows if table is empty.
Query:
select *, SUM(offers.price * announcement_product.amount) AS total, announcements.user_id AS creator_ann, announcements.id AS ann_id,
announcements.delivery AS deliveryAnn, announcements.payment AS
paymentAnn, SUM(announcement_product.amount) AS amount,
announcement_product.name as name_product
from `offers`
inner join `announcements` on `announcements`.`id` = `offers`.`announcement_id`
inner join `announcement_product` on `offers`.`announcement_product_id` = `announcement_product`.`id`
inner join `countries` on `countries`.`id` = `announcements`.`country`
where `offers`.`user_id` = 1 and `offers`.`status` = 1 and `offers`.`deleted_at` is null
You're using the aggregate function SUM(), but you don't have any GROUP BY clause.
When you do that you are instructing MySQL to add up all the row values in the column you mention in SUM(). It will do that even if there are no rows to add up.
For best results you should study up on the GROUP BY function and how to use it with SUM(). It's hard to guess what you want from your query.
I'm not sure, but I don't think
select *, ..
when there's multiple tables in the query is valid.
Try
select offers.*,..
This how Your select structure should be :
Select
Id,
Sku,
Sum(Onhand),
Sum(price)
From mytable
Where mytable Onhand > 0
Group by
Id,Sku
If you are going to use aggregate function such as Max,Sum,Min,....
you need to use group by for other table fields that your using in the select part.
I have a complicated query that boils down to this:
UPDATE usertable
SET userstatus =
(SELECT numposts*5 as status FROM
(SELECT count(*) as numposts FROM poststable WHERE poststable.userid = usertable.userid) as table2
)
WHERE usertable.userid > 0
It's a query that updates every user record and sets the user's status to some calculated value based on the number of rows in a child table.
The problem is that usertable.userid does not make it down to the second level subquery.
The query works when presented like this, with only one level down:
UPDATE usertable
SET userstatus =
(SELECT count(*) as numposts FROM poststable WHERE poststable.userid = usertable.userid)
WHERE usertable.userid > 0
The problem is that the calculation query in the real situation is very complicated.
The question is: is there a way I can get a 2nd level subquery to recognize a value from the top level query? In this example, is there a way to get usertable.userid recognized 2 levels down?
Thanks!
-Nico
Instead of doing a correlated subquery row by row, I would generate a derived table as a one-time subquery for all userid's, then join that to the table you want to update. MySQL supports multi-table update syntax:
UPDATE usertable AS u
LEFT OUTER JOIN (
SELECT userid, COUNT(*) AS numposts
FROM poststable
GROUP BY userid
ORDER BY NULL
) AS t USING (userid)
SET u.userstatus = 5 * COALESCE(t.numposts, 0)
WHERE u.userid > 0
I know you said your real query is more complex, but the same principle may solve it.
I'm completely new to SQL. I have a table with multiple rows for the same ID (Alpha_code).
I want to query this table to provide only 1 row for each Alpha_code.
I need it to first select the newest row if rows have been added on different (contact_date).
I have found the statement below does this no problem.
SELECT m.*
FROM (SELECT Alpha_code, max(Contact_date) AS MaxDate FROM contactsNTH GROUP BY Alpha_code)
AS mm INNER JOIN contactsNTH AS m ON (mm.MaxDate = m.Contact_Date) AND (mm.Alpha_code = m.Alpha_code)
However there are other multiple rows that were entered on the same (contact_date). I'm not sure how to add more code to then only show 1 of the multiple rows remaining.
I've tried the Select Distinct statement but it didn't work.
Any assistance would be appreciated. If I can answer this myself in the meantime I'll post the answer
thanks, shaun
This thread is close to what I'm asking but I find the answer confusing.
How To Select Distinct Row Based On Multiple Fields 4
Assuming your schema as given here :- http://sqlfiddle.com/#!2/4e6ff/2
You can use the following query
SELECT c1.Alpha_code,c1.Contct_date from contactsNTH as c1
LEFT JOIN
contactsNTH as c2
ON
c1.Alpha_code = c2.Alpha_code
AND
(c1.Contct_date > c2.Contct_date) group by c1.Alpha_code
This query doesn't works in Standard SQL because it says, "a query that includes a GROUP BY clause cannot refer to non aggregated columns in the select list that are not named in the GROUP BY clause"
Try using this query
SELECT c1.Alpha_code,MAX(c1.Contct_date) as contact_date from contactsNTH as c1
LEFT JOIN
contactsNTH as c2
ON
c1.Alpha_code = c2.Alpha_code
AND
(c1.Contct_date > c2.Contct_date) group by c1.Alpha_code
both will provide the same result http://sqlfiddle.com/#!2/4e6ff/4
You can do it easily by using inner query. Sql Fiddle Demo
SELECT Alpha_code, Contact_date AS MaxDate FROM contactsNTH AS t WHERE
Contact_date = (SELECT MAX(Contact_date) FROM contactsNTH
WHERE Alpha_code=t.Alpha_code)
I wrote a query using simply group by and having maximum date but it failed Then I wrote these words in google select group by having max and found the result. this is an example guide for you to get such problems fixed quickly
This question already has answers here:
Mysql: Select rows from a table that are not in another
(10 answers)
Closed 9 years ago.
I have two tables on two different databases.
I wish to select the values from the field MLS_LISTING_ID from the table mlsdata if they do not exist in the table ft_form_8.
There are a total of 5 records in the mlsdata table.
There are 2 matching records in the ft_form_8 table.
Running this query, I receive all 5 records from mlsdata instead of 3.
Changing NOT IN to IN, I get the 2 matching records that are in both tables.
Any ideas?
SELECT DISTINCT
flrhost_mls.mlsdata.MLS_LISTING_ID
FROM
flrhost_mls.mlsdata
INNER JOIN
flrhost_forms.ft_form_8 ON flrhost_mls.mlsdata.MLS_AGENT_ID = flrhost_forms.ft_form_8.nar_id
WHERE
flrhost_mls.mlsdata.MLS_LISTING_ID NOT IN ((SELECT flrhost_forms.ft_form_8.mls_id))
AND flrhost_mls.mlsdata.MLS_AGENT_ID = '260014126'
AND flrhost_forms.ft_form_8.transaction_type = 'listing'
SELECT DISTINCT
flrhost_mls.mlsdata.MLS_LISTING_ID
FROM
flrhost_mls.mlsdata
where
flrhost_mls.mlsdata.MLS_LISTING_ID NOT IN (SELECT
flrhost_forms.ft_form_8.mls_id
FROM
flrhost_forms.ft_form_8)
Your problem:
I wish to select the values from the field MLS_LISTING_ID from the table mlsdata if they do not exist in the table ft_form_8.
is THE classic OUTER JOIN problem
SELECT DISTINCT
flrhost_mls.mlsdata.MLS_LISTING_ID, flrhost_forms.ft_form_8.MLS_LISTING_ID
FROM
flrhost_mls.mlsdata
LEFT OUTER JOIN
flrhost_forms.ft_form_8
ON
flrhost_mls.mlsdata.MLS_LISTING_ID=flrhost_forms.ft_form_8.MLS_LISTING_ID
WHERE
flrhost_forms.ft_form_8.MLS_LISTING_ID IS NULL;
Which is much simpler than using some complicated subquery nest.
SELECT DISTINCT
flrhost_mls.mlsdata.MLS_LISTING_ID
FROM flrhost_mls.mlsdata
INNER JOIN flrhost_forms.ft_form_8
ON flrhost_mls.mlsdata.MLS_AGENT_ID = flrhost_forms.ft_form_8.nar_id
WHERE flrhost_mls.mlsdata.MLS_AGENT_ID = '260014126'
AND flrhost_forms.ft_form_8.transaction_type = 'listing'
AND flrhost_mls.mlsdata.MLS_LISTING_ID NOT IN (SELECT b.mls_id FROM flrhost_forms.ft_form_8 b)