Select unique results and null - sql

I need get all lines from table, that have unique value in certain fields and all lines, than have null in this fields. Example:
id | name | group
-----------------
1 | One | 1
2 | Two | null
3 | Three| 3
4 | Four | 2
5 | Five | 1
6 | Six | 2
7 | Seven| null
Result:
id | name | group
-----------------
1 | One | 1
2 | Two | null
3 | Three| 3
4 | Four | 2
7 | Seven| null
How to make it in one request?

select t.id, t.name, t.`group`
from tablename t
join (select `group`, min(id) as mid
from tablename
where `group` is not null
group by `group`) x on x.mid = t.id and x.`group` = t.`group`
union all
select id, name, `group`
from tablename
where `group` is null

Related

Postgres - Unique values for id column using CTE, Joins alongside GROUP BY

I have a table referrals:
id | user_id_owner | firstname | is_active | user_type | referred_at
----+---------------+-----------+-----------+-----------+-------------
3 | 2 | c | t | agent | 3
5 | 3 | e | f | customer | 5
4 | 1 | d | t | agent | 4
2 | 1 | b | f | agent | 2
1 | 1 | a | t | agent | 1
And another table activations
id | user_id_owner | referral_id | amount_earned | activated_at | app_id
----+---------------+-------------+---------------+--------------+--------
2 | 2 | 3 | 3.0 | 3 | a
4 | 1 | 1 | 6.0 | 5 | b
5 | 4 | 4 | 3.0 | 6 | c
1 | 1 | 2 | 2.0 | 2 | b
3 | 1 | 2 | 5.0 | 4 | b
6 | 1 | 2 | 7.0 | 8 | a
I am trying to generate another table from the two tables that has only unique values for referrals.id and returns as one of the columns the count for each apps as best_selling_app_count.
Here is the query I ran:
with agents
as
(select
referrals.id,
referral_id,
amount_earned,
referred_at,
activated_at,
activations.app_id
from referrals
left outer join activations
on (referrals.id = activations.referral_id)
where referrals.user_id_owner = 1),
distinct_referrals_by_id
as
(select
id,
count(referral_id) as activations_count,
sum(coalesce(amount_earned, 0)) as amount_earned,
referred_at,
max(activated_at) as last_activated_at
from
agents
group by id, referred_at),
distinct_referrals_by_app_id
as
(select id, app_id as best_selling_app,
count(app_id) as best_selling_app_count
from agents
group by id, app_id )
select *, dense_rank() over (order by best_selling_app_count desc) best_selling_app_rank
from distinct_referrals_by_id
inner join distinct_referrals_by_app_id
on (distinct_referrals_by_id.id = distinct_referrals_by_app_id.id);
Here is the result I got:
id | activations_count | amount_earned | referred_at | last_activated_at | id | best_selling_app | best_selling_app_count | best_selling_app_rank
----+-------------------+---------------+-------------+-------------------+----+------------------+------------------------+-----------------------
2 | 3 | 14.0 | 2 | 8 | 2 | b | 2 | 1
1 | 1 | 6.0 | 1 | 5 | 1 | b | 1 | 2
2 | 3 | 14.0 | 2 | 8 | 2 | a | 1 | 2
4 | 1 | 3.0 | 4 | 6 | 4 | c | 1 | 2
The problem with this result is that the table has a duplicate id of 2. I only need unique values for the id column.
I tried a workaround by harnessing distinct that gave desired result but I fear the query results may not be reliable and consistent.
Here is the workaround query:
with agents
as
(select
referrals.id,
referral_id,
amount_earned,
referred_at,
activated_at,
activations.app_id
from referrals
left outer join activations
on (referrals.id = activations.referral_id)
where referrals.user_id_owner = 1),
distinct_referrals_by_id
as
(select
id,
count(referral_id) as activations_count,
sum(coalesce(amount_earned, 0)) as amount_earned,
referred_at,
max(activated_at) as last_activated_at
from
agents
group by id, referred_at),
distinct_referrals_by_app_id
as
(select
distinct on(id), app_id as best_selling_app,
count(app_id) as best_selling_app_count
from agents
group by id, app_id
order by id, best_selling_app_count desc)
select *, dense_rank() over (order by best_selling_app_count desc) best_selling_app_rank
from distinct_referrals_by_id
inner join distinct_referrals_by_app_id
on (distinct_referrals_by_id.id = distinct_referrals_by_app_id.id);
I need a recommendation on how best to achieve this.
I am trying to generate another table from the two tables that has only unique values for referrals.id and returns as one of the columns the count for each apps as best_selling_app_count.
Your question is really complicated with a very complicated SQL query. However, the above is what looks like the actual question. If so, you can use:
select r.*,
a.app_id as most_common_app_id,
a.cnt as most_common_app_id_count
from referrals r left join
(select distinct on (a.referral_id) a.referral_id, a.app_id, count(*) as cnt
from activations a
group by a.referral_id, a.app_id
order by a.referral_id, count(*) desc
) a
on a.referral_id = r.id;
You have not explained the other columns that are in your result set.

How to get count from one table which is mutually dependent to another table

I have two table
Let's name as first table: QC_Meeting_Master
Second table: QC_Project_Master I want to calculate count of problems_ID Which is mutually depend on second table
ID | QC_ID | Problems_ID |
___|_______|_____________|
1 | 1 | 2 |
2 | 1 | 7 |
ID | QC_ID | Problem_ID |
___|_______|_____________|
1 | 1 | 7 |
2 | 1 | 7 |
3 | 1 | 7 |
4 | 1 | 7 |
5 | 1 | 2 |
6 | 1 | 2 |
7 | 1 | 2 |
select COUNT(Problem_ID) from [QC_Project_Master] where Problem_ID in
(select Problems_ID from QC_Meeting_Master QMM join QC_Project_Master QPM on QMM.Problems_ID = QPM.Problem_ID)
I have to calculate Count of QC_Project_Master (problem_ID) on basis of QC_Meeting_Master (Problems_ID)
it means for first table: QC_Meeting_Master(Problems_ID) = 2,
then count should be 3
And for Second table: QC_Project_Master (Problems_ID) = 7,
then count should be 4
use conditional aggregation
select sum(case when t2.Problem_ID=2 then 1 else 0 end),
sum(case when t2.Problem_ID=7 then 1 else 0 end) from
table1 t1 join table2 t2 on t1.QC_ID=t2.QC_ID and t1.Problems_ID=t2.Problems_ID
if you need all the group count then use below
select t2.QC_ID,t2.Problems_ID, count(*) from
table1 t1 join table2 t2
on t1.QC_ID=t2.QC_ID and t1.Problems_ID=t2.Problems_ID
group by t2.QC_ID,t2.Problems_ID
As far as I understood your problem this is simple aggregation and JOIN as below:
SELECT mm.QC_ID, mm.Problem_ID, pm.cnt
FROM QC_Meeting_Master mm
INNER JOIN
(
SELECT QC_ID, Problem_ID, COUNT(*) cnt
FROM QC_Project_Master
GROUP BY QC_ID, Problem_ID
) pm
ON pm.QC_ID = mm.QC_ID AND pm.Problem_ID = mm.Problem_ID;

joining more than two tables without repeating values

I want to join three tables,
I have three tables user, profession and education where "uid" is primary key for user table and foreign key for other two tables. I want to join these tables to produce result in one single table
user profession education
+------+-------+ +-----+----------+ +-----+---------+
| uid | uName | | uid | profName | | uid | eduName |
+------+-------+ +-----+----------+ +-----+---------+
| 1 | aaa | | 1 | prof1 | | 1 | edu1 |
| 2 | bbb | | 1 | prof2 | | 1 | edu2 |
| 3 | ccc | | 2 | prof1 | | 1 | edu3 |
| | | | 3 | prof3 | | 3 | edu4 |
| | | | 3 | prof2 | | | |
+------+-------+ +-----+----------+ +-----+---------+
Expected output
+------+-------+-----+----------+-----+---------+
| uid | uName | uid | profName | uid | eduName |
+------+-------+-----+----------+-----+---------+
| 1 | aaa | 1 | prof1 | 1 | edu1 |
| null | null | 1 | prof2 | 1 | edu2 |
| null | null |null | null | 1 | edu3 |
| 2 | bbb | 2 | prof1 | null| null |
| 3 | ccc | 3 | prof3 | 3 | edu4 |
| null | null | 3 | prof2 | null| null |
+------+-------+-----+----------+-----+---------+
I tried following query
select u.uid ,u.uName,p.uid , p.profName,e.uid,e.eduName
from user u inner join profession p on u.uid=p.pid
inner join education e on u.uid = e.uid
where u.uid=p.uid
and u.uid=e.uid
and i.uid=1
Which gives me duplicate values
+------+-------+-----+----------+-----+---------+
| uid | uName | uid | profName | uid | eduName |
+------+-------+-----+----------+-----+---------+
| 1 | aaa | 1 | prof1 | 1 | edu1 |
| 1 | aaa | 1 | prof2 | 1 | edu1 |
| 1 | aaa | 1 | prof1 | 1 | edu2 |
| 1 | aaa | 1 | prof2 | 1 | edu2 |
| 1 | aaa | 1 | prof1 | 1 | edu3 |
| 1 | aaa | 1 | prof2 | 1 | edu3 |
+------+-------+-----+----------+-----+---------+
Is there a way to get the output with not repeating the values.
Thanks
Bit of a swine this one.
I agree with #GordonLinoff that ideally this presentation would be done on the client side.
However, if we wish to do it in SQL, then the basic approach is that you have to get the maximum number of rows that will be consumed by each user (based on a count of how many entries they have in each of the professions and educations tables, and then of these counts, the max count).
Once we have the number of rows required for each user, we expand the rows out for each user as necessary using a numbers table (I've included a number generator for the purpose).
Then we join each table on, according to the uid and the row number of the entry in the joined table relative to the row number of the "expanded" rows for each user. Then we select the relevant columns, and that's us done. Pay the nurse on the way out!
WITH
number_table(number) AS
(
SELECT
(ones.n) + (10 * tens.n) + (100 * hundreds.n) AS number
FROM --available range 0 to 999
(VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) AS ones(n)
,(VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) AS tens(n)
,(VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) AS hundreds(n)
)
,users(u_uid, userName) AS
(
SELECT 1, 'aaa'
UNION ALL
SELECT 2, 'bbb'
UNION ALL
SELECT 3, 'ccc'
)
,professions(p_u_uid, profName) AS
(
SELECT 1, 'prof1'
UNION ALL
SELECT 1, 'prof2'
UNION ALL
SELECT 2, 'prof1'
UNION ALL
SELECT 3, 'prof3'
UNION ALL
SELECT 3, 'prof2'
)
,educations(e_u_uid, eduName) AS
(
SELECT 1, 'edu1'
UNION ALL
SELECT 1, 'edu2'
UNION ALL
SELECT 1, 'edu3'
UNION ALL
SELECT 3, 'edu4'
)
,row_counts(uid, row_count) AS
(
SELECT u_uid, COUNT(u_uid) FROM users GROUP BY u_uid
UNION ALL
SELECT p_u_uid, COUNT(p_u_uid) FROM professions GROUP BY p_u_uid
UNION ALL
SELECT e_u_uid, COUNT(e_u_uid) FROM educations GROUP BY e_u_uid
)
,max_counts(uid, max_count) AS
(
SELECT uid, MAX(row_count) FROM row_counts GROUP BY uid
)
SELECT
u_uid
,userName
,p_u_uid
,profName
,e_u_uid
,eduName
FROM
max_counts
INNER JOIN
number_table ON number BETWEEN 1 AND max_count
LEFT JOIN
(
SELECT u_uid, userName, ROW_NUMBER() OVER (PARTITION BY u_uid ORDER BY userName) AS user_match
FROM users
) AS users
ON u_uid = uid
AND number = user_match
LEFT JOIN
(
SELECT p_u_uid, profName, ROW_NUMBER() OVER (PARTITION BY p_u_uid ORDER BY profName) AS prof_match
FROM professions
) AS professions
ON p_u_uid = uid
AND number = prof_match
LEFT JOIN
(
SELECT e_u_uid, eduName, ROW_NUMBER() OVER (PARTITION BY e_u_uid ORDER BY eduName) AS edu_match
FROM educations
) AS educations
ON e_u_uid = uid
AND number = edu_match
ORDER BY
IIF(COALESCE(u_uid, p_u_uid, e_u_uid) IS NULL, 1, 0) ASC --nulls last
,COALESCE(u_uid, p_u_uid, e_u_uid) ASC
,IIF(COALESCE(p_u_uid, e_u_uid) IS NULL, 1, 0) ASC --nulls last
,COALESCE(p_u_uid, e_u_uid) ASC
,IIF(e_u_uid IS NULL, 1, 0) ASC --nulls last
,e_u_uid ASC
And the results:
u_uid userName p_u_uid profName e_u_uid eduName
----------- -------- ----------- -------- ----------- -------
1 aaa 1 prof1 1 edu1
NULL NULL 1 prof2 1 edu2
NULL NULL NULL NULL 1 edu3
2 bbb 2 prof1 NULL NULL
3 ccc 3 prof2 3 edu4
NULL NULL 3 prof3 NULL NULL
Did you try the distinct keyword?
select DISTINCT u.uid ,u.uName,p.uid , p.profName,e.uid,e.eduName
from user u inner join profession p on u.uid=p.pid
inner join education e on u.uid = e.uid
where u.uid=p.uid
and u.uid=e.uid
and i.uid=1

Select the latest message thread values from a table using sql

This is my table
Id | ReceiverId | SenderId | Text
-----------+---------------+--------------+-----------
1 | 5 | 1 | text
2 | 5 | 1 | text
3 | 1 | 5 | text
4 | 2 | 5 | text
5 | 2 | 5 | text
6 | 5 | 3 | text
7 | 5 | 4 | text
9 | 5 | 6 | text
10 | 5 | 4 | text
11 | 10 | 5 | text
12 | 5 | 10 | text
13 | 10 | 5 | text
14 | 5 | 10 | text
How do I select a row with out duplication based on [ReceiverId, SenderId] pair and Ordered by Id in Descending order. That is: [5, 1]=[1,5] are duplicate. OR [5,1] = [5,1] are also the duplicate.
So the final result should be:
Id | ReceiverId | SenderId | Text
-----------+---------------+--------------+-----------
14 | 5 | 10 | text
10 | 5 | 4 | text
9 | 5 | 6 | text
6 | 5 | 3 | text
5 | 2 | 5 | text
3 | 1 | 5 | text
Assuming that among records, which you consider to be the same by just checking the SenderId and ReceiverId (order doesn't matter), you want the one with the largest Id (which could probably be the latest). Then, this query will give you the result:
select Id, ReceiverId, SenderId, [Text]
from MyTable t
where t.Id in (
select top 1 tt.Id
from MyTable tt
where (tt.SenderId = t.SenderId and tt.ReceiverId = t.ReceiverId) or
(tt.SenderId = t.ReceiverId and tt.ReceiverId = t.SenderId)
order by tt.Id desc
)
order by t.Id desc
Replace MyTable with your table's name.
select b.ID,
a.senderid_final,
a.receiverid_final,
b.Text
from
(
select a.receiverid as a_receiverid,
a.senderid as a_senderid ,
b.receiverid as b_receiverid,
b.senderid as b_senderid,
case when max(a.id) > max (b.id) then a.receiverid else b.receiverid end as receiverid_final,
case when max(a.id) > max (b.id) then a.senderid else b.senderid end as senderid_final
from my_tables as a
inner join my_table as b
on a.receiverid = b.senderid
and b.receiverid = a.senderid
group by a.receiverid, a.senderid, b.receiverid, b.senderid
) as a
inner join my_tables as b
on a.receiverid_final = b.receiverid
and b.senderid = a.senderid_final
Order by b.id desc

How to only show Unique values in this existing SQL query

I am comparing two tables (dbo.new and dbo.old) and if the first three column match and the forth column doesnst match, it has to select it. Now this shows a lot of values, and I only want to display the unique values of column2. This is the code I have now:
SELECT dbo.new.[column1], dbo.new.[column2], dbo.new.[column3], dbo.new.[column4]
FROM dbo.new
JOIN dbo.old ON dbo.new.[column1]=dbo.old.[column1]
AND dbo.new.[column2]=dbo.old.[column2]
AND dbo.new.[column3]=dbo.old.[column3]
WHERE [dbo].[new].[column4] <> [dbo].[old].[column4]
First two tables I start with:
-----------------
| 1 | 1 | 1 | 1 |
-----------------
| 2 | 1 | 2 | 2 |
-----------------
| 3 | 3 | 3 | 3 |
-----------------
| 4 | 1 | 4 | 4 |
-----------------
-----------------
| 1 | 1 | 1 | 9 |
-----------------
| 2 | 1 | 2 | 9 |
-----------------
| 3 | 3 | 3 | 9 |
-----------------
| 4 | 1 | 4 | 9 |
-----------------
This is the outcome of the query above:
-----------------
| 1 | 1 | 1 | 1 |
-----------------
| 2 | 1 | 2 | 2 |
-----------------
| 3 | 3 | 3 | 3 |
-----------------
| 4 | 1 | 4 | 4 |
-----------------
^ delete those duplicates
This is what I want to be the outcome:
-----------------
| 1 | 1 | 1 | 1 |
-----------------
| 3 | 3 | 3 | 3 |
-----------------
I tried many things like UNIQUE and DISTINCT but I cant find the solution. It doenst even need to show the first value, as long as it show onea row with the unique number. So this is correct too:
-----------------
| 4 | 1 | 4 | 4 |
-----------------
| 3 | 3 | 3 | 3 |
-----------------
Choose the ordering you need in over() to get proper rows.
SELECT TOP(1) WITH TIES
dbo.new.[column1], dbo.new.[column2], dbo.new.[column3], dbo.new.[column4]
FROM dbo.new
JOIN dbo.old ON dbo.new.[column1]=dbo.old.[column1]
AND dbo.new.[column2]=dbo.old.[column2]
AND dbo.new.[column3]=dbo.old.[column3]
where [dbo].[new].[column4] <> [dbo].[old].[column4]
ORDER BY row_number() over(partition by dbo.new.[column2] order by dbo.new.[column1])
Quick demo, runs OK sql server 2014
create table dbo.new(
column1 int,
column2 int,
column3 int,
column4 int);
create table dbo.old(
column1 int,
column2 int,
column3 int,
column4 int);
insert dbo.new values
( 1 , 1 , 1 , 1 ),
( 2 , 1 , 2 , 2 ),
( 3 , 3 , 3 , 3 ),
( 4 , 1 , 4 , 4 );
insert dbo.old values
( 1 , 1 , 1 , 9 ),
( 2 , 1 , 2 , 9 ),
( 3 , 3 , 3 , 9 ),
( 4 , 1 , 4 , 9 );
SELECT TOP(1) WITH TIES
dbo.new.[column1], dbo.new.[column2], dbo.new.[column3], dbo.new.[column4]
FROM dbo.new
JOIN dbo.old ON dbo.new.[column1]=dbo.old.[column1]
AND dbo.new.[column2]=dbo.old.[column2]
AND dbo.new.[column3]=dbo.old.[column3]
where [dbo].[new].[column4] <> [dbo].[old].[column4]
ORDER BY row_number() over(partition by dbo.new.[column2] order by dbo.new.[column1]);
Result is
column1 column2 column3 column4
1 1 1 1
3 3 3 3
It looks like you're only interested in column 2 so let's start with only selecting that.
Then add a simple: GROUP BY at the end and you're done.
SELECT N.[column2] as myvalue
FROM dbo.new N
JOIN dbo.old O
ON N.[column1]=O.[column1]
AND N.[column2]=O.[column2]
AND N.[column3]=O.[column3]
WHERE N.[column4] <> O.[column4]
GROUP BY N.[column2]