Remove Grand Total Row......

  • 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:

    col1col2col3col4col5col6

    abc125

    def438

    ghi675

    111218

    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.

  • I don't understand your explanations. Post some example data and try to explain again using these data.

  • 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.

    col1col2col3col4col5col6

    abc125

    def438

    ghi675

    11 1218

  • 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 = ''

  • 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.

  • 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.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply