Click here to Skip to main content
15,918,168 members
Home / Discussions / Database
   

Database

 
GeneralRe: sql Update command Error Pin
phowarso27-Feb-08 2:27
phowarso27-Feb-08 2:27 
AnswerRe: sql Update command Error Pin
buchstaben27-Feb-08 8:09
buchstaben27-Feb-08 8:09 
QuestionSQL server query Pin
Jaggariah26-Feb-08 15:53
Jaggariah26-Feb-08 15:53 
GeneralRe: SQL server query Pin
Christian Graus26-Feb-08 19:18
protectorChristian Graus26-Feb-08 19:18 
GeneralRe: SQL server query Pin
Usharva26-Feb-08 19:49
Usharva26-Feb-08 19:49 
GeneralRe: SQL server query Pin
Jaggariah26-Feb-08 19:53
Jaggariah26-Feb-08 19:53 
GeneralRe: SQL server query Pin
Usharva26-Feb-08 19:58
Usharva26-Feb-08 19:58 
GeneralQuery to get most recent event for each item Pin
Dan Neely26-Feb-08 10:44
Dan Neely26-Feb-08 10:44 
I a C# app that queries against an accessDB having the following tables:

tblItems<br />
	int ItemID	string ItemName	...<br />
	1		Item1		...<br />
	2		Item2		...<br />
	3		Item3		...<br />
	...<br />
<br />
tblEvents<br />
	int EventID	string EventName<br />
	1		Event1<br />
	2		Event2<br />
	3		Event3<br />
	4		Event4<br />
	....<br />
   <br />
<br />
lnkItemEvents<br />
	int ItemID	int EventID	DateTime TimeStamp	...<br />
	1		1		Jan 1			...<br />
	1		2		Jan 3			...<br />
	1		4		Jan 4			...<br />
	1		1		Jan 8			...<br />
	1		2		Jan 13			...<br />
	2		1		Jan 2			...<br />
	2		4		Jan 3			...<br />
	2		3		Jan 8			...<br />
	...<br />
	<br />
<br />


What I need is a query to return something that looks like this.

ItemName	EventName	TimeStamp	... about 10 more fields from a half dozen tables.<br />
Item1		Event2		Jan 13		...<br />
Item2		Event3		Jan 8		...<br />
...<br />


Based on a bit of googling I believe that to make it work I'll need to use sub queries. One ran on lnkItemEvents to get the most recent time stamp for each item. And a second using the first as a sub query and adding the ItemName, EventName, etc fields to create the final result. I have the first working:

SELECT        MAX(TimeStamp) AS Expr1, ItemID<br />
FROM            lnkItemEvents<br />
GROUP BY ItemID


But when I try and add it to the WHERE clause of the second I get a syntax parsing error at the WHERE and a second error "You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field." If I remove ITemID from the sub query, I only get the single most recent event. Not the most recent event for each item.

To keep it simple while trying to debug, in my truncated main query I haven't added the SQL to use the ID fields to get the corresponding Names, or the other fields of data I need.
<br />
SELECT        ItemID, EventID, TimeStamp<br />
FROM            lnkItemEvents<br />
WHERE        (SELECT        MAX(TimeStamp) AS Expr1, ItemID<br />
                          FROM            lnkItemEvents<br />
                          GROUP BY ItemID)<br />
GROUP BY ItemID, EventID, TimeStamp


If i remove the WHERE clause it does return the selected fields of every row in the table. I do have a otherwise complete, working, version of the main query that gets everything except for the data linked via lnkItemEvents.

Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop.
-- Matthew Faithfull

GeneralRe: Query to get most recent event for each item Pin
Usharva26-Feb-08 19:56
Usharva26-Feb-08 19:56 
GeneralRe: Query to get most recent event for each item Pin
Dan Neely27-Feb-08 2:44
Dan Neely27-Feb-08 2:44 
GeneralRe: Query to get most recent event for each item Pin
Dan Neely27-Feb-08 4:01
Dan Neely27-Feb-08 4:01 
GeneralC# ado.net Pin
Member 462942126-Feb-08 10:26
Member 462942126-Feb-08 10:26 
GeneralRe: C# ado.net Pin
Nouman Bhatti27-Feb-08 2:30
Nouman Bhatti27-Feb-08 2:30 
GeneralRe: C# ado.net Pin
Member 462942128-Feb-08 9:00
Member 462942128-Feb-08 9:00 
Question[Message Deleted] Pin
vinodh.s26-Feb-08 7:12
vinodh.s26-Feb-08 7:12 
AnswerRe: use of datetime datatype Pin
pmarfleet26-Feb-08 8:33
pmarfleet26-Feb-08 8:33 
General[Message Deleted] Pin
vinodh.s26-Feb-08 9:07
vinodh.s26-Feb-08 9:07 
GeneralRe: use of datetime datatype Pin
pmarfleet26-Feb-08 9:20
pmarfleet26-Feb-08 9:20 
GeneralRe: use of datetime datatype Pin
vinodh.s26-Feb-08 9:25
vinodh.s26-Feb-08 9:25 
GeneralRe: use of datetime datatype Pin
pmarfleet26-Feb-08 9:43
pmarfleet26-Feb-08 9:43 
GeneralRe: use of datetime datatype Pin
vinodh.s26-Feb-08 9:48
vinodh.s26-Feb-08 9:48 
GeneralThey will not let me hit the database until the last minute. Help !!! [modified] Pin
crystal915426-Feb-08 5:52
crystal915426-Feb-08 5:52 
GeneralRe: They will not let me hit the database until the last minute. Help !!! Pin
SimulationofSai26-Feb-08 7:24
SimulationofSai26-Feb-08 7:24 
GeneralRe: They will not let me hit the database until the last minute. Help !!! Pin
crystal915426-Feb-08 8:13
crystal915426-Feb-08 8:13 
GeneralHelp! ADO . NET connecting to SQL Server 2005 Pin
D_Ana26-Feb-08 5:43
D_Ana26-Feb-08 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.