How to compare two columns in SQL for multiple rows? - sql

I have a data set with four columns (author, document, rating 1, rating 2)
How do I pick authors who have written a document that has been rated higher in rating 1 than rating 2, and has also written another document that has been rated higher in rating 2 than rating 1.
Basically:
AUTHOR DOCUMENT RATING 1 RATING 2
A 1 1 2
B 2 1 2
B 3 3 1
C 4 2 2
C 5 3 4
C 6 1 3
D 7 1 2
D 8 1 2
So my desired query will give me B and C because it has written docs that have had both higher and lower numbers in both ratings.
What I have:
SELECT DISTINCT author
FROM(
(SELECT author
FROM table_name
WHERE rating1 < rating2)
UNION
(SELECT author
FROM table_name
WHERE rating1 > rating2)
)
AS a
What I cant figure out is how to group the authors, test whether rating 1 and rating 2 are both higher and lower, output the name and then move on to the next group of authors. What the above prints is just the set of distinct names with either higher or lower numbers. So this one would print D as well for example.
What is my SQL code missing that would satisfy the criteria mentioned above

Try this,
select *
from myTable as t1
inner join MyTable as t2
on t1.author = t2.author
and t2.rating1 > t2.rating2
where t1.rating1 > t1.rating2

Related

Create multiple rows based on 1 column

I currently have a table with a quantity in it.
ID Code Quantity
1 A 1
2 B 3
3 C 2
4 D 1
Is there anyway to write a sql statement that would get me
ID Code Quantity
1 A 1
2 B 1
2 B 1
2 B 1
3 C 1
3 C 1
4 D 1
I need to break out the quantity and have that many number of rows
Thanks
Here's one option using a numbers table to join to:
with numberstable as (
select 1 AS Number
union all
select Number + 1 from numberstable where Number<100
)
select t.id, t.code, 1
from yourtable t
join numberstable n on t.quantity >= n.number
order by t.id
Online Demo
Please note, depending on which database you are using, this may not be the correct approach to creating the numbers table. This works in most databases supporting common table expressions. But the key to the answer is the join and the on criteria.
One way would be to generate an array with X elements (where X is the quantity). So for rows
ID Code Quantity
1 A 1
2 B 3
3 C 2
you would get
ID Code Quantity ArrayVar
1 A 1 [1]
2 B 3 [1,2,3]
3 C 2 [2]
using a sequence function (e.g, in PrestoDB, sequence(start, stop) -> array(bigint))
Then, unnest the array, so for each ID, you get a X rows, and set the quantity to 1. Not sure what SQL distribution you're using, but this should work!
You can use connect by statement to cross join tables in order to get your desired output.
check my solution it works pretty robust.
select
"ID",
"Code",
1 QUANTITY
from Table1, table(cast(multiset
(select level from dual
connect by level <= Table1."Quantity") as sys.OdciNumberList));

Count condition met

I have a table (stu_grades) that stores student data and their grades at the centers they attended
I want to find out how many times for e.g. each student in that table got 'A' and then 'B' etc at any center
stu_grades
stu_ID|grade1|grade2|Grade3|center
1 A A C 1
2 B B B 2
3 C C A 1
1 C A C 2
the same student could occur more than once in the table with the same grades or even a different grade, same or different center
I especially want to check where the grade has appeared more than 3 or more times and how many centeres they exist in
So the final output should be like:
Stu_ID|Grade|Count|centercount
1 A 3 2 (As they accquired 'A' from 2 centres)
1 C 3 2
2 B 3 1 (As they only exist in 1 centre)
3 C 2 1
3 A 1 1
select
stu_id,
grade,
sum(count) count,
count(distinct center) centercount
from (
select stu_id, grade, center, count(*)
from stu_grades,
lateral unnest(array[grade1, grade2, grade3]) grade
group by 1, 2, 3
) s
group by 1, 2
order by 1, 2;
Test it here.

Selecting Data from Same Table that Doesn't Match

I have found several solutions for my type of problem, but I having trouble applying it in my situation.
Essentially I have a Vehicle Table:
License VIN Region
1 1 1
1 2 2
2 3 1
2 3 2
3 4 1
3 4 2
3 5 3
I want to take the license and vin from region 1 and see if the vin matches in all other regions based on the license. If it doesn't I want all the rows that don't match, but if it does match I don't want the row. So a complexity does come in when say I have 3 licenses and region 1 matches one row, but not the other, I want both the unmatched and region 1; however, when I have 3 licenses that all match I don't want any rows including region 1.
So my results in this case would be:
License VIN Region
1 1 1
1 2 2
3 4 1
3 5 3
I am using SQL Server 2005.
I think this is what you're looking for
SELECT DISTINCT a.*
FROM Vehicle AS a
INNER JOIN Vehicle AS b
ON a.License = b.License
WHERE a.VIN != b.VIN
AND a.Region != b.Region
AND (a.Region = 1 OR b.Region = 1)

Efficient SQL to calculate # of shared affiliations

So I have
a table that stores asymmetrical connections between two persons
(like a Twitter follow; not like a Facebook friend) and
a table that stores a person's affiliations to various groups
My task is to find, for each asymmetrical relationship, the number of affiliations shared between the "from person" and the "to person".
I made this brute force solution, but I'm wondering if brighter minds could come up with something more efficient.
select frm01.from_person_id, frm01.to_person_id, count(*) num_affl
from
(
select lnk.from_person_id, lnk.to_person_id, ga.grp_id from_grp_id
from links lnk
left outer join grp_affl ga on lnk.from_person_id = ga.person_id
group by lnk.from_person_id, lnk.to_person_id, grp_id
) frm01
inner join
(
select lnk.from_person_id, lnk.to_person_id, ga.grp_id to_grp_id
from links lnk
left outer join grp_affl ga on lnk.to_person_id = ga.person_id
group by lnk.from_person_id, lnk.to_person_id, grp_id
) to01
on (
frm01.from_person_id = to01.from_person_id
and frm01.to_person_id = to01.to_person_id
and frm01.from_grp_id = to01.to_grp_id
)
group by frm01.from_person_id, frm01.to_person_id;
Using ANSI SQL on Netezza (which doesn't allow correlated subqueries).
TIA!
Edited to add table schema:
table lnk:
from_person_id to_person_id
1 4
2 5
3 6
4 2
5 3
table grp_affl:
person_id grp_id
1 A
1 B
1 C
2 A
3 B
4 C
5 A
5 B
5 C
6 A
expected output:
from_person_id to_person_id num_affl
1 4 1
2 5 1
3 6 0
4 2 0
5 3 1
Persons 1 & 4 have 1 affiliation in common (C), 2 & 5 have A in common, 5 & 3 have B in common. 3 & 6 have nothing in common. Likewise 4 & 2.
You can do this with aggregation and the right joins:
select pairs.from_person, pairs.to_person, count(*)
from links pairs join
grp_affil fromga
on fromga.person_id = pairs.from_person join
grp_affil toga
on toga.person_id = pairs.to_person and
toga.grp_id = fromga.grp_id
group by pairs.from_person, pairs.to_person;
The joins bring in the groups. The last condition only brings in matching groups between the two persons. The final group by counts them.

Count number of not exist in child table

Essentially what I'm trying to do is count the number of rows something doesn't exist in an audit/history table. I'd like the following query to return a count of one per detail. Currently it gives me one per row in the history table.
--Detail Table
ID DETAIL_GROUP
1 A
2 B
3 B
--Detail History Table
DETAIL_ID_FK VALUE1
1 NOT_MATCH
1 NOT_MATCH
2 MATCH
2 NOT_MATCH
3 MATCH
3 NOT_MATCH
SELECT D.DETAIL_GROUP, COUNT(*)
FROM DETAIL D
WHERE (NOT EXISTS(
SELECT NULL
FROM DETAIL_HISTORY HI
WHERE HI.D_ID_FK = D.ID
AND HI.VALUE1 = 'MATCH'))
GROUP BY D.DETAIL_GROUP;
I'd like to see the following result:
DETAIL_GROUP COUNT(*)
A 1
but I'm receiving the following result:
DETAIL_GROUP COUNT(*)
A 2
Thank you in advance for any assistance provided.
Assuming that your detail table is as follows:
D_ID VALUE1
1 MATCH
1 NOT_MATCH
2 MATCH
2 NOT_MATCH
3 MATCH
3 NOT_MATCH
The below query:
SELECT d.detail_group, count(*)
FROM detail d
JOIN detail_history dh ON dh.d_id = d.id
WHERE dh.value1 = 'MATCH'
GROUP BY d.detail_group
Would produce:
DETAIL_GROUP COUNT(*)
A 1
B 2
The above query creates the groups matching the ids and then goes into each group and restricts the items based on value1.