New to access and the site but I've learned a lot from here already. Hoping that this is a fairly easy question to answer.
I've got 2 tables of data and I'm trying to get all the possible permutations with the rows intact.
I have:
Table Day 1
Start Finish
A. B
B. C
C. D
D. A
Table Day 2
Start. Finish
B. D
C. A
D. B
A. C
What I want to generate is a table or query that combines the possible permutations of Day 1 and Day 2 to get a new start and finish:
Table Day1&2
Start. Finish
A. D
B. A
C. B
Etc. Etc
That could be a Cartesian query:
Select Distinct A.Start, B.Finish
From A, B
Where A.Start <> B.Finish
Related
I'm not even quite sure what to call what I am trying to do. I need a column that accumulates the total of its own calculations on previous rows. Here's a simplified example of the output I need (d is the column I need help with):
a b c d e
1 36 6 36 6
2 0 5 30 6
3 0 4 24 6
4 22 10 40 4
5 0 9 36 4
6 0 8 32 4
a is an autonumber column
b is user entered column
c is a column that usually counts down by one, but occasionally has other values added
d is equal to: the prior row of d + b - prior row of e
e is equal to d/c
It may help if I explain why I need this. I'm working on simulating the impact of rate shocks on a banking institution (commonly called stress testing). b is a column that represents some impact of an interest rate shock (example: a percentage change to the number of loans made). c is a column that represents the number of months before the market is expected to return to normal without any further rate shocks. d represents the impact on loans for that time period and e is the rate at which d is returning to zero.
Here is what I've tried so far and why it doesn't work:
SET var := 0;
SELECT var := var + b - var/c AS d
This would be my preferred solution, but I'm creating a view, and views are not allowed to reference user created variables. It has to be a view because I need to reference it in other queries and views.
SELECT (lag(d,1,0) OVER (ORDER BY a)) + b - (lag(e,1,0) OVER (ORDER BY a)) AS d
This doesn't work because d hasn't been defined yet. I just get an error that d isn't a column.
SELECT
((SELECT sum(temp.b)
FROM table as temp
WHERE temp.a <= a)
/(SELECT sum(temp2.c)
FROM table as temp2
WHERE temp2.a = a))
This doesn't work because I need to divide the reduced value of d, not just the sum of b.
d is the cumulative sum of the difference between b and c. This would be expressed as:
select a, b, c,
sum(b - c) over (order by a) as d,
d / sum(b - c) over (order by a) as e
from t;
I'm using window functions, even though the question is tagged MySQL. MySQL only supports them since version 8.0.
I'm not sure how stable the results would be in a VIEW, but you could use session variables.
SELECT a, b, c, #d := #d + b - #e AS d, #e := #d / c AS e
FROM t
, (SELECT #d := 0 AS initD, #e := 0 AS initE) As init
ORDER BY a;
Also, keep in mind this is kind of crossing over into behavior observed, not actually guaranteed; as MySQL doesn't officially guarantee the select expressions are evaluated from left to right.
I solved this with pure brute force. I created a permanent table, and then I created a trigger which deletes all rows of the table and then recreates the table using the variable solution. Thus any time the underlying data changes, MySQL automatically updates the table with the most current information. The table acts like a view in that it is updated in real time, but is a permanent table.
Unfortunately, MySQL doesn't offer a FOR EVERY STATEMENT option for triggers, which means my code is running over and over again for every line that gets updated, so if I update 100 rows, my table gets deleted and recreated 100 times.
Still looking for a better solution, but at least this works.
SELECT column_1 FROM table_1,table_2;
When I ran this on my database it returned huge number of rows with duplicate column_1 values. I could not understand why I got these results. Please explain what this query does.
it gives you a cross product from table 1 and table 2
In more layman's terms, it means that for each record in Table A, you get every record from Table B (all possible combinations).
TableA with 3 records and Table B with 3 records gives 9 total records in the result:
TableA-1/B-1
TableA-1/B-2
TableA-1/B-3
TableA-2/B-1
TableA-2/B-2
TableA-2/B-3
TableA-3/B-1
TableA-3/B-2
TableA-3/B-3
Often used as a basis for Cartesian Queries (which themselves are the means to generate, say, a list of future dates based on a recurrence schedule: give me all possible results for the next 6 months, then restrict that set to those whose factor matches my day of the week)
This is 'valid' way of cross joining two tables; it is not the preferred way though. Cross Join would be much clearer. An on condition would then be helpful to limit results,
Imagine that i have 3 friends named Jhon, Ana, Nick; then i have in the other table 2 are T-shirts a red and a yellow and i wanna know witch is from.
So in the query being tableA:Friends and tableB:Tshirts returns:
1|JHON | t-shirt_YELLOW
2|JHON | t-shirt_RED
3|ANA | t-shirt_YELLOW
4|ANA | t-shirt_RED
5|NICK | t-shirt_YELLOW
6|NICK | t-shirt_RED
As you see this join has no relational logic between friends and Tshirts so by evaluating all the posible combination generates what you call duplicates.
I'm having some issues with this MySQL query. I've got two tables, one that has a list of all the "Leaders of the Opposition"(People elected into office) with the date that they were elected. And I've got another table of all the people they've been married to, and the year they got married in.
I'm trying to make a query that returns all the Leaders of the Opposition ordered by their appointment date with their current spouses name at the time and the date of their marriage.
Here is some practice data of just one leader, dates changed a bit to fit the sort of problem I'm trying to solve.
TABLE ONE:
Leader_of_Opposition------Date Elected
Beazley K C, 1996-03-19
Beazley K C, 2005-01-28
TABLE TWO:
Leader_of_Opposition----Spouses's Name----Year Married
Beazley K C, Mary Ciccarelli, 1974
Beazley K C, Susie Annus, 2004
-
And I'm trying to get it to something like this:
Leader_of_Opposition------Date Elected------Spouses's name--------Year Married
Beazley K C, 1996-03-19, Mary Ciccarelli, 1974
Beazley K C, 2005-01-28, Susie Annus, 2004
-
So far I've got the basics of:
SELECT opposition.leader_name, opposition.time_begin, opposition_marriage.spouse_name, opposition_marriage.year_married'
FROM opposition, opposition_marriage
AND opposition.leader_name=opposition_marriage.leader_name
ORDER BY opposition.time_begin
But it gives me results where the leaders are mentioned multiple times for each marriage. And I can't figure out the syntax to search the other table then place that value into the row.
Any help would be extremely appreciated, been banging my head up against this one for a while now.
Thanks in advance.
I think this is going to be easiest with correlated subqueries. Alas, though, your tables do not have unique identifiers for each row.
SELECT o.leader_name, o.time_begin,
(select om.spouse_name
from opposition_marriage om
where o.leader_name = om.leader_name and om.year_married <= year(o.date_elected)
order by om.year_married desc
limit 1
) as spouse_name,
(select om.year_married
from opposition_marriage om
where o.leader_name = om.leader_name and om.year_married <= year(o.date_elected)
order by om.year_married desc
limit 1
) as year_married
FROM opposition o
ORDER BY o.time_begin;
This handles as many marriages as you like.
Now some comments:
It seems really strange to have a table only of marriages for the opposition leaders and not for all politicians.
The granularity is at the level of a "year", so a leader married in the same year after s/he takes office counts as being married to that spouse.
You do not have a "marriage end date", so a divorced or widowed partner would be considered current until the next marriage.
As I mention in the beginning, you should have a unique identifier for each row.
Can we get sum of several columns that have intergers in a mysql table?
A B C D
1 0 2 3
I need to get sum of all columns. I have one row.
It seems that the data model is awkward, however,
select a+b+c+d
from ...
where ...
select suma+sumb+sumc+sumd from
(select sum(A) as suma,sum(B) as sumb,sum(C)as sumc,sum(D) as sumd
from tab) mytab
I want to have a query that returns the best results from a table.
I am defining the best results to be the addition of two columns a + b (each column holds an int)
ie:
entry a b
1 4 5
2 3 2
3 20 30
Entry 3 would be returned because a + b is the highest in this case.
Is there a way to do this? One idea I had was to create another column in the table which holds the addition of a and b and then ORDER by DESC, but that seems a little bit messy.
Any ideas?
Thanks!
SELECT *
FROM mytable
ORDER BY
a + b DESC
LIMIT 1
Adding another column, however, would be a good option, since you could index this column which would improve the query.