How to edit values in sql access with Distinct? - sql

How to edit values in sql access with Distinct?
SELECT DISTINCT ProdutosOrcamento.IDProdutoOrc, ProdutosOrcamento.Produto, ProdutosOrcamento.PrecoOrc, ProdutosOrcamento.IPIOrc, detOrcamentos.IDOrcamento
FROM ProdutosOrcamento LEFT JOIN detOrcamentos ON ProdutosOrcamento.IDProdutoOrc=detOrcamentos.IDProdutoOrc
WHERE (((detOrcamentos.IDOrcamento)=[Formulários]![Orcamentos]![IDOrcamento]));

If you have a DISTINCT (or GROUP BY for that matter) in your SQL statement, MS Access will consider the query to be read-only.
A possible work-around is to make a new simple editable SELECT query that usea an IN() statement on the ID field.
EDIT:
SELECT * FROM ProdutosOrcamento WHERE ProdutosOrcamento.IDProdutoOrc IN
(SELECT ProdutosOrcamento.IDProdutoOrc
FROM ProdutosOrcamento LEFT JOIN detOrcamentos ON ProdutosOrcamento.IDProdutoOrc=detOrcamentos.IDProdutoOrc
WHERE (((detOrcamentos.IDOrcamento)=[Formulários]![Orcamentos]![IDOrcamento])))

Related

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.

How to find duplicates in a table using Access SQL?

I try to use an SQL query in Access but it doesn't work. Why?
SELECT * FROM table
EXCEPT
SELECT DISTINCT name FROM table;
I have a syntax error in FROM statement.
MS Access does not support EXCEPT keyword. You can try using the LEFT JOIN like this:
select t1.* from table t1 left join table t2 on t1.name = t2.name
EDIT:
If you want to find the duplicates in your table then you can try this:
SELECT name, COUNT(*)
FROM table
GROUP BY name
HAVING COUNT(*) > 1
You can also refer: Create a Query in Microsoft Access to Find Duplicate Entries in a Table and follow the steps to find the duplicates in your table.
First open the MDB (Microsoft Database) containing the table you want
to check for duplicates. Click on the Queries tab and New.
This will open the New Query dialog box. Highlight Find Duplicates
Query Wizard then click OK.
Now highlight the table you want to check for duplicate data. You can
also choose Queries or both Tables and Queries. I have never seen a
use for searching Queries … but perhaps it would come in handy for
another’s situation. Once you’ve highlighted the appropriate table
click Next.
Here we will choose the field or fields within the table we want to
check for duplicate data. Try to avoid generalized fields.
Name the Query and hit Finish. The Query will run right away and pop
up the results. Also the Query is saved in the Queries section of
Access.
Depending upon the selected tables and fields your results will look
something similar to the shots below which show I have nothing
duplicated in the first shot and the results of duplicates in the
other.
use HAVING COUNT(name) > 1 clause
SELECT * FROM Table1
WHERE [name] IN
(SELECT name, Count(name)
FROM Table1
GROUP BY name
HAVING COUNT(name)>1)
You can use LEFT JOIN or EXISTS
LEFT JOIN
SELECT DISTINCT t1.NAME FROM table1 as t1
LEFT JOIN table2 as t2 on t1.name=t2.name
WHERE t2.name is null
;
NOT EXITS
SELECT T1.NAME FROM table1 as t1 where not exists
(SELECT T2.NAME FROM table2 as t2 where t1.name=t2.name)
Whether Access supports except or not is one issue. The other is that you are not using it properly. You have select * above the word except and select name below. That is not valid sql. If you tried that in SQL Server, your error message would be All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Issue constructing SQL join query

I'm trying to construct an SQL query that selects data from both tables but it doesn't seem to be working:
SELECT DISTINCT name,$price
FROM room
WHERE capacity>=$partySize
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date';
What am I doing wrong?
Move the WHERE clause down to the bottom:
SELECT DISTINCT name,$price
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>=$partySize
Definitely read some more SQL tutorials. You cannot use two WHERE conditions in one query:
SELECT DISTINCT name,'$price'
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>='$partySize';
Also, variables in the query should be quoted.
try
SELECT DISTINCT r.name,$price
FROM room r, room_booking b
WHERE r.capacity>=$partySize
and r.room_id=b.room_id
and r.date_booked<>'$us_date';
What's $price? is it a value? if variable try r.$price

Need help forming SQLite join query

I've been able to make a query using MS Access 2010 that does just what I want but I am having trouble doing so in SQLite3. Here is the Access SQL
SELECT pubacc_lo.*
FROM pubacc_en
LEFT JOIN pubacc_lo ON pubacc_en.call_sign = pubacc_lo.call_sign;
Basically it selects all of the columns in the pubacc_lo table and the rows where the call_sign fields are equal between the tables. This does not select any of the pubacc_en data into the final query in MS Access.
Playing around in SQLite 3, the closest I've gotten was
SELECT * FROM PUBACC_LO, PUBACC_EN WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
But this statement selects all of the data in the EN table along with the LO table (cross join?). I've tried some left outer joins but haven't had any luck. Any tips would be appreciated!
You should be able to use the same query as you got from Access. SQLite3 does support left outer joins.
As for your query, if you only want the fields from the LO table, then ask for that in your SELECT clause like this:
SELECT PUBACC_LO.*
FROM PUBACC_LO, PUBACC_EN
WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
but the problem here is that it will only return call_signs with entries in both tables, while the outer join from access will return all rows from PUBACC_EN irrespective of whether there is a corresponding PUBACC_LO entry..
SELECT pubacc_lo.* FROM PUBACC_LO, PUBACC_EN WHERE UBACC_en.call_sign=PUBACC_LO.call_sign
If you only want to select pubbac_lo's field, this is what you can use.

problem with IN clause in SQL

We have observed that there seems to be a maximum number of ids/variables which one can pass in the IN clause of SQL as comma separated values. To avoid this we are storing all the ids in a table and doing a SELECT within the IN clause. This however means extra database operations to store and retrieve ids. Is there any other way to use IN without SELECT?
Regards,
Sameer
In SQL Server 2008 you can declare a table variable and pass it to your query from the client or between the procedures.
For a modest number of values I would not have thought an IN (SELECT ..) would be that expensive on any rdbms.
You could INNER JOIN you IDs table or just break down the INs:
WHERE X IN (123,456 ...)
OR X IN (789,987 ...)
...
Agree with Alex
Instead of
Select * From TableA Where ID IN (Select ID from IDTable)
Use
Select * From TableA
INNER JOIN IDTable ON TableA.ID = IDTable.ID
The join will automatically filter the IDs for you.
If you put it in atable why are you still using the in clause, why not just join to the table?