Showing posts with label group. Show all posts
Showing posts with label group. Show all posts

Wednesday, March 21, 2012

Nested Tables

Hi

I have 3 tables each with a diferent dataset's, is there any way i can nest 2 of the tables inside one of the other table's group's?

Any other way tath i could repeat 3 tables in some grouping method?

Using different datasets inside one top-level table is not currently supported, so you cannot nest the two tables inside the other table. But you can create two subreports and put the two tables in the subreports respectively, then nest the subreports in the other table.|||

That's exactly what i did. Was hopping there was some other way not using SubReports.

Never the less it works fine with Sub-Reports , thank you for your time

Nested Tables

Hi

I have 3 tables each with a diferent dataset's, is there any way i can nest 2 of the tables inside one of the other table's group's?

Any other way tath i could repeat 3 tables in some grouping method?

Using different datasets inside one top-level table is not currently supported, so you cannot nest the two tables inside the other table. But you can create two subreports and put the two tables in the subreports respectively, then nest the subreports in the other table.|||

That's exactly what i did. Was hopping there was some other way not using SubReports.

Never the less it works fine with Sub-Reports , thank you for your time

Monday, March 19, 2012

Nested operators

Hi all
I would like to submit a question to the SQL Server expert group about a
problem that I am having related with nested operators; it may seem somethin
g
foolish to ask but it is driving me crazy since 4 hours ago:
I am using a case statement that looks like this (I put it simplified just
for not being too long):
…..
CASE WHEN Field1 = ‘A’
THEN Field2*12/44
ELSE 0
END
….
The thing is that if I put the second line as shown above, it works well, it
gives me the right result.
But, if I put it as follows:
THEN Field2*(12/44)
It always gives me zero (0) in the result… and I don’t understand WHY
Even though this case can be easily fixed by excluding the parentheses; how
can it be managed in another case where the nesting must be specified?
Any ideas would be appreciated
Thanks in advance,
AnaIn your first example, I'll guess that Field2 is a float or decimal so it's
promoting 12 to decimal/float and then doing the same to 44.
In the second example, because of the parentheses, it's doing the division
first (12/44).
Because both numbers are integers, the result is 0.
Change Field2*(12/44) to Field2*(12.0/44) or cast one of the 2 numbers to
float or decimal and try it again.
"Ana Morales" <AnaMorales@.discussions.microsoft.com> wrote in message
news:D45BDEBC-C934-4B3F-BCDD-7FD65BE8213B@.microsoft.com...
> Hi all
> I would like to submit a question to the SQL Server expert group about a
> problem that I am having related with nested operators; it may seem
> something
> foolish to ask but it is driving me crazy since 4 hours ago:
> I am using a case statement that looks like this (I put it simplified just
> for not being too long):
> ...
> CASE WHEN Field1 = 'A'
> THEN Field2*12/44
> ELSE 0
> END
> ..
> The thing is that if I put the second line as shown above, it works well,
> it
> gives me the right result.
> But, if I put it as follows:
> THEN Field2*(12/44)
> It always gives me zero (0) in the result. and I don't understand WHY
> Even though this case can be easily fixed by excluding the parentheses;
> how
> can it be managed in another case where the nesting must be specified?
> Any ideas would be appreciated
> Thanks in advance,
> Ana
>|||I believe it is evaluating 12/44 as an integer value. this is around 0.27,
which gets rounded to 0 to make it an integer.
Add in a cast to make this work correctly...
Then cast(field2 as decimal(8,1) * (cast(12 as decimal(8,1)/cast(44 as
decimal(8,1))
Or, alternatively, the following should work, but the conversion is
implicit, not explicit...
THEN Field2*(12.0/44)
Adding the .0 after the number casts it as a decimal, and the calculation
will return a decimal value.
"Ana Morales" <AnaMorales@.discussions.microsoft.com> wrote in message
news:D45BDEBC-C934-4B3F-BCDD-7FD65BE8213B@.microsoft.com...
> Hi all
> I would like to submit a question to the SQL Server expert group about a
> problem that I am having related with nested operators; it may seem
something
> foolish to ask but it is driving me crazy since 4 hours ago:
> I am using a case statement that looks like this (I put it simplified just
> for not being too long):
> ...
> CASE WHEN Field1 = 'A'
> THEN Field2*12/44
> ELSE 0
> END
> ..
> The thing is that if I put the second line as shown above, it works well,
it
> gives me the right result.
> But, if I put it as follows:
> THEN Field2*(12/44)
> It always gives me zero (0) in the result. and I don't understand WHY
> Even though this case can be easily fixed by excluding the parentheses;
how
> can it be managed in another case where the nesting must be specified?
> Any ideas would be appreciated
> Thanks in advance,
> Ana
>|||My guess it the datatype of the Field2 is INT...
Without the brackets the order of execution leads to implicit
conversion to another more compatible type, whereas the second forces
an INT to be created since you multiply the another number by an INT
explicitly.|||Look at the change in results when you use 12/44 vs. 12.0/44.0
Try not to depend on the implicit data type conversion.|||Raymond, thank you very much.
I applied what you said and it works well now.|||No problem Ana.
Interesting problem.
I've seen many "integer division" questions asked here.
But it's the first time I've seen one like this.
"Ana Morales" <AnaMorales@.discussions.microsoft.com> wrote in message
news:DE51690B-E471-450E-A7F3-FEC2D8AF932D@.microsoft.com...
> Raymond, thank you very much.
> I applied what you said and it works well now.|||Johny D,
Can you please elaborate? When I run the code 12/44 I get 0, but 12.0/44 =
12/44.0 = 12.0/44.0 = .272727.
The implicit conversion seems to return the correct values.
However, I do avoid implicit conversion and use explicit conversion myself,
for two reasons (perhaps this is your point?)
1. The next programmer/DBA to view the code knows that the conversion is
happening.
2. If the rules for implicit conversion ever change, my code will still
work.
Was this your point?
"Johnny D" <john.dacosta@.gmail.com> wrote in message
news:1147118556.054867.100550@.i39g2000cwa.googlegroups.com...
> Look at the change in results when you use 12/44 vs. 12.0/44.0
> Try not to depend on the implicit data type conversion.
>|||Yes that was exactly my point...
My point was that without the decimal point, SQL Server is assuming the
values are INTEGERs, hence you always end up multiplying your value by
0 since 12/44 as an INT evaluates to 0.
However with the decimal specified, SQL Server then uses a more
appropriate datatype.
My assumption is that FLOAT is used by default when the '.' is
specified. if you prefer to specify the precision and scale you could
define the value as DECIMAL(10,4)
e.g.
DECLARE @.c_factor DECIMAL(10,4)
SET @.c_factor = 12.0/44.0 -- mandatory decimal positions to ensure
correct typing
PRINT @.c_factor
DECLARE @.c_factor FLOAT
SET @.c_factor = 12.0/44.0 -- mandatory decimal positions to ensure
correct typing
PRINT @.c_factor|||Ahhhh...
I had forgotten that the '.' casts the number to float rather than decimal.
"Johnny D" <john.dacosta@.gmail.com> wrote in message
news:1147185919.270887.310080@.e56g2000cwe.googlegroups.com...
> Yes that was exactly my point...
>
> My point was that without the decimal point, SQL Server is assuming the
> values are INTEGERs, hence you always end up multiplying your value by
> 0 since 12/44 as an INT evaluates to 0.
> However with the decimal specified, SQL Server then uses a more
> appropriate datatype.
> My assumption is that FLOAT is used by default when the '.' is
> specified. if you prefer to specify the precision and scale you could
> define the value as DECIMAL(10,4)
> e.g.
> DECLARE @.c_factor DECIMAL(10,4)
> SET @.c_factor = 12.0/44.0 -- mandatory decimal positions to ensure
> correct typing
> PRINT @.c_factor
>
> DECLARE @.c_factor FLOAT
> SET @.c_factor = 12.0/44.0 -- mandatory decimal positions to ensure
> correct typing
> PRINT @.c_factor
>

Monday, March 12, 2012

Nested dataregions

Hi!

I'm having some problems with a basic thing I guess.

I have a table of visits as the whole Dataset. Let's call it group A.

I then group the visits per customer, let's call that group B.

After that i filter out some unwanted visits and call that group C.

The hiarchy then looks like this, A contains B that contains C.

The problem is that I want to know the number of rows in C above the actual table. Something like

CustomerName (John Doe) Number of visits (54 from group C but printed out while in group B)

Visit 1 blablabal
Visit 2 blablabla
... and so on ...

As far as I understand the aggregate functions, you can only use them on the current group or a group above, never below. I tried to make an invisible textbox below C and it works if there is just one customer. If there are more than one customer the sum flipped between all the customers. (A gets the sum of B, and so on).

Thanks
Johan

But you can have multiple C group sections (each time your grouping expression changes), correct? If an outer group can reference an inner group, there will be multiple totals that are only known at runtime.|||

In my case, I only have one C groping per B grouping. Its a table that filters out preknown types of visits. Its the number of rows in this table that I would like to print out above the table in question, inside grouping B. What I did was to create a textbox within the C grouping that I referenced from a B grouping. Works for one (1) customer, but if there are more customers the totals get calculated correctly, but displayed in the wrong order.

I realise that it might be inpossible to do this, but I would hope that I at least could "transfer" a number higher up if I know that it only would be one other C grouping.

What I would like to do is something like this:

(pseudocode)

=Count(Fields!VisitId.Value, "not VisitType=BadVisit")

Am I making sence at all? :)

|||Yes, you are but in general you can have more than one group sections and referencing an inner group is not allowed. Why don't you bring the C totals at the B level in your dataset? A simple scalar function (assuming SQL Server) will do the job.|||

The problem is that I don't deside the datasource at the moment and all I have is a view containing Visits of two types. The first type is carried out visits and the second one is missed visits.

They are not in any specific order and missed and carried out visits could be at random rows.

If I could change the dataset, are you suggesting that I put the total amount of visits with each row per customer and just read the first row by each customer grouping to get the value? Kind of like how they denormalized the name of the customer in every row?

Thanks for a quick answer by the way! :)

|||Correct. In this way, you don't have waste your time to find a hack. In addition, you'll never go wrong by pushing work to the database.

Friday, March 9, 2012

Neet to know the sqlserver reporting news group

Neet to know the sqlserver reporting news group
microsoft.public.sqlserver.reportingsvcs
Jacco Schalkwijk
SQL Server MVP
"Atul Bahl" <atulbahl@.nospam.com> wrote in message
news:0AE210A3-5104-485F-A34F-F20A1165FC65@.microsoft.com...
> Neet to know the sqlserver reporting news group

Neet to know the sqlserver reporting news group

Neet to know the sqlserver reporting news groupmicrosoft.public.sqlserver.reportingsvcs
Jacco Schalkwijk
SQL Server MVP
"Atul Bahl" <atulbahl@.nospam.com> wrote in message
news:0AE210A3-5104-485F-A34F-F20A1165FC65@.microsoft.com...
> Neet to know the sqlserver reporting news group

Neet to know the sqlserver reporting news group

Neet to know the sqlserver reporting news groupmicrosoft.public.sqlserver.reportingsvcs
--
Jacco Schalkwijk
SQL Server MVP
"Atul Bahl" <atulbahl@.nospam.com> wrote in message
news:0AE210A3-5104-485F-A34F-F20A1165FC65@.microsoft.com...
> Neet to know the sqlserver reporting news group

Saturday, February 25, 2012

need tool for managing database object ownership

we have a group of developers which have created and asked us (DBAs) to
create many objects in the databases including tables / stored
procedures / functions / etc.

since our company is growing, however we have an increasing amount of
objects that have either been abandoned or have several versions.

in an effort to clean of the huge amount of clutter and anytime that
something simple like a stored proc needs to change, it is almost
impossible to predict exactly where we will see negative effects of
this change.

i am looking for a system (preferably without developing our own tool)
that would keep track of history of database objects (in terms of who
created it and what purpose it has) as well as link that to all the
developers/users we might need to notify of any changes to that object.
Also, this should be linked to the application which rely on the
object.

bottom line... every object in the database needs to have at least 1
corresponding contact as well as the applications which us it.

with this information, we can much more easily maintain objects in our
DBs.

thxPossibly this can be of help. Seems it isn't released quite yet, though:

http://msdn.microsoft.com/vstudio/t...ro/default.aspx

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/

"batman" <uspensky@.gmail.com> wrote in message
news:1151334096.716911.238760@.i40g2000cwc.googlegr oups.com...
> we have a group of developers which have created and asked us (DBAs) to
> create many objects in the databases including tables / stored
> procedures / functions / etc.
> since our company is growing, however we have an increasing amount of
> objects that have either been abandoned or have several versions.
> in an effort to clean of the huge amount of clutter and anytime that
> something simple like a stored proc needs to change, it is almost
> impossible to predict exactly where we will see negative effects of
> this change.
> i am looking for a system (preferably without developing our own tool)
> that would keep track of history of database objects (in terms of who
> created it and what purpose it has) as well as link that to all the
> developers/users we might need to notify of any changes to that object.
> Also, this should be linked to the application which rely on the
> object.
> bottom line... every object in the database needs to have at least 1
> corresponding contact as well as the applications which us it.
> with this information, we can much more easily maintain objects in our
> DBs.
> thx|||batman (uspensky@.gmail.com) writes:
> we have a group of developers which have created and asked us (DBAs) to
> create many objects in the databases including tables / stored
> procedures / functions / etc.
> since our company is growing, however we have an increasing amount of
> objects that have either been abandoned or have several versions.
> in an effort to clean of the huge amount of clutter and anytime that
> something simple like a stored proc needs to change, it is almost
> impossible to predict exactly where we will see negative effects of
> this change.
> i am looking for a system (preferably without developing our own tool)
> that would keep track of history of database objects (in terms of who
> created it and what purpose it has) as well as link that to all the
> developers/users we might need to notify of any changes to that object.
> Also, this should be linked to the application which rely on the
> object.
> bottom line... every object in the database needs to have at least 1
> corresponding contact as well as the applications which us it.

In one word: put everything under version control. If does not exist in
the version-control system, it does not exist at all. From tyhe version-
control system your build your baselines, and any changes to objects
requires you check out code from the version control system and and
check it in again, once the change has passed the module test.

The database should be seen as respository for binary objects and
you should be able to wipe it out at any time, to build a new database
from the version-control system. Developers who have neglected to
check out and in, will lose their changes.

A tip is to have several databases, that makes it easier for people
to understand that they need to use the version-control system.

--
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|||
Erland Sommarskog <esquel@.sommarskog.se> wrote:

> In one word: put everything under version control.

"put everything under version control" is one word? Seems like 5 to
me!

8-)

Paul...

--

plinehan __at__ yahoo __dot__ __com__

XP Pro, SP 2,

Oracle, 9.2.0.1.0 (Enterprise Ed.)
Interbase 6.0.1.0;

When asking database related questions, please give other posters
some clues, like operating system, version of db being used and DDL.
The exact text and/or number of error messages is useful (!= "it didn't work!").
Thanks.

Furthermore, as a courtesy to those who spend
time analysing and attempting to help, please
do not top post.

need tool for managing database object ownership

we have a group of developers which have created and asked us (DBAs) to
create many objects in the databases including tables / stored
procedures / functions / etc.
since our company is growing, however we have an increasing amount of
objects that have either been abandoned or have several versions.
in an effort to clean of the huge amount of clutter and anytime that
something simple like a stored proc needs to change, it is almost
impossible to predict exactly where we will see negative effects of
this change.
i am looking for a system (preferably without developing our own tool)
that would keep track of history of database objects (in terms of who
created it and what purpose it has) as well as link that to all the
developers/users we might need to notify of any changes to that object.
Also, this should be linked to the application which rely on the
object.
bottom line... every object in the database needs to have at least 1
corresponding contact as well as the applications which us it.
with this information, we can much more easily maintain objects in our
DBs.
thxPossibly this can be of help. Seems it isn't released quite yet, though:
http://msdn.microsoft.com/vstudio/t...ro/default.aspx
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"batman" <uspensky@.gmail.com> wrote in message
news:1151334096.716911.238760@.i40g2000cwc.googlegroups.com...
> we have a group of developers which have created and asked us (DBAs) to
> create many objects in the databases including tables / stored
> procedures / functions / etc.
> since our company is growing, however we have an increasing amount of
> objects that have either been abandoned or have several versions.
> in an effort to clean of the huge amount of clutter and anytime that
> something simple like a stored proc needs to change, it is almost
> impossible to predict exactly where we will see negative effects of
> this change.
> i am looking for a system (preferably without developing our own tool)
> that would keep track of history of database objects (in terms of who
> created it and what purpose it has) as well as link that to all the
> developers/users we might need to notify of any changes to that object.
> Also, this should be linked to the application which rely on the
> object.
> bottom line... every object in the database needs to have at least 1
> corresponding contact as well as the applications which us it.
> with this information, we can much more easily maintain objects in our
> DBs.
> thx
>|||batman (uspensky@.gmail.com) writes:
> we have a group of developers which have created and asked us (DBAs) to
> create many objects in the databases including tables / stored
> procedures / functions / etc.
> since our company is growing, however we have an increasing amount of
> objects that have either been abandoned or have several versions.
> in an effort to clean of the huge amount of clutter and anytime that
> something simple like a stored proc needs to change, it is almost
> impossible to predict exactly where we will see negative effects of
> this change.
> i am looking for a system (preferably without developing our own tool)
> that would keep track of history of database objects (in terms of who
> created it and what purpose it has) as well as link that to all the
> developers/users we might need to notify of any changes to that object.
> Also, this should be linked to the application which rely on the
> object.
> bottom line... every object in the database needs to have at least 1
> corresponding contact as well as the applications which us it.
In one word: put everything under version control. If does not exist in
the version-control system, it does not exist at all. From tyhe version-
control system your build your baselines, and any changes to objects
requires you check out code from the version control system and and
check it in again, once the change has passed the module test.
The database should be seen as respository for binary objects and
you should be able to wipe it out at any time, to build a new database
from the version-control system. Developers who have neglected to
check out and in, will lose their changes.
A tip is to have several databases, that makes it easier for people
to understand that they need to use the version-control system.
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|||Possibly this can be of help. Seems it isn't released quite yet, though:
http://msdn.microsoft.com/vstudio/t...ro/default.aspx
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"batman" <uspensky@.gmail.com> wrote in message
news:1151334096.716911.238760@.i40g2000cwc.googlegroups.com...
> we have a group of developers which have created and asked us (DBAs) to
> create many objects in the databases including tables / stored
> procedures / functions / etc.
> since our company is growing, however we have an increasing amount of
> objects that have either been abandoned or have several versions.
> in an effort to clean of the huge amount of clutter and anytime that
> something simple like a stored proc needs to change, it is almost
> impossible to predict exactly where we will see negative effects of
> this change.
> i am looking for a system (preferably without developing our own tool)
> that would keep track of history of database objects (in terms of who
> created it and what purpose it has) as well as link that to all the
> developers/users we might need to notify of any changes to that object.
> Also, this should be linked to the application which rely on the
> object.
> bottom line... every object in the database needs to have at least 1
> corresponding contact as well as the applications which us it.
> with this information, we can much more easily maintain objects in our
> DBs.
> thx
>|||batman (uspensky@.gmail.com) writes:
> we have a group of developers which have created and asked us (DBAs) to
> create many objects in the databases including tables / stored
> procedures / functions / etc.
> since our company is growing, however we have an increasing amount of
> objects that have either been abandoned or have several versions.
> in an effort to clean of the huge amount of clutter and anytime that
> something simple like a stored proc needs to change, it is almost
> impossible to predict exactly where we will see negative effects of
> this change.
> i am looking for a system (preferably without developing our own tool)
> that would keep track of history of database objects (in terms of who
> created it and what purpose it has) as well as link that to all the
> developers/users we might need to notify of any changes to that object.
> Also, this should be linked to the application which rely on the
> object.
> bottom line... every object in the database needs to have at least 1
> corresponding contact as well as the applications which us it.
In one word: put everything under version control. If does not exist in
the version-control system, it does not exist at all. From tyhe version-
control system your build your baselines, and any changes to objects
requires you check out code from the version control system and and
check it in again, once the change has passed the module test.
The database should be seen as respository for binary objects and
you should be able to wipe it out at any time, to build a new database
from the version-control system. Developers who have neglected to
check out and in, will lose their changes.
A tip is to have several databases, that makes it easier for people
to understand that they need to use the version-control system.
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|||Erland Sommarskog <esquel@.sommarskog.se> wrote:

> In one word: put everything under version control.
"put everything under version control" is one word? Seems like 5 to
me!
8-)
Paul...
plinehan __at__ yahoo __dot__ __com__
XP Pro, SP 2,
Oracle, 9.2.0.1.0 (Enterprise Ed.)
Interbase 6.0.1.0;
When asking database related questions, please give other posters
some clues, like operating system, version of db being used and DDL.
The exact text and/or number of error messages is useful (!= "it didn't work
!").
Thanks.
Furthermore, as a courtesy to those who spend
time analysing and attempting to help, please
do not top post.|||Erland Sommarskog <esquel@.sommarskog.se> wrote:

> In one word: put everything under version control.
"put everything under version control" is one word? Seems like 5 to
me!
8-)
Paul...
plinehan __at__ yahoo __dot__ __com__
XP Pro, SP 2,
Oracle, 9.2.0.1.0 (Enterprise Ed.)
Interbase 6.0.1.0;
When asking database related questions, please give other posters
some clues, like operating system, version of db being used and DDL.
The exact text and/or number of error messages is useful (!= "it didn't work
!").
Thanks.
Furthermore, as a courtesy to those who spend
time analysing and attempting to help, please
do not top post.

Monday, February 20, 2012

Need to update all but first record in group by

I'd like to use an UPDATE statement to set 'Y' and 'N' values to a flag
field in a table called SVCrossSales for each set of records keyed by
the CSNum field. The first record in each group would get a CSFlg
value of 'N' and the subsequent records would get a 'Y'. Here's a
sample of what the CSFlg values should end up being for each CSNum:
INum CSNum CSFlg
-- -- --
2 1 N
2 1 Y
2 1 Y
2 2 N
2 2 Y
2 3 N
2 3 Y
2 3 Y
Instead of using a cursor to cycle through each CSNum, I'd like to use
an UPDATE statement with an aggregate join. I can't think of anything
else except using TOP to pick out the first record in the set.
Obviously, if I can set the 'N' values for CSFlg, I would do a simple
UPDATE to set the 'Y' values for the rest of the records.
This UPDATE fails:
UPDATE
SVCrossSales
SET
CSFlg = 'N'
FROM
SVCrossSales
INNER JOIN (
SELECT TOP 1 CSNum
FROM SVCrossSales
WHERE INum = 2
GROUP BY CSNum) GroupedSales ON (
SVCrossSales.CSNum = GroupedSales.CSNum)
WHERE
SVCrossSales.INum = 2
What is the proper syntax to do my UPDATE?>> I'd like to use an UPDATE statement to set 'Y' and 'N' values to a flag
Since there is no column values that can uniquely identify a row, this is
logically impossible.
Since a well designed table is a set of rows, the concept of
first/second/last etc. does not really apply. In a relational database,
you'd identify a row using the key values, not by positional descriptions.
TOP 1 without ORDER BY clause relies on certain internal materialization of
values, and it is much better to have
Once you have the keys & constraints explicitly declared, you can formulate
an UPDATE statement along the lines of:
UPDATE SVCrossSales
SET CSFlg = 'N'
WHERE EXISTS ( SELECT *
FROM SVCrossSales s1
WHERE s1.INum = SVCrossSales .INum
AND <predicate involving keys> )
AND INum = 2 ;
Anith|||Anith, I'm not sure how a WHERE EXISTS will help. It returns a boolean
result (true or false). How can that help identify the first record of
each set of CSNums? I need something to pick out the records marked
with an x below and set the CSFlg = 'N':
INum CSNum CSFlg
-- -- --
x 2 1 N
2 1 Y
2 1 Y
x 2 2 N
2 2 Y
x 2 3 N
2 3 Y
2 3 Y|||Steve,
I am not sure if you have taken time to read what I wrote. Note that it is
impossible to accomplish what you are asking to do, since there is no
logical way of identify a row without a key.
Also, you have not posted you table structures & sample data (
www.aspfaq.com/5006 ). It is not clear whether the sample data you posted
includes all the columns in the table or not. It is not clear whether you
have keys and constraints in your table.
The UPDATE statement I posted include a correlation denoted as <predicate
involving keys>. This is the most critical part is solving your problem. If
you have no keys, all bets are off.
Anith|||Sorry...should have posted this to begin with...
DDL for the SVCrossSales table:
CREATE TABLE [dbo].[SVCrossSales] (
[INum] [int] NOT NULL ,
[SalesNum] [int] NOT NULL ,
[CSNum] [int] NULL ,
[CSFlg] [char] (1) NULL ,
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SVCrossSales] WITH NOCHECK ADD
CONSTRAINT [PK_SVCrossSales] PRIMARY KEY CLUSTERED
(
[INum],
[SalesNum]
) ON [PRIMARY]
GO
The INum is an ID field used in various tables of ours. The SalesNum
field is like an indentity seed in that it's a unique value for each
record in the table. Together, with INum, it forms the primary key for
the SVCrossSales table. I have a stored procedure that updates the
CSNum and then cycles through a cursor to update the CSFlg for each set
of recods with the same CSNum.
Sample data:
INum SalesNum CSNum CSFlg
-- -- -- --
2 333898 13295 N
2 333899 13295 Y
2 334366 13295 Y
2 335567 13269 N
2 335578 13269 Y
2 335579 13269 Y
2 336120 13255 N
2 336121 13255 Y
2 336122 13255 Y
So, is there a way to use a single update to set the 'N' values?|||I should have also mentioned that the INum and SalesNum fields link to
a sales table (SVSales) where my stored procedure's logic figures out
which sales to group together and assign the next available CSNum to in
SVCrossSales. The last step in the sproc is to update the CSFlg.|||Based on the sample data you posted, if the CSFlag column already has the
values 'Y', then do:
UPDATE SVCrossSales
SET CSFlg = 'N'
WHERE NOT EXISTS ( SELECT *
FROM SVCrossSales s1
WHERE s1.INum = SVCrossSales.INum
AND s1.CSNum = SVCrossSales.CSNum
AND s1.SalesNum < SVCrossSales.SalesNum )
AND INum = 2 ;
If both the values 'Y' and 'N' are to be set, then do:
UPDATE SVCrossSales
SET CSFlg = CASE WHEN EXISTS ( SELECT *
FROM SVCrossSales s1
WHERE s1.INum = SVCrossSales.INum
AND s1.CSNum = SVCrossSales.CSNum
AND s1.SalesNum <
SVCrossSales.SalesNum )
THEN 'Y' ELSE 'N' END
WHERE INum = 2 ;
You can re-write both these statements using a maxima function, but this
should give you what you are looking for.
Anith|||I replaced the cursor logic for updating CSFlg with the UPDATE
statement above. It didn't work. Also, it more than doubled the
execution time of the sproc. I don't understand what the EXISTS
subquery does. Shouldn't there be something to select the top 1 record
for each group set of records with the same CSNum?|||>> I replaced the cursor logic for updating CSFlg with the UPDATE statement
You will have to expand on that. Here is the data before & after update
based on the sample data you posted.
-- Before update
SELECT * FROM SVCrossSales ORDER BY INum, CSNum DESC, SalesNum
INum SalesNum CSNum CSFlg
-- -- -- --
2 333898 13295 Y
2 333899 13295 Y
2 334366 13295 Y
2 335567 13269 Y
2 335578 13269 Y
2 335579 13269 Y
2 336120 13255 Y
2 336121 13255 Y
2 336122 13255 Y
-- Do the update
UPDATE SVCrossSales
SET CSFlg = 'N'
WHERE NOT EXISTS ( SELECT *
FROM SVCrossSales s1
WHERE s1.INum = SVCrossSales.INum
AND s1.CSNum = SVCrossSales.CSNum
AND s1.SalesNum < SVCrossSales.SalesNum )
AND INum = 2 ;
-- After update
SELECT * FROM SVCrossSales ORDER BY INum, CSNum DESC, SalesNum
INum SalesNum CSNum CSFlg
-- -- -- --
2 333898 13295 N
2 333899 13295 Y
2 334366 13295 Y
2 335567 13269 N
2 335578 13269 Y
2 335579 13269 Y
2 336120 13255 N
2 336121 13255 Y
2 336122 13255 Y
That might have something to do with the overall construction of the stored
procedure and suboptimal indexing.
Well, as I said, there are several ways you can derive the solution. In this
case, you can use TOP 1 as well like:
UPDATE SVCrossSales
SET CSFlg = 'N'
WHERE SalesNum = ( SELECT TOP 1 s1.SalesNum
FROM SVCrossSales s1
WHERE s1.INum = SVCrossSales.INum
AND s1.CSNum = SVCrossSales.CSNum
ORDER BY s1.SalesNum )
AND INum = 2 ;
Anith|||Hi There,
Let us See if this can help your cause
--
Update yourTable Set CSFlag = 'N' Where SalesNum In (Select
Min(SalesNum) From yourTable Group By INum,CSNum)
--
Please let me know if it worked for You.
INum SalesNum CSNum CSFlg
-- -- -- --
2 333898 13295 N
2 333899 13295 Y
2 334366 13295 Y
2 335567 13269 N
2 335578 13269 Y
2 335579 13269 Y
2 336120 13255 N
2 336121 13255 Y
2 336122 13255 Y
With Warm regards
Jatinder Singh

Need to sum columns

I need to create a view (I believe) that will take a column named
'accountbalance' for a group of records, and total (sum) up those entries...
can someone point me to a tutorial to learn how to do this? THanks!
Never mind.. I found a reference. thanks tho

Need to sum columns

I need to create a view (I believe) that will take a column named
'accountbalance' for a group of records, and total (sum) up those entries...
can someone point me to a tutorial to learn how to do this? THanks!Never mind.. I found a reference. thanks tho

Need to sum columns

I need to create a view (I believe) that will take a column named
'accountbalance' for a group of records, and total (sum) up those entries...
can someone point me to a tutorial to learn how to do this? THanks!Never mind.. I found a reference. thanks tho