Count coincidences between rows in two tables - sql

I am trying to write a query in SQL Server 2014 that counts the number of coincidences (but just when those coincidences are more than 1) between the rows in two tables, into a new column. The structure is like this:
table_a:
================
c1 | c2 | c3 | c4
11 | 30 | 70 | 90
20 | 80 | 95 | 99
15 | 39 | 40 | 41
...
table_b:
================
n1 | n2 | n3 | n4
30 | 65 | 90 | 95
20 | 80 | 90 | 99
15 | 80 | 95 | 99
...
And I want to add a column in a query after the columns in table_a like this:
query:
================
c1 | c2 | c3 | c4 | c5
11 | 30 | 70 | 90 | 2
20 | 80 | 95 | 99 | 6
15 | 39 | 40 | 41 | 0
...
In other words, for each row in table_a check how many numbers match each row in table_b , afterwards sum all results greater than 1 and paste next to the row in table_a.
I have the following code but I get wrong output:
SELECT TOP (100) PERCENT dbo.table_a.c1, dbo.table_a.c2,
dbo.table_a.c3, dbo.table_a.c4, dbo.table_a.c5, dbo.table_a.c6,
COUNT('c1 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c2 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c3 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c4 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c5 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c6 = n1 or n2 or n3 or n4 or n5 or n6') AS c5
FROM dbo.table_a CROSS JOIN dbo.[table_b]
GROUP BY dbo.table_a.c1, dbo.table_a.c2, dbo.table_a.c3, dbo.table_a.c4,
dbo.table_a.c5, dbo.table_a.c6
HAVING (COUNT('c1 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c2 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c3 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c4 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c5 = n1 or n2 or n3 or n4 or n5 or n6')
+ COUNT('c6 = n1 or n2 or n3 or n4 or n5 or n6') > 1)
ORDER BY c5 DESC
Any help would be appreciated, thx!

;with cte(n) as(
select n1 from table_b union all
select n2 from table_b union all
select n3 from table_b union all
select n4 from table_b
)
,counted(n, c) as(
select n, c = count(n) from cte group by n
)
select
a.*, b.cc
from table_a a
cross apply(
select cc = sum(case when c > 1 then c else 0 end)
from counted
where
n = a.c1
or n = a.c2
or n = a.c3
or n = a.c4
)b

Related

presto DB group by rollup creating duplicate rows

for the following table
country_id
country_name
state_id
state_name
district_id
district_name
1
C1
1
S1
1
D1
1
C1
1
S1
2
D2
1
C1
1
S1
3
D3
1
C1
2
S2
4
D4
1
C1
2
S2
5
D5
I need the count of districts under each state and country
so I used this code
select country_id,
country_name,
state_id,
state_name,
district_id,
district_name,
count(distinct district_name) as ct
from test
group by rollup(country_id, country_name, state_id, state_name, district_id, district_name)
to rollup at every level but the output looks like this
country_id
country_name
state_id
state_name
district_id
district_name
ct
1
5
1
C1
5
1
C1
1
3
1
C1
1
S1
3
1
C1
2
2
1
C1
2
S2
2
1
C1
1
S1
1
1
1
C1
1
S1
1
D1
1
1
C1
1
S1
2
1
1
C1
1
S1
2
D2
1
1
C1
1
S1
3
1
1
C1
1
S1
3
D3
1
1
C1
2
S2
4
1
1
C1
2
S2
4
D4
1
1
C1
2
S2
5
1
1
C1
2
S2
5
D5
1
how do I get rid of the rows that have an id but no name? I can't remove the columns in rollup.

Get the lowest Score of each distinct Name and distinct Group

Need to get the lowest Score of each distinct Name and distinct Group. Thank you.
Data:
Group Name Score
A N1 1
B N1 5
C N1 3
A N1 4
A N2 2
A N3 6
A N1 8
B N1 7
B N2 9
Result:
A N1 1
B N1 5
C N1 3
A N2 2
A N3 6
B N2 9
select [Group], Name , min( Score)
from my_table
group by [Group], Name

sql query to find what is not in group three

i have one SQL Table in which some dummy data.
i want that dummy data and update that row with column type
my table tbl
ID D1 M1 C1 QTY TYPE
1 D1 M1 C1 1 Y
2 D1 M2 C1 2 Y
3 D1 M3 C1 3 Y
4 D1 M1 C1 1 Y
5 D2 M1 C1 1 Y
6 D2 M2 C1 2 Y
7 D2 M3 C2 3 Y
8 D2 M1 C1 1 Y
9 D2 M2 C1 2 Y
10 D3 M1 C1 1 Y
11 D3 M2 C1 2 Y
12 D3 M3 C1 3 Y
13 D3 M1 C1 1 Y
14 D3 M2 C1 2 Y
15 D3 M3 C1 3 Y
16 D3 M1 C2 1 Y
grouping on Column D1 and M1
I have a N no. of records, now I have to identify group of 3 record and if any record remain then it should be set as "No" else "yes"
Ex:
condition 1: If I have 4 records, then make a group of 3-3 records so remain last 1 record should be set an "no".
condition 2: If I have 5 records, then make a group of 3-3 records so remain last 2 records will be set as "No"
Condition 3: if I have 7 records, then make a group of 3-3 records so remain last 1 record will be set as "No"
my expected answer is as below
ID D1 M1 C1 QTY TYPE
1 D1 M1 C1 1 YES
2 D1 M2 C1 2 YES
3 D1 M3 C1 3 YES
4 D1 M1 C1 1 NO
5 D2 M1 C1 1 YES
6 D2 M2 C1 2 YES
7 D2 M3 C2 3 YES
8 D2 M1 C1 1 NO
9 D2 M2 C1 2 NO
10 D3 M1 C1 1 YES
11 D3 M2 C1 2 YES
12 D3 M3 C1 3 YES
13 D3 M1 C1 1 YES
14 D3 M2 C1 2 YES
15 D3 M3 C1 3 YES
16 D3 M1 C2 1 NO
SQLFIDDLE
Please tell me solution.
Is this what you are looking for?
with toupdate as (
select t.*, row_number() over (partition by d1 order by id) as seqnum,
count(*) over (partition by d1) as cnt
from tbl t
)
update toupdate
set type = (case when seqnum <= 3*(cnt /3) then 'yes' else 'no' end);
You can also run similar logic as a select:
select t.*, (case when seqnum <= 3*(cnt /3) then 'yes' else 'no' end)
from (
select t.*, row_number() over (partition by d1 order by id) as seqnum,
count(*) over (partition by d1) as cnt
from tbl t
) t;

sql query group by count

Hello I have a simple table like this:
name date
n1 d1
n1 d1
n2 d1
n3 d1
n1 d2
n2 d2
n2 d2
n1 d3
n1 d3
n1 d3
n4 d3
Where n# is some name and d# is some date. I need the following result:
name date number
n1 d1 2
n2 d1 1
n3 d1 1
n1 d2 1
n2 d2 2
n1 d3 3
n4 d3 1
What I'm doing is for each date, I count the number of times n# appears. Then I display for each name and date the number.
How can I do this on SQL? thank you
It's just a simple usage of group by
select name, date, count(*)
from table1
group by name, date
Group on the name and date:
select
name, date, count(*) as number
from
SomeTable
group by
name, date
order by
name, date

Select from two different tables

So, I have two tables, Table R and Table N. The data in Table R is from Table N. My problem is I don't know the SELECT query that will display the result such as below, because there are 4 names, and SQL can only choose 1. Is there a query or any other way to get the result?
Table R:
Id1 Id2 Id3 Id4
1 3 5 7
2 4 6 8
Table N:
Id Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
After the SELECT statement, the result should look like this:
Name1 Name2 Name3 Name4
A C E G
B D F H
Anyway, thanks for helping. ^_^
SELECT
N1.Name AS `Name1`,
N2.Name AS `Name2`,
N3.Name AS `Name3`,
N4.Name AS `Name4`
FROM
R
INNER JOIN
N N1
ON
N1.Id = R.Id1
INNER JOIN
N N2
ON
N2.Id = R.Id2
INNER JOIN
N N3
ON
N3.Id = R.Id3
INNER JOIN
N N4
ON
N4.Id = R.Id4
select
n1.Name as Name1,
n2.Name as Name2,
n3.Name as Name3,
n4.Name as Name4
from
R
inner join N n1 on n1.id = R.id1
inner join N n2 on n2.id = R.id2
inner join N n3 on n3.id = R.id3
inner join N n4 on n4.id = R.id4
You can rename the table in the query
You can join the same table multiple times provided you rename it, so:
select N1.Name, N2.Name, N3.Name, N4.Name
from R
join N as N1 on N1.Id = Id1
join N as N2 on N2.Id = Id2
join N as N3 on N3.Id = Id3
join N as N4 on N4.Id = Id4
(of course that's equivalent to
select N1.Name, N2.Name, N3.Name, N4.Name
from R, N as N1, N as N2, N as N3, N as N4
where N1.Id = Id1 and
N2.Id = Id2 and
N3.Id = Id3 and
N4.Id = Id4
(the on clauses are just added to where and comma is join)