Filter out combination of fields in HAVING clause - sql

I know this might be pretty basic but I thought it'll be much quicker here.
+----------+----------+---------+
| PersonID | No Shows | Cancels |
+----------+----------+---------+
| 1 | 2 | 0 |
| 1 | 5 | 1 |
| 1 | 0 | 0 |
| 2 | 0 | 0 |
| 2 | 0 | 0 |
| 3 | 1 | 0 |
| 3 | 0 | 0 |
| 4 | 1 | 1 |
| 4 | 3 | 2 |
| 4 | 0 | 0 |
+----------+----------+---------+
I'm trying to eliminate people which have combination of both Total of NoShows and Cancels as 0. Eg: PersonID=2
Attempt:
SELECT
PersonID
,SUM(NoShows) AS TotalNS
,SUM(Cancels) AS TotalCanc
FROM Table1
GROUP BY PersonID
HAVING SUM(NoShows) > 0 AND SUM(Cancels) > 0
This obviously won't work since it'll start removing persons if either of NoShows or Cancels is 0. Eg: PersonID=3
Desired Output:
+--------------+----------+---------+
| PersonID | No Shows | Cancels |
+--------------+----------+---------+
| 1 | 7 | 1 |
| 3 | 1 | 0 |
| 4 | 4 | 3 |
+--------------+----------+---------+

Try this out and let me know in case you face any difficulties.
SELECT
PersonID
,SUM(NoShows) AS TotalNS
,SUM(Cancels) AS TotalCanc
FROM Table1
GROUP BY PersonID
HAVING SUM(NoShows) > 0 or SUM(Cancels) > 0

Your question is:
I'm trying to obtain only those which have combination of both Total
of NoShows and Cancels as 0. Eg: PersonID=2
The answer is:
SELECT PersonID
FROM Table1
GROUP BY PersonID
HAVING SUM(NoShows) = 0 AND SUM(Cancels) = 0;
It is unclear to me why you would accept an answer with different logic.

Related

SQL create a new field sessions given the value of another field

I have problems approaching the following task.
Given a table like
| user_id | hit_id | new_session |
|---------------|--------------|--------------|
| 1 | 1 | 0 |
| 1 | 2 | 0 |
| 1 | 3 | 1 |
| 1 | 4 | 0 |
| ... | ... | ... |
| 5 | 19 | 0 |
where
the combination of user_id and hit_id is unique
new_session is a boolean that determines if the hit started a new session or not for this particular user
I want to create a new column, session_number that splits hit_ids into sessions, taking into account that:
the first row for each user_id, once ordered by hit_id asc gets a value of 1 for the new column session_number
as long as new_session is 0, the value of session_number stays the same
when new_session is 1, I have to sum up 1 to the actual session count
the logic works over a partition by user_id ordered by hit_id asc, and therefore once the user_id changes, the session count is reset
I have created a db-fiddle with some example data
The expected output for user_id = 1 (which cover multiple corner cases) would be:
| user_id | hit_id | new_session | session_number |
|---------------|--------------|--------------|----------------|
| 1 | 1 | 0 | 1 |
| 1 | 2 | 0 | 1 |
| 1 | 3 | 1 | 2 |
| 1 | 4 | 0 | 2 |
| 1 | 5 | 0 | 2 |
| 1 | 6 | 1 | 3 |
| 1 | 7 | 0 | 3 |
| 1 | 8 | 1 | 4 |
| 1 | 8 | 1 | 5 |
I have tried with a combination of lag(), rank(), and dense_rank(), but I always find a corner case that makes all the attempts unsuccessful. Additionally, I am totally sure that there is a very easy approach for that that I am not taking into account.
You can use a cumulative sum:
select pv.*,
(1 + sum(new_session) over (partition by user_id order by hit_id)) as session_number
from pageviews pv;
Here is a db-fiddle.

Semi-transposing a table in Oracle

I am having trouble semi-transposing the table below based on the 'LENGTH' column. I am using an Oracle database, sample data:
+-----------+-----------+--------+------+
| PERSON_ID | PERIOD_ID | LENGTH | FLAG |
+-----------+-----------+--------+------+
| 1 | 1 | 4 | 1 |
| 1 | 2 | 3 | 0 |
| 2 | 1 | 4 | 1 |
+-----------+-----------+--------+------+
I would like to lengthen this table based on the LENGTH row; basically duplicating the row for each value in the LENGTH column.
See the desired output table below:
+-----------+-----------+--------+------+
| PERSON_ID | PERIOD_ID | NUMBER | FLAG |
+-----------+-----------+--------+------+
| 1 | 1 | 1 | 1 |
| 1 | 1 | 2 | 1 |
| 1 | 1 | 3 | 1 |
| 1 | 1 | 4 | 1 |
| 1 | 2 | 1 | 0 |
| 1 | 2 | 2 | 0 |
| 1 | 2 | 3 | 0 |
| 2 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 |
| 2 | 1 | 3 | 1 |
| 2 | 1 | 4 | 1 |
+-----------+-----------+--------+------+
I typically work in Posgres so Oracle is new to me.
I've found some solutions using the connect by statement but they seem overly complicated, particularly when compared to the simple generate_series() command from Posgres.
A recursive CTE subtracting 1 from length until 1 is reached should work. (In Postgres too, BTW, should you need something working cross platform.)
WITH cte (person_id,
period_id,
number_,
flag)
AS
(
SELECT person_id,
period_id,
length number_,
flag
FROM elbat
UNION ALL
SELECT person_id,
period_id,
number_ - 1 number_,
flag
FROM cte
WHERE number_ > 1
)
SELECT *
FROM cte
ORDER BY person_id,
period_id,
number_;
db<>fiddle

SQL - How to avoid the joining of three queries into one

I have a SQL script that selects assignments that a student has been assigned. In order to find out if the student completed his assignment, i use a sub-query. Once the student finishes an assignment he should be able to work on the next one.
I figure I can do this by selected the top 1 assignment that has not been completed ( 0 value). Which I can do with an additional query, of the first query, but then i would need a third query to join that query together. Is there a way i can achieve this selection of the top 1 assignment that has a value of 0, with 2 queries or less?
First Attempt
SELECT ag.group_id,
ag.title,
ac.collection_id,
ag.order,
ac.NAME,
ac.isactive,
(SELECT top 1 iscompleted
FROM student_completion
WHERE fk_collection_id = collection_id
AND fk_student_id like '404')
AS isCompleted,
FROM assignments AS ag
JOIN assignments_collection AS ac
ON ag.fk_collection_id = ac.collection_id
Order BY group_id
/*
SELECT TOP 1 isCompleted
(SELECT ag.group_id,
ag.title,
ac.collection_id,
ag.order,
ac.NAME,
ac.isactive,
(SELECT top 1 iscompleted
FROM student_completion
WHERE fk_collection_id = collection_id
AND fk_student_id like '404')
AS isCompleted,
FROM assignments AS ag
JOIN assignments_collection AS ac
ON ag.fk_collection_id = ac.collection_id
Order BY group_id)
Where isCompleted = 0
.........
*/
Data
+----------+--------------+---------------+-------+----------------------+----------+-------------+
| group_id | title | collection_id | order | name | isactive | isCompleted |
+----------+--------------+---------------+-------+----------------------+----------+-------------+
| 1 | Assingment_1 | 5 | 0 | Welcome to Linux | 1 | 0 |
| 2 | Assingment_2 | 6 | 0 | Installation | 1 | 0 |
| 3 | Assingment_3 | 9 | 1 | Intro to Bash | 1 | 0 |
| 3 | Assingment_4 | 3 | 1 | Intro to Bash part 2 | 1 | 0 |
+----------+--------------+---------------+-------+----------------------+----------+-------------+
Expected Data
+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+
| group_id | title | collection_id | order | name | isactive | isCompleted | available |
+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+
| 1 | Assingment_1 | 5 | 0 | Welcome to Linux | 1 | 0 | 1 |
| 2 | Assingment_2 | 6 | 0 | Installation | 1 | 0 | 0 |
| 3 | Assingment_3 | 9 | 1 | Intro to Bash | 1 | 0 | 0 |
| 3 | Assingment_4 | 3 | 1 | Intro to Bash part 2 | 1 | 0 | 0 |
+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+
student_completion
+---------------+------------------+-------------+
| FK_studentKey | FK_collectionKey | isCompleted |
+---------------+------------------+-------------+
| 404 | 5 | 1 |
+---------------+------------------+-------------+
Your question is extremely lacking in explanation. I am taking a shot in the dark here. Does this do what you want?
select d.*
, available = sc.isCompleted
from Data d
left join student_completion sc on sc.FK_colletionKey = d.collection_id

How to select if similar field count is the maximum in the table?

I want to select from a table if row counts of similar filed is maximum depends on other columns.
As example
| user_id | team_id | isOk |
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 2 | 1 |
| 6 | 2 | 1 |
| 7 | 2 | 1 |
| 8 | 3 | 1 |
| 9 | 3 | 1 |
| 10 | 3 | 1 |
| 11 | 3 | 0 |
So i want to select team 1 and 2 because they all have 1 value at isOk Column,
i tried to use this query
SELECT Team
FROM _Table1
WHERE isOk= 1
GROUP BY Team
HAVING COUNT(*) > 3
But still i have to define a row count which can be maximum or not.
Thanks in advance.
Is this what you are looking for?
select team
from _table1
group by team
having min(isOk) = 1;

Exclude records from query where some columns are all not equal to a value

I am relatively new to ACCESS 2010 and to SQL. Having a bit of trouble figuring this one out.
I have a a query that returns a result from a table. I want to exclude any records where the values in columns A1-A4 all equal zero.
ex:
+----+----------+----+----+----+----+
| ID | CustName | A1 | A2 | A3 | A4 |
+----+----------+----+----+----+----+
| 1 | Ben | 0 | 0 | 0 | 0 |
| 2 | Tom | 0 | 0 | 0 | 1 |
| 3 | Jerry | 0 | 0 | 2 | 0 |
| 4 | Steve | 0 | 3 | 0 | 0 |
| 5 | Dave | 4 | 0 | 0 | 0 |
| 6 | Mike | 0 | 0 | 2 | 1 |
| 7 | Sheila | 0 | 3 | 2 | 1 |
| 8 | Jonesy | 4 | 3 | 2 | 1 |
+----+----------+----+----+----+----+
Using the table above as the source for the query, the only record I want to be excluded should be the very first record.
The SQL I am using for this query is
SELECT Test.CustName, Test.A1, Test.A2, Test.A3, Test.A4
FROM Test
WHERE (((Test.A1)<>0) AND ((Test.A2)<>0) AND ((Test.A3)<>0) AND ((Test.A4)<>0));
This SQL will only return records where all columns from A1-A4 are NOT equal to zero and only returns the final record. It should return record 2 - 7.
You want OR, not AND:
SELECT Test.CustName, Test.A1, Test.A2, Test.A3, Test.A4
FROM Test
WHERE (((Test.A1)<>0) OR ((Test.A2)<>0) OR ((Test.A3)<>0) OR ((Test.A4)<>0));
Use OR
SELECT t.CustName, t.A1, t.A2, t.A3, t.A4
FROM Test t
WHERE (((t.A1)!=0)
OR ((t.A2)!=0)
OR ((t.A3)!=0)
OR ((t.A4)!=0));
If those values are not negative you can also do
where A1+A2+A3+A3 > 0