April 12, 2016 at 10:48 am
Hi
I have this simple query:
SELECT
CompanyName as Company,
Batch,
USERNAME as [User Name],
SQLUserName as [System User Name],
--claim_form_type_id as [Claim Form Type],
CASE WHEN [claim_form_type_id] IS NULL THEN ('N/A') END as [Claim Form Type],
Claim_type as [Claim Type],--altered
status_type as [Status Type],
each as [Each],
member_last as [Member Name],
fromdate as [From Date],
todate as [To Date],
CASE WHEN [Status_Type] = 'Entered' THEN 1 ELSE 0 END AS Entered,
CASE WHEN [Status_Type] = 'Approved' THEN 1 ELSE 0 END AS Approved,
CASE WHEN [Status_Type] = 'Pending' THEN 1 ELSE 0 END AS Pending
FROM Report_data
The results come in fine but I need to add columns based on the . So that I end up with the number of claims per user name - but as additional columns. In other words - if user XYZ has 55 approved I need a column that says XYZ 55 approved and XYZ has 66 pending, etc. I still need the 'entered' as 1 columns.
April 12, 2016 at 12:14 pm
I'm thinking you'll need to group by your username, and maybe some other columns, and use the SUM(CASE trick to count the items meeting your criteria.
my first guess , based on your post:
SELECT
CompanyName as Company,
Batch,
USERNAME as [User Name],
--SQLUserName as [System User Name],
----claim_form_type_id as [Claim Form Type],
--CASE WHEN [claim_form_type_id] IS NULL THEN ('N/A') END as [Claim Form Type],
--Claim_type as [Claim Type],--altered
--status_type as [Status Type],
--each as [Each],
--member_last) as [Member Name],
MIN(fromdate) as [From Date],
MAX(todate) as [To Date],
SUM(CASE WHEN [Status_Type] = 'Entered' THEN 1 ELSE 0 END) AS Entered,
SUM(CASE WHEN [Status_Type] = 'Approved' THEN 1 ELSE 0 END) AS Approved,
SUM(CASE WHEN [Status_Type] = 'Pending' THEN 1 ELSE 0 END) AS Pending
FROM Report_data
GROUP BY
CompanyName,
Batch,
USERNAME
Lowell
April 12, 2016 at 1:01 pm
Okay my bad. I apologize. I didn't do a very good job of explaining what I am having trouble with.
I'm trying to get -
Number of APPROVED claims by username
Number of ENTERED claims by username
Number of PENDING claims by username
April 12, 2016 at 2:20 pm
yeah, i think my sum case will do exactly that for you.
what does this return? does it look right?
SELECT
USERNAME as [User Name],
SUM(CASE WHEN [Status_Type] = 'Entered' THEN 1 ELSE 0 END) AS Entered,
SUM(CASE WHEN [Status_Type] = 'Approved' THEN 1 ELSE 0 END) AS Approved,
SUM(CASE WHEN [Status_Type] = 'Pending' THEN 1 ELSE 0 END) AS Pending
FROM Report_data
GROUP BY
USERNAME
Lowell
April 13, 2016 at 6:23 am
I apologize Lowell.
I am not explaining this very well.
This query is going to be used for as part of a dashboard. Unfortunatley the dashboard software is kinda dumb and doesn't do aggreagations, groupings, etc.
So, what I need in my results is something like:
USER TOTAL OF APPROVED CLAIMS TOTAL OF ENTERED CLAIMS TOTAL OF PENDING CLAIMS
XYZ 100 5 3
RGC 44 22 22
April 13, 2016 at 6:37 am
did you try the query at all?
Lowell
April 13, 2016 at 8:11 am
Sure I did and the grouping is fine.
But after putting in the piece of junk dashboard software - I now think what I really have to have is dynamic column names so that the username and the number of approved, pending, etc for that user is actually the part of the column name.
XYZ_USER_APPROVED (column name) XYZ_USER_ENTERED
20 60
PDQ_USER_APPROVED PDQ_USER_ENTERED
22 33
April 14, 2016 at 9:27 am
krypto69 (4/13/2016)
Sure I did and the grouping is fine.But after putting in the piece of junk dashboard software - I now think what I really have to have is dynamic column names so that the username and the number of approved, pending, etc for that user is actually the part of the column name.
XYZ_USER_APPROVED (column name) XYZ_USER_ENTERED
20 60
PDQ_USER_APPROVED PDQ_USER_ENTERED
22 33
Does this mean that you do not want a single result set, but individual single-row result sets for each user?
April 14, 2016 at 10:38 am
Probably easier to kill the dashboard and rewrite it yourself so that you don't have to continue to tolerate its nuances.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 15, 2016 at 1:24 am
It's very difficult to attempt an answer without sample data and expected output.
That said, perhaps this will do the trick
SELECT src.*,
TotalEntered = SUM(Entered) OVER (PARTITION BY USERNAME),
TotalApproved = SUM(Approved) OVER (PARTITION BY USERNAME),
TotalPending = SUM(Pending) OVER (PARTITION BY USERNAME)
FROM (
SELECT
CompanyName as Company,
Batch,
USERNAME as [User Name],
SQLUserName as [System User Name],
--claim_form_type_id as [Claim Form Type],
CASE WHEN [claim_form_type_id] IS NULL THEN ('N/A') END as [Claim Form Type],
Claim_type as [Claim Type],--altered
status_type as [Status Type],
each as [Each],
member_last as [Member Name],
fromdate as [From Date],
todate as [To Date],
CASE WHEN [Status_Type] = 'Entered' THEN 1 ELSE 0 END AS Entered,
CASE WHEN [Status_Type] = 'Approved' THEN 1 ELSE 0 END AS Approved,
CASE WHEN [Status_Type] = 'Pending' THEN 1 ELSE 0 END AS Pending,
FROM Report_data
) AS src
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply