I have person data
Employee ID || Length of Service || Age
and a Payment table
Length of Service || Age || Paid weeks
I want to return Paid weeks for each of the person data. How would i go about this?
Thanks
The easiest way to go is to join those tables on universe level.
Alternatively you can load the two tables with two queries, and merge 'Length of Service' and 'Age'. This can be done by selecting both values, and right-click 'Merge'. Now you should be able to create a table where all four different columns are shown.
In case you are unable to create this table, you could make a variable containing the information of 'Length of Service' and 'Age', and make a detail of 'Paid weeks'.
Related
I have an old database that is used to keep track of employee attendance at training events. Each record is an employee and following the basic first name/last name phone number ect... there are about 150 fields representing past events, each with a yes/no checkbox indicating whether that employee attended that event.
I have a form that allows employee records to be easily viewed and edited. I currently have a massive list of the events with checkboxes that are tied to the corresponding fields in the master table.
I need to create a list of events that the employee currently being viewed has attended, so a display of all the field names that are checked for the specified employee.
I'm aware that the design of the database I'm working with is obviously imperfect, but is there any way that I can do this?
Thanks in advance!
You will essentially need to "unpivot" the ~150 columns for each event into a temporary table with
EmployeeID EventID
---------- -------
1 event1
1 event3
1 event7
The most common approach to "unpivot" in Access is to UNION ALL a series of individual SELECTs. However, that is only good for about a dozen UNIONs (after which Access throws a "query is too complex" error), so you'll have to break it up into smaller groups of "event" fields and store the results in a temporary table.
To avoid having to do that messy "unpivoting" over and over again, you should just fix your database structure to make that "unpivoted" table permanent and get rid of the ~150 fields in the other table. It's a classic case of "short-term pain, long-term gain".
I was poking around a TFS database today to try and run some statistics and I came across a table called tbl_Number. This table contains one column Number, and all the values are just the values 1 to 500,000. None of the values differ from their respective index in the list, as you can see in the screenshot from queries I ran in LinqPad:
Tbl_Numbers.Max(x => x.Number).Dump(); //max value
Tbl_Numbers.Count().Dump(); //number of entries
var asList = Tbl_Numbers.ToList();
asList.Where(x => asList[x.Number - 1].Number != x.Number).Any().Dump();
//False shows that every entry matches the value at its ordinal location in the list
My question is: What would the use of such a table be? Is this in case one of the referenced numbers needs to change for some reason? The only way to identify a number from this table is by using that same number, so I don't see what use this table could be.
I realize this question could lead to answers that are conjecture, but I'd be interested to see if there's some programming principal that I'm unaware of that's being used here.
It can be used in OUTER JOINS to make sure that you always get all the numbers in a given range, even if there is no data related to that number.
For example, suppose I want to return the count of customers who bought 3,4 or 5 products on their last order. But in fact, there are no customers who bought 4 products. If I just ran a count query on my data, I wouldn't get a row for the customers who bought 4 products at all.
However, If I query my numbers table and LEFT JOIN to my data, I will get the number 4, and a count of 0 or NULL, depending on how I wrote my query.
People also often do this with Date tables, by the way.
I am new to symfony and PHP.
I have a problem that I hope someone can help:
supose we have 3 entities.
Groups, specialities, works; works belong to a speciality, speciality belong to a group.
Supose that I want to have a form to create a "work". I want to filter my specialities according to a select with the list of groups ( much like on a travel site where we get the destinations filtered by the origin).
So my form will only have 2 fields. The speciality and a name for the work. But in my view i must have 3 fields, 1 for groups that will filter the specialities and the fields belonging to the form.
Much like a booking flights site I must see the fields from the begining.
This as to be so simple, but I'm really stuck on it.
Thank you
At this moment I can come up with 2 solutions:
1) Use ajax.
On the request populate a selectbox with the groups. when you select one of the groups, all the specialities come into a different selectbox of that group.
2)
Use uri segments. Let the user first choose group, then go to a next page and let them choose a speciality. After that the user gets a form where he fills in the data.
I have searched for a solution for this problem, but haven't found it (yet), probably because I don't quite know how to explain it properly myself. If it is posted somewhere already, please let me know.
What I have is three databases that are related to each other; main, pieces & groups. Basically, the main database contains the most elementary/ most used information from a post and the pieces database contains data that is associated with that post. The groups database contains all of the (long) names of the groups a post in the main database can be 'posted in'. A post can be posted in multiple groups simultaneously. When a new post is added to my site, I check the pieces too see if there are any duplicates (check if the post has been posted already). In order to make the search for duplicates more effective, I only check the pieces that are posted in the same group(s).
Hopefully you're still with me, cause here's where it starts to get really confusing I think (let me know if I need to specify things more clearly): right now, both the main and the pieces database contain the full name of the group(s) (basically I'm not using the groups database at all). What I want to do is replace the names of those groups with their associated IDs from the groups database. For example, I want to change this:
from:
MAIN_table:
id | group_posted_in
--------|---------------------------
1 | group_1, group_5
2 | group_15, group_75
3 | group_1, group_215
GROUPS_table:
id | group_name
--------|---------------------------
1 | group_1
2 | group_2
3 | group_3
etc...
into:
MAIN_table:
id | group_posted_in
--------|---------------------------
1 | 1,5
2 | 15,75
3 | 1,215
Or something similar to this. However, This format specifically causes issues as the following query will return all of the rows (from the example), instead of just the one I need:
SELECT * FROM main_table WHERE group = '5'
I either have to change the query to something like this:
...WHERE group = '5' OR group = '5,%' OR group = '%,5,%' OR group = '%,5'
Or I have to change the database structure from Comma Separated Values to something like this: [15][75]. The accompanying query would be simpler, but it somehow seems like a cumbersome solution to me. Additionally, (simple) joins will not be easy/ possible at all. It will always require me to run a separate query to fetch the names of the groups--whether a user searches for posts in a specific group (in which case, I first have to run a query to fetch the id's, then to search for the associated posts), or whether it is to display them (first the posts, then another query to match the groups).
So, in conclusion: I suppose I know there is a solution to this problem, but my gut tells me that it is not the right/ best way to do it. So, I suppose the question that ties this post together is:
What is the correct method to connect the group database to the others?
For a many-to-many relationship, you need to create a joining table. Rather than storing a list of groups in a single column, you should split that column out into multiple rows in a separate table. This will allow you to perform set based functions on them and will significantly speed up the database, as well as making it more robust and error proof.
Main
MainID ...
Group
GroupID GroupName
GroupsInMain
GroupsInMainID MainID(FK) GroupID(FK)
So, for MainID 1, you would have GroupsInMain records:
1,1,1
2,1,5
This associates groups 1 and 5 with MainID 1
FK in this case means a Foreign Key (i.e. a reference to a primary key in another table). You'd probably also want to add a unique constraint to GroupsInMain on MainID and GroupID, since you'd never want the same values for the pairing to show up more than once.
Your query would then be:
select GroupsInMain.MainID, Group.GroupName
from Group, GroupsInMain
where Group.GroupID=GroupsInMain.GroupID
and Group.GroupID=5
I've researched related questions on the site but failed to find a solution. What I have is a user activity table in MySQL. It lists all kind of events of a user within a photo community site. Depending on the event that took place, I need to query certain data from other users.
I'll explain it in a more practical way by using two examples. First, a simple event, where the user joined the site. This is what the row in the activity table would look like:
event: REGISTERED
user_id: 19 (foreign key to user table)
date: current date
image_id: null, since this event has nothing to do with images
It is trivial to query this. Now an event for which extra data needs to be queried. This event indicates a user that uploaded an image:
event: IMAGEUPLOAD
user_id: 19 (foreign key to user table)
date: current date
image_id: 12
This second event needs to do a join to the image table to get the image URL column from that table. A third event could be about a comment vote, where I would need to do a join to the comments table to get extra columns.
In essence, I need a way to conditionally select extra columns (not rows) per row based on the event type. This is easy to do when the columns come from the same table, but I'm struggling to do this using joins from other tables. I hope to do this in one, conditional query without the use of a stored procedure.
Is this possible?
You could make the joins depend on the event type, like:
select *
from Events e
left join Image i
on e.event = 'IMAGEUPLOAD'
and e.image_id = i.id
left join comments c
on e.event = 'COMMENT'
and e.comment_id = c.id
If there's one column that is shared among all linked tables, for example create_date, you can coalesce to select the one that's not NULL:
select coalesce(i.create_date, c.create_date, ...) as create_date
Doing precisely what you want to do is not possible. A SELECT is designed to return a list of tuples/rows, and each has the same number of elements/columns.
What you really are doing here is collecting 2 different kinds of information, and you're going to have to process the 2 different kinds of information separately anyway, which should be a hint that you're doing something slightly wrong. Instead, pull the various event types out individually, perform whatever additional operations you need to do to convert them to your common output type (eg. HTML if this is for a website), and then interleave them together at that stage.