Access query in SQL Server broken - sql

I have a UNION query that was working in a Microsoft Access environment. The error I am getting in SQL Server is: "Each GROUP BY expression must contain at least one column that is not an outer reference". The query is in the below format:
SELECT tblA.ProjectID,
tblB.PersonnelID,
"TeamMember" AS ProjectRole
FROM tblA INNER JOIN tblB ON (tblA.ProjectID = tblB.ProjectID)
AND (tblA.ProjectID = tblB.ProjectID)
GROUP BY tblA.ProjectID, tblB.PersonnelID, "TeamMember"
HAVING ((Not (tblB.PersonnelID) Is Null) AND ((Sum(tblB.Hours))>0))
How to get this query working for SQL Server?

afaik having only applies to aggragate functions like sum, so i moved the not null thing into the where.also the quotes need to be singles. i have also put the join criteria in a single parenthesis. try this:
SELECT tblA.ProjectID,
tblB.PersonnelID,
'TeamMember' AS ProjectRole
FROM tblA
INNER JOIN
tblB
ON (tblA.ProjectID = tblB.ProjectID
AND tblA.ProjectID = tblB.ProjectID)
where tblB.PersonnelID is not null
GROUP BY
tblA.ProjectID
, tblB.PersonnelID
, 'TeamMember'
HAVING Sum(tblB.Hours) > 0

Related

Handling Duplicate Column Name Issue in MariaDB Count Query

I am getting an error on the query I'm running to get a count in MariaDB. This is the error:
Error Code: 1060. Duplicate column name 'id_number'
And this is my SQL code:
SELECT COUNT(*) as count FROM (
SELECT * FROM ((cr.customers
INNER JOIN (progress_notes_details
INNER JOIN progress_notes ON progress_notes_details.progress_note_id = progress_notes.id_number)
ON customers.id_number = progress_notes.c_id)
INNER JOIN open_balances ON progress_notes_details.id_number = open_balances.progress_notes_detail_id)
INNER JOIN
customer_payer_xref ON customers.id_number = customer_payer_xref.c_id
WHERE
(((progress_notes_details.qb_isbillable) IS NULL
OR (progress_notes_details.qb_isbillable) <> 1)
AND ((progress_notes_details.date_of_visit) BETWEEN coverage_start AND coverage_end)
AND ((progress_notes_details.dynamics_status) = 3)
AND ((customer_payer_xref.payer_id) = 23)
AND ((customer_payer_xref.primary_secondary_account_type) = 1))
) AS qdat
Can this be resolved via aliases? If so, it's unclear to me where to add them. In the main query? In the subquery?
Also, to clarify, I just inherited this code - and yes, it's bracket-happy.
Alias customers table as c and use it as c.id_number at one of the places it will remove that duplicate error as this is the only table you are using multiple times with idnumber hence duplicate
Remove the outer query:
SELECT COUNT(*)
FROM ((cr.customers . . .
Clearly, you have tables with the same column name. This causes a problem with SELECT *.
All the parentheses are probably not needed for the JOINs as well.

How to order a recordest returned from a query by a field created within that same query (MS ACCESS)

The Query (For this question I do not think that you need to see schema):
SELECT Agencies.AgencyName, (SELECT DISTINCT MAX(Invoices.CostsTotal) FROM Invoices WHERE Contracts.ContractID = Invoices.ContractID) AS MaxInvoice
FROM Contracts
LEFT JOIN Agencies ON Contracts.AgencyID = Agencies.AgencyID
ORDER BY MaxInvoice DESC;
How do we order the recordset returned from a query by a field created within that same query?
I have seen the function FIELDS(INDEX) ? But this does not exist in access? Also not sure that it would even work. In this instance I want to sort the recordset by the MaxInvoice field.
MS Access prompts me to enter a parameter value for MaxInvoice when I attempt to run this query
You can write parent SELECT which wraps your current SELECT.
Like this:
SELECT * FROM (
SELECT Agencies.AgencyName,
(SELECT DISTINCT MAX(Invoices.CostsTotal) FROM Invoices
WHERE Contracts.ContractID = Invoices.ContractID) AS MaxInvoice
FROM Contracts LEFT JOIN Agencies
ON Contracts.AgencyID = Agencies.AgencyID
) AS ContractsLargestInvoice
ORDER BY ContractsLargestInvoice.MaxInvoice DESC;
Most SQL dialects support the use of aliases in the ORDER BY. But MS Access is further from SQL standards than most databases.
I would suggest you rewrite the query to move Invoices into the FROM clause -- using aggregation to get what you want:
SELECT a.AgencyName, MAX(i.CostsTotal) AS MaxInvoice
FROM (Contracts as c LEFT JOIN
Agencies as a
ON c.AgencyID = a.AgencyID) LEFT JOIN
Invoices as i
ON i.ContractID = c.ContractID
GROUP BY a.AgencyName
ORDER BY MAX(i.CostsTotal) DESC;
It seems strange that you are using a LEFT JOIN and choosing a field from the second table and not the first. This could be NULL.

SQL Fieldname vs Aliasname

I have been trying to figure out why the following SQL works
SELECT c_Supplier.Supplier_ID AS A_ID, c_Supplier.Name, c_Supplier.RFC, c_Supplier_Direccion.Description, c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN (c_Supplier_Direccion LEFT JOIN c_Supplier_Phone ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (c_Supplier.Supplier_ID=1);
But when I try to use the aliasname (A_ID) in the WHERE clause, I got an error
SELECT c_Supplier.Supplier_ID AS A_ID, c_Supplier.Name, c_Supplier.RFC, c_Supplier_Direccion.Description, c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN (c_Supplier_Direccion LEFT JOIN c_Supplier_Phone ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (A_ID=1);
Any ideas?
I don't understand your question. This is a reasonably formed SQL query:
SELECT c_Supplier.Supplier_ID AS Entidad_ID, c_Supplier.Name,
c_Supplier.RFC, c_Supplier_Direccion.Description,
c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN
(c_Supplier_Direccion LEFT JOIN
c_Supplier_Phone
ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID
) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (c_Supplier.Supplier_ID = 1);
(I would recommend table aliases for readability, but that is a separate issue.)
It has no alias called A_ID anywhere in the query, so there is no reason to ever expect a reference to A_ID to work (unless it is a column in one of the tables).
And, SQL doesn't allow the re-use of table aliases in the SELECT where they are defined or the WHERE clause. This is not an MS Access limitation; it is how the SQL language is defined.
If you want to do so in MS Access, you can use a subquery and reference the table alias in the outer query.

RedShift SQL subquery with Inner join

I am using AWS Redshift SQL. I want to inner join a sub-query which has group by and inner join inside of it. When I do an outside join; I am getting an error that column does not exist.
Query:
SELECT si.package_weight
FROM "packageproduct" ub "clearpathpin" cp ON ub.cpipr_number = cp.pin_number
INNER JOIN "clearpathpin" cp ON ub.cpipr_number = cp.pin_number
INNER JOIN (
SELECT sf."AWB", SUM(up."weight") AS package_weight
FROM "productweight" up ON up."product_id" = sf."item_id"
GROUP BY sf."AWB"
HAVING sf."AWB" IS NOT NULL
) AS si ON si.item_id = ub.order_item_id
LIMIT 100;
Result:
ERROR: column si.item_id does not exist
It's simply because column si.item_id does not exist
Include item_id in the select statement for the table productweight
and it should work.
There are many things wrong with this query.
For your subquery, you have an ON statement, but it is not joining:
FROM "productweight" up ON up."product_id" = sf."item_id"
When you join the results of this subquery, you are referencing a field that does not exist within the subquery:
SELECT sf."AWB", SUM(up."weight") AS package_weight
...
) AS si ON si.item_id = ub.order_item_id
You should imagine the subquery as creating a new, separate, briefly-existing table. The outer query than joins that temporary table to the rest of the query. So anything not explicitly resulted in the subquery will not be available to the outer query.
I would recommend when developing you write and run the subquery on its own first. Only after it returns the results you expect (no errors, appropriate columns, etc) then you can copy/paste it in as a subquery and start developing the main query.

Trouble with parenthesis in MS Access for SQL Inner Joins

Have tried the below SQL in MS Access but cannot seem to get it working, anyone got a better idea?
SELECT top 4 Student.STUDENT_DEGREE, Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM (((Student)
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;
Whenever i run this i just get the error:
Syntax error in FROM clause
Group is a reserved word. Enclose that name in square brackets to avoid confusing the db engine. You can also assign an alias for the table name.
FROM
(Student
INNER JOIN Result
ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN [Group] AS g
ON Result.RESULT_GROUP_ID = g.GROUP_ID
I had to guess which tables contain those fields in the last ON clause. If you set up the joins in Design View of the Access query designer, it will help you get the names right. It will also add the parentheses which the db engine requires for any query which includes more than one join.
Also qualify the table sources for the field names in your SELECT list and elsewhere in the query. Here again, the query designer can supply the correct names for you.
Remove the extra set of parentheses around Student:
SELECT top 4 Student.STUDENT_DEGREE,Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM ((Student
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;