How to SUM the value of the specific rows only? - mysql

Would like to get the SUM of two specific rows in a table in mysql.
create table people(
id int,
firstname varchar(50),
money int
);
id firstname money
1 david 100
2 jack 200
3 bob 300
Say I want the SUM of row 1(david) and row 3(bob) only. The total should be 400.
I used this query:
SELECT SUM(money)
FROM people
WHERE id = 1 AND 3;
but it turned out to be 100, and not 400 as I expected.

Here's your query. you can use case and in to get this.
select sum(case when id = 1 or id = 3 then money else 0 end) from people
or
select sum(money) from people where id in (1, 3)

The answer given by #Metal is the way to go:
SELECT SUM(money)
FROM people
WHERE id IN (1, 3);
I am mainly posting this to explain why your current query is giving you a sum of 100. The reason is that the WHERE condition:
WHERE id = 1 AND 3
will only ever be true when id = 1, i.e. for David's record. The value 3 is evaluating to true always. So, you simply sum David's record and get a total of 100.

You have to use OR to select both the rows.
SELECT SUM(money)
FROM people
WHERE id = 1 OR id = 3;

you can find like this:
SELECT SUM(money)
FROM people
WHERE id =1
union
SELECT SUM(money)
FROM people
WHERE id =3

You Have to use this way.
SELECT SUM(money) as Money
FROM people
WHERE id Between 1 and 3

Related

Mysql data extraction

I have a table called deals, it has records like this for example
id deal_ref objectname status
1 1234 tom correct
2 1234 tom correct
3 1234 jerry wrong
4 1234 tom correct
I need to identify all latest deals where the status is "correct for example, but the last entry(row 4) must meet the following criteria, where the Max ID is equal to the deal_ref and the status is correct
I tried this
select distinct deal_ref, deal_status
from dealv1 d
where d.deal_ref = max(id)
and d.deal_status = 'Prospect'
and date_created between '2022-11-02 00:00:00' and '2022-11-04 00:00:00'
You use other names in your SQL than in the table (deal_status, date_created).
Nevertheless try do it the following:
SELECT *
FROM dealv1 d
WHERE status = 'correct'
ORDER BY ID DESC
LIMIT 1
i donĀ“t get exactly what you are trying to do with the maxID. You just want the one row where deal_ref=max(id) and status is correct?
Then add
AND deal_ref = (SELECT MAX(id) from dealv1)
after 'correct' from the above statement.
Guys the query below worked, but its displaying multiple deal_refs that has the same deal_ref, for example 2 rows below each other with deal_ref 1234 twice.
SELECT *
FROM dealv1 d
WHERE status = 'correct'
ORDER BY ID DESC
LIMIT 1

Show DISTINCT user_id ONLY if record column matches threeconditions

Fairly new to MySQL and I'm struggling with a query for table of data I'm trying to filter through. What I'd like to be able to do is identify the user_id's only where a set of conditions is met on the record column.
Example
Return a SINGLE user_id of each of the users that hold ALL of the records 1, 2 & 3.
user_id record
---------------------
1000 1
1001 1
1002 1
1003 1
1004 1
1000 2
1000 3
1002 2
1002 3
The ideal output in this example would be...
user_id
-------
1000
1002
I've tried quite a few variants using HAVING, COUNT and IN but I never seem to get the correct output and I think I'm starting to confuse myself. Anyone that could help would be greatly appreciated.
Do aggregation :
select user_id
from t
where record in (1, 2, 3)
group by user_id
having count(*) = 3; -- Use distinct inside function in case of duplicate records
If you don't know what the records are, then you can do :
select user_id
from t
group by user_id
having count(*) = (select count(distinct record) from t);
You can use HAVING Clause with a query to distinctly count all user_id values :
SELECT user_id
FROM t
GROUP BY user_id
HAVING COUNT(*) = (SELECT COUNT(distinct record) FROM t );

Guidance required for sql query

I have a database with one table as shown below. Here I'm trying to write a query to display the names of medication manufactured by the company that manufactures the most number of medications.
By looking at the table we could say the medication names which belongs to the company id 1 and 2 - because those company manufactures the most medication according to this table, but I'm not sure how to write a query for selecting the same i said before.
ID | COMPANY_ID | MEDICATION_NAME
1 1 ASPIRIN
2 1 GLUCERNA
3 2 SIBUTRAMINE
4 1 IBUPROFEN
5 2 VENOFER
6 2 AVONEN
7 4 ACETAMINOPHEN
8 3 ACETAMINO
9 3 GLIPIZIDE
Please share your suggestions. Thanks!
Several ways to do this. Here's one which first uses a subquery to get the maximum count, then another subquery to get the companies with that count, and finally the outer query to return the results:
select *
from yourtable
where companyid in (
select companyid
from yourtable
group by companyid
having count(1) = (
select count(1) cnt
from yourtable
group by companyid
order by 1 desc
limit 1
)
)
SQL Fiddle Demo
This Query might work. I have not tested but the logic is correct
SELECT MEDICATION_NAME
FROM TABLE where
COMPANY_ID=(SELECT
MAX(counted)
FROM ( SELECT COUNT(*) AS counted FROM TABLE ) AS counts);

MYSQL: Count the number of times a specific integer appears in a column then making a count of how many of these are present?

I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;

Mysql Query problem?

ID NAME AMT
1 Name1 1000
2 Name2 500
3 Name3 3000
4 Name1 5000
5 Name2 2000
6 Name1 3000
consider above table as sample.
am having a problem in my sql query, Am using like this.
Select name,amt from sample where amt between 1000 and 5000
it returns all the values in the table between 1000 and 5000, instead I want to get maximum amount record for each name
i.e.,
3 name3 3000
4 name1 5000
5 name2 2000
select name, max(amt) from sample group by name
You'll have problems getting the id, though, as there may be more than one.
you should group by NAME:
SELECT `name`,MAX(amt) from sample GROUP BY `name` where amt between 1000 and 5000
If you only need ONE of the ids that contains the MAX(amt), then this will do the trick:
SELECT id, name, MAX(amt)
FROM sample
WHERE amt BETWEEN 1000 AND 5000
GROUP BY name;
If you need all the ids, then it gets more complicated. Two queries are required, plus a temporary table:
CREATE TEMPORARY TABLE maxamts
SELECT name AS maxname, MAX(amt) AS maxamt
FROM sample
WHERE amt BETWEEN 1000 AND 5000
GROUP BY maxname;
SELECT GROUP_CONCAT(id), maxname AS name, maxamt AS amt
FROM maxamts
LEFT JOIN sample ON ((maxname = sample.name) AND (maxamt = amt))
GROUP BY maxname;
In short: Create a temporary table from the query that finds each name/max(amt) pair, then use that temporary table to join back on the original table and pull out the IDs matching the name/amount combinations.
Just remember that group_concat is by default limited to 1,024 characters, (show variables like '%group_concat%' to see the max length on your server), so if you've got a large dataset, increase that limit, or remove the group_concat and group by from the second query and parse the information in your application.
select id, name, amt from sample where amt = (select max(amt) from sample)
that should return all records that have the max amt from the sample table
edit:
select id, name, amt
from sample
where amt = (
select max(amt)
from sample
where amt between 1000 and 5000)
this query will return all records that have an amount equal to the max amount between 1000 and 5000