473,805 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return Max Value from SubQuery

Hi,
I'm trying to outer join to a maximum date value using a subquery in
order to return company information and the last activity date
associated. The basic working "sub" query is:

SELECT actcomp.company _id, MAX(act.due_dat e)
FROM oncd_activity_c ompany AS actcomp, oncd_activity AS act
WHERE actcomp.activit y_id = act.activity_id
GROUP BY company_id

The overall (abbreviated) query I'm trying to insert this select into
is:

SELECT oncd_company.co mpany_id,
oncd_company.co mpany_name,
act.due_date
FROM oncd_company
LEFT OUTER JOIN oncd_activity_c ompany ON (oncd_company.c ompany_id =
oncd_activity_c ompany.company_ id)
LEFT OUTER JOIN (SELECT actcomp.company _id, MAX(act.due_dat e)

FROM oncd_activity_c ompany AS actcomp, oncd_activity AS act

WHERE actcomp.activit y_id = act.activity_id

GROUP BY company_id) ON
(oncd_activity_ company.company _id = actcomp.company _id)
I'm receiving an "invalid syntax near keyword ON" error (highlight
appears on the period in "oncd_activity_ company.company _id").
Any help would be appreciated!
Thanks,
Chris.

Apr 21 '07 #1
1 11364
Chris H (ch********@bro adreachpartners inc.com) writes:
LEFT OUTER JOIN (SELECT actcomp.company _id, MAX(act.due_dat e)

FROM oncd_activity_c ompany AS actcomp, oncd_activity AS act

WHERE actcomp.activit y_id = act.activity_id

GROUP BY company_id) ON
(oncd_activity_ company.company _id = actcomp.company _id)
I'm receiving an "invalid syntax near keyword ON" error (highlight
appears on the period in "oncd_activity_ company.company _id").
Aliases are mandatory for derived tables, thus you need something like:

GROUP BY company_id) AS act ON

Furthermore you cannot refer to tables in the derived table outside it,
you can only refer to your table as a whole. You must also give all columns
in the derived table a name. Here is a rewrite of your query that
demonstrates all this:

SELECT c.company_id, c.company_name, act.due_date
FROM oncd_company c
LEFT JOIN oncd_activity_c ompany ac ON c.company_id = ac.company_id
LEFT JOIN (SELECT ac.company_id, due_date = MAX(act.due_dat e)
FROM oncd_activity_c ompany ac
JOIN oncd_activity act ON ac.activity_id = act.activity_id
GROUP BY ac.company_id) act ON ac.company_id = act.company_id

Yes, the same aliases are used both inside and outside the derived table.
They
are however independent of each other. That's the great things with derived
tables, they are independent from the rest of the query.

If you are on SQL 2005, you might be able to get away with:

SELECT c.company_id, c.company_name,
MAX(ac.due_date ) OVER(PARITION BY c.company_id)
FROM oncd_company c
LEFT JOIN oncd_activity_c ompany ac ON c.company_id = ac.company_id

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 21 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2061
by: Nachi | last post by:
Hi, Urgent Help appreciated.... I am getting resultset with first condition and when try to get the resutlset from second condition i am getting the above error in SQL200. I know that i am getting more then one value in the second condition but even i am getting more then one value in my first statement
5
6596
by: Rod | last post by:
I have a client site where the code below has been working happily for at least four months. The site is using SQL Server 7. The code is ASP.NET Last week an error appeared related to the following SQL statement. INSERT INTO OrderItems (ClientID, ProductID, OrderHeaderID, Quantity, Dispatched, BackOrdered) SELECT ClientID, ProductID, 1371 AS OrderHeaderID, Quantity, Dispatched, BackOrdered FROM Basket WHERE RequisitionID = 1369 The...
8
7860
by: kingskippus | last post by:
I don't know if this is possible, but I haven't been able to find any information. I have two tables, for example: Table 1 (two columns, id and foo) id foo --- ----- 1 foo_a 2 foo_b
3
2923
by: Shals | last post by:
Hi, I'm using Form and a subform control within that form. Main form is getting data from one table(Buildings) and subform is getting data from another table(BldgDataByFloor). but both the form and subform are linked through a child field which is common in both the tables(BuildingNumber). Now, whenever I close the form I receive an error "Subquery returned more than 1 value. This is not permitted when the
1
1737
by: Arielle | last post by:
I don't even know if this is even possible but I figured it'd make my life a lot easier if it was. I have an organizational table with departments and subdepartments that has a chain of command listing in it. (I didn't design it, don't shoot me, I'm just having to fix it). What I need to do is find a department and all departments beneath it based on the passed in value of the department's ID. I'm right now keying off the chain of command...
0
621
by: Chris H | last post by:
Hi, I'm trying to outer join to a maximum date value using a subquery in order to return company information and the last activity date associated. The basic working "sub" query is: SELECT actcomp.company_id, MAX(act.due_date) FROM oncd_activity_company AS actcomp, oncd_activity AS act WHERE actcomp.activity_id = act.activity_id GROUP BY company_id
0
1282
by: fubaba | last post by:
hi, i execute a store procedure got Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.' message is there any one help with these codes below ....... SET @strSQL = (@strSQL + ' AND p.PropertyID NOT IN ( ' + (SELECT DISTINCT a.propertyID FROM propertyAvailability a ...
3
33768
by: Manikandan | last post by:
Hi, I have table with three columns as below table name:exp No(int) name(char) refno(int) I have data as below No name refno 1 a 2 b 3 c
9
3211
by: lenygold via DBMonster.com | last post by:
I have the following trigger: --#SET TERMINATOR ! CREATE TRIGGER CROSS_REFF_TRIG AFTER INSERT ON NEW_CATALOG REFERENCING NEW AS nnn FOR EACH ROW MODE DB2SQL BEGIN ATOMIC DECLARE reason VARCHAR(70); DECLARE OUT_SQLCODE1 INTEGER;
0
3072
prabirchoudhury
by: prabirchoudhury | last post by:
CRITERIA; +-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | CritCode | int(4) | NO | PRI | 0 | | | Description | varchar(150) | YES | | NULL | | | CritGroup | varchar(10) | YES | | NULL | | | Detail | varchar(30) | YES | | NULL | ...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.