I have two tables
tblData
ID Name
1 ABC
2 XYZ
tblDetails
ID DataID PayDate Amount ApprovedDate
1 1 15-08-2015 200 20-18-2015
2 1 16-08-2015 300 20-18-2015
3 1 17-08-2015 50 20-18-2015
4 2 18-08-2015 100 21-18-2015
5 2 19-08-2015 500 21-18-2015
I need to get the result like the following
ID Duration TotalAmount ApprovedDate
1 15-08-2015 - 17-08-2015 550 20-18-2015
2 18-08-2015 - 19-08-2015 600 21-18-2015
How can I accomplish this?
It seems like a simple GROUP BY
together with some aggregate functions can do the job:
SELECT DataID, CONCAT(MIN(PayDate), ' - ', MAX(PayDAte)) AS Duration,
SUM(Amount) AS TotalAmount, MAX(ApprovedDate) AS ApprovedDate
FROM tblDetails
GROUP BY DataID
Note: Table tblData
does not seem to play any role in producing the required result set.
You can use joins in your query which will join data from different tables into one. Simple example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.