Showing posts with label records. Show all posts
Showing posts with label records. Show all posts

Wednesday, March 28, 2012

Mirrored Records?

Hi I need some SQL script help. Need script to delete all table rows that
are duplicates in mirror image. Table has 2 columns, ColumnA and ColumnB.
Row1: ColumnA = x, ColumnB = y
Row2: ColumnA = y, ColumnB = x
Those 2 rows are exactly the same for me. Need a script that will delete
Row2 and all other rows where they're mirrored duplicates.
Thanks DarenDaren,
Once you do this, you should consider putting a constraint on the
table to enforce ColumnA <= ColumnB so this doesn't happen again.
It's a bad model if there are two ways of representing the same facts.
delete from T as T1
where ColumnA > ColumnB
and exists (
select * from T as T2
where T2.ColumnA = T1.ColumnB
and T2.ColumnB = T1.ColumnA
)
If you want to delete only these "mirror duplicates"
when they exist for the same customer/transaction/whatever,
you will need something like
delete from T as T1
where ColumnA > ColumnB
and exists (
select * from T as T2
where T2.customer = T1.customer
and T2.ColumnA = T1.ColumnB
and T2.ColumnB = T1.ColumnA
)
Steve Kass
Drew University
Daren Hawes wrote:

>Hi I need some SQL script help. Need script to delete all table rows that
>are duplicates in mirror image. Table has 2 columns, ColumnA and ColumnB.
>Row1: ColumnA = x, ColumnB = y
>Row2: ColumnA = y, ColumnB = x
>Those 2 rows are exactly the same for me. Need a script that will delete
>Row2 and all other rows where they're mirrored duplicates.
>Thanks Daren
>
>|||Thanks. I am a little with T and T2
Do I need to create a new Table? I have added the actual names below.
Column a = FromID ; Column b = ToID
CREATE TABLE [dbo].[tbl_Matrix] (
[FareID] [int] IDENTITY (1, 1) NOT NULL ,
[FromID] [int] NULL ,
[ToID] [int] NULL ,
[PriceCode] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tbl_Matrix] ADD
CONSTRAINT [PK_tbl_Matrix] PRIMARY KEY CLUSTERED
(
[FareID]
) ON [PRIMARY]
GO
"Steve Kass" <skass@.drew.edu> wrote in message
news:%23eKPQBxZFHA.2212@.TK2MSFTNGP14.phx.gbl...
> Daren,
> Once you do this, you should consider putting a constraint on the
> table to enforce ColumnA <= ColumnB so this doesn't happen again.
> It's a bad model if there are two ways of representing the same facts.
> delete from T as T1
> where ColumnA > ColumnB
> and exists (
> select * from T as T2
> where T2.ColumnA = T1.ColumnB
> and T2.ColumnB = T1.ColumnA
> )
> If you want to delete only these "mirror duplicates"
> when they exist for the same customer/transaction/whatever,
> you will need something like
> delete from T as T1
> where ColumnA > ColumnB
> and exists (
> select * from T as T2
> where T2.customer = T1.customer
> and T2.ColumnA = T1.ColumnB
> and T2.ColumnB = T1.ColumnA
> )
> Steve Kass
> Drew University
>
> Daren Hawes wrote:
>|||Try the following untested DELETE statement:
DELETE FROM tbl_Matrix
WHERE EXISTS
(SELECT *
FROM tbl_Matrix AS T
WHERE T.fromid = tbl_Matrix.fromid
AND T.toid = tbl_Matrix.toid
AND T.fareid < tbl_Matrix.fareid)
Now make FromID and ToID not nullable and add a unqiue constraint on those
two columns.
David Portas
SQL Server MVP
--|||Thanks for the reply Dave (I'm working with Daren),
Tryed that 1, deleted nothing, modified it to this...
DELETE FROM tbl_Matrix
WHERE EXISTS
(SELECT *
FROM tbl_Matrix AS T
WHERE T.fromid = tbl_Matrix.toid
AND T.toid = tbl_Matrix.fromid)
...and it deleted everything.
This is a copy of the script we've used to create the table's data, perhaps
if we wrote this script a bit better, wouldn't need another script to clean
it up...
declare @.Counter int
declare @.Counter2 int
select @.Counter=1
select @.Counter2=1
while @.Counter < 136
begin
while @.Counter2 < 136
Begin
Insert into dbo.tbl_Matrix (FromID,ToID)
Values (@.Counter,@.Counter2)
set @.Counter2 = @.Counter2 + 1
End
set @.Counter = @.Counter + 1
set @.Counter2 = 1
end
Regards,
Offal Eater
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:xZ6dndpRhvr8_gPfRVn-sw@.giganews.com...
> Try the following untested DELETE statement:
> DELETE FROM tbl_Matrix
> WHERE EXISTS
> (SELECT *
> FROM tbl_Matrix AS T
> WHERE T.fromid = tbl_Matrix.fromid
> AND T.toid = tbl_Matrix.toid
> AND T.fareid < tbl_Matrix.fareid)
> Now make FromID and ToID not nullable and add a unqiue constraint on those
> two columns.
> --
> David Portas
> SQL Server MVP
> --
>|||Thanks for the reply Dave (I'm working with Daren),
Not having any luck with these scripts, I've included the script we've used
to create the table. Perhaps if we wrote this differently, wouldn't have
need for the 'clean-up' script.
declare @.Counter int
declare @.Counter2 int
select @.Counter=1
select @.Counter2=1
while @.Counter < 136
begin
while @.Counter2 < 136
Begin
Insert into dbo.tbl_Matrix (FromID,ToID)
Values (@.Counter,@.Counter2)
set @.Counter2 = @.Counter2 + 1
End
set @.Counter = @.Counter + 1
set @.Counter2 = 1
end
Any feedback would be great.
Thx,
Offal Eater
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:xZ6dndpRhvr8_gPfRVn-sw@.giganews.com...
> Try the following untested DELETE statement:
> DELETE FROM tbl_Matrix
> WHERE EXISTS
> (SELECT *
> FROM tbl_Matrix AS T
> WHERE T.fromid = tbl_Matrix.fromid
> AND T.toid = tbl_Matrix.toid
> AND T.fareid < tbl_Matrix.fareid)
> Now make FromID and ToID not nullable and add a unqiue constraint on those
> two columns.
> --
> David Portas
> SQL Server MVP
> --
>|||T1 and T2 are table aliases. You need to compare each
row of T with the rest of T, and to distinguish the "this row"
table and the "rest of" table, you need the aliases:
Since FareID is the primary key, this will do
delete from tbl_Matrix as T1
where ToID > FromID
and exists (
select * from tbl_Matrix as T2
where T2.FromID = T1.ToID
and T2.ToID = T1.FromID
)
This will arbitrarily remove the one of the two mirror duplicates
for which ToID < FromID. If you want to remove, say, the
one with the smaller FareID, you could do this instead:
delete from tbl_Matrix as T1
where exists (
select * from tbl_Matrix as T2
where T2.FromID = T1.ToID
and T2.ToID = T1.FromID
and T2.FareID > T1.FareID
)
Seeing your table now, I'd recommend you change the primary
key to (FromID, ToID), and add a table constraint to enforce
FromID < ToID, or at least put a UNIQUE constraint on
(FromID, ToID), and make those two columns NOT NULL (if
not all columns). I don't see much use to the column FareID,
actually, but removing it might mess up other thing you rely on.
There could be other problems, too. You could have many rows
with the same FromID, ToID pair in the same order, but with
different FareID values. So you might want to do this:
delete from tbl_Matrix as T1
where exists (
select * from tbl_Matrix as T2
where (
T2.FromID = T1.ToID
and T2.ToID = T1.FromID
) or (
T2.FromID = T1.FromID
and T2.ToID = T1.ToID
)
and T2.FareID > T1.FareID
)
If you have rows where FromID = ToID, you can delete them
separately. I assume those would make no sense, but you have
nothing to prevent that from occurring right now.
SK
Daren Hawes wrote:

>Thanks. I am a little with T and T2
>Do I need to create a new Table? I have added the actual names below.
>Column a = FromID ; Column b = ToID
>CREATE TABLE [dbo].[tbl_Matrix] (
> [FareID] [int] IDENTITY (1, 1) NOT NULL ,
> [FromID] [int] NULL ,
> [ToID] [int] NULL ,
> [PriceCode] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL
> ) ON [PRIMARY]
>GO
>ALTER TABLE [dbo].[tbl_Matrix] ADD
> CONSTRAINT [PK_tbl_Matrix] PRIMARY KEY CLUSTERED
> (
> [FareID]
> ) ON [PRIMARY]
>GO
>
>"Steve Kass" <skass@.drew.edu> wrote in message
>news:%23eKPQBxZFHA.2212@.TK2MSFTNGP14.phx.gbl...
>
>
>|||> Tryed that 1, deleted nothing, modified it to this...
I ran your script and it didn't generate any duplicates so there is nothing
to delete - the combination of (FromID, ToID) is already unique (18225
rows). If you have something different in your data then please post a few
INSERT statements to generate some sample data that we can use to test it
out.

> This is a copy of the script we've used to create the table's data,
> perhaps if we wrote this script a bit better, wouldn't need another script
> to clean it up...
I posted a different solution to this in reply to Daren's post earlier
today.
David Portas
SQL Server MVP
--|||Here's a much better way to create the table. It uses
just one insert and no loops, and has much less chance of
off-by-one errors:
-- create a temporary table full of integers
declare @.Ints table (
IntVal int primary key
)
insert into @.Ints
select OrderID-10247
from Northwind..Orders
where OrderID-10247 between 1 and 136
-- Insert everything at once:
insert into dbo.tbl_Matrix(FromID, ToID)
select N1.IntVal, N2.IntVal
from @.Ints as N1 join @.Ints as N2
on N1.IntVal between 1 and 136
and N2.IntVal between N1.IntVal and 136
-- or N2.IntVal between N1.IntVal+1 and 136
If you want loops, but no mirror duplicates, replace
set @.Counter2 = 1
with
set @.Counter2 = @.Counter
This will continue to give you rows where FromID = ToID. If you don't
want those, start @.Counter2 one higher. Here's a guess, cleaned up
a bit, but I'd never trust this as much as the first suggestion I gave.
declare @.Counter int
declare @.Counter2 int
select @.Counter=0
while @.Counter < 135
begin
set @.Counter = @.Counter + 1
set @.Counter2 = @.Counter
while @.Counter2 < 135
Begin
set @.Counter2 = @.Counter2 + 1
Insert into dbo.tbl_Matrix (FromID,ToID)
Values (@.Counter,@.Counter2)
End
end
SK
Offal Eater wrote:

>Thanks for the reply Dave (I'm working with Daren),
>Not having any luck with these scripts, I've included the script we've used
>to create the table. Perhaps if we wrote this differently, wouldn't have
>need for the 'clean-up' script.
>declare @.Counter int
>declare @.Counter2 int
>select @.Counter=1
>select @.Counter2=1
>while @.Counter < 136
>begin
> while @.Counter2 < 136
> Begin
> Insert into dbo.tbl_Matrix (FromID,ToID)
> Values (@.Counter,@.Counter2)
> set @.Counter2 = @.Counter2 + 1
> End
>set @.Counter = @.Counter + 1
>set @.Counter2 = 1
>end
>Any feedback would be great.
>Thx,
>Offal Eater
>"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
>news:xZ6dndpRhvr8_gPfRVn-sw@.giganews.com...
>
>
>|||David,
I don't think the OP wants both (a, b) and (b, a). The 18225 rows
from the script are all (a,b) where a and b are between 1 and 135.
One way to eliminate the "mirror duplicates" is to generate only pairs
where a <= b (or perhaps a < b). There should be either 9180 or
9045 rows, depending on whether = is included.
SK
David Portas wrote:

>I ran your script and it didn't generate any duplicates so there is nothing
>to delete - the combination of (FromID, ToID) is already unique (18225
>rows). If you have something different in your data then please post a few
>INSERT statements to generate some sample data that we can use to test it
>out.
>
>
>I posted a different solution to this in reply to Daren's post earlier
>today.
>
>

Monday, March 19, 2012

min with a bit

Hi,

I'm trying to grab records with a priority over those marked as yes (-1) in
a certain field.

Trying "select id, min(bit) from tab group by id" does not work, as the min
operator doesn't work on bits.

Is there an alternative to my query?

Many thanks,
Chrismin(cast(deleted as int))

"Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
news:buoari$thc$1@.ucsnew1.ncl.ac.uk...
> Hi,
> I'm trying to grab records with a priority over those marked as yes (-1)
in
> a certain field.
> Trying "select id, min(bit) from tab group by id" does not work, as the
min
> operator doesn't work on bits.
> Is there an alternative to my query?
> Many thanks,
> Chris|||"mountain man" <hobbit@.southern_seaweed.com.op> wrote in message
news:opOPb.23515$Wa.14455@.news-server.bigpond.net.au...
> "Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
> news:buoari$thc$1@.ucsnew1.ncl.ac.uk...

> > Trying "select id, min(bit) from tab group by id" does not work, as the
> min
> > operator doesn't work on bits.
> > Is there an alternative to my query?

> min(cast(deleted as int))

Thanks for that, I do though get an error when trying it, I guess it's
because I'm using an mdb file and linked tables to the sql server... any
other ideas? Could create a quick function I guess...

Cheers,
Chris|||"Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message news:<buocfi$ub9$1@.ucsnew1.ncl.ac.uk>...
> "mountain man" <hobbit@.southern_seaweed.com.op> wrote in message
> news:opOPb.23515$Wa.14455@.news-server.bigpond.net.au...
> > "Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
> > news:buoari$thc$1@.ucsnew1.ncl.ac.uk...
> > > Trying "select id, min(bit) from tab group by id" does not work, as the
> min
> > > operator doesn't work on bits.
> > > > Is there an alternative to my query?
> > min(cast(deleted as int))
> Thanks for that, I do though get an error when trying it, I guess it's
> because I'm using an mdb file and linked tables to the sql server... any
> other ideas? Could create a quick function I guess...
> Cheers,
> Chris

Your question isn't really clear - a bit column can only hold 0,1 or
NULL. Perhaps the -1 is coming from Access, not from MSSQL? If it is
an MSSQL query, then please consider posting the CREATE TABLE
statement for your table, as well as the exact query that you're
using, and the output you expect (sample data would also be useful).

Simon|||How a bit could be (-1) ?

"Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
news:buoari$thc$1@.ucsnew1.ncl.ac.uk...
> Hi,
> I'm trying to grab records with a priority over those marked as yes (-1)
in
> a certain field.
> Trying "select id, min(bit) from tab group by id" does not work, as the
min
> operator doesn't work on bits.
> Is there an alternative to my query?
> Many thanks,
> Chris|||"Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
news:buocfi$ub9$1@.ucsnew1.ncl.ac.uk...
> "mountain man" <hobbit@.southern_seaweed.com.op> wrote in message
> news:opOPb.23515$Wa.14455@.news-server.bigpond.net.au...
> > "Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
> > news:buoari$thc$1@.ucsnew1.ncl.ac.uk...
> > > Trying "select id, min(bit) from tab group by id" does not work, as
the
> > min
> > > operator doesn't work on bits.
> > > > Is there an alternative to my query?
> > min(cast(deleted as int))
> Thanks for that, I do though get an error when trying it, I guess it's
> because I'm using an mdb file and linked tables to the sql server... any
> other ideas? Could create a quick function I guess...

How about .... min(cast(bit as varchar(1))) ?

Pete Brown
Falls Creek
Oz|||When you move a database from MS Access to SQL-Server, then do not
translate MS-Access Boolean columns into SQL-Server Bit columns, but use
Tinyint or Char(1) columns instead (and add appropriate CHECK
constraints to limit the column to (0,1) or ('Y','N')).

HTH,
Gert-Jan

Not Me wrote:
> Hi,
> I'm trying to grab records with a priority over those marked as yes (-1) in
> a certain field.
> Trying "select id, min(bit) from tab group by id" does not work, as the min
> operator doesn't work on bits.
> Is there an alternative to my query?
> Many thanks,
> Chris|||"Igor Raytsin" <n&i@.cyberus.ca> wrote in message
news:400fee3f_1@.news.cybersurf.net...
> "Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
> news:buoari$thc$1@.ucsnew1.ncl.ac.uk...
> > I'm trying to grab records with a priority over those marked as yes (-1)
> in
> > a certain field.
> > Trying "select id, min(bit) from tab group by id" does not work, as the
> min
> > operator doesn't work on bits.
> > Is there an alternative to my query?
> How a bit could be (-1) ?

Ask Bill :o)

Chris|||"Simon Hayes" <sql@.hayes.ch> wrote in message
news:60cd0137.0401220728.58b967ae@.posting.google.c om...
> "Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
news:<buocfi$ub9$1@.ucsnew1.ncl.ac.uk>...
> > "mountain man" <hobbit@.southern_seaweed.com.op> wrote in message
> > news:opOPb.23515$Wa.14455@.news-server.bigpond.net.au...
> > > "Not Me" <Not.Me@.faker.fake.fa.ke> wrote in message
> > > news:buoari$thc$1@.ucsnew1.ncl.ac.uk...
> > > > Trying "select id, min(bit) from tab group by id" does not work, as
the
> > min
> > > > operator doesn't work on bits.
> > > > > > Is there an alternative to my query?
> > > min(cast(deleted as int))
> > Thanks for that, I do though get an error when trying it, I guess it's
> > because I'm using an mdb file and linked tables to the sql server... any
> > other ideas? Could create a quick function I guess...
>
> Your question isn't really clear - a bit column can only hold 0,1 or
> NULL. Perhaps the -1 is coming from Access, not from MSSQL? If it is
> an MSSQL query, then please consider posting the CREATE TABLE
> statement for your table, as well as the exact query that you're
> using, and the output you expect (sample data would also be useful).

Thanks for your help, yes the -1 just seems to be how access likes to
display the info.

The full problem, is that I have a table of, for example careers that people
have. In the table certain people (reference numbers) may have a current
job, and a number of non-current jobs. They may have no current job at all
but some past ones.

So, a table could show

id current job
#1 yes databases
#1 no graphics
#2 no statistics
#2 no games

and I would want to return one record for each id#, with a preference of a
current job (if no current job, any non-current job will do)

So far I've only managed to do a "select all current jobs union select all
non-current jobs that don't appear in the current jobs list" The problem
here is that it becomes very very slow when performing the "jobs that don't
appear in the current jobs list" (done by where x not in (select x from y)).

So my effort was to somehow group up the reference numbers, and display the
min(current) job, which would pick the current job as a preference. But the
problem here is I can't add min(job) to the list can I? because that will
not necessary return the correct job associated with the value of
min(current)..

Hope you understand the problem!!
Any help is greatly appreciated.

Cheers,
Chris|||Not Me (Not.Me@.faker.fake.fa.ke) writes:
> So, a table could show
> id current job
> #1 yes databases
> #1 no graphics
> #2 no statistics
> #2 no games
> and I would want to return one record for each id#, with a preference of a
> current job (if no current job, any non-current job will do)
> So far I've only managed to do a "select all current jobs union select
> all non-current jobs that don't appear in the current jobs list" The
> problem here is that it becomes very very slow when performing the "jobs
> that don't appear in the current jobs list" (done by where x not in
> (select x from y)).

Here is one way that you may want to try:

DECLARE @.temp TABLE (ident int IDENTITY,
id int NOT NULL,
current bit NOT NULL,
job varchar(29) NOT NULL)

INSERT @.temp(id, current, job)
SELECT id, current, job
FROM source_table
ORDER BY id, current DESC

SELECT t.id, c.current, t.job
FROM @.temp t
JOIN (SELECT id, minident = MIN(ident)
FROM @.temp
GROUP BY id) m ON t.ident = m.minident
ORDER BY t.id

By inserting the data into a table variable with an identity column,
the rows are numbered, and the first identity value for each id is the
row you want.

I should add that this trick is not foolproof. You are not really
guaranteed that the identity values actually reflects the ORDER BY
clause, but it works most of the time. Particularly, if there is
no parallelism. Here I am relying on that INSERT into a table variable
never uses parallelism.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

min memory per query

I have a VB.NET application which reads thousands of records from a MSDE
Database, processes them, and writes them against a MYSQL Database. I'm
using the MySQL .NET DataProvider for accessing the MYSQL Database. When I
test the application on my lap top (512MB RAM; MSDE Memory limited to 260MB)
I don't get any errors. When I test the same application on another lap top
(2GB Ram; no limits) I get the error message, that there isn't enough memory
for the querz and I should reduce "min memory per query", which is already
at 512KB. On a desktop computer with 512MB Ram I don't get errors. On a
Server with 2GB Ram the application runs fine, but the MSDE reserves about
1.4GB of Ram. If I limit the Ram of the MSDE on that server to 800MB I get
the error message.
On every computer there is installed: Win XP Pro (or Win XP Server for the
server) SP1, MSDE 2000 (same version on each computer), same MySQL
DataProvider version, and .NET 1.1. On my lap top there is also .NET 1.0
installed. I'm working with VS 2002, which is only installed on my lap top.
I didn't get the errors when writing to the MySQL database with ODBC, but I
can't use ODBC because of the MySQL Database version I have to access.
For processing the first third of the records the memory which is used by
the MSDE is about 250MB on each computer. After that the memory usage on my
lap top and on the desktop pc stays at a low level, but on the other lap top
and on the server the memory usage goes through the roof.
I don't have any glue what the problem can be, it seems that the release of
the memory is handled differently on those machines. Can someone give me a
hint where to look for, what to do, or how to recreate the problem on the
other machines? I can't even look for the problem if I can't recreate the
error on my development machine (my lap top).
Any tipps are welcome!
Thanks
Peter
Hi Peter
I don't think the error is the fact that the min value is too high but the
fact that there is not enough memory to complete the query with what's
there.
See
http://msdn.microsoft.com/library/de...rr_2_65pt.asp.
If you look at the query plan, it could be that your stats are out of date,
or that you are missing indexes or unecessary/excessive hashing/sorting is
occuring
http://msdn.microsoft.com/library/de...onfig_68q6.asp
John
"Peter Zentner" <peter@._REM_zentner-online.de> wrote in message
news:%23DvjjHCxEHA.2632@.TK2MSFTNGP10.phx.gbl...
>I have a VB.NET application which reads thousands of records from a MSDE
> Database, processes them, and writes them against a MYSQL Database. I'm
> using the MySQL .NET DataProvider for accessing the MYSQL Database. When I
> test the application on my lap top (512MB RAM; MSDE Memory limited to
> 260MB)
> I don't get any errors. When I test the same application on another lap
> top
> (2GB Ram; no limits) I get the error message, that there isn't enough
> memory
> for the querz and I should reduce "min memory per query", which is already
> at 512KB. On a desktop computer with 512MB Ram I don't get errors. On a
> Server with 2GB Ram the application runs fine, but the MSDE reserves about
> 1.4GB of Ram. If I limit the Ram of the MSDE on that server to 800MB I get
> the error message.
> On every computer there is installed: Win XP Pro (or Win XP Server for the
> server) SP1, MSDE 2000 (same version on each computer), same MySQL
> DataProvider version, and .NET 1.1. On my lap top there is also .NET 1.0
> installed. I'm working with VS 2002, which is only installed on my lap
> top.
> I didn't get the errors when writing to the MySQL database with ODBC, but
> I
> can't use ODBC because of the MySQL Database version I have to access.
> For processing the first third of the records the memory which is used by
> the MSDE is about 250MB on each computer. After that the memory usage on
> my
> lap top and on the desktop pc stays at a low level, but on the other lap
> top
> and on the server the memory usage goes through the roof.
> I don't have any glue what the problem can be, it seems that the release
> of
> the memory is handled differently on those machines. Can someone give me a
> hint where to look for, what to do, or how to recreate the problem on the
> other machines? I can't even look for the problem if I can't recreate the
> error on my development machine (my lap top).
> Any tipps are welcome!
> Thanks
> Peter
>

Min and Max from one dimension based on Grouping from another dimension

Hi all,

I have 2 Dimensions D1 and D2. They both have a common Attribute "ID".

For one id in D1 there are multiple records in D2. This is like a parent child relationship with Product as parent Dimension and Product category as child dimension. For each product there are multiple product categories.

I want to get the Min and Max from child Dimension for each ID in Parent Dimension.

In sql this can be written as

select Distinct D1.ID,Min(D2.AttributeName) from D1,D2 where

D1.ID=D2.ID group by D2.ID

Can anybody write a MDX based on this?

Thanks

Girija.

Girija,

When there is Parent-Child Relationship between D1 and D2 as described Product and Product Category, I feel there should be Hierarchy in Cube for D1 and D2. And hoping you have set Order By Property to Key which stores ID and then simply you could use FirstChild for Min and LastChild for Max ID.

Bhudev

|||

Hi bhudev,

I gave parent - child reationship just as an example..... There is no hierarchy between those two dimensions.... They are connected only through ID.

Regards...

Girija Shankar

|||

Girija

I'm giving you the way how you can do it. Simply make Calculated Members for these ID under Measures Dimension and apply Min and Max function, I hope you will get yourself.

Bhudev

Monday, March 12, 2012

Miltivalue Parameters with Max Pool connections

I am trying to write a report with many different records needed (way the database was designed). Client will need multivalue parameter and I have reached max pool connections(can anyone please give me the number).

Normally I would handle this with a stored procedure to create a temp table with the needed information for each section(one row per section), but this will not work with the multivalued parameters(more than one in this report).

Any help on this issue would be appreciated.

Thanks!

Terry

I'm not sure I understand the issue why you are running out of connections, but if all datasets are based on the same data source, you could select the "Use Single Transaction" checkbox on the data source dialog. In that case, all datasets running against that data source will use the same connection. See also: http://msdn2.microsoft.com/en-us/library/ms181198.aspx

-- Robert

|||

I have found the data source, but not the single transaction area. Any help in this area would be appreciated. Will have to solve the multivalue parameter issue later. Idea without multivalue parameter would be a stored procedure to fix this.

Have one other idea with @.t table useage.

Thanks for the information.

Terry

|||

The "Use single transaction" checkbox is only available in report designer. You cannot change this setting once the report is published.

-- Robert

|||

That answered the question. I will have to find it at the start of the development for a new system.

Thanks!

Terry

millions records archiving and delete

The iussue:

Sql 2K
I have to keep in the database the data from the last 3 months.
Every day I have to load 2 millions records in the database.
So every day I have to export (in an other database as historical data
container) and delete the 2 millions records inserted 3 month + one day ago.

The main problem is that delete operation take a while...involving
transaction log.

The question are:
1) How can I improve this operation (export/delete)
2) If we decide to migrate to SQL 2005, may we use some feature, as
"partitioning" to resolve the problems ? In oracle I can use the "truncate
partition" statement, but in sql 2005, I'm reading, it cant be done.
This becouse we can think to create a partition on the last three mounts to
split data. The partitioning function can be dinamic or containing a
function that says "last 3 months ?" I dont think so.

May you help us
thank you

MastinoMassimo (mastino@.hotmail.it) writes:

Quote:

Originally Posted by

Sql 2K
I have to keep in the database the data from the last 3 months.
Every day I have to load 2 millions records in the database. So every
day I have to export (in an other database as historical data container)
and delete the 2 millions records inserted 3 month + one day ago.
>
The main problem is that delete operation take a while...involving
transaction log.
>
The question are:
1) How can I improve this operation (export/delete)
2) If we decide to migrate to SQL 2005, may we use some feature, as
"partitioning" to resolve the problems ? In oracle I can use the
"truncate partition" statement, but in sql 2005, I'm reading, it cant be
done. This becouse we can think to create a partition on the last three
mounts to split data. The partitioning function can be dinamic or
containing a function that says "last 3 months ?" I dont think so.


Permit me to start with SQL 2005. There you have partitioned tables,
and in a case like yours you would set up the table with let's say
four partitions, with the month as the partitioning column. To delete
old rows, you would simply take that table out of the partition
table, and then drop table that table. You in the same manner, shift
in a new table for the next month. Here I said month, but you have one
partition per day, and have 90 partitions if you like - whether this
is a good idea I don't know.

Note that partitioned tables are only available in the Enterprise
(and Developer) Edition of SQL 2005.

In SQL 2005, you would use partitioned views (and here 90 paritions
would definitely go beyond what is manageable). One table per month
and then they are united in a view with a UNION ALL statement. At
a new month you would run a job that dropped the table from four
months back, and create a new table. Notice that you can load directly
to the new, and data should turn in the right place.

See also Stefan Delmarco's article on partitioned views:
http://www.fotia.co.uk/fotia/FA.02...edViews.01.aspx
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||First, thank you for the answer, Erland, it's not the first time you help me
!

Quote:

Originally Posted by

Permit me to start with SQL 2005. There you have partitioned tables,
and in a case like yours you would set up the table with let's say
four partitions, with the month as the partitioning column. To delete
old rows, you would simply take that table out of the partition
table, and then drop table that table. You in the same manner, shift
in a new table for the next month. Here I said month, but you have one
partition per day, and have 90 partitions if you like - whether this
is a good idea I don't know.


Reading and reading over the internet, I found that I can transfer the data
to be dropped with a:

ALTER TABLE SWITCH...

and I can also make partition function dynamic:

http://msdn2.microsoft.com/en-us/library/aa964122.aspx
now I'm studying hard for the solution, the problems to resolve are many.

Quote:

Originally Posted by

>
Note that partitioned tables are only available in the Enterprise
(and Developer) Edition of SQL 2005.
>
In SQL 2005, you would use partitioned views (and here 90 paritions
would definitely go beyond what is manageable). One table per month
and then they are united in a view with a UNION ALL statement. At
a new month you would run a job that dropped the table from four
months back, and create a new table. Notice that you can load directly
to the new, and data should turn in the right place.
>
See also Stefan Delmarco's article on partitioned views:
http://www.fotia.co.uk/fotia/FA.02...edViews.01.aspx


I do not want, and I cannot use partitioned wiews, I have to delete what we
do not need any more.

Quote:

Originally Posted by

>
>
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
>
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx


Thank you

Massimo / Mastino|||Mastino (mastino@.hotmail.it) writes:

Quote:

Originally Posted by

I do not want, and I cannot use partitioned wiews, I have to delete what
we do not need any more.


Why would partitioned views prevent that? When it's time to delete old data,
you first redefine the view, so that the tables to be dropped are not
in the view any more. Then deleting is just dropping the table.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||I will evaluate this way too, but we have to work with millions records
tables.
Thanx

"Erland Sommarskog" <esquel@.sommarskog.seha scritto nel messaggio
news:Xns98E5ED0FE4612Yazorman@.127.0.0.1...

Quote:

Originally Posted by

Mastino (mastino@.hotmail.it) writes:

Quote:

Originally Posted by

I do not want, and I cannot use partitioned wiews, I have to delete what
we do not need any more.


>
Why would partitioned views prevent that? When it's time to delete old


data,

Quote:

Originally Posted by

you first redefine the view, so that the tables to be dropped are not
in the view any more. Then deleting is just dropping the table.
>
>
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
>
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

|||On Feb 28, 12:20 am, "Massimo" <mast...@.hotmail.itwrote:

Quote:

Originally Posted by

The iussue:
>
Sql 2K
I have to keep in the database the data from the last 3 months.
Every day I have to load 2 millions records in the database.
So every day I have to export (in an other database as historical data
container) and delete the 2 millions records inserted 3 month + one day ago.
>
The main problem is that delete operation take a while...involving
transaction log.
>
The question are:
1) How can I improve this operation (export/delete)
2) If we decide to migrate to SQL 2005, may we use some feature, as
"partitioning" to resolve the problems ? In oracle I can use the "truncate
partition" statement, but in sql 2005, I'm reading, it cant be done.
This becouse we can think to create a partition on the last three mounts to
split data. The partitioning function can be dinamic or containing a
function that says "last 3 months ?" I dont think so.
>
May you help us
thank you
>
Mastino


Just out of curiosity, do you have to log the delete operation? You
can truncate the tables but that is not logged.

Million records table

Hi everybody,
I set up a merge replication on Sql2K with one publisher/distributor
and 6 anonymous subscribers that run msde.
The db is growing bigger and bigger and i can't understand why.
I noticed that some of the MS_xxxxx tables are very large: in
particular one is 16.7 millions and another over 770K. Is there a way
to reduce/shrink these tables?
Thanks
Lorenzo
How many rows are being modified (inserted/updated/deleted) in the replicated
articles? I would expect this to be a similar order of magnitude to the
msmerge-contents, msmerge_tombstone tables. These tables will be
automatically cleared down during synchronization as part of the meta data
cleanup naturally run in merge by sp_mergemetadataretentioncleanup so
normally you don't need to do anything - the rows will be removed as they
reach the retention period defined in the publication.
HTH,
Paul Ibison
|||On 17 Mag, 17:32, Paul Ibison <Paul.Ibi...@.Pygmalion.Com> wrote:
> How many rows are being modified (inserted/updated/deleted) in the replicated
> articles? I would expect this to be a similar order of magnitude to the
> msmerge-contents, msmerge_tombstone tables. These tables will be
> automatically cleared down during synchronization as part of the meta data
> cleanup naturally run in merge by sp_mergemetadataretentioncleanup so
> normally you don't need to do anything - the rows will be removed as they
> reach the retention period defined in the publication.
> HTH,
> Paul Ibison
Hi Paul,
i don't know exactly haw many rows are being modified, but i can tell
you that the largest replicated table counts less than 130000 records
and i'm sure only a small part of these are changed/added/deleted
the msmerge_tombstone now counts almost 7 millions rows
i'm having some timeout troubles when i synchronize and i often get
the "can't enumerate changes" error
now i'm checking all my tables and indexes to see if some are missing
(the publication is partitioned with dynamic filtering)
thanks a lot for your help
lorenzo
|||Paul Ibison:
> I'd do a join between the msmerge_tombstone table and sysmergearticles. Make
> it a group by on article name and count the records per article then compare
> this to the rowcounts. It may be that there are other articles/publications
> contributing that you haven't accounted for, but even if not, this should
> clarify the situation a bit.
> HTH,
> Paul Ibison
that was a great idea, Paul
i found that there are over 5 million rows relating to a view that
returns 4800!
now i know what to investigate ;-)

Million Records Problem

Is there any way or approach on handling reports with million of records retrieved?

Had a problem for 25 silmultaneous users accessing the report.
Problems are:
- Timeout Expired.
- Server unavailable.
- Page cannot be displayed.

Please let me know if there is... Thanks in advance...

You should set the report to execute from a snapshot, otherwise each user access results in another 1M rows put into memory.

|||I am curious... why do your users want to see all million rows? Is it possible to reduce the amount of data coming into the report server by pushing filters down into the query expressions?

Friday, March 9, 2012

Migration SQL server 2000 to 2005, with heavy use of DTS

We have a big migration from SQL server 2000 to 2005
It is a big procedure aith millions of records and it uses DTSs
heavily, so I am asking some hints on s your experience on
1. "basic" migration of DB ib itself
2. DTS: we read that the Dynamic Properties used by DTS are NOT fully
supported and that would be a great problem for us
Any reporting of other known issues - small, medium or big -
will be greatly appreciated.
Thank you so muchSeveral things to do, the ones I remember:
1) Check BOL section 'Upgrading to SQL Server 2005', 'Backward
Compatibility'. Look for deprecated and discontinued features, breaking
changes and behavior changes.
2) Use the SQL Server Upgrade Advisor and follow its recommendations
3) You may want to consider moving the DTS packages without converting to
SSIS. Check also 'Upgrading to SQL Server 2005', 'Backward Compatibility' fo
r
DTS.
4) Test everything.
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"baruffa66@.gmail.com" wrote:

> We have a big migration from SQL server 2000 to 2005
> It is a big procedure aith millions of records and it uses DTSs
> heavily, so I am asking some hints on s your experience on
> 1. "basic" migration of DB ib itself
> 2. DTS: we read that the Dynamic Properties used by DTS are NOT fully
> supported and that would be a great problem for us
> Any reporting of other known issues - small, medium or big -
> will be greatly appreciated.
> Thank you so much
>|||Hi,
First, if you have not yet done so, get the "Microsoft SQL Server 2005
Upgrade Advisor". You can run this against your SQL Server 2000 server and
it will produce a report on problem areas that you may need to fix. We did
not have much problem, but the old style outer joins (*=, =*) are
deprecated. Also, if you have code that uses system tables some of those
have changed or vanished.
Passwords on 2005 are case-sensitive. This will cause you some problems if
you have code registered to login with a password in a different case from
that stored on the server. (SQL Server 2000 would forgive that, 2005 will
not.)
Since you are DTS heavy, there are "Microsoft SQL Server 2005 Backward
Compatibility Components" and the "Microsoft SQL Server 2000 DTS Designer
Components" to run on SQL Server 2005. Of course, it is the course of
wisdom to upgrade your packages to SSIS prior to SQL Server 2008, but this
can get you into SQL Server 2005 faster, and let you catch up on your DTS
packages one at a time, rather than all at once.
http://technet.microsoft.com/en-us/...aspx#designtime
These additional packages can be found at Feature Pack for Microsoft SQL
Server 2005 - February 2007
a42ec403d17&displaylang=en" target="_blank">http://www.microsoft.com/downloads/...&displaylang=en
RLF
<baruffa66@.gmail.com> wrote in message
news:b48d3fdf-2700-4855-b220-7bd616f33e40@.n20g2000hsh.googlegroups.com...
> We have a big migration from SQL server 2000 to 2005
> It is a big procedure aith millions of records and it uses DTSs
> heavily, so I am asking some hints on s your experience on
> 1. "basic" migration of DB ib itself
> 2. DTS: we read that the Dynamic Properties used by DTS are NOT fully
> supported and that would be a great problem for us
> Any reporting of other known issues - small, medium or big -
> will be greatly appreciated.
> Thank you so much|||Ben and Russel,
thanks a lot.
We asked 5 professionals,
4 of them have not even indirect experience on this migration,
the fifth says that he didn't migrate but is using a module of 2005
that allows to keep those oldies but goodies DTS
and launch them from 2005, without migrating to the new SSIS.
Ciao

Migration SQL server 2000 to 2005, with heavy use of DTS

We have a big migration from SQL server 2000 to 2005
It is a big procedure aith millions of records and it uses DTSs
heavily, so I am asking some hints on s your experience on
1. "basic" migration of DB ib itself
2. DTS: we read that the Dynamic Properties used by DTS are NOT fully
supported and that would be a great problem for us
Any reporting of other known issues - small, medium or big -
will be greatly appreciated.
Thank you so muchSeveral things to do, the ones I remember:
1) Check BOL section 'Upgrading to SQL Server 2005', 'Backward
Compatibility'. Look for deprecated and discontinued features, breaking
changes and behavior changes.
2) Use the SQL Server Upgrade Advisor and follow its recommendations
3) You may want to consider moving the DTS packages without converting to
SSIS. Check also 'Upgrading to SQL Server 2005', 'Backward Compatibility' for
DTS.
4) Test everything.
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"baruffa66@.gmail.com" wrote:
> We have a big migration from SQL server 2000 to 2005
> It is a big procedure aith millions of records and it uses DTSs
> heavily, so I am asking some hints on s your experience on
> 1. "basic" migration of DB ib itself
> 2. DTS: we read that the Dynamic Properties used by DTS are NOT fully
> supported and that would be a great problem for us
> Any reporting of other known issues - small, medium or big -
> will be greatly appreciated.
> Thank you so much
>|||Hi,
First, if you have not yet done so, get the "Microsoft SQL Server 2005
Upgrade Advisor". You can run this against your SQL Server 2000 server and
it will produce a report on problem areas that you may need to fix. We did
not have much problem, but the old style outer joins (*=, =*) are
deprecated. Also, if you have code that uses system tables some of those
have changed or vanished.
Passwords on 2005 are case-sensitive. This will cause you some problems if
you have code registered to login with a password in a different case from
that stored on the server. (SQL Server 2000 would forgive that, 2005 will
not.)
Since you are DTS heavy, there are "Microsoft SQL Server 2005 Backward
Compatibility Components" and the "Microsoft SQL Server 2000 DTS Designer
Components" to run on SQL Server 2005. Of course, it is the course of
wisdom to upgrade your packages to SSIS prior to SQL Server 2008, but this
can get you into SQL Server 2005 faster, and let you catch up on your DTS
packages one at a time, rather than all at once.
http://technet.microsoft.com/en-us/library/ms143706.aspx#designtime
These additional packages can be found at Feature Pack for Microsoft SQL
Server 2005 - February 2007
http://www.microsoft.com/downloads/details.aspx?FamilyID=50b97994-8453-4998-8226-fa42ec403d17&displaylang=en
RLF
<baruffa66@.gmail.com> wrote in message
news:b48d3fdf-2700-4855-b220-7bd616f33e40@.n20g2000hsh.googlegroups.com...
> We have a big migration from SQL server 2000 to 2005
> It is a big procedure aith millions of records and it uses DTSs
> heavily, so I am asking some hints on s your experience on
> 1. "basic" migration of DB ib itself
> 2. DTS: we read that the Dynamic Properties used by DTS are NOT fully
> supported and that would be a great problem for us
> Any reporting of other known issues - small, medium or big -
> will be greatly appreciated.
> Thank you so much|||Ben and Russel,
thanks a lot.
We asked 5 professionals,
4 of them have not even indirect experience on this migration,
the fifth says that he didn't migrate but is using a module of 2005
that allows to keep those oldies but goodies DTS
and launch them from 2005, without migrating to the new SSIS.
Ciao

Wednesday, March 7, 2012

Migration Millions of Records

Hi

Please guide me for the below given problem.

While doing migration by using cursors for the below given sample data its taking more hours to complete the process. Therefore want to know is there any way I can do it in simple query.

ACNo Amount Balance CalType
A001 10 10 +
A001 10 20 -
A001 40 40 +
A001 10 30 -
A002 90 90 +
A002 20 110 +
A002 40 150 +
A003 10 30 +
A003 10 40 +
A003 10 30 -
A004 40 40 +
A004 10 30 -

Iam having Amount value alone and Balance has to be calculated value based on CalType. At the same time the Balance has to be reset as 0 when AcNo has changed.

Please guide me for the faster approach.

Regards,
Mohanraj

It is not clear what you want help with.

Your data does not appear to be consistant. (It appears that the second row for [A001] should have a Balance of [0], and it appears that the first two rows of [A003] have incorrect Balance amounts.

Also, there is no column that can be used to determine sequencing, i.e., a datetime column or a IDENTITY field.

Do you wish ONLY a summary row with the correct Balance?

OR

Do you wish to 're-calculate' the Balance Column?

This can be very efficiently accomplished without using a CURSOR, #Temp tables, or table variables. It is really just a SET based operation.

|||

The data doesn't seem right.&nbsp; That said:&nbsp; there are ways to calculate running balances without using a cursor, but the methods (using COALESCE or cross-joins) are actually slower than using a cursor.&nbsp; (See <a href="http://www.sqlteam.com/article/calculating-running-totals">this article by Garth Wells</a> for a discussion of the various methods.)

What you don't want to do, though, is use a cursor to update your production table. Create a table variable to hold the running balances. Use a read-only cursor to read through your production table and populate the table variable. Then update the production table with the table variable when you're done.

|||

Hi Arnie,

Thanks for your reply.

ACNo Amount Balance CalType Ident_Column
A001 10 10 + 1
A001 10 20 - 2
A001 40 40 + 3
A001 10 30 - 4
A002 90 90 + 5
A002 20 110 + 6
A002 40 150 + 7
A003 30 30 + 8
A003 10 40 + 9
A003 10 30 - 10
A004 40 40 + 11
A004 10 30 - 12

Yeah, you are correct earlier data was wrongly posted, now it has highlighed as yellow in color.

Now I have added Identity column.

When AccountNumber has changed the balance need to recalculate.

Please provide me some faster approach.

Regards,

Mohanraj