I have 2 tables in MS Access with the following values
Customer
id | name
1 | jon
2 | bob
3 | jack
Order
id | amount | date | customer
5 | 50 | 3/10/2017 | 1
4 | 100 | 3/10/2017 | 1
3 | 45 | 2/28/2017 | 2
2 | 10 | 3/10/2017 | 3
1 | 5 | 3/10/2017 | 2
I want to get an output of
name | orderid | amount
jon | 5 | 50
bob | 3 | 45
jack | 2 | 10
I want to get amount of the latest order id per customer, however I keep getting this
name | orderid | amount
jon | 5 | 50
jon | 4 | 100
bob | 3 | 45
bob | 2 | 10
jack | 1 | 5
I used the query designer and have used the function MAX() to the order id, GROUP BY to all columns (MS Access does not allow to group the rows using a single column), DISTINCT and DISTINCTROW, as well as set the query properties "Unique Records" to Yes but the duplicate records still shows.
Related
I am trying to do these queries for a homework of a subject that the teacher didn't teach us, and none of my classmates can do it either, I have these 4 tables to MySQL
Activity
idActivity | nameActivity | idDepartment
---------: | :--------------------- | -----------:
1 | hiring | 1
2 | payroll | 1
3 | purchasing | 2
4 | quotes | 2
5 | Customer contact | 3
6 | Customer agreement | 3
7 | marketing | 3
8 | Product placement | 2
9 | Collection of products | 2
10 | assitance | 1
11 | Product delivery | 3
12 | Check-in and check-out | 4
13 | offers | 3
Employee
idEmployee | NameEmployee | Salary | idDepartment
---------: | :----------- | -----: | -----------:
10 | Maria | 4000 | 1
20 | Jorge | 3000 | 2
30 | Leonor | 5000 | 3
40 | Patricia | 3000 | 2
50 | Gilberto | 2000 | 4
60 | Gonzalo | 7000 | 2
70 | Beatriz | 3000 | 1
80 | Ana | 5000 | 3
90 | Manuel | 2000 | 4
100 | Silvestre | 7000 | 1
110 | Alejandra | 5000 | 2
120 | Fernando | 2000 | 1
130 | Joaquin | 4000 | 4
140 | Pedro | 4000 | 4
150 | Pablo | 2500 | 3
Department
idDepartment | nameDepartment
-----------: | :-------------
1 | staff
2 | Purchasing
3 | Sells
4 | finances
Activity-Employee
idEmployee | idActivity
---------: | ---------:
10 | 1
10 | 2
20 | 3
20 | 4
30 | 5
30 | 6
30 | 7
50 | 8
50 | 9
70 | 10
90 | 8
100 | 1
db<>fiddle here
Some columns have the same name because primary and foreign keys, the primary keys are in the tables that have there names. What am I trying to do are some queries like:
Show employees names, name of the department that they belong to, and name of the activities that they do, grouping by activity (Group by).
Using Subqueries Show employees names, name of the department that they belong to, and name of the activities that they do, grouping by activity (Group by).
We tried some queries but the employee name just repeat itself, and didn't group by activities.
https://drive.google.com/file/d/1f7AkdjHYltKxg640diUZfD2zs2gM-sol/view?usp=sharing
Hopefully someone can help us, we are kinda desperate :(
And sorry for not knowing how to put directly the tables in here, it's my first post in here:(
select
NameEmployee,
group_concat(nameActivity) nameActivity,
(select nameDepartment from Department where idDepartment=tmpx.idDepartment) as DeptName from (select t1.NameEmployee,
(select distinct nameActivity from Activity where idActivity=t2.idActivity) as nameActivity,
(select distinct idDepartment from Activity where idActivity=t2.idActivity) as idDepartment
from Employee t1 join `Activity-Employee` t2
on t1.idEmployee=t2.idEmployee) as tmpx GROUP BY tmpx.NameEmployee;
Check if it works for u
Consider the following sample table from a soccer tournament (let's call this table matches)
+----------+---------+--------------+
| match_id | club_id | goals_scored |
+----------+---------+--------------+
| 1 | 1 | 1 |
| 1 | 2 | 0 |
| 2 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 1 | 0 |
| 3 | 4 | 2 |
| 4 | 2 | 2 |
| 4 | 3 | 4 |
| 5 | 2 | 4 |
| 5 | 4 | 0 |
| 6 | 3 | 1 |
| 6 | 4 | 1 |
+----------+---------+--------------+
The resulting table we want should give us each club's total goals scored AND goals conceded:
+---------+--------------+----------------+
| club_id | goals_scored | goals_conceded |
+---------+--------------+----------------+
| 1 | 2 | 4 |
| 2 | 6 | 4 |
| 3 | 6 | 4 |
| 4 | 3 | 5 |
+---------+--------------+----------------+
Getting goals scored is straight forward enough...
SELECT SUM(goals_scored),
club_id
FROM matches
GROUP BY club_id
but I am absolutely flummoxed as to how to get it for each team's opponents.
I could, of course, construct a pretty complex array of subqueries to get there. If this were application-side work I'd likely just stuff it in a loop and iterate over each club to get there, but my use case requires a SQL answer if possible. Any thoughts?
edit: also if anyone has any better ideas on how to title this question, I'm all ears - I'm not really sure exactly how to describe this problem in the first place.
We can use a self-join approach here:
SELECT
m1.club_id,
SUM(m1.goals_scored) AS goals_scored,
SUM(m2.goals_scored) AS goals_conceded
FROM matches m1
INNER JOIN matches m2
ON m2.match_id = m1.match_id AND
m2.club_id <> m1.club_id
GROUP BY
m1.club_id
ORDER BY
m1.club_id;
This approach brings the goals conceded by each club to the other club, for each match, into a single row. We then just aggregate by club to get the two sums.
I have a single mysql table. Query should lookup prev_id multiple times till the insertimestamp is between cuurent and previous week and get the rows to columns.
ID | inserttimestamp| prev_id | category
-----------------------------
5 | 2017-06-08 | 4 | Level456
4 | 2017-06-05 | 3 | Level241
3 | 2017-05-31 | 2 | Level456
2 | 2017-05-28 | 1 | Level247
1 | 2017-05-27 | | Level231
The result should be something like this,
ID | inserttimestamp| prev_id | category | id1 | id1_category | id2 | id2_category |
--------------------------------------------------------------------------------------------
5 | 2017-06-08 | 4 | Level456 | 4 | Level 241 | 3 | Level456 |
In this case as you see it stopped to id 3 because id 2 and 1 are not falling under previous week.
Use Case: To find out how many level upgrade/downgrade happened since previous week
For the above records its Levelupgrade - 1 , LevelDowngrade -1
Any help would be highly appreciated. Thanks in advance.
I wonder what's the best practice is when you have a table like this below with orders. I need to calculate the total price of each order. Should I use triggers to calculate the price or should I hardcode the calculation before insert into database?
ORDERS:
id | Article | Price | Quantity | Total price
---------------------------------------------
1 | TV | 5 | 1 | 5
2 | CD | 3 | 2 | 6
3 | Book | 2 | 3 | 6
4 | XBOX | 1 | 1 | 1
I have four MySql tables (simplified here):
Table 1: factions (just a list to reference)
id | name
1 | FactionName1
2 | FactionName2
Table 2: currencies (just a list to reference)
id | name
1 | Currency1
2 | Currency2
3 | Currency3
Table 3: events (just a list to reference)
id | name | date
1 | Evebt1 | 2013-10-16
2 | Event2 | 2013-10-18 (Note: date out of order)
3 | Event3 | 2013-10-17
Table 4: event_banking (data entered after each event, remaining amount of each currency for each group)
id | faction_id | currency_id | event_id | amount
1 | 1 | 1 | 1 | 10
2 | 1 | 1 | 2 | 20
3 | 1 | 1 | 3 | 30
4 | 1 | 2 | 1 | 40
5 | 1 | 2 | 2 | 50
6 | 1 | 2 | 3 | 60
7 | 1 | 3 | 1 | 70
8 | 1 | 3 | 2 | 80
9 | 1 | 3 | 3 | 90
10 | 2 | 1 | 1 | 100
11 | 2 | 1 | 2 | 110
12 | 2 | 1 | 3 | 120
13 | 2 | 2 | 1 | 130
14 | 2 | 2 | 2 | 140
15 | 2 | 2 | 3 | 150
16 | 2 | 3 | 1 | 160
17 | 2 | 3 | 3 | 170
Note: Faction 2 didn't bank Currency 3 for Event 2
What I'm looking to be able to do is to get, for each currency, the total of the last banked (date wise) for each faction. (ie How much of each currency is currently banked in total if all factions are merged)
So, I need a table looking something like:
currency_id | total
1 | 130 (eg 20 + 110)
2 | 190 (eg 50 + 140)
3 | 250 (eg 80 + 170) <- Uses Event 3 for Group 2 as Event 2 doesn't exist
I can do basic joins etc, but I'm struggling to be able to filter the results so that I get the latest results for each Faction x Currency x Event so I can then sum them together to get the final total amounts for each currency.
I've tried various permutations of LEFT OUTER JOINs, GROUP BYss & HAVING COUNTs, and had some interesting (but incorrect results), and a variety of different error codes, but nothing remotely close to what I need.
Can anyone help?
I guess you can go on with something like this:
select eb.currency_id, sum(amount) as total
from events e
inner join (
select faction_id, currency_id, max(date) as md
from event_banking eb
inner join events e
on eb.event_id = e.id
group by faction_id, currency_id
) a
on e.date = a.md
inner join event_banking eb
on e.id = eb.event_id
and a.faction_id = eb.faction_id
and a.currency_id = eb.currency_id
group by currency_id;
Here is SQL Fiddle