Showing posts with label entries. Show all posts
Showing posts with label entries. Show all posts

Sunday, March 25, 2012

Computing a Grand Total

I have inherited a table that has entries such as:
AccountName, [Invoice No], [Sales Price] Qty, etc...
I would like to have a SubTotal and GrandTotal. So, I have a select
statement something like this:
Select AccountName, Qty, [Invoice No], [Sales Price], [Sales Price] * Qty as
SubTotal
FROM ...
That works fine, I get an extra column with the product of Qty and Sales
Price.
However, how would I get a Grand Total per invoice? I can do it in VB.Net,
but would like to see how to do it in SQL.
TIA,
PaoloCOMPUTE SUM(SubTotal) BY [Invoice No]
Here's the Books ONline URL
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/69009df2-dba5-4bcb-b2ae-
a7502537cb3e.htm
HTH. Ryan
"Paul" <PaulContactMe@.TheCornerStore.com> wrote in message
news:LesBf.1078$v81.173@.fe12.lga...
>I have inherited a table that has entries such as:
> AccountName, [Invoice No], [Sales Price] Qty, etc...
> I would like to have a SubTotal and GrandTotal. So, I have a select
> statement something like this:
> Select AccountName, Qty, [Invoice No], [Sales Price], [Sales Price] * Qty
> as SubTotal
> FROM ...
> That works fine, I get an extra column with the product of Qty and Sales
> Price.
> However, how would I get a Grand Total per invoice? I can do it in VB.Net,
> but would like to see how to do it in SQL.
> TIA,
> Paolo
>
>|||Try
SELECT [Invoice No], SUM([Sales Price] * Qty)
FROM [YourTable]
GROUP BY [Invoice No]
If you want grand totals for AccountName and [Invoice No]
SELECT AccountName, [Invoice No], SUM([Sales Price] * Qty) AS "Total"
FROM [YourTable]
GROUP BY AccountName, [Invoice No]
WITH ROLLUP
The out put of the above query will have rows like
AccountName [Invoice No] Total
---
Some Account NULL $3.50
A row with a NULL in the [Invoice No] column indicates the grand total
for that AccountName. You will also get a row like
AccountName [Invoice No] Total
---
NULL NULL $1500.78
which shows the total for all invoices and accounts.
"Paul" wrote:

> I have inherited a table that has entries such as:
> AccountName, [Invoice No], [Sales Price] Qty, etc...
> I would like to have a SubTotal and GrandTotal. So, I have a select
> statement something like this:
> Select AccountName, Qty, [Invoice No], [Sales Price], [Sales Price] * Qty
as
> SubTotal
> FROM ...
> That works fine, I get an extra column with the product of Qty and Sales
> Price.
> However, how would I get a Grand Total per invoice? I can do it in VB.Net,
> but would like to see how to do it in SQL.
> TIA,
> Paolo
>
>|||Try following examples on pubs database. You will have to use COMPUTE clause
.
use pubs
go
select ord_num, stor_id, qty from sales
order by ord_num
compute sum(qty) by ord_num
--using computed expression query will look like.
select ord_num, stor_id, (qty * 2) as subtotal from sales
order by ord_num
compute sum(qty * 2) by ord_num
"Paul" wrote:

> I have inherited a table that has entries such as:
> AccountName, [Invoice No], [Sales Price] Qty, etc...
> I would like to have a SubTotal and GrandTotal. So, I have a select
> statement something like this:
> Select AccountName, Qty, [Invoice No], [Sales Price], [Sales Price] * Qty
as
> SubTotal
> FROM ...
> That works fine, I get an extra column with the product of Qty and Sales
> Price.
> However, how would I get a Grand Total per invoice? I can do it in VB.Net,
> but would like to see how to do it in SQL.
> TIA,
> Paolo
>
>|||I agree with Mark. Using ROLLUP on a GROUP BY is the far better choice,
since it will always be returned in one result set. COMPUTE is pretty ugly
to deal with for the user program.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Mark Williams" <MarkWilliams@.discussions.microsoft.com> wrote in message
news:799AF000-43FC-483A-94E1-D5DFC3FA3CEF@.microsoft.com...
> Try
> SELECT [Invoice No], SUM([Sales Price] * Qty)
> FROM [YourTable]
> GROUP BY [Invoice No]
> If you want grand totals for AccountName and [Invoice No]
> SELECT AccountName, [Invoice No], SUM([Sales Price] * Qty) AS "Total"
> FROM [YourTable]
> GROUP BY AccountName, [Invoice No]
> WITH ROLLUP
> The out put of the above query will have rows like
> AccountName [Invoice No] Total
> ---
> Some Account NULL $3.50
> A row with a NULL in the [Invoice No] column indicates the grand total
> for that AccountName. You will also get a row like
>
> AccountName [Invoice No] Total
> ---
> NULL NULL $1500.78
> which shows the total for all invoices and accounts.
>
> --
> "Paul" wrote:
>

Sunday, February 19, 2012

complete bkup & log size

Hi ,
does a complete backup automatically reduce a log file size by removing
the inactive entries and those already written to the database ?
i have created a completed database backup on a weeklt basis but it still
seem that the log file is quite huge around 11 GB ?
could anyone kindly advise how i can approach this backup issue ?
tks & rdgsBackups do not reduce the size of any files. You must issue regular log
backups in addition to FULL backups if your database recover model is set to
FULL. or Bulk Logged. If you don't wish to do log backups you should change
it to simple mode. To shrink the file once you have fixed the process you
can use DBCC SHRINKFILE.
--
Andrew J. Kelly SQL MVP
"maxzsim" <maxzsim@.discussions.microsoft.com> wrote in message
news:4BF03439-3AA2-4B9C-9E90-D9C046F88A29@.microsoft.com...
> Hi ,
> does a complete backup automatically reduce a log file size by removing
> the inactive entries and those already written to the database ?
> i have created a completed database backup on a weeklt basis but it still
> seem that the log file is quite huge around 11 GB ?
> could anyone kindly advise how i can approach this backup issue ?
> tks & rdgs

complete bkup & log size

Hi ,
does a complete backup automatically reduce a log file size by removing
the inactive entries and those already written to the database ?
i have created a completed database backup on a weeklt basis but it still
seem that the log file is quite huge around 11 GB ?
could anyone kindly advise how i can approach this backup issue ?
tks & rdgsBackups do not reduce the size of any files. You must issue regular log
backups in addition to FULL backups if your database recover model is set to
FULL. or Bulk Logged. If you don't wish to do log backups you should change
it to simple mode. To shrink the file once you have fixed the process you
can use DBCC SHRINKFILE.
Andrew J. Kelly SQL MVP
"maxzsim" <maxzsim@.discussions.microsoft.com> wrote in message
news:4BF03439-3AA2-4B9C-9E90-D9C046F88A29@.microsoft.com...
> Hi ,
> does a complete backup automatically reduce a log file size by removing
> the inactive entries and those already written to the database ?
> i have created a completed database backup on a weeklt basis but it still
> seem that the log file is quite huge around 11 GB ?
> could anyone kindly advise how i can approach this backup issue ?
> tks & rdgs

complete bkup & log size

Hi ,
does a complete backup automatically reduce a log file size by removing
the inactive entries and those already written to the database ?
i have created a completed database backup on a weeklt basis but it still
seem that the log file is quite huge around 11 GB ?
could anyone kindly advise how i can approach this backup issue ?
tks & rdgs
Backups do not reduce the size of any files. You must issue regular log
backups in addition to FULL backups if your database recover model is set to
FULL. or Bulk Logged. If you don't wish to do log backups you should change
it to simple mode. To shrink the file once you have fixed the process you
can use DBCC SHRINKFILE.
Andrew J. Kelly SQL MVP
"maxzsim" <maxzsim@.discussions.microsoft.com> wrote in message
news:4BF03439-3AA2-4B9C-9E90-D9C046F88A29@.microsoft.com...
> Hi ,
> does a complete backup automatically reduce a log file size by removing
> the inactive entries and those already written to the database ?
> i have created a completed database backup on a weeklt basis but it still
> seem that the log file is quite huge around 11 GB ?
> could anyone kindly advise how i can approach this backup issue ?
> tks & rdgs