SELECT linvoice
, ltradat
, lmatter
, clname1
, mdesc1
, bop
, SUM(lamount) AS invamount
FROM ledger
INNER JOIN matter ON lmatter=mmatter
INNER JOIN client ON mclient=clnum
INNER JOIN batch ON lbatch=bbatch
WHERE ltradat >= '5/1/2010'
AND lzero <> 'R'
AND linvoice <> '0'
AND llcode IN ('FEES','HCOST','SCOST')
AND mclient <> '123456'
AND linvoice NOT IN
(
SELECT CAST(EMP_ID AS INT)
FROM Document_Catalog
WHERE EMP_ID NOT IN ('DRAFT','SEC_9999','TK_9999')
)
AND linvoice NOT IN (SELECT linvoice FROM v_jointinv WHERE NumMtr > 1)
AND batch.bop NOT IN ('abc','def')
GROUP BY linvoice,ltradat,lmatter,mdesc1,clname1,bop
ORDER BY bop,linvoice
You'll notice in the code above, I've set some exclusions. We have a view that counts the number of matters per invoice. Any invoices with more than one matter (e.g. a joint invoice) are excluded from this query and are included in a separate query that doesn't include matter information. I've also included the ability to exclude particular users and clients from the query. Regarding the subquery on the Document Catalog table, any non-numeric values will need to be excluded to enable the matching against the EMP_ID field (or you can cast linvoice as VARCHAR).
Monday, May 10, 2010
Missing Records in Whitehill Document Catalog
A while back we looked at missing files in Whitehill where the appropriate record existed in the Document Catalog. Sometimes, however, the Document Catalog is never updated. The following query will help identify which invoices have been created and not inserted in the Document Catalog.
Friday, April 16, 2010
Fee Discount by Year
This is a quick and dirty look at fee discounts by year for a given client. It utilizes the matter history table.
USE son_db; GO DECLARE @client NVARCHAR(10); SET @client = N'000001'; SELECT CAST(YEAR(peendt) AS VARCHAR(10)) AS 'Year' ,SUM(mhdowkdb) AS 'Worked' ,SUM(mhdobidb) AS 'Billed' ,SUM(mhdobidb)-SUM(mhdowkdb) AS 'Discount' ,CASE WHEN SUM(mhdowkdb)=0 THEN 0 ELSE (SUM(mhdobidb)-SUM(mhdowkdb))/SUM(mhdowkdb) END AS 'Disc %' FROM matths INNER JOIN periodt ON mhper=pe INNER JOIN matter ON mhmatter=mmatter WHERE mclient=@client GROUP BY YEAR(peendt) UNION SELECT 'Total' AS 'Year' ,SUM(mhdowkdb) AS 'Worked' ,SUM(mhdobidb) AS 'Billed' ,SUM(mhdobidb)-SUM(mhdowkdb) AS 'Discount' ,CASE WHEN SUM(mhdowkdb)=0 THEN 0 ELSE (SUM(mhdobidb)-SUM(mhdowkdb))/SUM(mhdowkdb) END AS 'Disc %' FROM matths INNER JOIN periodt ON mhper=pe INNER JOIN matter ON mhmatter=mmatter WHERE mclient=@client
Labels:
client,
enterprise,
fee,
sql
Thursday, March 25, 2010
General Ledger Transactions for a Range of Accounts
The following stored procedure will imitate a general ledger transaction report from within Elite, with easy use in Excel. The concept is faily simple with the first SELECT statement pulling Accounts Payable entries and the second SELECT statement pulling non-AP general journal entries. This also uses the GJ entry description user defined function. There are two variables passed to the stored procedure: beginning and ending dates. The GL accounts are hard coded, but could easily be changed to variables.
IF EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE SPECIFIC_SCHEMA = N'dbo'
AND SPECIFIC_NAME = N'usp_GL_Expenses'
)
DROP PROCEDURE dbo.usp_GL_Expenses
GO
CREATE PROCEDURE dbo.usp_GL_Expenses
@startdate DATETIME = NULL,
@enddate DATETIME = NULL
AS
SELECT
CAST(csddt.cknum AS VARCHAR(12)) AS 'Check #/GJ ID'
,csddt.ckline AS 'GJ Line'
,csddt.ckdate AS 'Date'
,MONTH(csddt.ckdate) AS 'Month'
,csddt.apnum AS 'Vendor #'
,csddt.apaddid AS 'Vendor Sub'
,ap.apname AS 'Vendor Name'
,apaddr.appynm AS 'Payee'
,csddt.vo_id AS 'Voucher'
,csddt.glnum AS 'GL Acct'
,csddt.glrc1 AS 'GL Dept'
,gl.gldes AS 'GL Description'
,gl.glnum + ' ' + gl.gldes AS 'GL Acct Description'
,csddt.amt AS 'Amount'
,ap.apct1 AS 'Vendor Type'
,voiddt AS 'Void Date'
,dbo.udfCsdDes(csddt.baid,csddt.cknum,csddt.ckline) AS 'Transaction Description'
FROM
csddt
INNER JOIN ap ON csddt.apnum = ap.apnum
INNER JOIN csd ON csddt.cknum = csd.cknum AND csddt.baid = csd.baid
INNER JOIN apaddr ON csddt.apnum = apaddr.apnum AND csddt.apaddid=apaddr.apaddid
INNER JOIN gl ON csddt.glnum = gl.glnum AND csddt.glrc1=gl.glrc1
WHERE
(csddt.glnum BETWEEN '62500' AND '63320'
OR csddt.glnum = '53100'
OR gl.glnum BETWEEN '84100' AND '84199'
OR gl.glnum BETWEEN '84400' AND '84499'
OR csddt.glnum = '93145')
AND csddt.ckdate BETWEEN @startdate AND @enddate
UNION
SELECT
gjid
,gjline
,gjtrdt
,MONTH(gjtrdt)
,NULL
,NULL
,NULL
,NULL
,NULL
,gj.glnum
,gj.glrc1
,gl.gldes
,gl.glnum + ' ' + gl.gldes AS 'glnumdes'
,ISNULL(dbamt,0)-ISNULL(cramt,0)
,NULL
,NULL
,des + ' ' + dbo.udfGJDes(gjid,gjline)
FROM gj
INNER JOIN gl ON gj.glnum=gl.glnum AND gj.glrc1=gl.glrc1
WHERE gjtrdt BETWEEN @startdate AND @enddate
AND (gl.glnum BETWEEN '62500' AND '63320'
OR gl.glnum = '53100'
OR gl.glnum BETWEEN '84100' AND '84199'
OR gl.glnum BETWEEN '84400' AND '84499'
OR gl.glnum = '93145')
GO
To execute the stored procedure, use this statementEXECUTE dbo.usp_GL_Expenses '1/1/2010','12/31/2010' GOMake sure to grant execute to any other users that need access to this stored procedure.
GRANT EXECUTE ON dbo.usp_GL_Expenses TO elitereadonly; GO
General Journal Entry Description User Defined Function
Below is a user defined function to concatenate all lines of a GJ entry description. Unlike most other tables in Elite, the GJ description table only includes records for the second through x lines. The first line of the description is stored in the gj table itself. So, to fully utilize this udf, it will need to be concatenated to the first line from the gj table.
Here's an example of how to use it properly
USE [son_db] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[udfGJDes] (@gjid VARCHAR(12), @gjline INT) RETURNS VARCHAR(1000) AS BEGIN DECLARE @description VARCHAR(1000) SELECT @description = '' SELECT @description = @description + ' ' + des FROM gjdes WHERE gjid = @gjid AND gjline = @gjline ORDER BY gjline RETURN LTRIM(RTRIM(@description)) END
Here's an example of how to use it properly
SELECT gjid ,gjline ,gjtrdt ,MONTH(gjtrdt) ,gj.glnum ,gj.glrc1 ,gl.gldes ,gl.glnum + ' ' + gl.gldes AS 'glnumdes' ,ISNULL(dbamt,0)-ISNULL(cramt,0) ,des + ' ' + dbo.udfGJDes(gjid,gjline) FROM gj INNER JOIN gl ON gj.glnum=gl.glnum AND gj.glrc1=gl.glrc1 WHERE gjtrdt BETWEEN '3/1/10' AND '3/31/10' AND gl.glnum = '20236'
Monday, March 15, 2010
Payments by Invoice Ledger Code
This is a simple little query to see how payments from a particular client have been applied by the original invoice ledger code. For example, you can determine how much of the payments were applied to fees or costs. You may need to tinker with the criteria to match your setups. This uses the matter description udf.
SELECT mmatter AS 'Matter #' ,dbo.udfMattDesc(mmatter) AS 'Matter Description' ,SUM(CASE WHEN inv.llcode = 'FEES' THEN pmt.lamount ELSE 0 END) as 'Fees Collected' ,SUM(CASE WHEN inv.llcode <> 'FEES' THEN pmt.lamount ELSE 0 END) AS 'Costs Recovered' ,SUM(pmt.lamount) AS TOTAL FROM ledger AS pmt INNER JOIN matter ON pmt.lmatter=mmatter INNER JOIN ledger AS inv ON pmt.laptoin=inv.lindex WHERE YEAR(pmt.ltradat)='2009' AND mclient='014523' AND pmt.llcode LIKE 'PAY%' GROUP BY mmatter
Monday, February 22, 2010
Linked Servers
As you know by now, I write quite a bit of SQL for mining data from the Enterprise database. Occasionally, I'll need some data from another database on a different SQL Server to pair with some information from Elite. In SQL Server, you can create a linked server to be able to join across SQL Servers. For example, we use Minisoft's ARCS accounts receivable program, and I've created a linked server to join ARCS notes to real-time Elite data.
- Create a Linked Server to the other SQL Server
USE master GO EXEC master.dbo.sp_addlinkedserver @server = N'ARCS' , @srvproduct=N'' , @provider=N'SQLOLEDB' , @datasrc=N'ARCS-SERVERNAME' , @catalog=N'ARCS-DBNAME' GO
- Add login credentials for the other SQL Server (if you don't use trusted login)
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'ARCS' , @locallogin = NULL , @useself = N'False' , @rmtuser = N'arcs-username' , @rmtpassword = N'arcs-password' GO
- Write Query to access ARCS from the Elite database
SELECT TOP 100 * FROM ARCS.ARCS-DBNAME.dbo.ARCS_HOLDS
Wednesday, February 17, 2010
Checks for a Vendor with General Ledger Distribution
It is fairly easy to pull a list of checks for a vendor within AP Inquiry in Elite Enterprise. However, sometimes you just need a little more information. The following query will pull a list of checks along with the GL accounts affected by those checks.
A couple things to note:
A couple things to note:
- Due to the GL distribution piece, there may be more than 1 row per check.
- The cash portion of the GL distribution has been excluded (take out the udtrtp line if you would like that included)
- The glnum and glrc1 are combined into one number
- The user indicated is the user who created the batch. More than one user can post items to a batch.
SELECT ap.apnum AS 'Vendor #' ,ap.apname AS 'Vendor Name' ,csddt.cknum AS 'Check #' ,csddt.ckline AS 'Line' ,csddt.baid AS 'Bank' ,csddt.ckdate AS 'Date' ,csddt.vo_id AS 'Voucher #' ,csddt.glnum+'-'+csddt.glrc1 AS 'GL #' ,gldes AS 'GL Description' ,dbo.udfCsdDes(csddt.baid,csddt.cknum,csddt.ckline) AS 'Description' ,csddt.amt AS 'Amount' ,fmsbatch.opuserid AS 'User' FROM csddt INNER JOIN ap ON csddt.apnum=ap.apnum INNER JOIN fmsbatch ON csddt.cdbtid=fmsbtid AND fmsbatch.trtype='CK' INNER JOIN gl ON csddt.glnum=gl.glnum AND csddt.glrc1=gl.glrc1 INNER JOIN csd ON csd.baid=csddt.baid AND csd.cknum=csddt.cknum WHERE csddt.ckdate >= '1/1/2009' AND ap.apnum = '123456' AND udtrtp <> 'CA' AND csd.voiddt IS NULL ORDER BY csddt.ckdate
Subscribe to:
Posts (Atom)

