Modifying a query to work with multiple users using SEDE - tsql

I am having a hard time getting my query to do what I want it to do...
If I run it for one UserId: 2140173 it seems to be working fine
Select UserId,
(Select Count(VoteTypeId) From SuggestedEditVotes where UserId = 2140173) as 'Total',
(Select Count(VoteTypeId) From SuggestedEditVotes where UserId = 2140173 and VoteTypeId = 2) As 'Accepted',
(Select Count(VoteTypeId) From SuggestedEditVotes where UserId = 2140173 and VoteTypeId = 3) As 'Rejected'
from SuggestedEditVotes
inner join Users on SuggestedEditVotes.UserId = Users.Id
where Users.Reputation > 2000 and UserId = 2140173
group by UserId
having Count(VoteTypeId) > 0
it returns
UserId Total Accepted Rejected
2140173 2230 1145 1085
But when I am trying to modify it slightly and run it for all users with more than 2000 reputation it does not give me the correct results :/
I am stuck with the sub Select statements as I am not sure what to put in their where clause..
This is what I have tried but it returns the totals and I want it to be a count for each user Id
Select UserId,
(Select Count(VoteTypeId) From SuggestedEditVotes) as 'Total',
(Select Count(VoteTypeId) From SuggestedEditVotes where VoteTypeId = 2) As 'Accepted',
(Select Count(VoteTypeId) From SuggestedEditVotes where VoteTypeId = 3) As 'Rejected'
from SuggestedEditVotes
inner join Users on SuggestedEditVotes.UserId = Users.Id
where Users.Reputation > 2000
group by UserId
having Count(VoteTypeId) > 0
Can anyone help?
NOTE: https://data.stackexchange.com/stackoverflow/query/new

Try this:
SELECT UserId,
COUNT(VoteTypeId) AS 'Total',
COUNT(
CASE
WHEN VoteTypeId = 2
THEN VoteTypeId
ELSE NULL
END) AS 'Accepted',
COUNT(
CASE
WHEN VoteTypeId = 3
THEN VoteTypeId
ELSE NULL
END) AS 'Rejected'
FROM SuggestedEditVotes
INNER JOIN Users
ON SuggestedEditVotes.UserId = Users.Id
WHERE Users.Reputation > 2000
GROUP BY UserId
HAVING COUNT(VoteTypeId) > 0

Related

View query without sub selecting T-SQL

so I'm trying to build a view query but I keep failing using only joins so I ended up with this deformation.. Any tips on how I can write this query so I don't have to use 6 subselects?
The FeeSum and PaymentSum can be null, so ideally I do not want those in my result set and I also wouldn't like results where the FeeSum and the PaymentSum are equal.
Quick note: client is the table where the clients informations are stored (name, adress, etc..)
customer has a fk on client and is kind of a shell table for the client that store more information for the client,
payment is a list of all payments a customer did,
order is a list of all orders a customer did.
The goal is to get a list where we can track which customer has open fees to pay, based on the orders. It's a legacy project so don't ask why people can order before paying :)
SELECT
cu.Id as [CustomerId]
, CASE
WHEN cl.IsPerson = 1
THEN cl.[AdditionalName] + ' ' + cl.[Name]
ELSE cl.AdditionalName
END as [Name]
, cl.CustomerNumber
, (SELECT SUM(o.Fee) FROM [publication].[Order] o WHERE o.[State] = 2 AND o.CustomerId = cu.Id) as [FeeSum]
, (SELECT SUM(p.Amount) FROM [publication].[Payment] p WHERE p.CustomerId = cu.Id) as [PaymentSum]
, (SELECT MAX(o.OrderDate) FROM [publication].[Order] o WHERE o.[State] = 2 AND o.CustomerId = cu.Id) as [LastOrderDate]
, (SELECT MAX(p.PaymentDate) FROM [publication].[Payment] p WHERE p.CustomerId = cu.Id) as [LastPaymentDate]
, (SELECT MAX(f.Created) FROM [client].[File] f WHERE f.TemplateName = 'Reminder' AND f.ClientId = cl.Id) as [LastReminderDate]
, (SELECT MAX(f.Created) FROM [client].[File] f WHERE f.TemplateName = 'Warning' AND f.ClientId = cl.Id) as [LastWarningDate]
FROM
[publication].[Customer] cu
JOIN
[client].[Client] cl
ON cl.Id = cu.ClientId
WHERE
cu.[Type] = 0
Thanks in advance and I hope I didn't do anything wrong.
Kind regards
You could rewrite the correlated subqueries to instead use joins:
SELECT
cu.Id AS [CustomerId],
CASE WHEN cl.IsPerson = 1
THEN cl.[AdditionalName] + ' ' + cl.[Name]
ELSE cl.AdditionalName END AS [Name],
cl.CustomerNumber,
o.FeeSum,
p.PaymentSum,
o.LastOrderDate,
p.LastPaymentDate,
f.LastReminderDate,
f.LastWarningDate
FROM [publication].[Customer] cu
INNER JOIN [client].[Client] cl
ON cl.Id = cu.ClientId
INNER JOIN
(
SELECT CustomerId, SUM(Fee) AS [FeeSum], MAX(OrderDate) AS [LastOrderDate]
FROM [publication].[Order]
WHERE o.[State] = 2
GROUP BY CustomerId
) o
ON o.CustomerId = cu.Id
INNER JOIN
(
SELECT CustomerId, SUM(Amount) AS [PaymentSum], MAX(PaymentDate) AS [LastPaymentDate]
FROM [publication].[Payment]
WHERE o.[State] = 2
GROUP BY CustomerId
) p
ON p.CustomerId = cu.Id
INNER JOIN
(
SELECT ClientId,
MAX(CASE WHEN TemplateName = 'Reminder' THEN Created END) AS [LastReminderDate],
MAX(CASE WHEN TemplateName = 'Warning' THEN Created END) AS [LastWarningDate]
FROM [client].[File]
GROUP BY ClientId
) f
ON f.ClientId = cl.Id
WHERE
cu.[Type] = 0;

Aggregating columns and getting count of values as row

I have a query output as below:
ID ID2 Working Leave Off Day
14595 76885302 10 0 0
178489 78756208 0 0 1
178489 78756208 0 1 0
I want to receive an output like below:
ID ID2 code value
14595 76885302 Working 10
178489 78756208 Off day 1
178489 78756208 Leave 1
My query is like below:
select tei.organisationunitid,pi.trackedentityinstanceid as tei,
count(case when tedv.value = 'Working' then tedv.value end) Working,
count(case when tedv.value = 'Off day' then tedv.value end) Offday,
count(case when tedv.value = 'Leave' then tedv.value end) Leave
from programstageinstance psi
inner join programinstance pi on pi.programinstanceid = psi.programinstanceid
inner join trackedentitydatavalue tedv on tedv.programstageinstanceid = psi.programstageinstanceid
inner join dataelement de on de.dataelementid = tedv.dataelementid
inner join trackedentityinstance tei on tei.trackedentityinstanceid = pi.trackedentityinstanceid
where psi.executiondate between '2017-01-01' and '2019-06-01'
and de.uid in ('x2222EGfY4K')
and psi.programstageid in (select programstageid
from programstage
where uid = 'CLoZpO22228')
and tei.organisationunitid in (select organisationunitid
from organisationunit
where path like '%Spd2222fvPr%')
group by pi.trackedentityinstanceid,de.uid,tei.organisationunitid,tedv.value
How can I achieve this?
I would try the JSON approach. I made a step-by-step fiddle:
demo:db<>fiddle
SELECT
id, id2,
elements ->> 'code' AS code,
SUM((elements ->> 'value')::int) AS value
FROM (
SELECT
id,
id2,
json_build_object('code', 'working', 'value', working) AS working,
json_build_object('code', 'leave', 'value', leave) AS leave,
json_build_object('code', 'off_day', 'value', off_day) AS off_day
FROM
mytable
) s,
unnest(ARRAY[working, leave, off_day]) as elements
GROUP BY 1,2,3
HAVING SUM((elements ->> 'value')::int) > 0

Join 4 tables with group by, 2 having and where clause

I have the database consists of 4 tables:
users(id, "name", surname, birthdate)
friendships(userid1, userid2, "timestamp")
posts(id, userid, "text", "timestamp")
likes(postid, userid, "timestamp")
I need to get a result set of unique usernames having more than 3 friendships within January of 2018 and their "likes" average per "post" in the range of [10; 35).
I wrote this statement for the first step:
select distinct u."name"
from users u
join friendships f on u.id = f.userid1
where f."timestamp" between '2018-01-01'::timestamp and '2018-01-31'::timestamp
group by u.id
having count(f.userid1) > 3;
It's working fine and returns 3 rows. But when I'm adding the second part this way:
select distinct u."name"
from users u
join friendships f on u.id = f.userid1
join posts p on p.userid = u.id
join likes l on p.id = l.postid
where f."timestamp" between '2018-01-01'::timestamp and '2018-01-31'::timestamp
group by u.id
having count(f.userid1) > 3
and ((count(l.postid) / count(distinct l.postid)) >= 10
and (count(l.postid) / count(distinct l.postid)) < 35);
I'm getting crazy 94 rows. I don't know why.
Will be thankful for possible help.
You don't need distinct in u.name because aggregate will remove the duplicate.
select
u."name"
from
users u
inner join friendships f on u.id = f.userid1
inner join posts p on u.id = p.userid
inner join likes l on p.id = l.postid
where
f."timestamp" >= '2018-01-01'::timestamp
and f."timestamp" < '2018-02-01'::timestamp
group by
u."name"
having
count(distinct f.userid1) > 3
and ((count(l.postid) / count(distinct l.postid)) >= 10
and (count(l.postid) / count(distinct l.postid)) < 35);
As in comment stated. Not good idea when you use between for date to do range.
f."timestamp" >= '2018-01-01'::timestamp
and f."timestamp" < '2018-02-01'::timestamp
Will give you a full month of January.
Try the below! The issue with using "count(f.userid1) > 3" is that if a user has , e.g. 2 friends and 6 posts and 3 likes they'll get 2 x 6 = 12 rows, so 12 records with non-null f.userid1. By counting distinct f.userid2 you can count distinct friends. Similar issues appear for the other counts used for filtering.
select u."name"
from users u
join friendships f on u.id = f.userid1
join posts p on p.userid = u.id
left join likes l on p.id = l.postid
where f."timestamp" > '2018-01-01'::timestamp and f."timestamp" < '2018-02-01'::timestamp
group by u.id, u."name"
having
--at least three distinct friends
count( distinct f.userid2) > 3
--distinct likes / distinct posts
--we use l.* to count distinct likes since there's no primary key
and ((count(distinct l.*) / count(distinct p.id)) >= 10
and ((count(distinct l.*) / count(distinct p.id)) < 35);

calculate the percentage of users from CTE

I've 2 CTE. The first counts the number of users. The second does the same. It is necessary to calculate the percentage ratio between them.
Prompt how it can be done?
WITH count AS ( SELECT user_id
from users u
where u.status = 'Over'),
users as (Select user_id
from users u
where u.status LIKE 'LR'
and user_id IN (select * from count))
Select COUNT(*) From users
WITH count AS ( SELECT user_id
from users u
where u.description = 'Track'),
users as (Select user_id
from from users u
where u.status NOT LIKE 'LR'
and user_id IN (select * from count))
Select COUNT(*) From users
You can do it without CTE, just simple select with 2 counts:
SELECT count( CASE WHEN description = 'Over' AND status LIKE 'LR' THEN 1 END )
/
count( CASE WHEN description = 'Track' AND status NOT LIKE 'LR' THEN 1 END )
As Ratio
FROM users
With minimal changes, you can just do one bigger CTE:
WITH count_1 AS
(
SELECT user_id
FROM users u
WHERE u.status = 'Over'
),
users_1 AS
(
SELECT user_id
FROM users u
WHERE u.status LIKE 'LR'
AND user_id IN (SELECT user_id FROM count_1)
),
count_2 AS
(
SELECT user_id
FROM users u
WHERE u.description = 'Track'
),
users_2 AS
(
SELECT user_id
FROM users u
WHERE u.status NOT LIKE 'LR'
AND user_id IN (select user_id from count_2)
)
SELECT
CAST( (SELECT count(*) FROM users_1) AS FLOAT) /
(SELECT count(*) FROM users_2) AS ratio
NOTE 1: The query doesn't make any sense, so I guess there is some misspelling, or some columns messed up. The count_1 will choose users with a status = 'Over', the users_1 will choose the ones which have also a status = 'LR' (the result is already ZERO).
NOTE 2: You wouldn't make queries this way... The following query means exactly the same, and is much simpler (and faster):
WITH
count_1 AS
(
SELECT count(user_id) AS c
FROM users u
WHERE u.description = 'Over'
AND u.status = 'LR'
),
count_2 AS
(
SELECT count(user_id) AS c
FROM users u
WHERE u.description = 'Track'
AND u.status <> 'LR'
)
SELECT
(count_1.c + 0.0) / count_2.c AS ratio
FROM
count_1, count_2 ;
Yet another version:
SELECT count(*) FILTER (WHERE description = 'Over' AND status LIKE 'LR')
/
count(*) FILTER (WHERE description = 'Track' AND status NOT LIKE 'LR')
As Ratio
FROM users

how to select top 10 without duplicates

Using SQL Server 2012
I need to select TOP 10 Producer based on a ProducerCode. But the data is messed up, users were entering same Producers just spelled differently and with the same ProducerCode.
So I just need TOP 10, so if the ProducerCode is repeating, I just want to pick the first one in a list.
How can I achieve that?
Sample of my data
;WITH cte_TopWP --T
AS
(
SELECT distinct ProducerCode, Producer,SUM(premium) as NetWrittenPremium,
SUM(CASE WHEN PolicyType = 'New Business' THEN Premium ELSE 0 END) as NewBusiness1,
SUM(CASE WHEN PolicyType = 'Renewal' THEN Premium ELSE 0 END) as Renewal1,
SUM(CASE WHEN PolicyType = 'Rewrite' THEN Premium ELSE 0 END) as Rewrite1
FROM ProductionReportMetrics
WHERE YEAR(EffectiveDate) = 2016 AND TransactionType = 'Policy' AND CompanyLine = 'Arch Insurance Company'--AND ProducerType = 'Wholesaler'
GROUP BY ProducerCode,Producer
)
,
cte_Counts --C
AS
(
SELECT distinct ProducerCode, ProducerName, COUNT (distinct ControlNo) as Submissions2,
SUM(CASE WHEN QuotedPremium IS NOT NULL THEN 1 ELSE 0 END) as Quoted2,
SUM(CASE WHEN Type = 'New Business' AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as NewBusiness2,
SUM(CASE WHEN Type = 'Renewal' AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as Renewal2,
SUM(CASE WHEN Type = 'Rewrite' AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as Rewrite2,
SUM(CASE WHEN Status = 'Declined' THEN 1 ELSE 0 END ) as Declined2
FROM ClearanceReportMetrics
WHERE YEAR(EffectiveDate)=2016 AND CompanyLine = 'Arch Insurance Company'
GROUP BY ProducerCode,ProducerName
)
SELECT top 10 RANK() OVER (ORDER BY NetWrittenPremium desc) as Rank,
t.ProducerCode,
c.ProducerName as 'Producer',
NetWrittenPremium,
t.NewBusiness1,
t.Renewal1,
t.Rewrite1,
c.[NewBusiness2]+c.[Renewal2]+c.[Rewrite2] as PolicyCount,
c.Submissions2,
c.Quoted2,
c.[NewBusiness2],
c.Renewal2,
c.Rewrite2,
c.Declined2
FROM cte_TopWP t --LEFT OUTER JOIN tblProducers p on t.ProducerCode=p.ProducerCode
LEFT OUTER JOIN cte_Counts c ON t.ProducerCode=c.ProducerCode
You should use ROW_NUMBER to fix your issue.
https://msdn.microsoft.com/en-us/library/ms186734.aspx
A good example of this is the following answer:
https://dba.stackexchange.com/a/22198
Here's the code example from the answer.
SELECT * FROM
(
SELECT acss_lookup.ID AS acss_lookupID,
ROW_NUMBER() OVER
(PARTITION BY your_distinct_column ORDER BY any_column_you_think_is_appropriate)
as num,
acss_lookup.product_lookupID AS acssproduct_lookupID,
acss_lookup.region_lookupID AS acssregion_lookupID,
acss_lookup.document_lookupID AS acssdocument_lookupID,
product.ID AS product_ID,
product.parent_productID AS productparent_product_ID,
product.label AS product_label,
product.displayheading AS product_displayheading,
product.displayorder AS product_displayorder,
product.display AS product_display,
product.ignorenewupdate AS product_ignorenewupdate,
product.directlink AS product_directlink,
product.directlinkURL AS product_directlinkURL,
product.shortdescription AS product_shortdescription,
product.logo AS product_logo,
product.thumbnail AS product_thumbnail,
product.content AS product_content,
product.pdf AS product_pdf,
product.language_lookupID AS product_language_lookupID,
document.ID AS document_ID,
document.shortdescription AS document_shortdescription,
document.language_lookupID AS document_language_lookupID,
document.document_note AS document_document_note,
document.displayheading AS document_displayheading
FROM acss_lookup
INNER JOIN product ON (acss_lookup.product_lookupID = product.ID)
INNER JOIN document ON (acss_lookup.document_lookupID = document.ID)
)a
WHERE a.num = 1
ORDER BY product_displayheading ASC;
You could do this:
SELECT ProducerCode, MIN(Producer) AS Producer, ...
GROUP BY ProducerCode