Monday, May 10, 2010

Joint Invoice View

Often times it is helpful to know whether or not an invoice is a single matter or joint invoice. The following view is a pretty simple query that determines the answer to that question. You can then join this view to any query to include or exclude joint invoices from your results (or treat them differently).
USE [son_db]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[v_jointinv]
AS
SELECT
linvoice,
COUNT(DISTINCT lmatter) AS NumMtr,
CASE WHEN COUNT(DISTINCT lmatter) >1 THEN 'Yes' ELSE 'No' END AS JointInv
FROM
ledger
INNER JOIN ledcode ON llcode=lccode
WHERE
lcfco IS NOT NULL
GROUP BY linvoice
GO

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