I have a table that has Act ID, and another table that has Act ID, percentage complete. This can have multiple entries for different days. I need the sum of the percentage added for the Act ID on the first tableZA.108381.080
First table
Act ID Percent Date
ZA.108381.110 Total from 2 table
ZA.108381.120
ZA.108476.020
ZA.108381.110 25% 5/25/19
ZA.108381.110 75 6/1/19
ZA.108381.120
ZA.108476.020
This would be generally considering not good practice. Your primary key should be uniquely identifiable for that specific table, and any other data related to that key should be stored in separate columns.
However since an answer is not a place for a lecture, if you want to store multiple values in you Act ID column, I would suggest changing your primary key to something more generic "RowID". Then using vba to insert multiple values into this field.
However changing the primary key late in a databases life may cause alot of issues or be difficult. So good luck
Storing calculated values in a table has the disadvantage that these entries may become outdated as the other table is updated. It is preferable to query the tables on the fly to always get uptodate results
SELECT A.ActID, SUM(B.Percentage) AS SumPercent
FROM
table1 A
LEFT JOIN table2 B
ON A.ActID = B.ActID
GROUP BY A.ActID
ORDER BY A.ActID
This query allows you to add additional columns from the first table. If you only need the ActID from table 1, then you can simplify the query, and instead take it from table 2:
SELECT ActID, SUM(Percentage) AS SumPercent
FROM table2
GROUP BY ActID
ORDER BY ActID
If you have spaces other other special characters in a column or table name, you must escape it with []. E.g. [Act ID].
Do not change the IDs in the table. If you want to have the result displayed as the ID merged with the sum, change the query to
SELECT A.ActID & "." & Format(SUM(B.Percentage), "0.000") AS Result
FROM ...
See also: SQL GROUP BY Statement (w3schools)
Related
It says that The database reported a syntax error:
Duplicate column name 'Product_Number'.
Below is the code:
SELECT *
FROM `df_all_orders_merged_la`
LEFT JOIN `product_database_la`
ON `df_all_orders_merged_la`.`Product_Number` = `product_database_la`.`Product_Number`
WHERE `product_database_la`.`Product_Number` IS NULL;
If both df_all_orders_merged_la and product_database_la contain a column named product_number you will get this error.
What you need to do is to disambiguate your request by using table aliases such as the letters p and m in this rewritten query:
SELECT m.*
FROM `df_all_orders_merged_la` m
LEFT JOIN `product_database_la` p
ON m.`Product_Number` = p.`Product_Number`
WHERE p.`Product_Number` IS NULL;
Here I have used the letters p and m to refer to particular tables in the FROM and JOIN section. Also notice what I did to the SELECT clause: I'm no longer pulling in anything from table p. (But I did this only to get rid of the immediate ambiguity ...)
In general, I recommend that you specify the fields that you actually need in your result, rather than relying on the SELECT * crutch in any of its many forms. Document, in the query, exactly what columns you really need, and which tables they come from:
SELECT m.order_id, p.product_number, p.product_name [...]
Actually you are joining the table and a new temporary table is formed on the basis of Product_Number column in the both parent table so in new table two duplicate column will be formed.
Now since you are calling select * from ... , due to * in select query mysql doesn't know which particular Product_Number column you want to select as having two same name column in this new table is ambigious to it without any specification(like Product_Name of which parent table you want to see) because astrick(*) replace * in query with all the column names in the specified table(without specifying any parent table and it becomes sometimes problematic in case of joins when there is/are column name having same name but different parent tables).
Think about if you are calling just by column name, how does mysql will know which parent table column it should show as there name can be same but there is possibility that content can be different or in just different order and it can't possibly know in this way which particular type column you want. maybe it shows you some random column among duplicates but you wanted some other column having same name!
so the way to select in this type of cases is by specifying the name of all the unique column just by column name(you can specify parent table too but it doesn't matter) without specifying parent table and for duplicate column you will have to call like
select `df_all_orders_merged_la`.`Product_Number,`product_database_la`.`Product_Number` from (... your join query here ...);
i.e specify the parent table of the column from which you want to show like above, again you can give alias to parent tables for your convenience
I have created a table with several columns and a related form. In that form, I have created a combobox to show the columns of the table in differents rows in combobox.
I have 3 fields in my table: product1, product2 and product3 for a same order I have named with a number. When I create the combobox, values show in 3 differents columns in the same row so I can just select the data from the first row and column. But what I need is the field's data displayed in different rows instead showing data in different columns.
I researched in forums, and I read It can be solved with a join query and selecting that in row source in combobox menu, but I have tried it and I got the same result. I donĀ“t know what am I doing wrong. I'd appreciate your help.
Thanks in advance.
You have to pivot the results
this is an example of how to restructure using sub queries bad data designs
SELECT *
FROM
(
SELECT QualifierID -- <--- this is a unique identifier
,
(
SELECT QualifierText
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS QualifierText
,
(
SELECT [ExampleText]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS ExampleText
,
(
SELECT [WhenModified]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS WhenModified
FROM [QDB].[dbo].[Qualifier] q -- this is a table alias
) z -- this is the alias fo the subquery combining everything together.
I have two tables. The first is named master_list. It has these fields: master_id, item_id, name, img, item_code, and length. My second table is named types_join. It has these fields: master_id and type_id. (There is a third table, but it is not being used in the queries. It is more for reference.) I need to be able to combine these two tables so that I can sift the results to only show certain ones but part of the information to sift is on one table and the other part is on the other one. I don't want duplicate answers.
For example say I only want items that have a type_id of 3 and a length of 18.
When I use
SELECT * FROM master_list LEFT JOIN types_join ON master_list.master_id=types_join.master_id WHERE types_join.type_id = 3 AND master_list.length = 18"
it finds the same thing twice.
How can I query this so I won't get duplicate answers?
Here are the samples from my tables and the result I am getting.
This is what I get with an INNER JOIN:
BTW, master_id and name both only have unique information on the master_list table. However, the types_join table does use the master_id multiple times later on, but not for Lye. That is why I know it is duplicating information.
If you want unique rows from master_list, use exists:
SELECT ml.*
FROM master_list ml
WHERE ml.length = 18 AND
EXISTS (SELECT 1
FROM types_join tj
WHERE ml.master_id = tj.master_id AND tj.type_id = 3
);
Any duplicates you get will be duplicates in master_list. If you want to remove them, you need to provide more information -- I would recommend a new question.
Thank you for the data. But as you can see enter link description here, there is nothing wrong with your query.
Have you tried create an unique index over master_id, just to make sure that you do not have duplicated rows?
CREATE UNIQUE INDEX MyMasterUnique
ON master_list(master_id);
Here is my Database structure (basic relations):
I'm attempting to formulate a one-line query that will populate the clients_ID, Job_id, tech_id, & Part_id and return back all the work orders present. Nothing more nothing less.
Thus far I've struggled to generate this Query:
SELECT cli.client_name, tech.tech_name, job.Job_Name, w.wo_id, w.time_started, w.part_id, w.job_id, w.tech_id, w.clients_id, part.Part_name
FROM work_orders as w, technicians as tech, clients as cli, job_types as job, parts_list as part
LEFT JOIN technicians as techy ON tech_id = techy.tech_name
LEFT JOIN parts_list party ON part.part_id = party.Part_Name
LEFT JOIN job_types joby ON job_id = joby.Job_Name
LEFT JOIN clients cliy ON clients_id = cliy.client_name
Apparently, once all the joining happens it does not even populate the correct foreign key values according to their reference.
[some values came out as the actual foreign key id, not even
corresponding value.]
It just goes on about 20-30 times depending on largest row of a table that I have (one of the above).
I only have two work orders created, So ideally it should return just TWO Records, and columns, and fields with correct information. What could I be doing wrong? Haven't been with MySQL too long but am learning as much as I can.
Your join conditions are wrong. Join on tech_id = tech_id, not tech_id = tech_name. Looks like you do this for all your joins, so they all need to be fixed.
I really don't follow the text of your question, so I am basing my answer solely on your query.
Edit
Replying to your comment here. You said you want to "load up" the tech name column. I assume you mean you want tech name to be part of your result set.
The SELECT part of the query is what determines the columns that are in the result set. As long as the table where the column lives is referenced in the FROM/JOIN clauses, you can SELECT any column from that table.
Think of a JOIN statement as a way to "look up" a value in one table based on a value in another table. This is a very simplified definition, but it's a good way to start thinking about it. You want tech name in your result set, so you look it up in the Technicians table, which is where it lives. However, you want to look it up by a value that you have in the Work Orders table. The key (which is actually called a foreign key) that you have in the Work Orders table that relates it to the Technicians table is the tech_id. You use the tech_id to look up the related row in the Technicians table, and by doing so can include any column in that table in your result set.
I'm sure there are a ton of ways to do this, but right now I'm struggling to find the way that will work properly given the data.
I basically have a table containing duplicates which have additional fields tied to them and source details that take priority over others. So basically I added a "priority" field to my table which I then updated based on source priority. I now need to select the distinct records to populate my "unique" records table (which I'll then apply unique key constraint to prevent this from happening again on the field required!)....
So I have basically, something like this:
Select phone, carrier, src, priority
from dbo.mytable
So basically I need to pull distinct on phone in order of priority (1,2,3,4, etc), and basically pull the rest of the other data along with it and still keep UNIQUE on phone.
I've tried a few things using sub-select from the same table with min(priority) value, but outcome still doesn't seem to make sense. Any help would be greatly appreciated. Thanks!
EDIT I need to dedupe from the same table, but I can populate a new table with the uniques if needed based on my select statement to pull the uniques. This is in MSSQL, but figured anyone with SQL knowledge could answer.
For example, let's say I have the following rows:
5556667777, ATT, source1, 1
5556667777, ATT, source2, 2
5556667777, ATT, source3, 3
I need to pull uniques based on priority 1 first..... the problem is, I need to remove any all other dupes from the table based on the priority order without ending up with the same phone number twice again. Make sense?
So you're saying the combination (phone, priority) is unique in the existing table, and you want to select the rows for which the priority is smallest?
SELECT mytable.phone, mytable.carrier, mytable.src
FROM mytable
INNER JOIN (
SELECT phone, MIN(priority) AS minpriority
FROM mytable
GROUP BY phone
) AS minphone
ON mytable.phone = minphone.phone
AND mytable.priority = minphone.minpriority