Hi Erland
Thanks for getting back to me. The breakdown is as follows:
TABLE 1 - ACCOUNTS
CREATE TABLE [dbo].[ACCOUNTS] (
[ACCOUNTID] [T_ACCOUNTSDOMAIN] NOT NULL ,
[NAME] [varchar] (40) NOT NULL ,
[COMMENT] [varchar] (255) NULL ,
[PEOPLEID] [T_PEOPLEDOMAIN] NULL ,
[LEDGER] [smallint] NULL ,
[Status] [smallint] NOT NULL ,
[CURRENCYID] [T_CURRENCIESDOMAIN] NULL ,
[FIXEDVATRATE] [tinyint] NOT NULL ,
[VATRATEID] [T_VATRATESDOMAIN] NULL ,
[OURACCOUNTID] [varchar] (40) NULL ,
[AGENTID] [T_AGENTSDOMAIN] NULL ,
[SALESCENTREID] [T_SALESCENTRESDOMAIN] NULL ,
[COSTCENTREID] [T_COSTCENTRESDOMAIN] NULL ,
[DEPARTMENTID] [T_DEPARTMENTSDOMAIN] NULL ,
[WAREHOUSEID] [T_WAREHOUSESDOMAIN] NULL ,
[DELETETRANSACTIONS] [tinyint] NOT NULL ,
[BALANCE] [money] NOT NULL ,
[TURNOVER] [money] NOT NULL ,
[CREDITLIMIT] [money] NOT NULL ,
[REMITTANCECONTACTID] [T_CONTACTDETAILSDOMAIN] NULL ,
[STATEMENTCONTACTID] [T_CONTACTDETAILSDOMAIN] NULL ,
[InvoiceContactID] [T_CONTACTDETAILSDOMAIN] NULL ,
[DeliveryContactID] [T_CONTACTDETAILSDOMAIN] NULL ,
[OrdersContactID] [T_CONTACTDETAILSDOMAIN] NULL ,
[TRADINGTERMS] [varchar] (100) NULL ,
[SETTLEMENTPERCENT] [float] NULL ,
[TRADINGTYPE] [smallint] NULL ,
[TRADINGDAYOFMONTH] [smallint] NULL ,
[TRADINGDAYSTIME] [smallint] NULL ,
[USETRADINGTERMS] [tinyint] NOT NULL ,
[PRICELEVELID] [T_PRICELEVELSDOMAIN] NULL ,
[DISCOUNTPERCENT] [float] NULL ,
[LASTINVOICEDATE] [datetime] NULL ,
[LASTPAYMENTDATE] [datetime] NULL ,
[PRINTSTATEMENTS] [tinyint] NOT NULL ,
[PRINTRUNNINGBALANCES] [tinyint] NOT NULL ,
[BANKNAME] [varchar] (40) NULL ,
[BANKADDRESSLINE1] [varchar] (40) NULL ,
[BANKADDRESSLINE2] [varchar] (40) NULL ,
[BANKADDRESSLINE3] [varchar] (40) NULL ,
[BANKTOWN] [varchar] (40) NULL ,
[BANKCOUNTY] [varchar] (40) NULL ,
[BANKCOUNTRY] [varchar] (40) NULL ,
[BANKPOSTCODE] [varchar] (40) NULL ,
[BANKTITLE] [varchar] (40) NULL ,
[BANKFIRSTNAME] [varchar] (40) NULL ,
[BANKSURNAME] [varchar] (40) NULL ,
[BANKPOSITION] [varchar] (40) NULL ,
[BANKTELEPHONE] [varchar] (40) NULL ,
[BANKTELEX] [varchar] (40) NULL ,
[BANKFAX] [varchar] (40) NULL ,
[BANKEMAIL] [varchar] (40) NULL ,
[BANKSORTCODE] [varchar] (40) NULL ,
[BANKACCOUNTID] [varchar] (40) NULL ,
[BANKACCOUNTNAME] [varchar] (40) NULL ,
[BANKACCOUNTSWIFT] [varchar] (40) NULL ,
[SPOOLLOCK] [tinyint] NOT NULL ,
[TIMESTAMP] [timestamp] NOT NULL
)
GO
Sample data from this is as follows:
accountid name
---------------------------------------- -----------------------------------
-----
G01 German Customer
NL01 Netherlands Customer
UK01 UK Customer
I only want the Account ID and Name from this table where the Ledger = 1
TABLE 2 - ACCOUNTSBUDGET
CREATE TABLE [dbo].[ACCOUNTSBUDGET] (
[ACCOUNTID] [T_ACCOUNTSDOMAIN] NOT NULL ,
[DEPARTMENTID] [T_DEPARTMENTSDOMAIN] NOT NULL ,
[YEAR] [smallint] NOT NULL ,
[PERIOD] [smallint] NOT NULL ,
[TURNOVER] [money] NOT NULL ,
[TURNOVERBUDGET] [money] NOT NULL ,
[PAYMENTS] [money] NOT NULL ,
[PAYMENTSBUDGET] [money] NOT NULL ,
[PROFIT] [money] NOT NULL ,
[PROFITBUDGET] [money] NOT NULL ,
[TIMESTAMP] [timestamp] NOT NULL
)
GO
Sample data from this is as follows:
accountid year period turnover
---------------------------------------- ------ ------ ---------------------
UK01 2003 3 200.0000
UK01 2003 6 300.0000
UK01 2003 12 500.0000
I want the total sumation from period 1 to whatever the current month is (eg
it is 12 at the moment) for last year (ie, currently 2002) and this year
(ie, currently 2003) for each account in the ACCOUNTS table. The reason for
this is that there might not be entries for certain accounts in the
ACCOUNTSBUDGET table, as it is a sort of line-by-line storer when a
transaction is placed for a particular period (ie, month) and year.
RESULTS
The results I want are as follows:
December Turnover YTD Deviation
Acc No: Account Name 2003 2002 £ %
A001 Alpha Motor 9,250 5,398 3,852 71%
etc etc etc etc etc
etc
I hope this defines it better, but please ask me if you need anymore.
Rgds
Robbie
[quoted text, click to view] Erland Sommarskog <sommar@algonet.se> wrote in message
news:Xns946224FAF2D0Yazorman@127.0.0.1...
AstrA (noemail@noemail.com) writes:
> TABLE 1 - ACCOUNTS
>
> This contains all of the account codes (PK) and names as ACCOUNTID and
> NAME respectively. It also contains a field called LEDGER which I would
> need to use in a Where clause to set this to 1 (1 = sales and 2 =
> purchases).
>
> TABLE 2 - ACCOUNTBUDGETS
>
> This contains the Turnover details and is linked to the ACCOUNTS table
> by an ACCOUNTID (FK??). This table contains a TURNOVER field for the
> actual turnover amount, a PERIOD field for the months of a year (eg, 1 -
> 12 - that being Jan to Dec) and a YEAR field for the relevant years.
This is a complete guessing game, but this may get you started:
SELECT a.accountid, a.name, a.ledger,
turnover = colaesce(ab.turnover, 0), ab.stdev
FRON accounts a
LEFT JOIN (SELECT accountid, turnover = SUM(turnover),
stdev = STDEV(turnover)
FROM accountbudgets ab
GROUP BY accountid, year) ON a.accoundid = b.accountid
If you desire further assistance, here are the standard recommendation
for this kind of inquiries:
o Include CREATE TABLE definitions for your tables.
o Include INSERT statements with sample data.
o Include the desired output from that data.
--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp