I have a Ms Access table with four columns; PledgeID, Ref, Paid, and Balance. One PledgeID may have several records. I need a query that will help me get the sum of the Balance for ONLY the last record of each PledgeID. The last PledgeID will be the one with the highest ref. I have attached a photo of the table for easy reference.
Click To View Image
You could use this SQL:
SELECT T2.PledgeID
, T2.REF
, T2.Balance
FROM (
SELECT PledgeID
, MAX(Ref) AS REF_RETURN
FROM MyTable
GROUP BY PledgeID
) T1 INNER JOIN MyTable T2 ON T1.REF_RETURN = T2.REF
It would probably be easier to limit the Ref field to just numeric - 1, 2 rather than PID/2018/00007-1 & PID/2018/00007-2.
You could use a subquery to find the latest balance per PledgeID and sum these balances:
SELECT Sum(Balance)
FROM Balances
WHERE Ref = (SELECT Max(Ref) FROM Balances AS b WHERE PledgeID = Balances.PledgeID);
Looking at your sample table, the result seems to be 0.00.
Related
Actually, i did counted distinct empid rows according to dates. But the problem is i get only one empid record of that specific dates.Please let me know how to get all empid records. Here is my sql query.
$sql = "
SELECT COUNT(DISTINCT subcount.empid) AS CountOf
, subcount.name
, subcount.date
, subcount.empid
, calendar.cdate
FROM subcount
, calendar
WHERE subcount.date = calendar.cdate
GROUP
BY subcount.date
";
Here is sql database.
For example, When you look at 2020-11-10 there are two empid with 10 and 7.
When i tried to get both records i get only empid 10 record or 7 record, though i need both record counts:
Here is the output:
Please help me on this.
I think what you are asking is to get list of employees with count of their submissions on a given date, this could show do it:
SELECT cnt.empid AS EmpId
, sc.Name
, cnt.`date` AS Timestamp
, cnt.CountOf AS SubmissionCount
FROM subcount AS sc
INNER JOIN
(
SELECT subcount.empid
subcount.`date`,
count(*) AS CountOf
FROM subcount
INNER JOIN calendar
ON subcount.`date` = calendar.cdate
GROUP BY
subcount.`date`, subcount.empid
) AS cnt
ON sc.empid == cnt.empid
It uses nested SELECT with GROUP BY to calculate count per employee (empid) and date (not only employee). Outer SELECT join nested SELECT to get subcount.Name piece of data which isn't retrieved in nested SELECT so it needs to be retrieved using outer SELECT.
GROUP BY ___ means result rows per ___. If you group by employee ID, you get one row per employee ID. If you want one row per employee ID and date, group by employee ID and date.
SELECT any_value(s.name), s.`date`, s.empid, c.cdate, count(*)
FROM subcount s
JOIN calendar c on c.cdate = s.`date`
GROUP BY s.`date`, s.empid
ORDER BY s.`date`, s.empid;
I expect a calendar table to have one row per date, so there is exactly one cdate for a result row. The name, however, can be different from row to row, so we must tell the DBMS, which to pick. With ANY_VALUE I tell it that I don't care which.
enter image description hereI have an employee table. In that table I have n number of loan.. Now I want to select all the employees with his last taken loan value and date.. can anyone help me out in this.
There's what I've done so far:
SELECT employee_id,employee_name,department_name,
designation_name,PF_type,PF_number,Opening_balance,
MAX(Loan_id),MAX(loan_date), MAX(loan_amount)
FROM single_roww1
GROUP BY employee_id
I tried group by, limit and order by for this but of no use... I also tried to take the last occurence of the employee_id that also didn't work..
Update: to get the full row of max id:
SELECT *
FROM loan
INNER JOIN
(SELECT max(id) max_id
FROM loan
GROUP BY employee_id) x ON x.max_id = loan.id
group by employee_id;
I have to mention that your DB table design without primary key isn't well designed: https://i.stack.imgur.com/DTYO8.png
If only the row containing the last loan_id is required than a correlated sub query is what you want
SELECT *
FROM single_roww1 SR
WHERE LOAN_ID = (SELECT MAX(LOAN_ID) FROM single_roww1 WHERE EMPLOYEE_ID = SR.EMPLOYEE_ID);
With a correlated subquery in the WHERE clause:
SELECT s.employee_id, s.employee_name, s.department_name,
s.designation_name, s.PF_type, s.PF_number, s.Opening_balance,
s.Loan_id, s.loan_date, s.loan_amount
FROM single_roww1 s
WHERE s.Loan_id = (SELECT MAX(Loan_id) FROM single_roww1 WHERE employee_id = s.employee_id)
last taken loan value and date
You seem to want:
SELECT sr.*
FROM single_roww1 sr
WHERE sr.loan_date = (SELECT MAX(sr2.load_date)
FROM single_roww1 sr2
WHERE sr2.employee_id = sr.employee_id
);
This assumes shat loan_date has a proper date/time type in the database (despite what the data in the image looks like).
For performance, you want an index on (employee_id, load_date).
Note that if an employee has multiple loans on the same date, this will retrieve the last one.
I am trying to create an SQL Query to select rows from a database, ordered by a numerical field, however there are repeated entries in the table.
The table consists of the following columns.
UID - Numerical Unique ID
ACCOUNT_NAME - Account Name, unchanged
NICK_NAME - Can be changed by the user at any time
POINTS - Records points held by the user's account
The goal of the query is to display the Account_Name ordered by Points. However, Account_Name is not unique and can appear multiple times in the table.
To deal with this I would like to display only the latest row for each Account_Name.
This meaning that in the results from the select each Account_Name should only appear once. I am trying to have the selection be decided by the UID, meaning that I want only the row with the greatest UID where each account_name appears to be displayed.
I have tried the following without desired results. (The name of the table is ACCOUNT)
SELECT DISTINCT A.account_name , A.uid, A.points
FROM account A, account B
where A.account_name = B.account_name
and A.points > 0
and A.uid >= B.uid
order by A.points DESC;
This doesn't give me the desired results, specifically, there is an account in the database where an outdated row exists with a high value in the Points column. This record appears as the first result in the select, even though it is outdated.
How would you recommend adjusting this Query to select the desired information?
I hope this is enough information to work off of (first time posting a question) Thank you for you help :)
EDIT: Adding in examples with data.
Sample Table Data:
Sample Table Data
Current Results:
Current Results
Desired Results:
Desired Results
Consider joining on an aggregate query calculating MAX(UID)
SELECT a.account_name, a.uid, a.points
FROM account a
INNER JOIN
(
SELECT account_name, MAX(uid) AS max_uid
FROM account
GROUP BY account_name
) agg
ON a.account_name = agg.account_name
AND a.uid = agg.max_uid)
WHERE a.points > 0
ORDER by a.points DESC;
Alternatively, with MySQL 8.0, consider a window function:
SELECT a.account_name, a.uid, a.points
FROM account a
WHERE a.points > 0
AND a.uid = MAX(a.uid) OVER (PARTITION BY a.account_name)
ORDER by a.points DESC;
I am currently doing a task where i need to calculate the sum of total amount of product each employee has. Then, I need to set a fix amount(5000) and display those employees who have total amount less than the fix amount(5000). Can anyone provide examples of queries for this particular problem?
Taking the above image as dummy table, you can use this query for getting your appropriate result
SELECT productid, price + productid as Amount
FROM Products
where Amount < 15
order by productid
Execution of this query will give the result as shown in below image:
Table T2 will return list of employees less than 5000,then join it with your employee master table T2. Change the table names as needed.
SELECT * FROM EMPLOYEE T1 JOIN
(SELECT EmployeeID,SUM(AMOUNT) AMOUNT FROM PRODUCT
GROUP BY EmployeeID
HAVING SUM(AMOUNT)<5000) T2 ON T1.EmployeeID=T2.EmployeeID
If you are using SQL Server 2012 above:
SELECT *
FROM ( SELECT * ,
SUM(AMOUNT) OVER ( PARTITION BY EmployeeID ) AS EmpTotal
FROM PRODUCT
) t
WHERE t.EmpTotal > 5000;
What I have:
I have two table , first is user_faktorha save invoices data and second is u_payment save payment data .
What I want:
I want to group all data from this two table and have a result as one table with sum both table.
My two table with sample query's is on sqlfiddle : http://sqlfiddle.com/#!2/b9f9e/4
What's problem:
I try to solve this problem , but give wrong result each time , for example (can be see on sqlfiddle) , user/tell named as habib on give wrong sum(price) result.
habib's faktorhaprice = -508261 and habib's paymentprice = 648000 but sum result in main query have wrong data -7115654 and 13000000
what's the solution ?
(Updated) One way:
SELECT tell,SUM(FAKTORHAPRICE) FAKTORHAPRICE, SUM(PaymentPrice) PaymentPrice
FROM (SELECT tell, price as FAKTORHAPRICE, null PaymentPrice
from user_faktorha
union all
SELECT Username as tell, null as FAKTORHAPRICE, Price as PaymentPrice
FROM `u_payment` WHERE Active='1') sq
GROUP BY tell ORDER BY FAKTORHAPRICE ASC;
SQLFiddle here.
The essence of your problem here is that you are trying to relate to unrelated tables. Sure they have common data in the user name, but there is not a clean relation between them like an invoice id that can be used to relate the items together such that the OUTER JOIN wouldn't duplicate records in your result set. My suggestion would be to do the aggregation on each table individually and then join the results like this:
SELECT f.tell, f.faktorhaprice, p.paymentprice
FROM
(SELECT tell, SUM(price) AS faktorhaprice FROM user_faktorha GROUP BY tell) AS f
INNER JOIN
(SELECT username, SUM(price) AS paymentprice FROM u_payment GROUP BY username) AS p
ON f.tell = p.username