• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

TSQL syntax to get total for documents type in a open text field

I have this tally I need to build, but I don't have a set field.  In the data below anything that is in [brackets] is considered a document type.  I need to be able to get a total for all document type processed.  

Is there an sql syntax I could use to get this total?  The problem is that there may more document types than depicted here...

results expected would be:
[BATCH COVER SHEET] = 1
[TRANSFER FORMS] = 10
ETC...
DESC
===============================================================
Document[BATCH COVER SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[TRANSFER FORMS],New Pages[2],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[FACE SHEET],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[SIGNATURE AUTH FORM],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[PHYSICIAN ORDERS],New Pages[41],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[HISTORY AND PHYSICAL],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EMERGENCY DEPT],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[PROGRESS NOTE],New Pages[5],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[TRANSFUSION],New Pages[3],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]
Document[EKG],New Pages[1],Batch Prog[QCI], Id[4236894]

Open in new window

0
epicazo
Asked:
epicazo
1 Solution
 
edlunadCommented:
something like this should do it...

SELECT        SUBSTRING([desc], CHARINDEX('[', [desc]) + 1, CHARINDEX(']', [desc]) - CHARINDEX('[', [desc]) - 1) AS DocType, COUNT(SUBSTRING([desc], CHARINDEX('[', [desc]) + 1, CHARINDEX(']', [desc]) - CHARINDEX('[', [desc]) - 1)) AS Total

FROM            [YourTable]

GROUP BY SUBSTRING([desc], CHARINDEX('[', [desc]) + 1, CHARINDEX(']', [desc]) - CHARINDEX('[', [desc]) - 1)

Open in new window

0
 
PortletPaulCommented:
a variant is to use cross apply so that the calculations are available by their aliases which not only handy but can helps avoid errors if the source data doesn't contain [~~~~~]
SELECT
      doc
    , count(*)
FROM table1
CROSS apply (
             SELECT CHARINDEX('[', [desc]) + 1 as lft, CHARINDEX(']', [desc]) AS rght
            ) ca1
CROSS apply (
             SELECT CASE WHEN rght > lft THEN SUBSTRING([desc],lft,rght-lft) END AS doc
            ) ca2
WHERE doc IS NOT NULL
GROUP BY doc
;

|                  DOC | COLUMN_1 |
|----------------------|----------|
|    BATCH COVER SHEET |        1 |
|                  EKG |       10 |
|       EMERGENCY DEPT |       10 |
|           FACE SHEET |       10 |
| HISTORY AND PHYSICAL |       10 |
|     PHYSICIAN ORDERS |       10 |
|        PROGRESS NOTE |       10 |
|  SIGNATURE AUTH FORM |       10 |
|       TRANSFER FORMS |       10 |
|          TRANSFUSION |       10 |

http://sqlfiddle.com/#!3/0bdeb/7

Open in new window

0
 
epicazoAuthor Commented:
perfect!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now