Author |
Topic |
noks
Starting Member
9 Posts |
Posted - 2007-05-10 : 05:07:05
|
Good day.Am new with SQL server and i use SQL Server 2005i have 3 tables (1 = customers, 2 = orders, 3 = keys) both these tables are linked by the key tableI wanna be able to "view" (as am only allowed to view) the last three orders for every customer.I tried using the top predicate/ clause with a group by & count but unfortunately am getting no where.Please kindly help |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-10 : 05:15:08
|
SELECT CustomerID FROM (SELECT CustomerID, OrderNumber, ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate DESC) AS RecID) AS d WHERE RecID BETWEEN 1 AND 3Peter LarssonHelsingborg, Sweden |
|
|
noks
Starting Member
9 Posts |
Posted - 2007-05-10 : 05:37:34
|
thanks Peso. I tried that as well (saw it in 1 of the previuosly posted threads) but i keep getting the following error msg"The OVER SQL construct or statement is not supported." |
|
|
drewsalem
Constraint Violating Yak Guru
304 Posts |
Posted - 2007-05-10 : 05:39:33
|
Peter should be banned from posting until at least 3 others have a go and post first! Drew"It's Saturday night; I've got no date, a two litre bottle of Shasta, and my all-Rush mix tape... LET'S ROCK." |
|
|
drewsalem
Constraint Violating Yak Guru
304 Posts |
Posted - 2007-05-10 : 05:41:58
|
Are you using the Query Designer or the Query window?Drew"It's Saturday night; I've got no date, a two litre bottle of Shasta, and my all-Rush mix tape... LET'S ROCK." |
|
|
noks
Starting Member
9 Posts |
Posted - 2007-05-10 : 05:42:58
|
quote: Originally posted by chiragkhabaria which version of SQL SERVER you are using??Chiraghttp://chirikworld.blogspot.com/
Microsoft SQL Server Management Studio Express (2005) |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-10 : 05:48:16
|
[code] SELECT CustomerID FROM (SELECT CustomerID, OrderNumber, ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate DESC) AS RecID From YourTable ) AS dWHERE RecID BETWEEN 1 AND 3[/code]Chiraghttp://chirikworld.blogspot.com/ |
|
|
noks
Starting Member
9 Posts |
Posted - 2007-05-10 : 05:48:44
|
quote: Originally posted by drewsalem Are you using the Query Designer or the Query window?Drew"It's Saturday night; I've got no date, a two litre bottle of Shasta, and my all-Rush mix tape... LET'S ROCK."
Query Designer |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-10 : 05:51:13
|
declare @cust table (cust_id int, cust_name varchar(50))insert @custselect 124, 'raja' union allselect 145, 'vikram' union allselect 160, 'visu' declare @order table (orid int, ordername varchar(20))insert @order select 1, 'hamam' union all select 2, 'rex' union allselect 3, 'bril' union allselect 4, 'ink' union allselect 5, 'food' union allselect 6, 'lays' union allselect 7, 'snacks'declare @keys table (cust_id int, orid int, dt datetime)insert @keysselect 124, 3, '02/01/2007' union allselect 160, 6, '03/02/2007' union allselect 124, 4, '03/03/2007' union allselect 124, 1, '04/06/2007' union allselect 145, 2, '05/05/2007' union allselect 124, 5, '05/02/2007' union allselect 160, 2, '04/05/2007' union allselect 160, 2, '02/02/2007' union allselect 160, 2, '05/02/2007' Select b.cust_id, b.cust_name, b.orid, b.ordername, b.dt From(Select *, count = (Select Count(Distinct(dt)) from @keys where dt >= a .dt and cust_id = a.cust_id) from(Select k.cust_id, k.orid,k.dt, o.ordername, c.cust_name from @keys k left outer join @order o on k.orid = o.orid left outer join @cust c on c.cust_id = k.cust_id )as a ) as b where b.count between 1 and 3 order by b.cust_id, b.dt descSorry if u have 2005...u may have easy way to do |
|
|
drewsalem
Constraint Violating Yak Guru
304 Posts |
Posted - 2007-05-10 : 05:52:24
|
I don't think Query Designer supports it. Try Peters suggestion in the Query window.Drew"It's Saturday night; I've got no date, a two litre bottle of Shasta, and my all-Rush mix tape... LET'S ROCK." |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-10 : 05:54:12
|
Yes query designer doest support this kind of queries..BOL Says.. quote: This dialog box may appear when you enter or edit an SQL statement in the SQL pane; you then move to another pane, verify the query, or attempt to execute the query; and one of the following conditions applies: The SQL statement is incomplete or contains one or more syntax errors.The SQL statement is valid but is not supported in the graphical panes (for example, a Union query).The SQL statement is valid but contains syntax specific to the data connection you are using.
Chiraghttp://chirikworld.blogspot.com/ |
|
|
noks
Starting Member
9 Posts |
Posted - 2007-05-10 : 06:12:35
|
WOW this is the first time ever am using windows query (didn't even know it existed ) and it's working. Now i have to see if it's gonna work if i select everything i needThanks guys and thank you for responding so quicklyMuch appreciatedI can tell dat if i stick wit u, my sql server skills sure will improve |
|
|
noks
Starting Member
9 Posts |
Posted - 2007-05-10 : 06:58:39
|
SELECT CustomerCode FROM (SELECT Customercode, OrderDate, OrderNumber, ROW_NUMBER() OVER (PARTITION BY CustomerCode ORDER BY OrderDate DESC) AS RecIDfrom Customer,PurchaseOrder) AS d , purchaseorderWHERE RecID BETWEEN 1 AND 3this is working perfectly fine but now i wanna see the orderdate as well but when i add it in the first select statement i get an error message sayin that orderdate is ambigious . please kindly help |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-10 : 07:32:55
|
Somthing like this ?? SELECT * FROM ( SELECT C.Customercode, O.OrderDate, O.OrderNumber, ROW_NUMBER() OVER ( PARTITION BY C.CustomerCode ORDER BY O.OrderDate DESC ) AS RecID from Customer C Left Outer Join Keys K on K.Cust_ID = C.Cust_ID Left Outer Join Orders O on O.Order_ID = K.Order_ID) AS d WHERE RecID BETWEEN 1 AND 3 Chiraghttp://chirikworld.blogspot.com/ |
|
|
noks
Starting Member
9 Posts |
Posted - 2007-05-10 : 08:16:54
|
quote: Originally posted by chiragkhabaria Somthing like this ?? SELECT * FROM ( SELECT C.Customercode, O.OrderDate, O.OrderNumber, ROW_NUMBER() OVER ( PARTITION BY C.CustomerCode ORDER BY O.OrderDate DESC ) AS RecID from Customer C Left Outer Join Keys K on K.Cust_ID = C.Cust_ID Left Outer Join Orders O on O.Order_ID = K.Order_ID) AS d WHERE RecID BETWEEN 1 AND 3 Chiraghttp://chirikworld.blogspot.com/
works perfectly.Thank u so much |
|
|
|