Click here to Skip to main content
15,891,513 members
Home / Discussions / Database
   

Database

 
GeneralRe: SQL Server script to find table dependencies Pin
Mycroft Holmes13-Apr-12 4:22
professionalMycroft Holmes13-Apr-12 4:22 
GeneralRe: SQL Server script to find table dependencies Pin
jujiro13-Apr-12 4:55
jujiro13-Apr-12 4:55 
GeneralRe: SQL Server script to find table dependencies Pin
Mycroft Holmes13-Apr-12 12:54
professionalMycroft Holmes13-Apr-12 12:54 
Questioncan not get union recordset from two Sql Server2K store procedure Pin
Zhenjie Fu11-Apr-12 22:28
Zhenjie Fu11-Apr-12 22:28 
AnswerRe: can not get union recordset from two Sql Server2K store procedure Pin
Eddy Vluggen12-Apr-12 9:02
professionalEddy Vluggen12-Apr-12 9:02 
GeneralRe: can not get union recordset from two Sql Server2K store procedure Pin
Zhenjie Fu12-Apr-12 22:31
Zhenjie Fu12-Apr-12 22:31 
GeneralRe: can not get union recordset from two Sql Server2K store procedure Pin
Eddy Vluggen13-Apr-12 7:25
professionalEddy Vluggen13-Apr-12 7:25 
QuestionSQL Top n By Group Pin
Richard.Berry10011-Apr-12 16:47
Richard.Berry10011-Apr-12 16:47 
I am trying to retrieve the product, price, and date_despatched for the last three orders (date_despatched) for each product.

There are two tables involved:

opheadm (Sales Order Header)
date_despatched
order_no


opdetm (Sales Order Detail)
order_no
product
net_price

Desired Result
product_a 2.999 2012/03/23
product_a 2.600 2012/02/01
product_a 3.812 2012/01/23
product_b 1.456 2012/04/01
product_b 4.786 2012/02/13
product_b 2.563 2012/10/10
...

I have seen two approaches, but cant get either to work (since in this case there needs to be a join on the header and detail tables)

a) OVER PARTITION BY (below is nowhere near correct - just to show example)
SELECT product_code, price, date_entered
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY product_code ORDER BY date_entered DESC) AS OrderedDate,*  FROM opdetm  )  AS ordlist
WHERE ordlist.OrderedDate<=3;


b) SELECT.. WHERE IN (SELECT TOP n...)
SELECT E.scheme_opdetm.product, scheme_opdetm.net_price, opheadm.date_despatched
FROM scheme_opdetm, scheme_opheadm AS E
WHERE E.date_despatched IN (SELECT TOP 3 E2.date_despatched from scheme_opdetm, scheme_opheadm AS E2 WHERE E.product = E2.product Order by E2.date_despatched DESC)
ORDER BY E.product, net_price DESC;


Questions:

a) Which approach is best? Why?

b) Please could someone assist with the query I need, and if possible a short explanation of how to construct this type of query and what the different parts do. I'd like to try to understand what I'm diong as opposed to just copying someones SQL Smile | :)

Thanks in advance
AnswerRe: SQL Top n By Group Pin
Corporal Agarn12-Apr-12 3:08
professionalCorporal Agarn12-Apr-12 3:08 
GeneralRe: SQL Top n By Group Pin
Richard.Berry10012-Apr-12 6:39
Richard.Berry10012-Apr-12 6:39 
GeneralRe: SQL Top n By Group Pin
Corporal Agarn12-Apr-12 6:50
professionalCorporal Agarn12-Apr-12 6:50 
GeneralRe: SQL Top n By Group Pin
Richard.Berry10012-Apr-12 7:10
Richard.Berry10012-Apr-12 7:10 
QuestionI have a problem in a SSIS solution !!! Pin
ma.amer11-Apr-12 2:30
ma.amer11-Apr-12 2:30 
AnswerRe: I have a problem in a SSIS solution !!! Pin
PIEBALDconsult11-Apr-12 3:15
mvePIEBALDconsult11-Apr-12 3:15 
GeneralRe: I have a problem in a SSIS solution !!! Pin
ma.amer11-Apr-12 3:24
ma.amer11-Apr-12 3:24 
GeneralRe: I have a problem in a SSIS solution !!! Pin
PIEBALDconsult11-Apr-12 14:38
mvePIEBALDconsult11-Apr-12 14:38 
GeneralRe: I have a problem in a SSIS solution !!! Pin
ma.amer24-Apr-12 13:02
ma.amer24-Apr-12 13:02 
GeneralRe: I have a problem in a SSIS solution !!! Pin
PIEBALDconsult24-Apr-12 13:29
mvePIEBALDconsult24-Apr-12 13:29 
AnswerRe: I have a problem in a SSIS solution !!! Pin
Mycroft Holmes11-Apr-12 12:50
professionalMycroft Holmes11-Apr-12 12:50 
GeneralRe: I have a problem in a SSIS solution !!! Pin
PIEBALDconsult11-Apr-12 14:39
mvePIEBALDconsult11-Apr-12 14:39 
QuestionWeird character conversion Pin
fd975010-Apr-12 22:21
professionalfd975010-Apr-12 22:21 
AnswerRe: Weird character conversion Pin
fd975011-Apr-12 2:19
professionalfd975011-Apr-12 2:19 
Newsconnection to database Pin
Micah David10-Apr-12 7:18
Micah David10-Apr-12 7:18 
GeneralRe: connection to database Pin
Richard MacCutchan10-Apr-12 8:33
mveRichard MacCutchan10-Apr-12 8:33 
QuestionNative paging on Sql Server Pin
SSEAR10-Apr-12 6:13
SSEAR10-Apr-12 6:13 

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.