Trying to get count of episodes in a series [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 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

Related

How to fetch data using where clause 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 4 months ago.
Improve this question
There is no error, but the results show the values excluding the row that meets both conditions. The result shows 3, in fact, it should be 4 excluding data that has 22 for age and UK for the country. Any idea why the result is 3, not 4?
SELECT COUNT(*) FROM CUSTOMERS WHERE CUSTOMERS.AGE = '22' OR CUSTOMERS.COUNTRY = 'UK';
You have two customers at the age of 22 and 2 residing in the UK.
This is actually a correct result here - how on earth do you come to the conclusion that you have four people meeting that criteria? If more than one point matches for a database entry, it will only count as one for the result, since it's still the same database entry.

SQL - Getting count by Distinct based on Type [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 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)

Aggregation depends from value [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 1 year ago.
Improve this question
I have a sample table
ID
Invoice_ID
docType
1
100
email
2
100
sms
3
200
email
4
200
email
5
300
sms
and I have to get results with only rows that have invoices with email and sms docType. For this example its only invoice_id 100
One approach is to query those type and count the distinct number of different types that appear:
SELECT invoice_id
FROM invoices
WHERE doctype IN ('sms', 'email')
GROUP BY invoice_id
HAVING COUNT(DISTINCT doctype) = 2
One method uses aggregation:
select invoice_id
from t
where docType in ('email', 'sms')
group by invoice_id
having count(distinct doctype) = 2;

Selecting Item that has Max of one but Min of another 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 have a table that lists players on a team, the number of years on the team, and the number of points scored. What I'm trying to do is return the person that has the most number of points scored and the least number of years on the team. This is what I've done:
SELECT player
FROM team
GROUP BY player
HAVING MAX(points) and MIN(years);
Example of a table and expected output:
player | years | points
-------+-------+---------
p1 | 2 | 200
p2 | 5 | 10
p3 | 1 | 500
From this I expect to get back p3 since they have the min for years and the max for points.
This will give you players with max points, and also players with min years:
SELECT player, points, years
FROM team
where points = (select max(points) from team)
or years = (select min(years) from team)

Hive output query for selecting particular value based on groupby clause [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 years ago.
Improve this question
I have a table with data like:
cust_id, acct_no, ind
123111, 1233, Y
123111, 2311, N
222111, 1112, N
222111, 2111, N
I have to get output as cust_id, 1 (a binary indicator if any of the acct under that customer is Y)
so from the above table I have to get below output.
123111 1
222111 0
A simple way to achieve this is something like:
select cust_id, max(case when ind = 'Y' then 1 else 0 end) as flag from customers group by cust_id;