Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Remove Grand Total Row...... Expand / Collapse
Author
Message
Posted Wednesday, December 5, 2012 6:43 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:59 AM
Points: 45, Visits: 342
Hi all,

I am trying to see if any one can help me on how to remove a grand total row from a table. I have a table in which there are 6 columns. first 3 columns there is some data but at first 3 columns, last row is blank and the last 3 columns have data which is added at the last 3 row as a grand total. here it is looks like:

col1 col2 col3 col4 col5 col6
a b c 1 2 5
d e f 4 3 8
g h i 6 7 5
11 12 18


So, the last three column row is grand total and the first 3 columns last row is blank. So, I am trying to delete that but can't.Need help

Thanks.
Post #1393287
Posted Wednesday, December 5, 2012 11:58 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 11, 2014 1:53 AM
Points: 194, Visits: 716
I don't understand your explanations. Post some example data and try to explain again using these data.
Post #1393336
Posted Thursday, December 6, 2012 6:58 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:59 AM
Points: 45, Visits: 342
I mean my question is that a table have some columns and at the row there is a grand total of a data in the above column. So, if i have 4 columns and the last two columns each row have some numbers which will add at the last row as a total number. The other columns have data also but the last row first two columns doesnt have anything in it just blanks. how to remove the last row.

thanks.


col1 col2 col3 col4 col5 col6
a b c 1 2 5
d e f 4 3 8
g h i 6 7 5
11 12 18
Post #1393829
Posted Thursday, December 6, 2012 11:44 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 11, 2014 1:53 AM
Points: 194, Visits: 716
You say the rows with the total contain blanks in the first two columns, the other rows do not. So if this is your criteria the query is quite easy:
select * 
from dbo.TableX
where col1 = '' and col2 = ''

Try this select to check if it returns the rows you want to delete. If so replace the "select *" by "delete" and these rows will be deleted.
delete 
from dbo.TableX
where col1 = '' and col2 = ''

Post #1393879
Posted Friday, December 7, 2012 5:36 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:59 AM
Points: 45, Visits: 342
OK. You its right if I use this statement to delete this it will delete it. The whole purpose is that I want the last row to be deleted where the grand total of numbers and blank spaces on the other two columns. So if there is another row with blank spaces shows in the two columns.Is it going to delete those rows with this statement?If it does then I don't think this statement will work.
Post #1394013
Posted Friday, December 7, 2012 8:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 11, 2014 1:53 AM
Points: 194, Visits: 716
I'm still not sure, but yes, the statement above deletes ALL rows that contain the free spaces in the two columns.
If you only want delete the last row there are several possibilities. For the beginning I wold take an easy way. Using the top clause you can select the primary key value of the "last row" in your table. Then you can delete the row using the primary key value.

Let's say there is a column InsertDate in your table und you want to delete only the last inserted row with blanks. Let's also say there is a primary key column Named "Id" in your table.
This would be:
declare @IdToDelete int;
select @IdToDelete = (
select top (1) Id
from dbo.TableX
where col1 = '' and col2 = ''
order by InsertDate desc
)

delete from dbo.TableX where Id = @IdToDelete

Again use a select instead of a delete statement to check the rows the query returns.

For a more exactly reply you really should post the table structure you use and some sample data.
Post #1394082
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse