How to do a count 2 columns - sql

I have the following table
cus_id
gov_id
name
1
aa
Bob
1
bb
Bill
1
aa
James
2
cc
Sam
3
aa
Sarah
1
aa
Joe
2
cc
Nathan
As you can see when the cus_id=1 and gov_id=aa there are 3 duplicates, thus the count is 3. I want to count how many instances where the cus_id and gov_id are the same, as in the row.
When cus_id=2 and gov_id=cc there are 2 duplicates. I want the output like this:
cus_id
gov_id
name
count
1
aa
Bob
3
1
bb
Bill
1
1
aa
James
3
2
cc
Sam
2
3
aa
Sarah
1
1
aa
Joe
3
2
cc
Nathan
2
I tried:
SELECT cus_id, gov_id, name, count(*) as count
FROM test_table;

You can use analytics functions:
select t.*,
count(*) over (partition by cus_id, gov_id) as cnt
from t;

Related

Count children from hierarchy path

I have a table like this:
id
name
path
1
John
/1
2
Mark
/2
3
Kevin
/1/3
4
Sarah
/1/3/4
5
Andy
/2/5
...
...
...
So, I can say that Sarah is Kevin's child which is John's child.
I would like to have this:
id
name
path
number of children
1
John
/1
2
2
Mark
/2
1
3
Kevin
/1/3
1
4
Sarah
/1/3/4
0
5
Andy
/2/5
0
...
...
...
...
TASK NUMBER 2:
Let's say that I have this table too
id
income
user_id
1
200
1
2
120
1
3
340
2
4
500
3
5
600
5
6
80
5
I can say that John has a Total income of 320$, but if I also want to count John's children, it is 820$ (because id =3 is John's child). So, I would also like a query where I can count all the hierarchical incomes.
You can do:
select
t.*,
(select count(*) from t c where c.path like t.path || '/%') as c_count,
i.income + (
select coalesce(sum(i.income), 0) from t c join i on i.user_id = c.id
where c.path like t.path || '/%'
) as c_income
from t
left join (
select user_id, sum(income) as income from i group by user_id
) i on i.user_id = t.id
Result:
id name path c_count c_income
--- ------ ------- -------- --------
1 John /1 2 820
2 Mark /2 1 1020
3 Kevin /1/3 1 500
4 Sarah /1/3/4 0 null
5 Andy /2/5 0 680
See example at DB Fiddle.

Hive SQL: Select all rows before event

In Hive, I have the following data
sess,person,type,number
a mary I 1
a mary I 2
a mary V 3
a mary V 4
b mary I 1
b mary V 2
b mary C 3
a john I 1
a john I 2
a john V 3
a john V 4
b john I 1
b john V 2
b john C 3
How do I select everything for each person and session up to and including the first type=V? The output should look like
sess,person,type,number
a mary I 1
a mary I 2
a mary V 3
b mary I 1
b mary V 2
a john I 1
a john I 2
a john V 3
b john I 1
b john V 2
You can use window functions:
select t.*
from (select t.*,
min(case when type = 'V' then number end) over (partition by session, person order by number) as min_aid
from t
) t
where min_aid is null or number <= aid;

sql top 1 based on group records

Table ProductMst (combination is unique - CodeNum, MainCatid, SubCatid)
id CodeNum MainCatid SubCatid Desc Qty
1 001 1 1 prod1 5
2 001 1 2 prod2 10
3 001 2 3 prod3 1
4 002 2 3 prod4 2
5 003 2 3 prod5 3
6 004 1 1 prod6 4
Sub Table SubCatMst
id name subcode
1 scat1 00
2 scat2 00
3 scat3 02
4 scat4 03
Desired Result
id CodeNum MainCatid SubCatid Desc Qty subcode
2 001 1 2 prod2 15 00
3 001 2 3 prod3 1 02
4 002 2 3 prod4 2 02
5 003 2 3 prod5 3 02
6 004 1 1 prod6 4 00
Basically, i wanted to group subcode if same and sum (Qty) based on subcode. And, take the TOP 1 records data as all columns based on MAX(Qty).
Summary: Please check first 2 records which combines.
Query Attempt:
select * from (
select A.*, B.subcode,
ROW_NUMBER() OVER( PARTITION BY A.CodeNum, A.MainCatid, B.subcode ORDER BY A.Qty desc) as row
from ProductMst A
inner join SubCatMst B on A.SubCatid=B.id
) as A where row<=1
Your question says:
i wanted to group subcode if same
If what is the same? I assume this means if codenum is the same.
Regardless of the exact field(s), the logic is the same. You can use window functions to aggregate the data and determine the row to choose:
select ps.*
from (select p.*, sc.subcode,
sum(qty) over (partition by codenum, subcode) as qty,
row_number() over (partition by codenum, subcode order by qty desc) as seqnum
from ProductMst p join
SubCatMst sc
on p.subcatid = sc.id
) ps
where seqnum = 1;

Retrieve data from Table

I have a table as follows,
Id Form_Id Form_Layout_Txt
-------------- ----------- -----------------
2 1 aa
3 1 bb
5 2 dddd
6 2 eeeee
7 3 fffff
8 3 gggg
i need to retreive the rows which has same Form_id as follows,
2 1 aa
3 1 bb
How to write a query for the above data?
Thanks
Here is what you need at most basic level, unless there's a hidden immunity to find out more :)
SELECT * FROM YOUR TABLE
WHERE FORM_ID = 1
Select *
from nameoftable
where Form_Id = 1;

Trying to figure out the sum of a certain variable not total sum using sql

I'm having a mindblank here but my question is quick.
I have a table like the following:
Cust Part Qty Rtn Qty
Joe A1 2 0
Joe A2 3 1
Joe A1 2 2
Bob A1 3 4
Bob A2 4 0
I want the results to be like:
Joe A1 4 2
Joe A2 3 1
Bob A1 3 0
Bob A2 4 0
I'm trying to figure out how to create a sum for each customer individually according to the part?
That looks like a straightforward group by on Cust, Part:
select Cust
, Part
, sum(Qty)
, sum([Rtn Qty])
from YourTable
group by
Cust
, Part