SQL v Access table selection alias - sql

I'm trying to convert some SSMS SQL to Access SQL and am finding the whole process rather frustrating! I have SQL that works perfectly well in SSMS but cannot get it to work in Access. The SQL is relatively simple. All it does is update one field in a table based on a count of items in a second table.
update Summary_Complaint_Table set period1_count = sql.mycount from
(
select t2.category,count(t2.category)as mycount
from complaints t2
where t2.date_received between #1/9/2015# and #23/12/2016#
group by category
) as sql
where Summary_Complaint_Table.category = sql.category
The inner Select works perfectly well as does the outer update when I substitute sql.count and sql_category with values.
The error I'm getting is
Syntax error (missing operator) in query expression 'sql.mycount from
(select t2.category,count(t2.category)as mycount from complaints t2
where t2.date_received between #1/9/2015# and #23/12/2016#
group by category) as sql'
The original SSMS (SQL server 2005) syntax that works is
update #temp set period1_count = sql.mycount
from
(
select t2.category,count(t2.category)as mycount
from complaints t2
where t2.date_received between #period1_from and #period1_to
group by category
) as sql
where
#temp.category = sql.category

Access cannot update data in one SQL if it contains aggregation/group by functions in any part of SQL. As workaround you can use DCount function instead of Count()..Group By.

I believe you need a space and an "as":
'sql.mycount from
(select t2.category, count(*) as mycount from complaints as t2
where t2.date_received between #2015/09/01# and #2016/12/23#
group by category) as sql'
Also, the dd/mm/yyyy date sequence will not work where dd is 12 or less.

Related

Update largest date, matching two fields

tables
Hi, I'm looking to update the last column in a blank table. Picture shows input and desired output. Trying to pick the largest date where workorder and state match.
I've tried a couple different codes:
UPDATE mytable
SET mytable.orderstartdate = MAX(table2.earliestdate)
FROM mytable as table2
WHERE (mytable.workorder = table2.workorder AND
mytable.state = table2.state)
;
"Syntax Error (missing operator) in query expression 'MAX(table2.earliestdate) FROM mytable as table2'."
UPDATE mytable
SET mytable.orderstartdate = (
SELECT max(earliestdate)
FROM mytable as table2
WHERE (mytable.workorder = table2.workorder AND
mytable.state = table2.state)
)
;
"Operation must use an updateable query"
Edit - click tables link for image.
Write PL/SQL Code.
First, select DISTINCT WorkOrder and State and capture in variables.
Now, Iterate the list and Write a query to get max date i.e. max(date) using work_order and State in where clause. Capture the
date.
Now, In the same loop write update query setting max(date) and workorder and State in where clause.
UPDATE table A
SET A.orderstartDate = (SELECT max(earliestdate)
FROM table B
WHERE A.WorkOrder = B.WorkOrder
GROUP BY WorkOrder)
not sure if access supports correlated subqueries but if it does...
and if not...
UPDATE table A
INNER JOIN (SELECT WorkOrder, max(OrderStartDate) MOSD
FROM Table B
GROUP BY WorkOrder) C
ON A.WorkOrder = C.workOrder
SET A.OrderStartDate = C.MOSD
Check database open mode, it may be locked for editing, or you may have no permission to to file.

Oracle SQL: The equivalent way to execute the following expression from PostgreSQL?

The following thread successfully showed how to use a an UPDATE SET and FROM clause together, to update an entire row of a specific column with a value derived from a different table.
When executing following expression in Oracle SQL:
UPDATE territory2_t
SET total_sales_person = t.total_count
FROM (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
WHERE territoryid = t.salesterritoryid;
Oracle states: SQL Error: ORA-00933: SQL command not properly ended
In Oracle you can use merge to do the job:
merge into territory2_t
using (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
on (territoryid = t.salesterritoryid)
when matched then
update SET total_sales_person = t.total_count
This can be done with MERGE (in Oracle and most other DB systems - those that implement the MERGE statement from the SQL Standard). MERGE is the preferred solution in most cases.
There is a misconception that this cannot be done with an UPDATE statement in Oracle. I show below how it can be done - not to encourage its use (MERGE is better), but to show that UPDATE can be used as well. This is the transformation of the Postgre SQL the OP solicited in the original post.
update ( select t2.total_sales_person, t.total_count
from territory2_t t2 inner join
( select salesterritoryid, count(*) as total_count
from salesperson_t
group by salesterritoryid
) t
on t2.territoryid = t.salesterritoryid
)
set total_sales_person = total_count;

Convert SQl query to MS Access

SELECT *
FROM data WHERE (object,TCH_Traffic) IN
( SELECT object, MAX(TCH_Traffic)
FROM data
GROUP BY object
)
Can this above query be modified to run with MS Access database.
I am getting error to "revise the select statement of " prompt as attached in MS access 2010 db.
Just use a correlated subquery:
SELECT *
FROM data
WHERE TCH_Traffic = (SELECT MAX(d2.TCH_Traffic)
FROM data as d2
WHERE d2.object = data.object
);
MS Access' default Jet/ACE SQL Engine can process a subquery in a WHERE condition using IN clause. However, subquery must return one column value. Consider the following adjustment:
SELECT *
FROM data WHERE (object) IN
( SELECT object
FROM data
GROUP BY object
HAVING TCH_Traffic = MAX(TCH_Traffic)
);
However, you can simply use a derived table with INNER JOIN and avoid WHERE clause subquery. Also, same derived table query can be saved as a stored query object in MS Access and explicitly referenced in join clause:
SELECT *
FROM data
INNER JOIN
( SELECT object, MAX(TCH_Traffic) As maxTraffic
FROM data
GROUP BY object
) As dT
ON data.object = dT.object
AND data.TCH_Traffic = dt.maxTraffic

SELECT query to return a row from a table with all values set to Null

I need to make a query but get the value in every field empty. Gordon Linoff give me the clue to this need here:
SQL Empty query results
which is:
select t.*
from (select 1 as val
) v left outer join
table t
on 1 = 0;
This query wors perfectly on PostgreSQL but gets an error when trying to execute it in Microsoft Access, it says that 1 = 0 expression is not admitted. How could it be fixed to work on microsoft access?
Regards,
If the table has a numeric primary key column whose values are non-negative then the following query will work in Access. The primary key field is [ID].
SELECT t2.*
FROM
myTable AS t2
RIGHT JOIN
(
SELECT TOP 1 (ID * -1) AS badID
FROM myTable AS t1
) AS rowStubs
ON t2.ID = rowStubs.badID
This was tested with Access 2010.
I am offering this answer here, even though you didn't think it worked in my edit to your original question. What is the problem?
select t.*
from (select max(col) as maxval from table as t
) as v left join
table as t
on v.val < t.col;
You can use the following query, but it would still need a little "manual coding".
EDITS:
Actually, you do not need the SWITCH function. Modified query below.
Removed the reference to Description column from one line. Still, you would need to use a Text column name (such as Description) in the last line of the query.
For example, the following query would work for the Months table:
select Months.*
from Months
RIGHT OUTER JOIN
(select "" as DummyColumn from Months) Blank_Data
ON Months.Description = Blank_Data.DummyColumn; --hardcoded Description column

RODBC, SQL Order By clause + field ID = Order conflict

Does this make sense? I otherwise don't see the error.
Using RODBC, R returns a 'Could not SQLExecDirect' error for a sqlQuery statement issued to a table containing a field ID = Order. The SQL otherwise works. I can however read the entire table to a df using sqlFetch (see below).
The target db is on SQL Server.
Example of table structure:
Taxon_Id = c(3000,3001,3002)
Group_Id = c(6,5,5)
Type = c('Fish','Fish','Fish')
Order = c('Petromyzontidae','Acipenseridae','Clupeidae')
Family = c('Petromyzontidae','Acipenseridae','Clupeidae')
txn = data.frame(Taxon_Id,Group_Id,Type,Order,Family)
Example of SQL issued to table:
txn2<-as.data.frame(sqlQuery(channel, paste('SELECT T.Taxon_Id,
T.GroupId,
T.Type,
T.Order,
T.Family
FROM Taxon T
ORDER BY 1
')) )
sqlFetch reads all table fields without error.
txn<-as.data.frame(sqlFetch(channel,"Taxon"))
Thanks for your comments.
This is your query:
SELECT T.Taxon_Id, T.GroupId, T.Type, T.Order, T.Family
FROM Taxon T
ORDER BY 1
In SQL (in general) and SQL Server (in particular), the word Order is a reserved word. You need to escape it with either double quotes or square braces:
SELECT T.Taxon_Id, T.GroupId, T.Type, T.[Order], T.Family
FROM Taxon T
ORDER BY 1