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
Related
I am learning about linked server in sql server and I linked the oracle database in sqlserver and ran a query successfully against the oracle linked database as below:
SELECT *
FROM OPENQUERY(DB_ORCL,'select Name, ID from OdataLink.patients')
I have a table in sql server as well that I want to use that data in a where clause of OPENQUERY above but do not know how.
Here is the query:
Select ID from PatientTable
so based on the above sql queries, I want something like this:
SELECT *
FROM OPENQUERY(DB_ORCL,'select Name, ID from OdataLink.patients')
where "--ID in OPENQUERY above" IN (Select ID from PatientTable)
Or
SELECT *
FROM OPENQUERY(DB_ORCL,'select Name, ID from OdataLink.patients where ID in (--Select ID from PatientTable)')
Update:
I tested the resolution Stu provided and it almost worked but I cannot call the column field in openquery within the external where clause
Screenshot of the error
Try an exists correlation
select *
from OpenQuery(DB_ORCL,'select Name, ID from OdataLink.patients') q
where exists (
select * from dbo.PatientTable p
where p.Id = q.Id
);
To join the tables, I am using the following query.
SELECT *
FROM(select user as uservalue1 FROM [projectname.FullData_Edited]) as FullData_Edited
JOIN (select user as uservalue2 FROM [projectname.InstallDate]) as InstallDate
ON FullData_Edited.uservalue1=InstallDate.uservalue2;
The query works but the joined table only has two columns uservalue1 and uservalue2.
I want to keep all the columns present in both the table. Any idea how to achieve that?
#legacySQL
SELECT <list of fields to output>
FROM [projectname:datasetname.FullData_Edited] AS FullData_Edited
JOIN [projectname:datasetname.InstallDate] AS InstallDate
ON FullData_Edited.user = InstallDate.user
or (and preferable)
#standardSQL
SELECT <list of fields to output>
FROM `projectname.datasetname.FullData_Edited` AS FullData_Edited
JOIN `projectname.datasetname.InstallDate` AS InstallDate
ON FullData_Edited.user = InstallDate.user
Note, using SELECT * in such cases lead to Ambiguous column name error, so it is better to put explicit list of columns/fields you need to have in your output
The way around it is in using USING() syntax as in example below.
Assuming that user is the ONLY ambiguous field - it does the trick
#standardSQL
SELECT *
FROM `projectname.datasetname.FullData_Edited` AS FullData_Edited
JOIN `projectname.datasetname.InstallDate` AS InstallDate
USING (user)
For example:
#standardSQL
WITH `projectname.datasetname.FullData_Edited` AS (
SELECT 1 user, 'a' field1
),
`projectname.datasetname.InstallDate` AS (
SELECT 1 user, 'b' field2
)
SELECT *
FROM `projectname.datasetname.FullData_Edited` AS FullData_Edited
JOIN `projectname.datasetname.InstallDate` AS InstallDate
USING (user)
returns
user field1 field2
1 a b
whereas using ON FullData_Edited.user = InstallDate.user gives below error
Error: Duplicate column names in the result are not supported. Found duplicate(s): user
Don't use subqueries if you want all columns:
SELECT *
FROM [projectname.FullData_Edited] as FullData_Edited JOIN
[projectname.InstallDate] as InstallDate
ON FullData_Edited.uservalue1 = InstallDate.uservalue2;
You may have to list out the particular columns you want to avoid duplicate column names.
While you are at it, you should also switch to standard SQL.
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;
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.
I am retreiving values from a Table which in turn i am comparing with values from another table using the SQL keywords 'EXCEPT'
My query looks something like follows
SELECT DISTINCT TDC_TREE_FAMILY_CLASSIFICATION AS DPC_Level1,
TDC_TREE_CLASSIFICATION AS DPC_Level2,
TDC_TREE_SUB_CLASSIFICATION AS DPC_Level3
FROM TD_DATA_PACK_CONTENTS
EXCEPT
SELECT DPC_Level1,DPC_Level2,DPC_Level3 FROM DATA_PACK_CATEGORIES
ORDER BY DPC_Level1
Now this query works fine . What i want to do is save the results in a single string variable.
So I declare 3 temps variables to save the values of DPC_Level1,Lvl2,Lvl3 and then i can join them into a single string variable.
So i modify my Query like this.
SELECT DISTINCT #m_DPC_Level11=TDC_TREE_FAMILY_CLASSIFICATION AS DPC_Level1
,#m_DPC_Level2=TDC_TREE_CLASSIFICATION AS DPC_Level2,
,#m_DPC_Level13=TDC_TREE_SUB_CLASSIFICATION AS DPC_Level3
FROM TD_DATA_PACK_CONTENTS
EXCEPT
SELECT DPC_Level1,DPC_Level2,DPC_Level3 FROM DATA_PACK_CATEGORIES
ORDER BY DPC_Level1
But this throws the error
'A SELECT statement that assigns a value to a variable must not be
combined with data-retrieval operations'
. How i resolve this issue. I am using SQL Server 2008
I would go for a subquery
select #m_DPC_LEvel11 = DPC_Level1,
#m_DPC_Level2 = DPC_Level2,
#m_DPC_Level13 = DPC_Level3,
FROM
(SELECT DISTINCT TDC_TREE_FAMILY_CLASSIFICATION AS DPC_Level1,
TDC_TREE_CLASSIFICATION AS DPC_Level2,
TDC_TREE_SUB_CLASSIFICATION AS DPC_Level3
FROM TD_DATA_PACK_CONTENTS
EXCEPT
SELECT DPC_Level1,DPC_Level2,DPC_Level3 FROM DATA_PACK_CATEGORIES
ORDER BY DPC_Level1) s