How can i apply Multiple In clause in a single MySQL query - mysql

I have list of ids and corresponding creation dates
for Exmple :
1 2014-05-01
2 2014-07-01
3 2014-08-01
Need suggestion regarding writing a MySQL select statement which gives id details after corresponding creation date.
select id,count(*) from id_details where id IN(1,2,3) where resolved_at >(2014-05-01,2014-07-01,2014-08-01) group by id

The date condition for resolved_at column is not correct. Again, if you have two WHERE clause in your query, that as well not correct. You can's specify > condition in IN clause like you are trying. Your query should look like
select id,count(*)
from id_details
where id IN (1,2,3)
and (resolved_at >= '2014-05-01'
and resolved_at <= '2014-08-01')
group by id
I think you are just trying to use IN operator for resolved_at column like
select id,
count(*)
from id_details
where id IN (1,2,3)
and resolved_at IN ('2014-05-01','2014-07-01','2014-08-01')
group by id

Related

Operating on a table received from query within the same query SQL

So I have this table resulting from a query. Is there a way to combine all of the purchases for the same username and order them in desc order to find the most loyal customers within the same query? maybe saving it to a variable and then doing something?
username
number_of_purchase
Bob
1
Marry
3
Mike
2
Bob
2
Marry
3
Mike
4
Ariana
3
Sally
1
This should do the work!
You can read about CTE here - https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-ver15.
with users as (
**YOUR QUERY HERE **
)
Select username, sum(number_of_purchase) from users
group by username
Example from Microsoft site:
-- Define the CTE expression name and column list.
WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)
AS
-- Define the CTE query.
(
SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
FROM Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
FROM Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
Depending on your MySql version. If prior to v8 then you can use a derived table, basically, wrapping your existing query as follows
Select username, sum(number_of_purchase) purchases
from (
> Existing query <
)t
group by username
order by purchases desc
If you are using MySql 8 you could use window functions in your existing query to materialize the sum of purchases per user which you can then order by.
Without your existing query all I can do us suggest you incorporate the following, using the applicable column names
sum(purchase count column) over(partition by username) TotalPurchases

Multiple selection of ID's (MySQL)

Is it possible for me to select multiple ID? For example: I am executing this command
SELECT
sum(amount)
FROM
bets_logs
where
fight_id=1
Assumming that I have id that is up to 500 Of course I'll do it one by one... My target is, is there a query that I can select id from 1 to 10. So that I won't do it 1 by one.
My target is, is there a query that I can select id from 1 to 10. So
that I won't do it 1 by one.
You can write the query like following.
SELECT fight_id, sum(amount)
FROM
bets_logs
where
fight_id >=1 and fight_id<=10
group by fight_id

Questions for having clause and where clause

I have a very simple question. I am using Mysql bench, and i had a data which likes below:
dateordered_new orderstatus orders
2016-06-23 23:19:23 returned 8
2016-06-01 23:19:23 completed 12
2016-06-22 23:19:23 returned 9
2016-06-04 23:19:23 completed 27
...etc...
The question is simple, I want to show the amount of orders which has been returned in each month.
And here is my query:
select month(dateorderednew) as Month, sum(orders) as return_orders
from table_a
group by month
having orderstatus='returned;
Considering the difference between where clause and having clause, my syntax should be worked. However, the system told me that "Error Code: 1054. Unknown column 'orderstatus' in 'having clause'" And it was wired.
However, when I modified my query like this:
select month(dateorderednew) as Month, sum(orders) as return_orders
from table_a
where orderstatus='returned
group by month;
And it worked.
So, it was really confusing. I think having clause should follow by the group by statement. But I cannot answer why this case happened?
Do you guys have any idea for this?
In your situation, you should use a where clause:
select month(dateorderednew) as Month, sum(orders) as return_orders
from table_a
where orderstatus='returned'
group by month
Because you want to filter rows from the table before they are aggregated.
You only use having clause when you want to filter on an aggregate value, eg
select month(dateorderednew) as Month, sum(orders) as return_orders
from table_a
group by month
having sum(orders) > 10
However, mysql is flexible and allows you to use a having on a non-aggregate value.
HAVING is used to filter out results of aggregations, like MIN/MAX/AVERAGE, while WHERE is used to filter on non-aggregate columns.
For example, you can do this:
select month(dateorderednew) as Month, sum(orders) as return_orders
from table_a
WHERE orderstatus='returned'
group by month
having sum(orders) < 100
You have to replace the condition mentioned the having clause with where clause. Because having clause filter the data on aggregated group and where clause filter the data on whole record set.
Try the below SQL:
Select month(dateorderednew) as Month, sum(orders) as return_orders
from table_a
where orderstatus='returned
group by month;
A 'WHERE' clause filters on individual row values...
where colname > 0
or
where colname = 'sometext'
A 'HAVING clause filters on a group or aggregate of a row and comes after the 'group by' statement if there is one...
group by colname
having count(*) > 0
or
group by colname
having sum(colname) < 1

MySQL count select query with condition

I have a mysql table called Game which has two columns, Name and Score. I want to select only the Names whose scores have been atleast 100 and atleast twice. In the below example Ron and Mary will get selected. I am not sure how to write the select statement for this.
Game table
Use GROUP BY with a HAVING clause:
SELECT Name
FROM mytable
GROUP BY Name
HAVING COUNT(CASE WHEN Score >= 100 THEN 1 END) >= 2
HAVING clause checks for Name groups, having at least two records with Score >= 100.

MySQL wrong results with GROUP BY and ORDER BY

I have a table user_comission_configuration_history and I need to select the last Comissions configuration from a user_id.
Tuples:
I'm trying with many queries, but, the results are wrong. My last SQL:
SELECT *
FROM(
SELECT * FROM user_comission_configuration_history
ORDER BY on_date DESC
) AS ordered_history
WHERE user_id = 408002
GROUP BY comission_id
The result of above query is:
But, the correct result is:
id user_id comission_id value type on_date
24 408002 12 0,01 PERCENTUAL 2014-07-23 10:45:42
23 408002 4 0,03 CURRENCY 2014-07-23 10:45:41
21 408002 6 0,015 PERCENTUAL 2014-07-23 10:45:18
What is wrong in my SQL?
This is your query:
SELECT *
FROM (SELECT *
FROM user_comission_configuration_history
ORDER BY on_date DESC
) AS ordered_history
WHERE user_id = 408002
GROUP BY comission_id;
One major problem with your query is that it uses a MySQL extension to group by that MySQL explicitly warns against. The extension is the use of other columns in the in theselect that are not in the group by or in aggregation functions. The warning (here) is:
MySQL extends the use of GROUP BY so that the select list can refer to
nonaggregated columns not named in the GROUP BY clause. This means
that the preceding query is legal in MySQL. You can use this feature
to get better performance by avoiding unnecessary column sorting and
grouping. However, this is useful primarily when all values in each
nonaggregated column not named in the GROUP BY are the same for each
group. The server is free to choose any value from each group, so
unless they are the same, the values chosen are indeterminate.
So, the values returned in the columns are indeterminate.
Here is a pretty efficient way to get what you want (with "comission" spelled correctly in English):
SELECT *
FROM user_commission_configuration_history cch
WHERE NOT EXISTS (select 1
from user_commission_configuration_history cch2
where cch2.user_id = cch.user_id and
cch2.commission_id = cch.commission_id and
cch2.on_date > cch.on_date
) AND
cch.user_id = 408002;
Here's one way to do what your trying. It gets the max date for each user_ID and commissionID and then joins this back to the base table to limit the results to just the max date for each commissionID.
SELECT *
FROM user_comission_configuration_history A
INNER JOIN (
SELECT User_ID, Comission_Id, max(on_Date) mOn_Date
FROM user_comission_configuration_history
Group by User-Id, Comission_Id
) B
on B.User_ID = A.User_Id
and B.Comission_Id = A.Comission_ID
and B.mOnDate=A.on_date
WHERE user_id = 408002
ORDER BY on_Date desc;