SQL - Getting count by Distinct based on Type [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have these three different result set.
If typeId 7 is duplicated between two different Ids, then they count as two. Any TypeId other than 7, count should distinct of Ids.
First table count is 5 because Id 1 is unique between 4 & 7. Id 2 is 3 times but other than Type 7, it is distinct. So, it count as 2.
How can I write a query for this.
I tried Distinct(Id) + Distinct Iff(TypeId = 7) and of course this return wrong result.

Count those that are not 7 distinct. (NULL isn't counted)
Then add the total unique id's of 7.
COUNT(DISTINCT CASE WHEN TypeId = 7 THEN NULL ELSE Id END) +
COUNT(DISTINCT CASE WHEN TypeId = 7 THEN Id END)

Related

Trying to get count of episodes in a series [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I want to figure out total number of episodes in a series. Lets say this series has 3 seasons, 10 episodes each season. I want a count that would provide the value of 30 (30 total episodes). The kicker is that this series has aired say in 2 different countries, some countries got only 1 season, and the other got 3. I still want to only pull the 30 unique total episode count.
The series has its own ID Each season has its own ID Each episode has its own ID Each country has its own ID
How can I accomplish this?
I am looking to get a total count of 30.
Ive tried various statements...
(CONCAT(seasonid,'_', episodeid, '_',seriesid,'_', countryid)) as a
count(distinct(CONCAT(seasonid,'_', episodeid, '_',seriesid,'_', countryid))) + sum(case when a is null then 0 else 1 end) as count
sum(case when (count(distinct (CONCAT(seasonid,'_', episodeid, '_',seriesid)) )) is not null then 1 else 0 end) as count
count( distinct(CONCAT(seriesid,'_', episodeid ))) as count

Auto increment for duplicate rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I have a table with column name No, which have duplicate values (which is necessary). I need to add an incrementing value in another column sub_no
For Example:
No
sub_no
1
0
1
1
1
2
2
0
2
1
3
0
3
1
3
2
3
3
I have tried update and select queries.
A window function will give you the results you want:
SELECT [No], ROW_NUMBER() OVER(PARTITION BY [No] ORDER BY [No]) - 1 AS
[sub_no]
FROM <table_name>;
You can find more information on window functions in T-SQL here.

How to do running subtraction in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I know how to do running sum but can't figure out how to do running subtract.
Example: we have single column of data and a new column will be formed for running subtraction.
Value
------
10
1
3
4
Output needs to be:
10
9
6
2
I need both these columns next to each other
Any suggestion please.
Your question only makes sense if you have a column that specifies the ordering. In that case:
select (case when row_number() over (order by <ordercol>) = 1
then col
else 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>)
end) as output
from t;
That is, return the column value on the first row. Otherwise, the math is a little tricky. But you want the first value minus the sum of the rest of the column. Arithmetically, this is the same as twice the first value minus the cumulative sum.
EDIT:
As Shawn points out, this can be simplified to:
select 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>) as output
from t;

Count by ID if value is lower than and set the value 1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a problem.
My table is tableXYZ
I have a row a where i set the year.
I have a row b where i need to set value 1 if the year from row a is lower than 2019 and 0 if is > 2019.
After all i need to make a count from row b only the 1 values as total.
How can i do that, because i tried a lot of examples but doesn't work.
This might help:
SELECT COUNT(1) AS [b_count]
FROM (
-- Use case to evaluate column a and set value for column b
SELECT a,
CASE WHEN a < 2019 then '1' else '0' end as [b]
FROM tableXYZ
) AS X -- Alias of subquery
WHERE X.b = '1' -- Select only rows where b = '1'

query to check multiple revsions on a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Need a query to check ids with multiple revisionnums
id reviionnum
1 0
2 0
1 1
3 1
2 1
The query should result
id revisionnum
3 1
Please help
If you want ids with only one revision number, you can use aggregation:
select id, min(revissionnum)
from t
group by id
having count(*) = 1;
The min() is the value if there is only one row.