Showing posts with label row. Show all posts
Showing posts with label row. Show all posts

Friday, March 30, 2012

How return the duplicate row?

Hello,

I would like return in a file the duplicates lines from a table.

Is it possible with a composant?

Or must I use a sql request.

But my problem with the request is the null value in the cell of the line. When U do a join and there is a null value in the row, there no resoult from the request.

How can U do to solve this problem?

Thank you

Regards.

Laurent Albiac

You could copy them into a plain file, for example, keep in mind that your statement is like this:

SELECT f1,f2,f3,count(*)

from table

group by f1,f2,f3

having count(*)> 1

I don't see where's the problem

|||

or you can try this

SELECT ROW_NUMBER() OVER (ORDER BY field1 ASC, field2 ASC) AS ROWID,
field1,field2 FROM table

|||

rabbiwan wrote:

Hello,

I would like return in a file the duplicates lines from a table.

Is it possible with a composant?

Or must I use a sql request.

But my problem with the request is the null value in the cell of the line. When U do a join and there is a null value in the row, there no resoult from the request.

How can U do to solve this problem?

Thank you

Regards.

Laurent Albiac

If you want to find duplicates use the Aggregate component to see how many rows there are of each combination. Anything that is plural - those are your duplicates.

-Jamie

sql

Friday, March 23, 2012

How many user current "connect" to a row in table.

Hi all...
Iam a newbie and i have one question want to ask experts ... .
I am current working on one App which have some user connect to database at the same time.
And i want to know: how many user connect or use (read , update) to one row in a table?
Could it be possible to know that? If you have one solution to solve this problem , please let me know :) ...
Could it be done by Software , T-SQL or anything ...., iam happy to know.
Thanks all.

Hello,

There's actually no straightforwarffd answer as this will depend upon the transaction isolation level which will in turn determine how many transactions may read a row as well as what state the data read will be in (dirty, comitted etc). Only one transaction can ever modify a row.

Have a look at http://www.microsoft.com/technet/prodtechnol/sql/2005/cncrrncy.mspx and http://msdn2.microsoft.com/en-US/library/ms173763.aspx as well as BOL for information.

Cheers,

Rob

|||Iam using SQL server 2000 and don't have intended to update to sql server 2005, so i can't use RLV.
I ask this question because i want to notify my clients when data was changed by users.
Is there any idea for me to solve this problem? if have , please show me more details
Thanks .:)|||Couldn't you try to use a trigger to make such a mecanism ?|||I think when a user changed data on the table and trigger on that table was fired then only that user was recieved alert from Server. Do you think so ?|||A trigger is fired on a statement level. When the trigger is fire you could write a message / row in a event table which is polled by the application. In another table you could save the information that the application of User A has received the notification but the application of user B did not. Althouh the trigger is fired once you could spread the *news* anout the changed data.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Thanks for your advise.
But i think traffic network is expensive resource so if my App keep polled the table which store info about what table has changed , it'll impact my performence which i want to avoid.
do you have anothet idea?
Thanks again.|||

Can you state the underlying purpose to what you are doing? It is not a natural thing to be interested in what row a user might be "connected" to. Now if you are talking about locking it for modification, that makes it a bit more clear, and you could use some form of lock to make that happen.

The thing is, SQL works in a very set oriented architecture, so you might touch a million rows when you think you end up only looking at one (it might require a scan of all values to find just one). SQL Server does nice things with locking, and there are techniques like optimistic locking that you can use to enhance concurrency, so if you could give a bit more information we might could suggest a better way.

|||

Louis Davidson wrote:

Can you state the underlying purpose to what you are doing? It is not a natural thing to be interested in what row a user might be "connected" to. Now if you are talking about locking it for modification, that makes it a bit more clear, and you could use some form of lock to make that happen.

The thing is, SQL works in a very set oriented architecture, so you might touch a million rows when you think you end up only looking at one (it might require a scan of all values to find just one). SQL Server does nice things with locking, and there are techniques like optimistic locking that you can use to enhance concurrency, so if you could give a bit more information we might could suggest a better way.

Thanks for your post :).

My purpose at

begin is Sql server or My App will notify my client when data was

changed for any client who current connect to that row.
For example:
-user A connect and load row R1 into dataset(vb.net).
-user B also connect to that table and load row R2
- R1 and R2 has some column which has the same data
==>

when user change data on some column and finished it,a litle later,

because user B did not know data was changed and continue changed that

data...so if i sum on total column(for example)then may be the value

was double and that is what i don't expect.

Sorry about my poor English, but if you understand what i mean , please give me your advise
Thanks.

Wednesday, March 21, 2012

How many rows before optimizer will use index

Hi
2 questions for you Gentlemen ;-)
1:
I think I have heared that SQL Server optimizer (always ?)
will use a table scan, if the row count of a table is
under a certain value even if there exist an - othervise
appropriate - index, simply because this is considered
faster.
How many rows do a table need to have before indexes are
considered usefull - does a roughly value exist?
(I think I read the number 8000 rows once)
Is there any differences between indextypes (CL, NCL,
single/multicolumn...)in the minimum "required" row count
2:
In a table A with many rows a column colA1 exists (with
a "code type" value), referencing table B with few rows
containing the list of codes.
Though a FK (and sometimes used in queries, joins etc.)
woundn't it be superfluous to index the FK column colA1,
because of the relative bad/low selectivity of the
different domain values (both if an fairly even spread of
the different few codes are assumed or if one of the codes
are in majority).
kind regards
Jakob Persson
Denmarkactually,
SQL Server will almost always try to use an index unless
there are atleast 6-12 rows, and the plan involves a NC
index and bookmark lookups
see:
http://www.sql-server-
performance.com/jc_sql_server_quantative_analysis1.asp
for more info
2. if you don't expect a plan to use the index, it may not
be necessary. i am assuming you do not delete from table B.
>--Original Message--
>Hi
>2 questions for you Gentlemen ;-)
>1:
>I think I have heared that SQL Server optimizer
(always ?)
>will use a table scan, if the row count of a table is
>under a certain value even if there exist an - othervise
>appropriate - index, simply because this is considered
>faster.
>How many rows do a table need to have before indexes are
>considered usefull - does a roughly value exist?
>(I think I read the number 8000 rows once)
>Is there any differences between indextypes (CL, NCL,
>single/multicolumn...)in the minimum "required" row count
>2:
>In a table A with many rows a column colA1 exists (with
>a "code type" value), referencing table B with few rows
>containing the list of codes.
>Though a FK (and sometimes used in queries, joins etc.)
>woundn't it be superfluous to index the FK column colA1,
>because of the relative bad/low selectivity of the
>different domain values (both if an fairly even spread of
>the different few codes are assumed or if one of the
codes
>are in majority).
>kind regards
>Jakob Persson
>Denmark
>.
>|||Jakob Persson wrote:
<snip>
> 2:
> In a table A with many rows a column colA1 exists (with
> a "code type" value), referencing table B with few rows
> containing the list of codes.
> Though a FK (and sometimes used in queries, joins etc.)
> woundn't it be superfluous to index the FK column colA1,
> because of the relative bad/low selectivity of the
> different domain values (both if an fairly even spread of
> the different few codes are assumed or if one of the codes
> are in majority).
This is only true if the values of colA1 are evenly distributed. If the
data distribution is skewed (for example, many 'A' but few 'B'), then
the index can still be beneficial when querying on 'B'.
And (as Joe mentioned), the index is also useful when deleting rows from
the referenced table.
Gert-Jan

Friday, March 9, 2012

How is the row filter clause and join filter should be....

Hi,
I am new to replication, I have the following tables in my
database, and I would like to replicate to other
subscriber using Merge Replication:
tblProduct (Product master)
ProdID (PK)
ProdGrp (FK to Product grouping)
ProdDesc
tblProdGrp (Product grouping)
ProdGrp (PK)
ProdGrpDesc
tblCustomer (Customer master)
CustID (PK)
BranchID (FK to branch master)
OfferGrp (FK to trade offer grouping)
CustName
tblBranch (Branch master)
BranchID (PK)
BranchName
tblTradeOfferGroup (Trade offer grouping)
OfferGrp (PK)
OfferGrpDesc
tblTradeOffer (Trade offer)
OfferID (PK)
OfferGrp (FK to Trade offer grouping)
StartEffDate
EndEffDate
tblTradeOfferProduct (Trade offer's product)
SeqID (PK)
ProdGrp (PK/FK)
OfferID (PK/FK)
OfferQty
*Note: SeqID + ProdGrp + OfferID is unique
I need all rows from tblProduct, tblProdGrp, tblBranch,
tblTradeOfferGroup, tblTradeOffer, tblTradeOfferProduct to
replicate to subscriber, but only single branch's customer
in tblCustomer at subscriber. How should I configure the
row filter and join filter in my Merge Replication?
Currently, I configure as following:-
Row Filter:
tblCustomer row filter tblCustomer.BranchID = '001'
tblProduct <publish all rows>
tblProdGrp <publish all rows>
tblBranch <publish all rows>
tblTradeOfferGroup <publish all rows>
tblTradeOffer <publish all rows>
tblTradeOfferProduct <publish all rows>
Join Filter:
Filtered table Table to filter
tblTradeOfferGroup tblTradeOffer
tblTradeOffer.OfferGrp = tblTradeOfferGroup.OfferGrp
tblTradeOffer tblTradeOfferProduct
tblTradeOfferProduct.OfferID = tblTradeOffer.OfferID
But, tblTradeOfferProduct not replicated over. A conflict
occurs saying FOREIGN KEY constraint etc. The weird case
is, when I synchorise again, the rows publisher's
tblTradeOfferProduct are deleted.
Please advice.
Thank you.
HKM
I think, replicating tblTradeOfferProduct should fix your problem.
Since tblTradeOfferProduct has relations to tblTradeOffer (and I believe to
tblProduct too ) it is better to replicate this table.
Otherwise yuo have to declare all those relations as "NOT FOR REPLICATION"
Since you dont have "NOT FOR REPLICATION" whenever that are constraint
violatins you will see merge failing with constraint violations.
And once some entries fail to propagate to the subscriber, in the next
merge, compensating actions (deletes for all the failed inserts) are made
and hence you will see that those rows vanish from the database.
You can either set the constraints to "NOT FOR REPLICATION" or replicate the
tblTradeOfferProduct table too. One of them should fix the problem
Hope that helps
--Mahesh
[ This posting is provided "as is" with no warranties and confers no
rights. ]
"HKM" <anonymous@.discussions.microsoft.com> wrote in message
news:04e801c49a0e$f4c9ebf0$a601280a@.phx.gbl...
> Hi,
> I am new to replication, I have the following tables in my
> database, and I would like to replicate to other
> subscriber using Merge Replication:
>
> tblProduct (Product master)
> --
> ProdID (PK)
> ProdGrp (FK to Product grouping)
> ProdDesc
> tblProdGrp (Product grouping)
> --
> ProdGrp (PK)
> ProdGrpDesc
> tblCustomer (Customer master)
> --
> CustID (PK)
> BranchID (FK to branch master)
> OfferGrp (FK to trade offer grouping)
> CustName
> tblBranch (Branch master)
> --
> BranchID (PK)
> BranchName
> tblTradeOfferGroup (Trade offer grouping)
> --
> OfferGrp (PK)
> OfferGrpDesc
> tblTradeOffer (Trade offer)
> --
> OfferID (PK)
> OfferGrp (FK to Trade offer grouping)
> StartEffDate
> EndEffDate
> tblTradeOfferProduct (Trade offer's product)
> --
> SeqID (PK)
> ProdGrp (PK/FK)
> OfferID (PK/FK)
> OfferQty
> *Note: SeqID + ProdGrp + OfferID is unique
> I need all rows from tblProduct, tblProdGrp, tblBranch,
> tblTradeOfferGroup, tblTradeOffer, tblTradeOfferProduct to
> replicate to subscriber, but only single branch's customer
> in tblCustomer at subscriber. How should I configure the
> row filter and join filter in my Merge Replication?
> Currently, I configure as following:-
> Row Filter:
> tblCustomer row filter tblCustomer.BranchID = '001'
> tblProduct <publish all rows>
> tblProdGrp <publish all rows>
> tblBranch <publish all rows>
> tblTradeOfferGroup <publish all rows>
> tblTradeOffer <publish all rows>
> tblTradeOfferProduct <publish all rows>
> Join Filter:
> Filtered table Table to filter
> tblTradeOfferGroup tblTradeOffer
> tblTradeOffer.OfferGrp = tblTradeOfferGroup.OfferGrp
> tblTradeOffer tblTradeOfferProduct
> tblTradeOfferProduct.OfferID = tblTradeOffer.OfferID
> But, tblTradeOfferProduct not replicated over. A conflict
> occurs saying FOREIGN KEY constraint etc. The weird case
> is, when I synchorise again, the rows publisher's
> tblTradeOfferProduct are deleted.
> Please advice.
> Thank you.
> HKM
>

Wednesday, March 7, 2012

How is 'change' defined ?

Can anyone tell me how a change is defined when replicating ? in particular, if an update is done to an existing row, but the value is actually the same as it was originally, does this count as a change and will the replication attempt to update/resolve
it or not ?
I am working in a scenario where it is quite important to minimise the time/quantity of replication so want to minimise any unnecessary replicating. Are there any techniques I can use to help with this ?
any change is replicated.
|||Bother!
But thanks
|||Sarah wrote:
> Can anyone tell me how a change is defined when replicating ? in particular, if an update is done to an existing row, but the value is actually the same as it was originally, does this count as a change and will the replication attempt to update/resolv
e it or not ?
> I am working in a scenario where it is quite important to minimise the time/quantity of replication so want to minimise any unnecessary replicating. Are there any techniques I can use to help with this ?
Basically the log is replicated. So any action that will make a commited
change in the log will be replicated.
Furthermore, if you are doing updated, there is a possiblity that
inserts/deletes are replicated instead.
Typically, if you have a table with a unique index and you make an
update which update this index, sql server will replace your update by
an insert/delete and this is what will be sent to subscribers
|||Hi Sarah,
If you have a lot of updates that are setting columns to the same value
repeatedly, you can minimize the the replication volume by adding additional
"where" criteria to your update statements. For example, if you have a
statement (psuedo-code)
set @.val1 = 'XXX'
set @.tblKey = '123'
update TABLEX set fieldA = @.val1 where tblKey= @.tblKey
and you execute this 100 times, it will generate 100 updates, and 100
replication transactions.
If you add a where clause like so
update TABLEX set fieldA = @.val1 where tblKey= @.tblKey and fieldA <> @.val1
ii will not update the row after the first time. This type of clause
generally doesn't add much overhead to the transaction.
Ed
"Sarah" <anonymous@.discussions.microsoft.com> wrote in message
news:A8B78B80-4AF0-43FE-B3A8-8678C072C3E2@.microsoft.com...
> Can anyone tell me how a change is defined when replicating ? in
particular, if an update is done to an existing row, but the value is
actually the same as it was originally, does this count as a change and
will the replication attempt to update/resolve it or not ?
> I am working in a scenario where it is quite important to minimise the
time/quantity of replication so want to minimise any unnecessary
replicating. Are there any techniques I can use to help with this ?

How ineffcient is this cursor?

This cursor is a select on a large table. Each update occurs once for
each row. Does SQL hold all changes in memory on dirty pages until the
cursor is done (or check point is reached) and is this cursor pinning a
large table in memory?
while @.@.FETCH_STATUS = 0
BEGIN
while @.ldt_min <= @.ldt_max
Begin
update Deal_Details_ZN
set {some values}
from Deal_Details_ZN a, Temp_Zainet_Detail b
where a.Xkey = b.Xkey
select @.ldt_min = @.ldt_min + 1
end
Fetch next from Deal_Cur into @.ls_xkey, @.ls_zkey,@.ls_dealxref,@.ls_flowdate
End
Close Deal_Cur
Deallocate Deal_Cur"Shaun Farrugia" <far!!!!ugia!!!s@.dte!!!!ener!gy.com> wrote in message
news:eEgF9zrhDHA.2452@.TK2MSFTNGP10.phx.gbl...
> This cursor is a select on a large table. Each update occurs once for
> each row. Does SQL hold all changes in memory on dirty pages until the
> cursor is done (or check point is reached) and is this cursor pinning a
> large table in memory?
Cursors are inherently less efficient that set-based DML, but the way people
use cursors contributes to their slowness. Not wrapping your cursor-driven
DML in a transaction is a common performance problem that can degrade the
performance fo cursor-driven solutions from "ok" to "terrible".
Changing data in an RDBMS involves 3 basic steps. Editing the in-memory
data pages, writing log entries, and flushing the log entries to disk. Of
these, flushing the log entries to disk is the most expensive since writes
to the disk are slow and serialized.
A single DML statement is always atomic, so SQL Server will make all the
data page changes and write all the log entries first, and then flush all of
the log entries to disk at once. This is much more efficient than flushing
the log entries after each row. But when people write cursor-driven DML,
this is often exactly what they do, and it appears that it's what you're
doing.
If you wrap the whole loop in a transaction it will be more efficient
because the changes will be made to the in-memory structres, but you won't
have to flush the log to disk after each update.
David|||David thanks for your reply.
Is this cursor fully loaded in memory as a data structure the size of
the SELECT that the cursor is based on?
We are having problems where there are no free pages on the server and
this locks the server up. I am trying to determine the cause of the
failure and things like this are suspicious.
So in this situation, every update gets logged and flushed to disk at
each UPDATE statement? Would this cause an issue with memory? Or would
holding a transaction open until the cursor runs take up more memory?
David Browne wrote:
> "Shaun Farrugia" <far!!!!ugia!!!s@.dte!!!!ener!gy.com> wrote in message
> news:eEgF9zrhDHA.2452@.TK2MSFTNGP10.phx.gbl...
>>This cursor is a select on a large table. Each update occurs once for
>>each row. Does SQL hold all changes in memory on dirty pages until the
>>cursor is done (or check point is reached) and is this cursor pinning a
>>large table in memory?
>
> Cursors are inherently less efficient that set-based DML, but the way people
> use cursors contributes to their slowness. Not wrapping your cursor-driven
> DML in a transaction is a common performance problem that can degrade the
> performance fo cursor-driven solutions from "ok" to "terrible".
> Changing data in an RDBMS involves 3 basic steps. Editing the in-memory
> data pages, writing log entries, and flushing the log entries to disk. Of
> these, flushing the log entries to disk is the most expensive since writes
> to the disk are slow and serialized.
> A single DML statement is always atomic, so SQL Server will make all the
> data page changes and write all the log entries first, and then flush all of
> the log entries to disk at once. This is much more efficient than flushing
> the log entries after each row. But when people write cursor-driven DML,
> this is often exactly what they do, and it appears that it's what you're
> doing.
> If you wrap the whole loop in a transaction it will be more efficient
> because the changes will be made to the in-memory structres, but you won't
> have to flush the log to disk after each update.
> David
>|||Did you try re-writing the logic without a cursor?
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as ugroup=microsoft.public.sqlserver
"Shaun Farrugia" <far!!!!ugia!!!s@.dte!!!!ener!gy.com> wrote in message
news:eEgF9zrhDHA.2452@.TK2MSFTNGP10.phx.gbl...
> This cursor is a select on a large table. Each update occurs once for
> each row. Does SQL hold all changes in memory on dirty pages until the
> cursor is done (or check point is reached) and is this cursor pinning a
> large table in memory?
>
> while @.@.FETCH_STATUS = 0
> BEGIN
> while @.ldt_min <= @.ldt_max
> Begin
> update Deal_Details_ZN
> set {some values}
> from Deal_Details_ZN a, Temp_Zainet_Detail b
> where a.Xkey = b.Xkey
> select @.ldt_min = @.ldt_min + 1
> end
> Fetch next from Deal_Cur into @.ls_xkey, @.ls_zkey,@.ls_dealxref,@.ls_flowdate
> End
> Close Deal_Cur
> Deallocate Deal_Cur
>|||No i just wanted to get an ide of what happens with memory consumption
with that particular cursor.
Tibor Karaszi wrote:
> Did you try re-writing the logic without a cursor?
>

Friday, February 24, 2012

How I get the random row from the table?

When I execute the following query several times, I get the same row if there is no new data inserted:
SELECT TOP 1 * FROM TableName
Is there a way to get a random row from the table? Thanks in advance.Declare @.value As Integer
set @.value = (RAND() * (select count(*) from table))+1

select *
from table
where id = @.value|||If I'm not mistaken this will require that there are no gaps whatsoever in the ID-field which is quite rare I think. You would have to make a while-loop and check if the ID exists I belive, and then loop for each ID that doesn't exist. have never done this myself so there might be a better way...|||The only issue with that is that you have to have an unique integer for your ID. Additionally your ID can not have gaps and must start at one (or is it zero).

Perhaps something like this??
----
Declare @.value As Integer
set @.value = (RAND() * (select count(*) from table))+1

executesql 'select top ' + @.value + ' from table order by XYZ'
----

then you need to select the first one or last one that is selected...

I don't know how you would do it though...

HTH|||If I'm not mistaken this will require that there are no gaps whatsoever in the ID-field which is quite rare I think

Providing the id column is unique, then

Declare @.value As Integer
set @.value = (RAND() * (select count(*) from table))+1

select *
from
(select columns,
(select count(*) from table where id <= t1.id) AS ID2
from table t1) v
where v.id2 = @.value|||Unfortunately I can't test this out on the machine I am on, for the above example wouldn't you need a group by clause in your select count(*) and a having instead of a where clause...

so something like...

Declare @.value As Integer
set @.value = (RAND() * (select count(*) from table))+1

select *
from
(select columns,
(select count(*) from table where id <= t1.id group by t1.id) AS ID2
from table t1) v
having v.id2 = @.value

once again, sorry I can't check it.|||No aggregate functions have been computed on the set 'V', meaning that the group by and having clauses are not required.

Consider,

Select a, b, (select count(*) from table) AS COUNT
from table t1
group by a, b

This is invalid as COUNT is interpreted as a column as opposed to an aggregate function of t1.|||Okie cool. :) Like I said, I couldn't check so. ;)

It's an interesting problem though... personally I wouldn't try and get the database to do this...

I'd get the app to generate a random id to select and just do a standard query on that id...

Each to their own though. :)|||Thank you for your all helps.

Since the ID field (primary key) does not start at one and also there may be a gap in this field (some data may be deleted), I modified the query posted by r123456

Declare @.value As Integer
SET @.value = (RAND() * (SELECT Count(*) FROM Users)) + 1

SELECT TOP 1 *
FROM Users
WHERE UserID >= @.value
ORDER BY UserID

How do you think about it?|||Try this and see:
SELECT TOP 1 * FROM TableName
order by newid()|||Originally posted by gyuan
Thank you for your all helps.

Since the ID field (primary key) does not start at one and also there may be a gap in this field (some data may be deleted), I modified the query posted by r123456

Declare @.value As Integer
SET @.value = (RAND() * (SELECT Count(*) FROM Users)) + 1

SELECT TOP 1 *
FROM Users
WHERE UserID >= @.value
ORDER BY UserID

How do you think about it?

The code above will still encounter problems with the gaps and the not starting at zero...

this is the one you want

Originally posted by r123456

Declare @.value As Integer
set @.value = (RAND() * (select count(*) from table))+1

select *
from
(select columns,
(select count(*) from table where id <= t1.id) AS ID2
from table t1) v
where v.id2 = @.value|||The problem you get with this solution

Declare @.value As Integer
SET @.value = (RAND() * (SELECT Count(*) FROM Users)) + 1

SELECT TOP 1 *
FROM Users
WHERE UserID >= @.value
ORDER BY UserID

is say you have 5000 records and you delete 4000 records.

Your rand value will be between 1 and 4000 but your max UserId is 5000 anything with an Id over 4000 is pretty much unreachable...

I think you'd be better with this...

Declare @.value As Integer
SET @.value = (RAND() * (SELECT max(UserID) FROM Users)) + 1

SELECT TOP 1 *
FROM Users
WHERE UserID >= @.value
ORDER BY UserID

It would mean when gaps occur the row after the gap would be hit more often, but atleast you would cover your entire collection of rows.

Hope that makes sense.|||quote:
------------------------
Originally posted by r123456

Declare @.value As Integer
set @.value = (RAND() * (select count(*) from table))+1

select *
from
(select columns,
(select count(*) from table where id <= t1.id) AS ID2
from table t1) v
where v.id2 = @.value

------------------------

What does columns stand for in the query?|||errr...won't my simple statement solve it?

SELECT TOP 1 * FROM TableName
order by newid()

I don't get it...|||the columns you want to select eg * or username, firstname, lastname etc...|||Hi Patrick,

I'm really not sure how your solution would work, what is the newid()?|||Its a build in command specifically to be use to "select random rows".
It is also use as a comand to auto gen Unique Identifier ids.

But when use in "order by newid()" it generates random rows.

Try it out and see...it works...unless..well theres something I'm missing in the whole discussion.|||Originally posted by Patrick Chua
errr...won't my simple statement solve it?

SELECT TOP 1 * FROM TableName
order by newid()

I don't get it...

It works too, but it takes 2 seconds, a little slower. Now I know the function NewID() and it helps. Thanks.|||rokslide,

That is a good idea to replace Count(*) to MAX(UserID). Thanks.|||Cool, I think I'll have to have a look into that one some time. :)

Thanks Patrick. :)|||No worries gyuan, please note though that (as I said above) it's not truely random.|||Correct.

Count(*) should not be replaced with max(id). The reason being that should a "gap" occur then the probability is increased for those values that occur past the "gap".

If TOP * 1 is used in conjunction with max(id) then only the first value past a "gap" value will be returned, should @.value be equal to a "gap" value.|||I think Patrick's query is better:

SELECT TOP 1 *
FROM Users
ORDER BY NewID()

although the running time is a little longer.|||I've used Patrick's method in the past with success.|||Isn't anyone going to ask WHY do you want to do this?|||I use Patricks method daily and it works out great for me.
NewID() takes a little longer due to it generates a GUID but it is truely random.|||Originally posted by Brett Kaiser
Isn't anyone going to ask WHY do you want to do this?

Brett ...
You are always after the "why" instead of the "how"? I like you for the spirit coz I believe "Prevention is better than cure".|||How's easy.....

And thanks...

Seen to many rocket ships built...|||I have to admit to orginally thinking why would you want to, but I have seen a few "scuffles" break out on here over the "why" of things so I decided to leave it alone.

Then of course the curiosity took over and I started to think,... hey, how would you do that...

anyhow...

r123456

with this statement...

Count(*) should not be replaced with max(id). The reason being that should a "gap" occur then the probability is increased for those values that occur past the "gap".

but if you use count and then compare count to the ID value you are going to completely miss some sections of the data entirely (eg. they will never have a chance to be selected). See the example that I noted eariler. The the max(id) option atleast you cover your entire span of data.

Of course Patrick's solution will cover everything perfectly so....|||Not true.

ID | ID2
1 1
2 2
3 3
4 4
5 5

Delete from table where id=2 OR id=3;

ID | ID2
1 1
4 2
5 3

You have two solutions. One of which requires very little code and an SQL Server function. The other requires a unique id, which for example can be the value of ROWID for an Oracle database.|||Ah yes, sorry, I forgot the second version of your solution with the new ID. :)

My fault entirely.

How I do to eliminate duplicate rows?

Hello,
I need to eliminate the duplicated rows in sql server 2000, but the duplicate is only for some fields of the row. However, I need all the fields of the row. For example, I have the next structure:
Id_type, number_type, date, diagnosis, sex, age, city

After many analysis I get many rows where the tree first field are repeated, so I need to leave only one but with the all another fields. This is because I need only the first time when the diagnosis appear.

How I can do it?

Thank you very much.

Regards,
Angela

As I understand you issue, when there are rows that have the same values for (ID_Type, Number_Type, Date), you wish to keep ONLY one (1) row, and it doens't matter which one of the duplicated rows is kept.

What if the non-duplicated fields is different, i.e., different diagnosis, or different sex, or different age, or different city (if that could happen)?

There are several methods to accomplish this task. First, a little more information is useful:

Version of SQL Server?

Are there other tables that have foreign key relationships to this table?

Approximately how much data is in the table (rows)?

Are there periods of time when no one is using the table?

Send this information and we can better assist you.

|||Hi Arnie, thanks you for your response.

Well, the problem is the information is the very bad quality .... so, I suppose that I get one row to the first time that some diagnosis appear to the pacient, but with data this not happen. So

I have found that to the same ID_Type, Number_Type, Date and same diagnosis exists rows that they have different sex or age or any other field, so I need to select only one, because I need the first time that this diagnosis appears...

Let me to response the questions:

Version of SQL Server?

R: Sql server 2000

Are there other tables that have foreign key relationships to this table?

R: yes, because some fields are only codes

Approximately how much data is in the table (rows)?

R: this table have 25 millions of rows... so much...

Are there periods of time when no one is using the table?

R: yes, this table is to datamining exercise.

I appreciate so much your help.

|||This kb should help:
http://support.microsoft.com/kb/139444|||

Here is an article that provides a bit more detailed instructions that the kb article.

http://www.sql-server-performance.com/rd_delete_duplicates.asp

One issue that neither article touches on is the size of your table. If there are many duplicates, attempting to work on the entire table could be a major struggle for your server due to the amount of Transaction Log activity and locks that will be required. You may find it more efficient to work with batches of, say 50,000 rows at a time. If there is a large amount of delete activity, there may be some Transaction Log issues that would have to be addressed.

|||

Thaks a lot, both articles are very nice....

Regards,

Angela

How I can scan all rows of the datasource?

How I can scan all rows of the datasource?
How I can move at the first row of the datasource?
How I can move at the next row, the last, the previous...
It's the reporting designer of Visual Basic .Net.
I would like to scan all rows to find an element or to do an operation, ...
Thanks.

Huh? I didn't get the point.
Mira, si eres de un paĆ­s de habla hispana escribe tu pregunta en espa?ol; tal vez pueda ayudarte.

|||Reporting Services is not a programmatic / cursor method of accessing data. The rows of the query results are processed by evaluating the expressions in the report definition. Your operations are included in these expressions (e.g. =Fields!A.Value - Fields!B.Value).

How I can pick between 5 - 20 rows in table

Mostly we are using to get 100 or more record with Top operator, but I want to take specific row in between like

10 - 100 or 100 to 200 etc.

How I can pick it. plz give suggestion

See

http://www.aspfaq.com/show.asp?id=2120

|||If you are using SQL 2005, look at ROW_NUMBER function at BOL.

How I can make trigger to Add Row 4 to my data base?

Hi

I need help about this problem

I have a data grid view like this:

Index

Code(releted to other table)

Name

Price +

Price _

1

1

BMW X5

1000000 $

2

1

BMW X3

500000 $

3

1

BMW Z3

250000 $

4

1

SUM

1750000 $

Row 4 must add by code automatically.
How? I must Use trigger or . . .?

my user may want to edit the data (some row OR price) I have a Problem.

How I can make a Code for Make SUM add row 4 and Delete this row when it loaded to edit? (Select and delete)

Or I can do some thing better than delete the row and write it again?

This doesn't seem like a SQL Server problem. If you do not want to persist the results in the backend you should do it in the client-side and add the extra row to the DataGrid from code. You should post this question in the Visual Studio or .NET Framework newsgroups.