SQL Show All column by one column distinct - sql

I have a table with duplicate item's ...
I need show the list of all columns without duplicate item's
for example i have this table:
ID CODE RANK TIME
1 12345 2 10:00
2 12345 2 11:00
3 98765 3 20:00
4 98765 3 22:00
5 66666 2 10:00
6 55555 5 11:00
result , i need :
ID CODE RANK TIME
1 12345 2 10:00
3 98765 3 20:00
5 66666 2 10:00
6 55555 5 11:00
The time column in not Important , only one of them most be show ...

try this:
SELECT * FROM myTable WHERE ID IN(SELECT MIN(ID) FROM myTable GROUP BY Code)

If there is no specific way the ID should show (just like the time column), and the ID and TIME column are always sorted that way,this should work.
SELECT MIN(id), code, rank, MIN(time)
FROM table
GROUP BY code, rank

So you only want rows where the CODE is not duplicated in the table.
SELECT "CODE"
FROM table1
GROUP BY "CODE"
HAVING COUNT(*) = 1
This will return the distinct CODE-s. Based on them - as they are unique - you can self-join it to fetch the whole rows:
SELECT *
FROM table1
WHERE "CODE" IN (
SELECT "CODE"
FROM table1
GROUP BY "CODE"
HAVING COUNT(*) = 1
)

I think you are looking for the DISTINCT clause;
SELECT DISTINCT
column_1,
column_2
FROM
tbl_name;

Related

combine two table with different content to make a table where each id has two rows in bigquery

hope you can help me with this. so i have one table that contains value
user_id
Monday.
Tuesday
Wednesday
aa11
100
164
284
bb22.
223
143
346
and another table that contains count
user_id
Monday.
Tuesday
Wednesday
aa11
2
3
4
bb22
5
2
3
how can i combine both so it looks like this?
user_id
type
Monday.
Tuesday
Wednesday
aa11
value
100
164
284
aa11
count
2
3
4
bb22.
value
223
143
346
bb22
count
5
2
3
is it even a good practice to pivot long like this or should i just pivot wide so there would be columns monday_value, monday_count, tuesday_value, tuesday_count, etc?
Simple union? :
select * from (
select user_id, 'value' as type,Monday.,Tuesday,Wednesday
from table1
union all
select user_id, 'count' as type,Monday.,Tuesday,Wednesday
from table2
) t
You can use simple way as given in answer by #eshirvana.
Or as per your ask, if you want to do it horizontally you can do it following way:
WITH week_value AS
(
SELECT 1 as user, 100 as monday
UNION ALL SELECT 2 as user, 200 as monday
),
week_count as (
SELECT 1 as user, 10 as monday
UNION ALL SELECT 2 as user, 2 as monday
)
select A.user, A.monday as monday_value, B.monday as monday_count
from week_value as A
FULL JOIN week_count as B on A.user = B.user

Count values separately until certain amount of duplicates SQL

I need a Statement that selects all patients and the amount of their appointments and when there are 3 or more appointments that are taking place on the same date they should be counted as one appointment
That is what my Statement looks so far
SELECT PATSuchname, Count(DISTINCT AKTDATUM) AS AKTAnz
FROM tblAktivitaeten
LEFT OUTER JOIN tblPatienten ON (tblPatienten.PATID=tblAktivitaeten.PATID)
WHERE (AKTDeleted<>'J' OR AKTDeleted IS Null)
GROUP BY PATSuchname
ORDER BY AKTAnz DESC
The result should look like this
PATSuchname Appointments
----------------------------------------
Joey Patner 13
Billy Jean 15
Example Name 13
As you can see Joey Patner has 13 Appointments, in the real table though he has 15 appointments but three of them have the same Date and because of that they are only counted as 1
So how can i write a Statement that does exactly that?
(I am new to Stack Overflow, sorry if the format I use is wrong and tell me if it is.
In the table it looks like this.
tblPatienten
----------
PATSuchname PATID
------------------------
Joey Patner 1
Billy Jean 2
Example Name 3
tblAktivitaeten
----------
AKTDatum PATID AKTID
-----------------------------------------
08.02.2021 1 1000 ----
08.02.2021 1 1001 ---- So these 3 should counted as 1
08.02.2021 1 1002 ----
09.05.2021 1 1003
09.07.2021 2 1004 -- these 2 shouldn't be counted as 1
09.07.2021 2 1005 --
Two GROUP BY should do it:
SELECT
x.PATID, PATSuchname, SUM(ApptCount)
FROM (
SELECT
PATID, AKTDatum, CASE WHEN COUNT(*) < 3 THEN COUNT(*) ELSE 1 END AS ApptCount
FROM tblAktivitaeten
GROUP BY
PATID, AKTDatum
) AS x
LEFT JOIN tblPatienten ON tblPatienten.PATID = x.PATID
GROUP BY
x.PATID, PATSuchname

Query to find value in column dependent on a different column in table being the minimum date

I have a dataset that looks like this. I would like to pull a distinct id, the minimum date and value on the minimum date.
id date value
1 01/01/2020 0.5
1 02/01/2020 1
1 03/01/2020 2
2 01/01/2020 3
2 02/01/2020 4
2 03/01/2020 5
This code will pull the id and the minimum date
select Distinct(id), min(nav_date)
from table
group by id
How can I get the value on the minimum date so the output of my query looks like this?
id date value
1 01/01/2020 0.5
2 01/01/2020 3
Use distinct on:
select distinct on (id) t.*
from t
order by id, date;
This can take advantage of an index on (id, date) and is typically the fastest way to do this operation in Postgres.

SQL: Adding new column to show count of ID by date

I am hoping someone can help me with my query.
I have a table with the columns, 'Date', 'ID_Num and 'Name'. What I want to do is add a column at the end to show the total amount of times each ID_Num is within the data but based on the date. So although 'ID_Num' 1001 shows 4 times in total, it is twice on the 20/04/2018 and once on both the 21/04/2018 and 22/04/2018.
EDIT: I should have stipulated that I will be pulling several other columns with information, which I cant use a group by on everything.
Date ID_Num Name Count
20/04/2018 1001 John 2
20/04/2018 1001 John 2
20/04/2018 1002 Paul 2
20/04/2018 1002 Paul 2
20/04/2018 1003 David 2
20/04/2018 1003 David 2
20/04/2018 1004 Stephen 1
21/04/2018 1001 John 1
21/04/2018 1002 Paul 3
21/04/2018 1002 Paul 3
21/04/2018 1002 Paul 3
21/04/2018 1004 Stephen 1
22/04/2018 1001 John 1
22/04/2018 1002 Paul 1
22/04/2018 1003 David 1
22/04/2018 1004 Stephen 1
Thanks
Unless I'm missing something here, a simple group by and count should do it:
SELECT Date, ID_Num, Name, Count(*)
FROM TableName
GROUP BY Date, ID_Num, Name
(That is, assuming there can only be one Name for each ID_Num)
Update
Assuming your rdbms supports it, you can use count with an over clause:
SELECT Date, ID_Num, Name, Count(*) OVER(PARTITION BY Date, Id_Num)
FROM TableName
If not, you can use a sub query:
SELECT Date,
ID_Num,
Name,
(SELECT Count(*)
FROM TableName As t1
WHERE t1.Date = t0.Date
AND t1.ID_NUM = t0.ID_NUM)
FROM TableName As t0
Try this:
SELECT
Date,
Id_num,
count(*) count
FROM
tabel_name
GROUP BY
Date,
Id_num
If you want name as well:
SELECT
Date,
Id_num,
Name
count(*) count
FROM
tabel_name
GROUP BY
Date,
Id_num,
Name
You can use a normal select query and then add a sub query to do a group and show the total. Simple example below
SELECT Date, ID_Num, Name,
(SELECT Count(ID_Num) FROM TableName AS CHILD WHERE CHILD.Id_Num = Parent.Id_Num) AS Total
FROM TableName AS Parent

Getting a row with two group by constraints

I have a table
TIMESTAMP ID Name
5/30/2016 11:45 1 Ben
5/30/2016 11:45 2 Ben
5/30/2016 23:15 2 Ben
5/30/2016 7:30 1 Peter
5/30/2016 6:05 1 Peter
5/30/2016 14:40 2 May
5/30/2016 1:05 1 May
Now, I need to get the MIN timestamp for each distinct Name.
Then if there are more than one MIN entry, choose the one with the MAX ID.
So the result should be
TIMESTAMP ID Name
5/30/2016 11:45 2 Ben
5/30/2016 6:05 1 Peter
5/30/2016 1:05 1 May
I tried using the query below:
SELECT MIN(TIMESTAMP),NAME FROM TBLSAMPLE WHERE TIMESTAMP BETWEEN TO_DATE('5/30/2016', 'MM/DD/YYYY' ) AND TO_DATE('5/30/2016', 'MM/DD/YYYY' ) + 1
GROUP BY NAME
and I could get the minimum time. But once I add in MAX(ID) the result return an entry that does not match any of the rows.
Your help are really appreciated.
You can do this with row_number():
select t.*
from (select t.*,
row_number() over (partition by name order by timestamp asc, id desc) as seqnum
from tblsample t
) t
where seqnum = 1;
Your question doesn't specify a condition on the dates. But if you want to add a where clause, then add it to the subquery.