I have the following dataset:
I am trying to convert the table on the left into the table on the right. I have several duplicates of orders with the same name but different products sold. I would like to combine the rows so it shows just one orderID. I've tried joining the table to itself based on order but I must be doing something wrong. Do you guys have any suggestions? this is probably super easy but I am not proficient with SQL yet. Thank you in advance.
If there is at most one value in each column, you can use group by:
select order, name, max(product1) as product1, max(product2) as product2,
max(product3) as product3
from lefttable
group by order, name;
That said, I suspect that the table on the left is the result of a query on the data. You probably simply need the right aggregation for that query.
Also, if you have more than one value in any column for an order, you can still do this, but the query is a bit more complicated.
Related
This code cannot be executed, showed an error like
column "o.total_amt_usd" must appear in the GROUP BY clause or be used in an aggregate function
SELECT a.name, MIN(o.total_amt_usd)
FROM accounts a
JOIN orders o ON a.id = o.account_id
GROUP BY a.name
ORDER BY o.total_amt_usd
LIMIT 3
But after I use an alias, it worked:
SELECT a.name, MIN(o.total_amt_usd) small
FROM accounts a
JOIN orders o ON a.id = o.account_id
GROUP BY a.name
ORDER BY small
LIMIT 3
Could anybody explain a little bit about this, please?
Both logically make sense to me. But one of them is not working.
Thanks a lot.
You can't order by o.total_amt_usd since it's not available in the result set (after grouping on name).
You need to order by a grouped field or using an aggregate function like MIN. In this case you'll want to order by MIN(o.total_amt_usd) which is essentially what you are doing after using the alias in the order-clause.
Think about what you're asking of the first query and perhaps try a visual example by hand.
Imagine a table with just 4 rows, John has two rows with amounts of 50 and 20, Bob has two rows with amounts of 30 and 60.
You are asking for each unique name and the corresponding minimum amount, so the results are naturally John:20, Bob:30.
By asking to order your results by referring specfically to every row's Total (and not the aggregated total) you are saying, order my two rows by looking at all four rows, which means John could go both before Bob and after Bob given 20 is less than 30 and 50 is greater than 30.
You might look at the data visually and see the "correct" order, however for the query engine this makes no sense, you can only prioritise the resulting two rows based on their aggregated values, therefore you must order by those aggregated values, either using that column's alias or using the same expression. You cannot order by non-aggregated columns.
Basically, I'm trying to retrieve only 1 record from a table based on catalog_no and packing_list_no. However, the table I'm retrieving the information from has additional details that I don't need but makes the 1 record I need into 3 distinct records.
I tried summing and grouping the info, but I'm still getting 3 records instead of 1.
Any ideas of how to solve this issue?
Your GROUP BY groups your result on the columns quantity picked, quantity shipped and weight shipped. A different value in any of those columns will result into a different row.
You can drop the GROUP BY clause all together, if the desirable result is the packing list and catalog no that you have specified. You can use the GROUP BY clause to columns that you do not use sum to group the result set.
SELECT catalog_no, sum(qty_picked), sum(qty_shipped), sum(weight_shipped), packing_list_no, bay_no, carrier_code, tracking_no FROM oeorder_shipping
WHERE packing_list_no='CP12618525' AND catalog_no='437656500'
GROUP BY bay_no, carrier_code, tracking_no;
Good morning/afternoon! I was hoping someone could help me out with something that probably should be very simple.
Admittedly, I’m not the strongest SQL query designer. That said, I’ve spent a couple hours beating my head against my keyboard trying to get a seemingly simple three way join working.
NOTE: I'm querying a Vertica DB.
Here is my query:
SELECT A.CaseOriginalProductNumber, A.CaseCreatedDate, A.CaseNumber, B.BU2_Key as BusinessUnit, C.product_number_desc as ModelNumber
FROM pps_sfdc.v_Case A
INNER JOIN reference_data.DIM_PRODUCT_LINE_HIERARCHY B
ON B.PL_Key = A.CaseOriginalProductLine
INNER JOIN reference_data.DIM_PRODUCT C
ON C.product_line_code = A.CaseOriginalProductLine
WHERE B.BU2_Key = 'XWT'
LIMIT 20
I have a view (v_Case) that I’m trying to join to two other tables so I can lookup a value from each of them. The above query returns identical data on everything EXCEPT the last column (see below). It's like it's iterating through the last column to pull out the unique entries, sort of like a "GROUP BY" clause. What SHOULD be happening is that I get unique rows with specific "BusinessUnit" and "ModelNumber" for that record.
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 1
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 2
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 3
DUMEPRINT 5/2/2014 8:56:27 AM 3002845327 JJT Product 4
I modeled my solution after this post:
How to deal with multiple lookup tables for beginners of SQL?
What am I doing wrong?
Thank you for any help you can provide.
Data issue. General rule in trouble shooting these is the column that is distinct (in this case C.product_number_desc as ModelNumber) for each record is generally where the issue is going to be...and why I pointed you towards dim_product.
If you receive duplicates, this query below will help identify if this table is giving you the issues. Remember key in this statement can be multiple fields...whatever you are joining the table on:
Select key,count(1) from table group by key having count(1)>1
Other options for the future...don't assume it's your code, duplicates like this almost always point towards dirty data (other option is you are causing cross joins because keys are not correct). If you comment out the 'c' table and the column referred to in the select clause, you would have received one row...hence your dupes were coming from the 'c' table here.
Good luck with it
I need to find the sum of the prices of a number of products, however the prices are stored in a different table to products that need pricing.
But, there is a catch, it needs to select these items based on criteria from a third table too.
So, I need the sum of the price of all products in Table 1 where CutID in Table 2 = 001.
Table 1 and Table 2 are linked on SCID, one to many respectively.
If this makes no sense tell me and I will try to clarify?
Thanks,
Bob P
Based on your question, I don't think there's a need for VBA. Excel formulas should be sufficient.
Add a few columns to your primary table. In these columns, use vlookup() to get all your information in one place, including the criteria.
If you only need to sum based on one criteria, use sumif(). If there's multiple criteria, use sumproduct().
Generally, with Access, I initially try to work with something as close a possible to a standard SQL query for ease of maintenance and portability. This ran for me in Access 2010:
SELECT Products.ProductID, Sum(Prices.Price) AS PriceSum
FROM Prices INNER JOIN (Critera INNER JOIN Products ON Critera.SCID = Products.SCID) ON Prices.ProductID = Products.ProductID
WHERE Critera.CutID="001"
GROUP BY Products.ProductID;
Please let us know if that works with your data (I'm not sure of your column names, either).
I have two tables. One with a list of shops and their ID's (shop_id)
and one with a list of employees with the ID (shop_id) of the shop they work at.
I have to print out each employee with a certain position form a certain shop.
My query is normally correct but I seem to get an error like tblEmployees.
Normally my query would look something like.
SELECT tblEmployees.Name, tblEmployees.Surname, tblShops.shop_id
FROM tblEmployees, tblShops
GROUP BY tblEmployees.shop_id
HAVING tblEmployees.shop_id = tblShops.shop_id;
Normally I get an error saying something like:
tblEmployees.Name is not part of an aggregate function.
What I want to know is if it would solve my problem if I put every column that gives me this error under the GROUP BY statement. Or is there another way of fixing this error without it affecting the result I need to get from this query.
Drop the GROUP BY and HAVING clauses. You aren't aggregating here. You want to be joining your tables.
SELECT tblEmployees.Name, Surname, tblShops.shop_id
FROM tblEmployees JOIN tblShops
ON tblEmployees.shop_id=tblShops.shop_id