mysql - count two columns in same query with specific variables - mysql

I have a mysql database structured like:
table transactions:
id paymentid clientid status
------------------------------
1 001 12345 0
2 002 11223 1
3 003 12345 4
4 004 12345 4
What is the most efficient way to run a query in mysql to give me the number of times a certain clientid had payments with the status of 4?
like:
select clientid, count(*)
where status = 4
but only return the count of a specific clientid.

You will just add a WHERE filter for clientId if you want to filter by a specific client. If not, then you can remove the and clientid = 12345:
select clientid, count(*)
from transactions
where status = 4
and clientid = 12345
GROUP BY clientId;
See SQL Fiddle with Demo

Related

How to calculate the values of one column taking into account the values from another table

I need a query that allows me to calculate the values of a column taking into account the values in another table, more precisely, I need to calculate if the users are still active(if a user has all expired roles, then he is inactive), taking into account the departure_date the second table
user_role table
id_user
id_role
departure_date
1
1
2022-05-05
1
2
2022-06-18
2
1
2022-04-12
user table
id_user
name
1
George
2
John
3
Alex
I want to return this table, where 1 is active and 0 is inactive:
user table
id_user
name
status
1
George
1
2
John
0
3
Alex
0
At this point, I've made a query, which returns all my inactive users and active users that have one or more assigned roles. I want to get all users including those who do not have an assigned role (like Alex, in my example)
SELECT user_management.user.*,
if(count(CASE when current_date() > departure_date_organization
then 1 END) = count(*),0,1) as status
FROM user_management.user,
user_organization_role
WHERE user.id_user = user_management.user_organization_role.id_user
GROUP BY user.id_user;
Using my query, I got this result, that not contain Alex:
id_user
name
status
1
George
1
2
John
0
Thank you in advance
You don't need joins and aggregation.
Use EXISTS:
SELECT u.*,
EXISTS (
SELECT 1
FROM user_management.user_organization_role r
WHERE r.id_user = u.id_user AND r.departure_date_organization > CURRENT_DATE
) status
FROM user_management.user u;

How to take n rows of data from a table referring data from a column in the same table? (SQL)

I have a table with many data and a sample is as follows
product_table
data
data2
data3
data4
data5
data6
data7
product id
001
002
003
004
005
006
007
product name
a
b
c
d
e
f
g
status
available
not available
damaged
available
available
not available
damaged
order id
1
2
3
4
5
6
7
9
I need to extract 2 order ids from each of the statuses from one query.
The out put I want is as follows
status
order id
available
1
available
4
not available
2
not available
6
damaged
3
damaged
7
I tried using "Limit" but it only brings out the first few data which is in the table.
Query I used
Select order id , status
from table product_table
limit 2
;
output I got was
status
order id
available
1
not available
2
You can partition the database by status and use row_number to extract two records.
select status, order_id from (
select status, order_id, row_number() over (partition by status) as rn from product_table)
where rn <=2;

How to loop through a db table in mysql and show a column once even thogh it appears on different rows

database name = db_structure
id | param_id | occupation_id | amount | active
1. 1 1 20000 1
2. 2 1 20000 1
3. 3 1 20000 1
4. 1 1 20000 1
5. 2 2 20000 1
6. 3 2 20000 1
7. 4 2 20000 1
what query will I write to loop through the table to fetch the records on each row but only show occupation_id once on a table even if it appears more than once?
SELECT tbl_salarystructure.occupationid, SUM(amount), tbl_occupation.*
FROM `tbl_salarystructure`
INNER JOIN tbl_occupation ON
tbl_salarystructure.occupationid = tbl_occupation.id
WHERE tbl_salarystructure.active = 1 ORDER BY occupationid
the result of the query is this when I use php to fetch the rows on the table
Report table
Department | Amount
1 20000
1 20000
1 20000
1 20000
2 20000
2 20000
2 20000
I want the department to show once with the total sum of the amount column using php where their occupation id is thesame
Thanks guys, I got the solution to my problem... I changed the ORDER BY in my sql to GROUP BY the column I want and it came out perfect
I think you just want GROUP BY:
SELECT o.*, SUM(amount)
FROM tbl_occupation o JOIN
tbl_salarystructure ss
ON ss.occupationid = o.id
WHERE ss.active = 1
GROUP BY o.occupationid;
In general, you don't want to use SELECT * with GROUP BY. The one exception is when the primary key for the table (or any unique key actually) is one of the GROUP BY keys.

Remove Duplicate Amounts

I have two tables with invoice detail and serial numbers.
Both tables have invoice number, but when I attempt to join using left or inner join the amount duplicates in each lines. I want to list down all the serials associated with my invoice # in table 1 without duplicating the amount each lines. I'm currently using MS Access.
Thanks for the help.
Table 1
Invoice# amount
001 500
Table 2
Invoice# serial
001 123
001 456
001 789
001 1011
001 1213
Desired Output:
Invoice# amount serial
001 500 123
456
789
1011
1213
My Current Query Output:
Query:
Select invoice.invoice,invoice.amount,tblmachine.serial
From tblmachine inner join invoice on tblmachine.invoice =invoice.invoice;
Use UNION ALL for 2 queries.
The 1st will return the 1st row that you need and the 2nd all the rest:
select i.[invoice#], i.amount, min(t.serial) as serial
from tblmachine as t inner join invoice as i
on t.[invoice#] = i.[invoice#]
where i.[invoice#] = '001'
group by i.[invoice#], i.amount
union all
select null, null, t.serial
from tblmachine as t
where t.[invoice#] = '001'
and t.serial > (select min(serial) from tblmachine where [invoice#] = t.[invoice#])
Results:
invoice# amount serial
001 500 123
456
789
1011
1213
Can't do that desired output in query for multiple invoices.
Build a report and set textbox HideDuplicates property to Yes.

what is mysql query for below four tables

I have four tables.
User:
user_id user_name client_id
1 abhi 1
2 ravi 2
Client:
client_id client_name products
1 tom cake, patties
2 pet cookie, cake
Products:
product_id product_name
1 cake
2 cookie
3 patties
Report:
report_id product_id
1 1
2 3
3 2
4 1
5 3
If Ravi login then he only able see report like below:
report_id product_id
1 1
3 2
4 1
I need only MySQL Query
In your "Client" table instead of saving multiple values of "product" split it and put only "product id" rather than actual product value that's why you have table "product". Your table "client" is not in First normal form. So if possible, change the structure of table
I agree with Simemon's answer, you need to normalize your DB tables.
Then also if you want to go with this table structure only, you can try this.
select * from reports where product_id in (select product_id from products where product_name in (select products from clients where client_id = (select client_id from users where username = 'ravi' limit 1)))
I have not tested this. let me know if it gives any error, I will check it.