Join two tables and show distinct records - ms-access

I have two tables in MS Access 2010:
PART LOG-OUT TABLE:
PART_ID DRAWN_DATE LOCATION_ID
C0001 07/29/2013 501
C0002 07/29/2013 604
C0003 08/01/2013 703
C0004 08/01/2013 807
C0005 08/02/2013 505
C0006 08/02/2013 602
C0007 08/02/2013 707
C0008 08/03/2013 802
C0009 08/03/2013 803
C0001 10/01/2013 605
C0002 10/02/2013 704
C0004 10/05/2013 806
PART RETURN TABLE:
PART_ID RETURN_DATE LOCATION_ID
C0001 09/04/2013 STORE
C0002 09/05/2013 STORE
C0004 09/10/2013 STORE
C0007 09/12/2013 STORE
C0008 09/13/2013 STORE
C0002 10/03/2013 STORE
This is the result I want:
PART_ID DRAWN_DATE LOG-OUT LOCATION RETURN_DATE RETURN LOCATION
C0001 07/29/2013 501 09/04/2013 STORE
C0001 10/01/2013 605
C0002 07/29/2013 604 09/05/2013 STORE
C0002 10/02/2013 704 10/03/2013 STORE
C0003 08/01/2013 703
C0004 08/01/2013 807 09/10/2013 STORE
C0004 10/05/2013 806
C0005 08/02/2013 505
C0006 08/02/2013 602
C0007 08/02/2013 707 09/13/2013 STORE
C0008 08/03/2013 802 10/03/2013 STORE
C0009 08/03/2013 803
But I can only get this:
PART_ID DRAWN_DATE LOG-OUT LOCATION RETURN_DATE RETURN LOCATION
C0001 07/29/2013 501 09/04/2013 STORE
C0001 10/01/2013 605 `09/04/2013 STORE`
C0002 07/29/2013 604 09/05/2013 STORE
`C0002 07/29/2013 604` 10/03/2013 STORE
C0002 10/02/2013 704 `09/05/2013 STORE`
`C0002 10/02/2013 704 10/03/2013 STORE`
C0003 08/01/2013 703
C0004 08/01/2013 807 09/10/2013 STORE
C0004 10/05/2013 806 `09/10/2013 STORE`
C0005 08/02/2013 505
C0006 08/02/2013 602
C0007 08/02/2013 707 09/13/2013 STORE
C0008 08/03/2013 802 10/03/2013 STORE
C0009 08/03/2013 803
after I wrote:
SELECT L.PART_ID, L.DRAWN_DATE, L.LOCATION_ID AS [LOG-OUT LOCATION], R.RETURN_DATE, R.LOCATION_ID AS RETURN_LOCATION FROM (SELECT * FROM [PART LOG-OUT] ORDER BY PART_ID) AS L LEFT JOIN (SELECT * FROM [PART RETURN] ORDER BY PART_ID) AS R ON L.PART_ID = R.PART_ID ORDER BY L.PART_ID, L.DRAWN_DATE, R.RETURN_DATE;
Can somebody correct me? Thanks!

Your requirements are a bit vague so I may be off, but you seem to want to pair returns with times that are before the return only, something like this?
SELECT L.PART_ID, L.DRAWN_DATE, L.LOCATION_ID AS [LOG-OUT LOCATION],
MIN(R.RETURN_DATE), MIN(R.LOCATION_ID) AS RETURN_LOCATION
FROM (SELECT * FROM [LOG_OUT LOCATION]) AS L
LEFT JOIN (SELECT * FROM [PART_RETURN]) AS R
ON L.PART_ID = R.PART_ID AND L.DRAWN_DATE < R.RETURN_DATE
GROUP BY L.LOCATION_ID,L.PART_ID,L.DRAWN_DATE
ORDER BY L.PART_ID, L.DRAWN_DATE, MIN(R.RETURN_DATE)
An SQLfiddle to test with.
Note that since there's nothing pairing a single purchase with a single return (and no such sample in your question), the logic is very basic for pairing them up.

As you are joining on the PART_ID column, the result you are getting is what you would expect to get.
For example, there are two C0001 in the LOG-OUT table and these will join to the same C0001 in the PART RETURN table as there is nothing to distringuish the rows in the return table from each other:
PART LOG-OUT TABLE -> PART RETURN TABLE
C0001 07/29/2013 501 -> C0001 09/04/2013 STORE
C0001 10/01/2013 605 -> C0001 09/04/2013 STORE
You need another criteria to join on, or you need to join on some more unique identifier in order to achieve the results you are looking for.

SELECT L.PART_ID, L.DRAWN_DATE, L.LOCATION_ID AS [LOG-OUT LOCATION], R.RETURN_DATE, R.LOCATION_ID AS RETURN_LOCATION FROM (SELECT * FROM [PART LOG-OUT] ORDER BY PART_ID) AS L LEFT OUTER JOIN (SELECT * FROM [PART RETURN] ORDER BY PART_ID) AS R ON L.PART_ID = R.PART_ID ORDER BY L.PART_ID, L.DRAWN_DATE, R.RETURN_DATE;

Related

Need help altering my query to keep min rows intact

I'm having a problem with a query I'm running. Here's a dbfiddle: https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=8dc0b4f201e7d25c8817dcecd35b47f0
Basically, I'm trying to keep the rows intact, what I mean is: You can see in the 1st query that playerID 147 set a score of 10450 on the 2018-03-24 13:37:02. However, sadly in the 2nd query the row breaks, I still get the min score I need, but the date is wrong. it sets 2018-03-05 16:24:28 even though it should be 2018-03-24 13:37:02.
I tried doing what's described here: How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL? but I can't get it to work. I've no idea how to rewrite my query in order for it to work. I'd appreciate any help in altering my query. Thanks for any help.
With this SELECT Statement
SELECT MIN(t.ID),t.Score,t.ChallengeId,t.playerID,MIN( from_unixtime(date) )
FROM `times` t inner Join
(SELECT MIN(Score) score,challengeID,playerID
from `times`
group by challengeID,playerID
Order by Score ASC) s
ON t.challengeID = s.challengeID
and t.playerID = s.playerID
and t.Score = s.score
GROUP BY t.Score,t.ChallengeId,t.playerID
order by t.Score
You get this result(Only the first are here Displayed
ID Score ChallengeId playerID from_unixtime(date)
90488 10450 466 28 2018-03-20 02:16:29
92155 10450 466 8 2018-03-24 02:05:18
92448 10450 466 147 2018-03-24 13:37:02
92763 10450 466 97 2018-03-24 19:15:42
92787 10450 466 410 2018-03-24 19:23:24
85201 10460 466 255 2018-03-06 05:13:46
86256 10460 466 66 2018-03-09 10:48:16
92778 10460 466 1846 2018-03-24 19:21:07
84801 10470 466 47 2018-03-05 16:29:41
84804 10470 466 944 2018-03-05 16:30:34
85724 10470 466 599 2018-03-07 08:07:30
88139 10470 466 1651 2018-03-15 11:16:54
DBfiddle new sample https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=de581e5a5b6fa9a184cc2e235337b2d5

join 2 mysql select based on text field

I have the 2 following select in Mysql:
1st select:
(SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date As FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) as Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player`<>'Player'
ORDER BY `Online_playerdatabase_v2`.`Balance` DESC;
2d select:
SELECT DISTINCT(Online_customer_activity_v2.Customers) as Player,
max(Online_customer_activity_v2.Date) as LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets`>0
Group by Online_customer_activity_v2.Customers
Output Select 1
Player FirstDep Balance
Ray 2014-10-19 9100.00
Ramzi 2014-11-02 9.61
tareq 2014-11-06 805.00
STAN 2014-10-17 7.50
Bill 2014-03-25 68.40
karam 2014-11-16 676.50
Abdul 2014-11-13 650.00
Renaud 2014-03-12 507.00
John 2014-11-22 500.00
Output select 2
Player LastAction
John 2015-11-13
Bill 2014-12-14
Renaud 2015-03-14
Abdul 2015-11-16
Ray 2015-11-22
STAN 2015-10-29
Ramzi 2015-11-10
Tarek 2015-05-10
karam 2014-12-10
Abdul 2015-02-10
Desired Output, a join on both Select that adds following calculations:
active days (FirstDep-LastAction) and Days_last_Visit (CurrentDate - Last Action)
Summarized in following table:
Player FirstDep Balance LastAction Active_days Days_last_Visit
Ray 2014-10-19 9100.00 2015-11-22 399 1
Ramzi 2014-11-02 9.61 2015-11-10 373 13
tareq 2014-11-06 805.00 2015-05-10 185 197
STAN 2014-10-17 7.50 2015-10-29 377 25
Bill 2014-03-25 68.40 2014-12-14 264 344
karam 2014-11-16 676.50 2014-12-10 24 348
Abdul 2014-11-13 650.00 2015-02-10 89 286
Renaud 2014-03-12 507.00 2015-03-14 367 254
John 2014-11-22 500.00 2015-11-13 356 10
Your help is greatly appreciated!
Thanks
The following query should give the result you want. I will add that I joined the two tables from your intermediate queries above using the Player field. This is not a very robust way to join, because the name may not be unique among all players in the table. A better way to join would be to use a unique identifier of some sort.
SELECT t1.Player, t1.FirstDep, t1.Balance, t2.LastAction,
DATEDIFF(t2.LastAction, t1.FirstDep) AS Active_days,
DATEDIFF(NOW(), t2.LastAction) AS Days_last_Visit
FROM
(
SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date AS FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) AS Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player` <> 'Player'
) t1
INNER JOIN
(
SELECT DISTINCT(Online_customer_activity_v2.Customers) AS Player,
MAX(Online_customer_activity_v2.Date) AS LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets` > 0
GROUP BY Online_customer_activity_v2.Customers
) t2
ON t1.`Player` = t2.`Player`
You need to join the 2 selects as subqueries in a 3rd select using the player field. The Active_days and Days_last_Visit fields can be calculated using the DateDiff() function.
SELECT *
,DateDiff(t2.LastAction,t1.FirstDep) AS Active_days
,DateDiff(CURDATE(), t2.LastAction) AS Days_last_Visit
FROM
(SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date As FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) as Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player`<>'Player'
ORDER BY `Online_playerdatabase_v2`.`Balance` DESC) t1
LEFT JOIN
(SELECT DISTINCT(Online_customer_activity_v2.Customers) as Player,
max(Online_customer_activity_v2.Date) as LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets`>0
Group by Online_customer_activity_v2.Customers) t2
ON t1.Player=t2.Player
You have to consider, however, how you join the 2 datasets. I used left join, since the players table will probably hold all players, but you may want to go for inner join or simulate a full outer join depending your requirements and your data.

MySQL query to find sum from the result of another query

Here is my SQL query:
SELECT (
SELECT COUNT(appointment_doctor) FROM appointment
JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
WHERE appointment_doctor = doctor_id
) * doctor_payment_rate AS fees FROM doctor
JOIN doctor_payment ON doctor.doctor_id=doctor_payment.doctor_payment_doctor
Result:
fees 500 1000 615 11615 1160 615 1610 285 660 220 3835 215 615 555 555 1065 1110 620 445 220 660 625 60 1715 615 10 115 615 60 625 60 330 615 625 720 625 615 670 615 500 615 500
I want the sum of all these value as total_fee.
Try this query:
SELECT SUM (
(SELECT COUNT(appointment_doctor) FROM appointment
JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
WHERE appointment_doctor = doctor_id) * doctor_payment_rate))
AS total_fee
FROM doctor
JOIN doctor_payment
ON doctor.doctor_id=doctor_payment.doctor_payment_doctor
Use mysql sum function SUM() function returns the total sum of a numeric column
SELECT SUM (
SELECT (
SELECT COUNT(appointment_doctor) FROM appointment
JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
WHERE appointment_doctor = doctor_id
)
select
sum(select
count(appointment_doctor)
from
appointment
join
doctor
on appointment.appointment_doctor= doctor.doctor_id
where
appointment_doctor=doctor_id)*doctor_payment_rate as 'Total fee'
from
doctor
join
doctor_payment
on doctor.doctor_id=doctor_payment.doctor_payment_doctor

Get the latest record in a table using two fields as an identifier

I am trying to find the latest order that is put in for a shop for a specific location in a shop. Therefore each shop has multiple orders to a location inside the shop.
Here is my table:
LocationId StoreId OrderStreet OrderCity PCode OrderState OrderDate
37 494 Store Store 3000 NT 11/13/2013 2:43:04 PM
38 494 Store Store 3000 NSW 12/04/2013 11:32
41 496 Underword Narrabeen 2100 NSW 12/04/2013 11:37
38 494 Store Store 3000 NT 12/09/2013 10:47
38 494 Store Store 3000 NT 12/09/2013 10:51
40 496 Underword Narrabeen 2100 NSW 3/03/2014 8:5
42 497 345 Malltomall Sydney 3999 NSW 4/01/2014 15:17
42 497 345 Malltomall Sydney 3999 NSW 4/01/2014 16:58 92
41 496 Underword Narrabeen 2100 NSW 4/01/2014 17:14
43 497 345 Malltomall Sydney 3999 NSW 4/02/2014 15:21
Here is what i have done so far:
This gives me the latest order for each shop, but only returns 1 single location for each shop.
SELECT A.OrderDate, A.OrderId, A.StoreId, A.LocationId, A.OrderStreet, A.OrderCity, A.OrderState, A.OrderPostCode, A.StoreId + A.LocationId AS Sum1
FROM tblOrder AS A INNER JOIN
(SELECT StoreId, MAX(OrderDate) AS MaxDate
FROM tblOrder
GROUP BY StoreId) AS B
ON A.StoreId = B.StoreId AND A.OrderDate = B.MaxDate
Here is what it returns:
I thought i could add the columns together to get a unique code eg. Sum1 but I could not get it to work.
Orderdate StoreId LocationId OrderStreet OrderCity OrderState Pcode Sum1
4/02/2014 497 43 345 Malltomall Sydney NSW 3999 540
4/01/2014 496 41 Underword Narrabeen NSW 2100 537
12/09/2013 494 38 Store Store NT 3000 532
God i feel dumb...
Group the derived table by LocationId as well as StoreId, then join back to the original table on that column too. I think that queries for groupwise maxima, like this, are a perfect use case for NATURAL JOIN (which is more concise as it does not require an explicit predicate, instead joining on all columns having the same name in both tables):
SELECT * FROM tblOrder NATURAL JOIN (
SELECT StoreId, LocationId, MAX(OrderDate) AS OrderDate
FROM tblOrder
GROUP BY StoreId, LocationId
) t
Alternatively, if LocationId is unique across all stores, you could merely group/join on that column (and entirely remove StoreId from the derived table):
SELECT * FROM tblOrder NATURAL JOIN (
SELECT LocationId, MAX(OrderDate) AS OrderDate
FROM tblOrder
GROUP BY LocationId
) t

Join condition retrieving undesired answer

I have 2 tables and is as follows
select Event_ID,[Gross Salary] from tblEar where Event_ID=14
Result:
Event_ID Gross Salary
14 56128
14 51984
14 42028
And:
select EventId, [Order Date],Amount from tblBudget where EventId=14
Result:
EventId Order Date Amount
14 10/10/2011 20000
14 10/10/2011 20000
14 20/03/2012 2500
14 02/04/2012 -50000
if i write a join statment on these 2 tables to get it is retrieving duplicate records.I used Distinct But no Positive Result.
select DISTINCT tba.[Order Date],ISNULL(tba.Amount,0),ISNULL(te.[Gross Salary],0) from tblBudget tba
join
tblEar te on tba.EventId=te.Event_ID where tba.EventId=14
I got the following ans:
Order Date (No column name) (No column name)
2011-10-10 20000.00 42028.00
2011-10-10 20000.00 51984.00
2011-10-10 20000.00 56128.00
2012-03-20 2500.00 42028.00
2012-03-20 2500.00 51984.00
2012-03-20 2500.00 56128.00
2012-04-02 -50000.00 42028.00
2012-04-02 -50000.00 51984.00
2012-04-02 -50000.00 56128.00
Can any one show the Way to get Accuarate data
I guess you want to group the data and aggregate the amounts:
SELECT tba.[Order Date], SUM(tba.Amount), SUM(te.[Gross Salary])
FROM tblBudget tba
JOIN tblEar te on tba.EventId = te.Event_ID
WHERE tba.EventId = 14
GROUP BY tba.[Order Date]