Showing posts with label related. Show all posts
Showing posts with label related. Show all posts

Wednesday, March 7, 2012

Obtaining the latest record by date

Hi,

I have two tables, one stores items, and another one that stores history for the items from first table. They are related by a foreign key from items table.

In one of my queries, I need to obtain the latest history entry from table 2. For this I use MAX(RecordDate) aggregate. Another way that I know is:

SELECT TOP(1) RecordDate, HistoryID, ....
FROM ProductHistory
WHERE (SerialNumber = '20070101000010')
ORDER BY RecordDate DESC, HistoryID DESC

The query is part of a larger query where I obtain more data from other tables.

The reason I prefer the second option is that, at times I have multiple entries for the same item on the same date (no time info). In such cases, the data returned is the latest entry (HistoryID) in to the system of the rows with same date, thanks to ORDER BY clause. Another reason is that I can select any columns, whereas in the first query type I need to use subquery to obtain other fields after I find my relevant row.

My question is, is there any problem using the second method that I am not aware of? Is this method reliable (TOP(1))? I appreciate if you can guide me to the right method. Thanks!

You're absolutely fine to use TOP in the way that you have done.

The most common mistake when using TOP is to omit an ORDER BY clause where it should really be included, which can make the results unpredicatable and inconsistent. As you have included an ORDER BY clause then you'll be fine.

Chris

|||

Thank you for your answer Chris. My main concern was whether TOP was applied before ORDER BY or to the result of the query at the end. It seems it is applied after all the filters are applied, showing only the portion of the result matching the whole query. Please corrent me if I am wrong. I will mark your response as the answer.

Migrant

Obtaining Login ID from page

Hello,

By using DBCC Page I am able to exemine data entered to row and other changes related to the rows. What I want to know is; is it possible to find out (and how) which Login ID commited these change directly from pages or where do I need to look to find it out?

Any guidance is appreciated....

Two parts to this answer:

1. We don't document the on-disk structures except by special licensing/NDA agreement with ISVs

2. That information is not on the page anyway.

Monday, February 20, 2012

Objects related to DB

Hi Folks,
I suppose should exist a way to find the relationship between all external
objects (logins, dts, jobs, ...) with one database. I mean, the objects
related\attached with database.
That is because we have to take off several databases including objects
related.
Of course we have the respective documentation of the relationships but in
the past several engineers did mistakes and they missed the removement of
some object related...So, we got sql server with dirty objects.
The idea is to make a kind of script that checks all external objects
related to database before to delete it.
Thanks in advance.HI ,
You could try a documentation tool like my SQL Scribe Documentation Builder.
There a 14 day trial . Document the entire system , the documentation
details logins, jobs and other server elements as well as the schema details
http://www.geckoware.com.au/Content.aspx?Doc_id=1001
-
Greg O
SQL Server Documentation the easy way
SQL 2000, SQL 2005 the best is back
http://www.geckoware.com.au/Content.aspx?Doc_id=1001
"Jaisol" <jaisol@.QUITARMEhotmail.com> wrote in message
news:eNYZB%23WrHHA.4180@.TK2MSFTNGP04.phx.gbl...
> Hi Folks,
> I suppose should exist a way to find the relationship between all external
> objects (logins, dts, jobs, ...) with one database. I mean, the objects
> related\attached with database.
> That is because we have to take off several databases including objects
> related.
> Of course we have the respective documentation of the relationships but in
> the past several engineers did mistakes and they missed the removement of
> some object related...So, we got sql server with dirty objects.
> The idea is to make a kind of script that checks all external objects
> related to database before to delete it.
> Thanks in advance.
>

Objects related to DB

Hi Folks,
I suppose should exist a way to find the relationship between all external
objects (logins, dts, jobs, ...) with one database. I mean, the objects
related\attached with database.
That is because we have to take off several databases including objects
related.
Of course we have the respective documentation of the relationships but in
the past several engineers did mistakes and they missed the removement of
some object related...So, we got sql server with dirty objects.
The idea is to make a kind of script that checks all external objects
related to database before to delete it.
Thanks in advance.HI ,
You could try a documentation tool like my SQL Scribe Documentation Builder.
There a 14 day trial . Document the entire system , the documentation
details logins, jobs and other server elements as well as the schema details
http://www.geckoware.com.au/Content.aspx?Doc_id=1001
-
Greg O
SQL Server Documentation the easy way
SQL 2000, SQL 2005 the best is back
http://www.geckoware.com.au/Content.aspx?Doc_id=1001
"Jaisol" <jaisol@.QUITARMEhotmail.com> wrote in message
news:eNYZB%23WrHHA.4180@.TK2MSFTNGP04.phx.gbl...
> Hi Folks,
> I suppose should exist a way to find the relationship between all external
> objects (logins, dts, jobs, ...) with one database. I mean, the objects
> related\attached with database.
> That is because we have to take off several databases including objects
> related.
> Of course we have the respective documentation of the relationships but in
> the past several engineers did mistakes and they missed the removement of
> some object related...So, we got sql server with dirty objects.
> The idea is to make a kind of script that checks all external objects
> related to database before to delete it.
> Thanks in advance.
>