SQL distinct ID and everything else - sql

Suppose I have a table that contains the following:
CUS_ID A B
1 1 0
2 2 0
3 3 1
3 1 3
4 2 0
Is there a way to write a select statement and grab all the distinct cus_id? So basically it would only bring back one record for cus_id 3.

SELECT DISTINCT CUS_ID FROM table_name;

Or:
select
CUS_ID
-- columns below are optional and exist just to
-- help you understand for what reason 'group by' is needed
-- , count(A) cntA
-- , sum(B) sumB
from YourTable
group by
CUS_ID
order by
CUS_ID
UPD. I meant to comment whole lines :)

Related

One query that matches values with only one condition out of two, one query that matches values with both conditions

I'm having some sort of a blank about how to do this in SQL.
Consider this reprex in R
set.seed(123)
data.frame(ID = (sample(c(1:5), 10, replace = T)),
status = (sample(c("yes", "no"), 10, replace = T)),
amount = (sample(seq(1,50,0.01),10)))
which gives out this table
ID status amount
1 3 no 29.87
2 3 yes 26.66
3 2 yes 15.49
4 2 yes 18.89
5 3 yes 44.06
6 5 no 30.79
7 4 yes 17.13
8 1 yes 6.54
9 2 yes 45.68
10 3 yes 12.66
I need to find two SQL queries.
One where I select the ID's that only have status of 'NO'
meaning ID 5.
and
One where I select the ID's that match both conditions, meaning ID 3
I have a query for both but I'm almost sure it's not correct so any lead is more than welcome.
Thanks
One where I select the ID's that only have status of 'NO' meaning ID 5.
select id from your_table where status='no' and id not in (select id from
your_table where status='yes')
One where I select the ID's that match both conditions, meaning ID 3
select id from your_table where status='no' and id in (select id from
your_table where status='yes')
At last I think you are expecting ids which do not match these conditions. so UNION both queries and get ids of your table which not exists after UNION
select id from your_table where id not in (
select id from your_table where status='no' and id not in
(select id from your_table where status='yes')
union all
select id from your_table where status='no' and id in
(select id from your_table where status='yes')
)

SQL How to count one column when another column is distinct

I have a table that looks something like this:
fin_aid_status | student_number
---------------|---------------
YES | 111222
YES | 111222
| 111333
YES | 111444
I want to count the number of fin_aid_status but not double count rows where student_number is duplicated. So the result I would like from this table would be 2. Not 3 because 111222 is in the table twice. There are many other columns in the table as well though so just looking for unique values in the table will not work.
EDIT: This is Oracle.
For example I am using code like this already:
select count(*), count(distinct student_number) from table
So for third column I would want to count the number on financial aid with unique student numbers.
So my expected output would be:
count(*) | count(distinct student_number) | count_fin_aid
4 | 3 | 2
Use a case statement to evaluate the student_number when the fin_aid_status is not null; then count the distinct values.
SELECT count(Distinct case when fin_aid_status is not null
then student_number end) as Distinct_Student
FROM tbl;
Result using sample data: 2
Given Oracle:
With cte (fin_aid_status, student_number) as (
SELECT 'YES' , 111222 from dual union all
SELECT 'YES' , 111222 from dual union all
SELECT '' , 111333 from dual union all
SELECT 'YES' , 111444 from dual )
SELECT count(Distinct case when fin_aid_status is not null
then student_number end) as DistinctStudentCnt
FROM cte;
If you are using MySQL you can write something as follows, if all you want is count
SELECT count(DISTINCT student_number) FROM your_table WHERE fin_aid_status = 'YES';
I'm assuming here, add some expected results but:
SELECT fin_aid_status,
COUNT(DISTINCT student_number)
FROM tablename
GROUP BY fin_aid_status;
Will give you count of distinct values in the student_number column for each value in the fin_aid_status column

To find total number of rows

I have a table like this
Table1
=======
A B
8 5
2 9
null 4
2 5
How to find total number of rows from table1,note if any column value is null in a row then that row should be considered as 2 rows?
I have tried with count(*)*2 and nvl function it doesn't work
Try this
SELECT SUM(CASE WHEN A IS NULL OR B IS NULL THEN 2 ELSE 1 END) AS CountVal
FROM TABLE1
Fiddle Demo
O/P:
COUNTVAL
--------
5
COUNT() is rowbased.. you can tweak it using SUM() instead..
select sum(NVL2(a,NVL2(b,1,2),2)) FROM TABLE1
CASE as suggested by #Vignesh is the simplest and more readable !!
COUNT() can also done like this.. But NOT a optimal solution at all!
SELECT COUNT(1) FROM
(
SELECT NVL(a,NVL(b,1)) FROM TABLEA
UNION ALL
SELECT NVL(a,NVL(b,1)) FROM TABLEA
WHERE A OR B iS NULL
)

How to declare a row as a Alternate Row

id Name claim priority
1 yatin 70 5
6 yatin 1 10
2 hiren 30 3
3 pankaj 40 2
4 kavin 50 1
5 jigo 10 4
7 jigo 1 10
this is my table and i want to arrange this table as shown below
id Name claim priority AlternateFlag
1 yatin 70 5 0
6 yatin 1 10 0
2 hiren 30 3 1
3 pankaj 40 2 0
4 kavin 50 1 1
5 jigo 10 4 0
7 jigo 1 10 0
It is sorted as alternate group of same row.
I am Using sql server 2005. Alternate flag starts with '0'. In my example First record with name "yatin" so set AlternateFlag as '0'.
Now second record has a same name as "yatin" so alternate flag would be '0'
Now Third record with name "hiren" is single record, so assign '1' to it
In short i want identify alternate group with same name...
Hope you understand my problem
Thanks in advance
Try
SELECT t.*, f.AlternateFlag
FROM tbl t
JOIN (
SELECT [name],
AlternateFlag = ~CAST(ROW_NUMBER() OVER(ORDER BY MIN(ID)) % 2 AS BIT)
FROM tbl
GROUP BY name
) f ON f.name = t.name
demo
You could use probably an aggregate function COUNT() and then HAVING() and then UNION both Table, like:
SELECT id, A.Name, Claim, Priority, 0 as AlternateFlag
FROM YourTable
INNER JOIN (
SELECT Name, COUNT(*) as NameCount
FROM YourTable
GROUP BY Name
HAVING COUNT(*) > 1 ) A
ON YourTable.Name = A.Name
UNION ALL
SELECT id, B.Name, Claim, Priority, 1 as AlternateFlag
FROM YourTable
INNER JOIN (
SELECT Name, COUNT(*) as NameCount
FROM YourTable
GROUP BY Name
HAVING COUNT(*) = 1 ) B
ON YourTable.Name = B.Name
Now, this assumes that the Names are unique meaning the names like Yatin for example although has two counts is only associated to one person.
See my SqlFiddle Demo
You can use Row_Number() function with OVER that will give you enumeration, than use the reminder of integer division it by 2 - so you'll get 1s and 0s in your SELECT or in the view.

Counting in sql and subas

I have the following code
select ID, count(*) from
( select ID, service type from database
group by 1,2) suba
group by 1
having count (*) > 1
And I get a table where i see the IDs and a count of changes. Similar to this
ID | Count(*)
5675 | 2
5695 | 3
5855 | 2
5625 | 4
5725 | 3
Can someone explain to me how to count all the count(*) into groups such that i get a table similar to...
count (*) | number
2 | 2
3 | 2
4 | 1
and so forth. Can someone also explain to be me what suba means?
MY NEWEST CODE:
select suba.id, count(*) from
( select id, service_type from table_name
group by 1,2) as suba
group by 1
having count (*) > 1
Haven't tried it, but I think this should work
select NoOfChanges, count (*) from
(
select suba.id, count(*) as NoOfChanges from
( select id, service_type from table_name
group by 1,2) as suba
group by 1
having count (*) > 1
)
subtableb
group by NoOfChanges
You can think of that as
select NoOfChanges, count (*) from subtableb
group by NoOfChanges
but subtableb isn't a real table, but the results from your previous query
suba is the alias of the subquery. Every table or subquery needs a unique name or an alias so you can refer to it in other parts of the query (and disambiguate). Note there is a missing implicit AS between the closing parenthesis and "suba".