CTEs count and sum - sql

I am trying to get the sum of the count I did on "cll.Claim_number" column.The below CTE is part of 3 othe CTEs and the CTEs run perfectly but now I want to only bring in the "sum" of "COUNTOFSERVICES" from the count I did on cll.Claim_number. I want the output field to have only the sum with the rest of the fields I call out that's it not the count. Below is what I have but it gives both the count and sum in the output when I only want the sum . Thanks
Select
cll.company_desc as state,
cll.line_of_business_desc as Lineofbus,
cll.product_desc as Product,
cll.paymentdate as YearMonth,
cll.typeofservice,
prt.prov_type_short_desc as ProviderSpecialty,
cll.whole_claim_status_desc as Outcome,
count(cll.Claim_number) as "COUNTOFSERVICES",
sum(cll.Claim_number) as total
from claims cll
left join dw.DIM_PROVIDER_TYPE prt -- bringing in provider spec from claim
on cll.prov_type_dim_id = prt.prov_type_dim_id
Where cll.uniquerow = '1'--dedupping
Group By
cll.company_desc,
Cll.line_of_business_desc,
cll.product_desc,
cll.paymentdate,
--cll.memb_dim_id,
cll.typeofservice,
prt.prov_type_short_desc,
cll.whole_claim_status_desc,
cll.Claim_number

Thanks #jarlh
I removed the cll.claimnumber from Group and did the below and I got exactly what I was looking for . I also removed the sum since the count did exactly what I wanted after removing the claimnumber from GROUP.
Providers as (
Select
cll.company_desc as state,
cll.line_of_business_desc as Lineofbus,
cll.product_desc as Product,
cll.paymentdate as YearMonth,
cll.typeofservice,
prt.prov_type_short_desc as ProviderSpecialty,
cll.whole_claim_status_desc as Outcome,
count(cll.Claim_number) as "COUNTOFSERVICES"
--sum(cll.Claim_number) as total --removed
from claims cll
left join dw.DIM_PROVIDER_TYPE prt -- bringing in provider spec from claim
on cll.prov_type_dim_id = prt.prov_type_dim_id
Where cll.uniquerow = '1'--dedupping
Group By
cll.company_desc,
Cll.line_of_business_desc,
cll.product_desc,
cll.paymentdate,
--cll.memb_dim_id,
cll.typeofservice,
prt.prov_type_short_desc,
cll.whole_claim_status_desc
) select * from providers

Related

SQL - Returning fields based on where clause then joining same table to return max value?

I have a table named Ticket Numbers, which (for this example) contain the columns:
Ticket_Number
Assigned_Group
Assigned_Group_Sequence_No
Reported_Date
Each ticket number could contain 4 rows, depending on how many times the ticket changed assigned groups. Some of these rows could contain an assigned group of "Desktop Support," but some may not. Here is an example:
Example of raw data
What I am trying to accomplish is to get the an output that contains any ticket numbers that contain 'Desktop Support', but also the assigned group of the max sequence number. Here is what I am trying to accomplish with SQL:
Queried Data
I'm trying to use SQL with the following query but have no clue what I'm doing wrong:
select ih.incident_number,ih.assigned_group, incident_history2.maxseq, incident_history2.assigned_group
from incident_history_public as ih
left join
(
select max(assigned_group_seq_no) maxseq, incident_number, assigned_group
from incident_history_public
group by incident_number, assigned_group
) incident_history2
on ih.incident_number = incident_history2.incident_number
and ih.assigned_group_seq_no = incident_history2.maxseq
where ih.ASSIGNED_GROUP LIKE '%DS%'
Does anyone know what I am doing wrong?
You might want to create a proper alias for incident_history. e.g.
from incident_history as incident_history1
and
on incident_history1.ticket_number = incident_history2.ticket_number
and incident_history1.assigned_group_seq_no = incident_history2.maxseq
In my humble opinion a first error could be that I don't see any column named "incident_history2.assigned_group".
I would try to use common table expression, to get only ticket number that contains "Desktop_support":
WITH desktop as (
SELECT distinct Ticket_Number
FROM incident_history
WHERE Assigned_Group = "Desktop Support"
),
Than an Inner Join of the result with your inner table to get ticket number and maxSeq, so in a second moment you can get also the "MAXGroup":
WITH tmp AS (
SELECT i2.Ticket_Number, i2.maxseq
FROM desktop D inner join
(SELECT Ticket_number, max(assigned_group_seq_no) as maxseq
FROM incident_history
GROUP BY ticket_number) as i2
ON D.Ticket_Number = i2.Ticket_Number
)
SELECT i.Ticket_Number, i.Assigned_Group as MAX_Group, T.maxseq, i.Reported_Date
FROM tmp T inner join incident_history i
ON T.Ticket_Number = i.Ticket_Number and i.assigned_group_seq_no = T.maxseq
I think there are several different method to resolve this question, but I really hope it's helpful for you!
For more information about Common Table Expression: https://www.essentialsql.com/introduction-common-table-expressions-ctes/

Select only the row with the max value, but the column with this info is a SUM()

I have the following query:
SELECT DISTINCT
CAB.CODPARC,
PAR.RAZAOSOCIAL,
BAI.NOMEBAI,
SUM(VLRNOTA) AS AMOUNT
FROM TGFCAB CAB, TGFPAR PAR, TSIBAI BAI
WHERE CAB.CODPARC = PAR.CODPARC
AND PAR.CODBAI = BAI.CODBAI
AND CAB.TIPMOV = 'V'
AND STATUSNOTA = 'L'
AND PAR.CODCID = 5358
GROUP BY
CAB.CODPARC,
PAR.RAZAOSOCIAL,
BAI.NOMEBAI
Which the result is this. Company names and neighborhood hid for obvious reasons
The query at the moment, for those who don't understand Latin languages, is giving me clients, company name, company neighborhood, and the total value of movements.
in the WHERE clause it is only filtering sales movements of companies from an established city.
But if you notice in the Select statement, the column that is retuning the value that aggregates the total amount of value of sales is a SUM().
My goal is to return only the company that have the maximum value of this column, if its a tie, display both of em.
This is where i'm struggling, cause i can't seem to find a simple solution. I tried to use
WHERE AMOUNT = MAX(AMOUNT)
But as expected it didn't work
You tagged the question with the whole bunch of different databases; do you really use all of them?
Because, "PL/SQL" reads as "Oracle". If that's so, here's one option.
with temp as
-- this is your current query
(select columns,
sum(vrlnota) as amount
from ...
where ...
)
-- query that returns what you asked for
select *
from temp t
where t.amount = (select max(a.amount)
from temp a
);
You should be able to achieve the same without the need for a subquery using window over() function,
WITH T AS (
SELECT
CAB.CODPARC,
PAR.RAZAOSOCIAL,
BAI.NOMEBAI,
SUM(VLRNOTA) AS AMOUNT,
MAX(VLRNOTA) over() AS MAMOUNT
FROM TGFCAB CAB
JOIN TGFPAR PAR ON PAR.CODPARC = CAB.CODPARC
JOIN TSIBAI BAI ON BAI.CODBAI = PAR.CODBAI
WHERE CAB.TIPMOV = 'V'
AND STATUSNOTA = 'L'
AND PAR.CODCID = 5358
GROUP BY CAB.CODPARC, PAR.RAZAOSOCIAL, BAI.NOMEBAI
)
SELECT CODPARC, RAZAOSOCIAL, NOMEBAI, AMOUNT
FROM T
WHERE AMOUNT=MAMOUNT
Note it's usually (always) beneficial to join tables using clear explicit join syntax. This should be fine cross-platform between Oracle & SQL Server.

How to use SUM in this situation?

I have the following tables below and their schema:
INV
id, product code, name, ucost, tcost, desc, type, qoh
1,123,CPASS 700,1.00,5.00,CPASS 700 Lorem, COM,5
2,456,Shelf 5,2.00,6.00,Shelf 5 KJ, BR,3
GRP
id,type,desc
1,COM,COMPASS
2,BR,SHELF
Currently I have a query like this:
SELECT INV.*,GRP.DESCR AS CATEGORY
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
There is no problems with that query.
Right now,I want to know the SUM of the TCOST of every INV record where their QOH is 0.
In this situation, does that I mean all I have to do is to write a separate query like the one below:
SELECT SUM(TCOST)
FROM INV
WHERE QOH = 0
Does it make any sense for me to try and combine those two queries as one ?
First understand that SUM is the aggregate function hence either you can run the Query like
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as total
This will return Sum of TCOST in INV Table for mentioned condition.
Another approach is finding the Sum based on the some column (e.g. Type)
you could write query like
SELECT Type , SUM(TCOST) FROM INV WHERE QOH=0 GROUP BY type ;
Its not clear on what criteria you want to sum . But I think above two approaches would provide you fare idea .
Mmm, you could maybe use a correlated query, though i'm not sure it's the best approach since I'm not sure I understand what your attempting to do:
SELECT INV.*,
GRP.DESCR AS CATEGORY ,
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as your_sum
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
If you want only one value for the sum(), then your query is fine. If you want a new column with the sum, then use window functions:
SELECT INV.*, GRP.DESCR AS CATEGORY,
SUM(INV.TCOST) OVER () as sum_at_zero
FROM INV LEFT JOIN
GRP
ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0;
It does not make sense to combine the queries by adding a row to the first one, because the columns are very different. A SQL result set requires that all rows have the same columns.

mSSQL, SQL view, select, percentage query

So this is the requirement I need to meet:
Aggregated data of all the schools in the ESD, grouped by
SchoolDistrict.SchoolDistrictID
(get the same data as the school district scenario above, then add grouping by district, filtered to
EducationServiceDistrict. EducationServiceDistrictID
)
Also calculate percentage of pass, fail, and untested
How do I calculate the percentage pass, fail, and untested?
This is the query I have written so far.
CREATE VIEW district_map AS
SELECT * and SchoolDistrictID,
EducationServiceDistrict
FROM SchoolDistrict_View
and SchoolDistrict,
EducationServiceDistrict
GROUP BY EducationServiceDistrict.EducationServiceDistrictID
ORDER BYLeadWaterTestLocation.PassFail
This is the general idea of how these problems are solved - if you understand this simplified version you will be able to solve your problem.
select d.districtName,
s.studentCount,
case when s.studentCount=0 then 0 else s.passed / s.studentCount * 100 end as PassedPct
from district d
join (select districtId,
sum(studentCount) as studentCount,
sum(passed) as passed
from schools
group by districtId) as s
on d.districtId = s.districtId
order by d.districtName

PSQL: sum doesn't work on fields

I have this query which works:
SELECT
partners.name,
roles.name,
(SELECT count(*) FROM partner_member_bindings WHERE roles.id = partner_member_bindings.role_id AND verify_status != 'pending') AS Num_verifications,
CAST(roles.price_partner / 100 AS money) AS partner_price
FROM partners
JOIN roles ON roles.partner_id = partners.id
ORDER BY partners.name, roles.name
What I want is to display an additional field showing the "partner_price * Num_verifications" value in dollars. Nothing I do works:
sum(Num_verifications * partner_price) returns that the Num_verification doesn't exist. Copying over the whole sub-query (yes, i know, but I'm just testing) also doesn't work.
How can this be done?
The fields in select can only contain input columns and they become output columns when they are run. That is why you cannot use a computed column in another column definition.
But if you want a SUM that would mean some kind of aggregation. If you want the total sum of the multiplication of the fields, that is not usually done per row. Or do you only need the multiplication result? That is usually done in the presentation layer.
If you only need the multiplication (since there is no GROUP BY the SUM would be that anyway) you can put the whole query into a subquery and calculate from that, leaving the sorting outside:
SELECT
partnername,
rolename,
Num_verifications,
partner_price,
partner_price*Num_verifications
FROM
(SELECT
partners.name as partnername,
roles.name as rolename,
(SELECT count(*) FROM partner_member_bindings WHERE roles.id = partner_member_bindings.role_id AND verify_status != 'pending') AS Num_verifications,
CAST(roles.price_partner / 100 AS money) AS partner_price
FROM partners
JOIN roles ON roles.partner_id = partners.id) temp
ORDER BY partners.name, roles.name