Wrong data on SQL query - sql

My requirement is to fetch details from two tables REPORT_API_USAGE & REPORT_API_PAGES. I need to get TERMNAME, COUNT(TERMNAME) from REPORT_API_USAGE and COUNT(CTYPE) for both event and download from REPORT_API_PAGES. Both table has common field termid. The requirement is to get TERMNAME, COUNT(TERMNAME) with related events and file-download.
There is only one TERMID 24 in REPORT_API_PAGES With CTYPE as download. So the result should be Face-to-Face Learning 1 0 1
I have a page Face-to-Face Learning with TERMID'24. I have added an event and document with reference ofFace-to-Face Learning. Once i viewedFace-to-Face Learningpage,COUNT(TERMNAME)will store the page hit value inREPORT_API_USAGEtable. Once the event and document pages are viewed,REPORT_API_USAGEwill store the details withTERMIDreference. I need to create a report of views. Need to getFace-to-Face Learning` view count with related event and document hits.

You have two rows with termname='Face-to-Face Learning' in table report_api_usage therefore returned two lines.
You should use:
select ap.termname,
count(distinct ap.termname) as tot,
count(distinct ai.ctype) as event,
sum(case when ai.ctype = 'event' then 1 else 0 end) as page
from ...
I don't understand what you want count.. If you want count how many repoet_api_pages have ctype 'event' or 'download' use follows query:
select ap.termname,
count(ap.termname) as tot, -- if it total count rows in table report_api_usage, if you need unique count use distinct inside count function
sum(case when ai.ctype = 'download' then 1 else 0 end) as page_download
sum(case when ai.ctype = 'event' then 1 else 0 end) as page_event
from ...

Finlay i found the expensive query.
SELECT AIP.TERMID,
(select count(RAU.TERMNAME) FROM REPORT_API_USAGE RAU WHERE RAU.TERMID = AIP.TERMID AND RAU.VOCID = 4) as page_views,
(select sum(count(RAU.TERMNAME)) FROM REPORT_API_USAGE RAU WHERE RAU.VOCID = 4
and RAU.TERMID in (select AIP.TERMID from REPORT_API_PAGES AIP GROUP BY AIP.TERMID) group by RAU.VOCID) as page_views_sum,
(select sum(count(AIP2.TERMID)) from REPORT_API_PAGES AIP2 join REPORT_API_USAGE RAU ON (RAU.TERMID = AIP2.TERMID AND RAU.VOCID = 4) where aip2.ctype = 'download' GROUP by AIP2.termid) as download_sum,
(select sum(count(AIP2.TERMID)) from REPORT_API_PAGES AIP2 join REPORT_API_USAGE RAU ON (RAU.TERMID = AIP2.TERMID AND RAU.VOCID = 4) where aip2.ctype = 'event' GROUP by AIP2.termid) as event_sum,
COUNT(case when aip.ctype = 'download' then 1 else null end) as page_download,
COUNT(case when aip.ctype = 'event' then 1 else null end) as page_event
from REPORT_API_PAGES AIP
GROUP BY AIP.TERMID

Related

Aggregate Function on an Expression Containing A Subquery

With the following t-sql query:
select u.userid
into #temp
from user u
where u.type = 1;
select top 50
contentid,
count(*) as all_views,
sum(case when hc.userid in (select userid from #temp) then 1 else 0 end) as first_count,
sum(case when hc.userid in (40615, 40616) then 1 else 0 end) as another_count
from hitcounts hc
inner join user u on u.userid = hc.userid
group by hc.contentid
order by count(*) desc;
I get an error message
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
However, if just include the column 'another_count' (with the hard-coded list of identifiers), everything works as I expected. Is there a way I should go about only getting the count for userids contained within a subquery? I plan to have multiple columns, each counting up a set/subquery of different userids.
Performance is not a concern at this point and I appreciate any guidance.
You don't need a temporary table for this purpose. Just use a conditional aggregation:
select top 50 contentid,
count(*) as all_views,
sum(case when u.type = 1 then 1 else 0 end) as first_count,
sum(case when hc.userid in (40615, 40616) then 1 else 0 end) as another_count
from hitcounts hc join
user u
on u.userid = hc.userid
group by hc.contentid
order by count(*) desc;

Select how many documents are in other table for each person

I have 3 tables.
Client,Documents and ClientDocuments.
The first one is the clients,where the information for each client are.
The second one is the documents, which document can go in the system.
The third one is the ClientDocuments, which client has which document.
Tables Here
I have to do a select where i get the information from the clients, and how many documents of the 3 types of the documents they have.
Example,Client 1 Have one document called 'Contrato Social',and 2 called 'Ata de Negociação'.
In the select must return every client and in the columns ContratoSocial returns 1,Ata Negociacao returns 2 and Aceite de Condições Gerais returns 0.
I did this to show.
select idFornecedor,
txtNomeResumido,
txtNomeCompleto,
--txtEmail,
--txtSenha,
bitHabilitado,
(SELECT Count(idDocumentosFornecedoresTitulo) FROM tbDocumentosFornecedores WHERE idDocumentosFornecedoresTitulo = 1) AS 'Contrato Social',
(SELECT Count(idDocumentosFornecedoresTitulo) FROM tbDocumentosFornecedores WHERE idDocumentosFornecedoresTitulo = 2) AS 'Ata de Negociação',
(SELECT Count(idDocumentosFornecedoresTitulo) FROM tbDocumentosFornecedores WHERE idDocumentosFornecedoresTitulo = 3) AS 'Aceite de Condições Gerais'
from dbo.tbFornecedores tbf
order by tbf.txtNomeResumido asc
returns this:
Returns of the query
But its just counting how many documents from that type is in the database, i want to filter for each client, how should i do?
Working answer:
select tbf.idFornecedor, tbf.txtNomeResumido, tbf.txtNomeCompleto,
tbf.bitHabilitado,
sum(case when idDocumentosFornecedoresTitulo = 1 then 1 else 0 end) as contrato_social,
sum(case when idDocumentosFornecedoresTitulo = 2 then 1 else 0 end) as Ata_de_Negociação,
sum(case when idDocumentosFornecedoresTitulo = 3 then 1 else 0 end) as Aceite_de_Condições_Gerais
from dbo.tbFornecedores tbf left join
tbDocumentosFornecedores df
on tbf.idFornecedor = df.idFornecedor
group by tbf.idFornecedor, tbf.txtNomeResumido, tbf.txtNomeCompleto, tbf.bitHabilitado
order by tbf.txtNomeResumido asc
You need some way of matching the rows in tbDocumentosFornecedores to the rows in tbFornecedores. Your question is not clear on what column is used for that, but I might guess something like idDocumentosFornecedore.
You could fix your query by using a correlation clause. However, I might instead suggest conditional aggregation:
select tbf.idFornecedor, tbf.txtNomeResumido, tbf.txtNomeCompleto,
tbf.bitHabilitado,
sum(case when idDocumentosFornecedoresTitulo = 1 then 1 else 0 end) as contrato_social,
sum(case when idDocumentosFornecedoresTitulo = 2 then 1 else 0 end) as Ata_de_Negociação,
sum(case when idDocumentosFornecedoresTitulo = 3 then 1 else 0 end) as Aceite_de_Condições_Gerais
from dbo.tbFornecedores tbf left join
tbDocumentosFornecedores df
on tbf.idDocumentosFornecedore = df.idDocumentosFornecedore -- this is a guess
group by tbf.idFornecedor, tbf.txtNomeResumido, tbf.txtNomeCompleto, tbf.bitHabilitado
order by tbf.txtNomeResumido asc

Deriving values based on result of the query and grouping the data

I am writing a sql where I am trying to pull out information of the status of the courses the user has enrolled. I am trying to return single record for each user. Two fields in the select list would derive the value in the following manner
CourseResultStatusId -
If the status of all the courses is passed then return the status as passed otherwise.
If any status is fail, the overall status is fail.
If any of the status is expired, then overall status is expired.
If any of the status is in-progress then overall status is in-progress
ExpiryDateTime - Training expiring (nearest date)
I need to apply the following logic on courses he has been assigned.
cr.ExpiryDateTime > GetDate() and cr.ExpiryDateTime <= dateadd(dd,30,getdate()) )
If you see below , the query I have written so far pulls the courses that each user has been enrolled but it is not a cumulative result. Do I need to group, if yes would need help.
DECLARE #Rep1 INT;
SET #Rep1 = 13119;
SELECT
cr.[CourseID]
,cr.[UserID]
,u.[Code]
,u.[DisplayName]
,t.[Name]
,cr.[CourseResultStatusID] AS [CourseResultStatusID]
,crs.[Description] AS [CourseResultStatusDescription]
,c.[PointsRequired]
,cr.[ExpiryDateTime]
FROM [training].[CourseResult] cr
INNER JOIN [training].[Course] c
ON cr.[CourseID] = c.[ID] and c.[IsOptional] = 0 -- and cr.ExpiryDateTime > GetDate() and cr.ExpiryDateTime <= dateadd(dd,30,getdate())
INNER JOIN [training].[CourseResultStatus] crs
ON cr.[CourseResultStatusID] = crs.[ID]
INNER JOIN org.RepresentativeTierHistory rth on rth.RepresentativeID = cr.[UserID] and GetDate() between rth.StartDate and rth.EndDate
INNER JOIN org.tier t on t.ID = rth.TierID
LEFT JOIN [org].[User] u
ON u.[ID] = cr.[UserID]
WHERE cr.[UserID] IN (
SELECT hd.DescendantId FROM org.HierarchyDescendant hd WHERE hd.RepresentativeId = #Rep1 UNION ALL SELECT #Rep1 -- for management exchange info
)
order by UserID
The result of the query is as follows. I have circled to show you records that belong to a particular user and the columns that I am interested in . I need help in getting single record for each user based on the of logic that I mentioned above.
If I followed you correctly, you can implement the priorization rules on the overall result of each user using conditional aggregation.
Starting from your existing query, the logic would be:
select
cr.[UserID],
case
when min(case when crs.[Description] = 'Complete' then 1 else 0 end) = 1
then 'Complete'
when max(case when crs.[Description] = 'Fail' then 1 else 0 end) = 1
then 'Fail'
when max(case when crs.[Description] = 'Expired' then 1 else 0 end) = 1
then 'Expired'
when max(case when crs.[Description] = 'In Progress' then 1 else 0 end) = 1
then 'In Progress'
end as ResultStatus
from ...
where ...
group by cr.[UserID]
As for the date filtering logic, you should be able to implement it directly in the where clause.
It is possible that other parts of your query can be optimized - you might want to ask a new question for this, providing proper sample data and desired results.

Count rows with and combine them with math operator

I have a table with different events for a case. I want to calculate how many times event A occurs for each case but subtract the amounts of event B
I have started with a code like this, but it does not work.
SELECT
((SELECT
case_id,
Count(*)
from database
where event = "A"
Group by case_id)
-
(SELECT
case_id,
Count(*)
from database
where event = "B"
Group by case_id)) as count,
case-id,
from database
I suspect that you want conditional aggregation:
select case_id,
( sum(case when event = 'A' then 1 else 0 end) -
sum(case when event = 'B' then 1 else 0 end)
) as diff
from database
group by case_id;

Sum a column and perform more calculations on the result? [duplicate]

This question already has an answer here:
How to use an Alias in a Calculation for Another Field
(1 answer)
Closed 3 years ago.
In my query below I am counting occurrences in a table based on the Status column. I also want to perform calculations based on the counts I am returning. For example, let's say I want to add 100 to the Snoozed value... how do I do this? Below is what I thought would do it:
SELECT
pu.ID Id, pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed,
Snoozed + 100 AS Test
FROM
Prospects p
INNER JOIN
ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE
p.Store = '108'
GROUP BY
pu.Name, pu.Id
ORDER BY
Name
I get this error:
Invalid column name 'Snoozed'.
How can I take the value of the previous SUM statement, add 100 to it, and return it as another column? What I was aiming for is an additional column labeled Test that has the Snooze count + 100.
You can't use one column to create another column in the same way that you are attempting. You have 2 options:
Do the full calculation (as #forpas has mentioned in the comments above)
Use a temp table or table variable to store the data, this way you can get the first 5 columns, and then you can add the last column or you can select from the temp table and do the last column calculations from there.
You can not use an alias as a column reference in the same query. The correct script is:
SELECT
pu.ID Id, pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END)+100 AS Snoozed
FROM
Prospects p
INNER JOIN
ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE
p.Store = '108'
GROUP BY
pu.Name, pu.Id
ORDER BY
Name
MSSQL does not allow you to reference fields (or aliases) in the SELECT statement from within the same SELECT statement.
To work around this:
Use a CTE. Define the columns you want to select from in the CTE, and then select from them outside the CTE.
;WITH OurCte AS (
SELECT
5 + 5 - 3 AS OurInitialValue
)
SELECT
OurInitialValue / 2 AS OurFinalValue
FROM OurCte
Use a temp table. This is very similar in functionality to using a CTE, however, it does have different performance implications.
SELECT
5 + 5 - 3 AS OurInitialValue
INTO #OurTempTable
SELECT
OurInitialValue / 2 AS OurFinalValue
FROM #OurTempTable
Use a subquery. This tends to be more difficult to read than the above. I'm not certain what the advantage is to this - maybe someone in the comments can enlighten me.
SELECT
5 + 5 - 3 AS OurInitialValue
FROM (
SELECT
OurInitialValue / 2 AS OurFinalValue
) OurSubquery
Embed your calculations. opinion warning This is really sloppy, and not a great approach as you end up having to duplicate code, and can easily throw columns out-of-sync if you update the calculation in one location and not the other.
SELECT
5 + 5 - 3 AS OurInitialValue
, (5 + 5 - 3) / 2 AS OurFinalValue
You can't use a column alias in the same select. The column alias do not precedence / sequence; they are all created after the eval of the select result, just before group by and order by.
You must repeat code :
SELECT
pu.ID Id,pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END)+ 100 AS Test
FROM
Prospects p
INNER JOIN
ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE
p.Store = '108'
GROUP BY
pu.Name, pu.Id
ORDER BY
Name
If you don't want to repeat the code, use a subquery
SELECT
ID, Name, LeadCount, Working, Uninterested,Converted, Snoozed, Snoozed +100 AS test
FROM
(SELECT
pu.ID Id,pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed
FROM Prospects p
INNER JOIN ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE p.Store = '108'
GROUP BY pu.Name, pu.Id) t
ORDER BY Name
or a view