MYSQL how to increment column count if different combination row exist [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
MYSQL how to increment column count if different combination row exist
for example if table have
count user certNo
0 a 001
0 b 886
if same user with different certNo comes then i need to increment count by 1,
like
count user certNo
0 a 001
0 b 886
1 a 673

For legacy MySQL version you can use next query:
select
t.*,
count(t1.certNo) as count
from test t
left join test t1 on t1.user = t.user and t1.certNo < t.certNo
group by t.user, t.certNo;
Test it on SQLize.online

You can use row number():
select row_number() over (partition by user order by certNo) - 1 as count,
user, certNo
from t;

Related

how do I select records that are missing a particular type of row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a payment table. Type 1 is new/first payment. Type 11 is renew payment. So each user should have one record of type 1. But due to bad coding, right now, some records have only renew records. I want to find all the records that are missing the type 1 record. In the example below, it should return user 3.
user type amount
1 1 10
1 11 10
2 1 10
3 11 10
How should I write the query?
One way to find all users in the table that don't have a record of Type 1 is a NOT EXISTS using a subquery, as follows:
SELECT p.[user]
FROM payment p
WHERE NOT EXISTS (
SELECT 1
FROM payment
WHERE `user` = p.[user]
AND type = 1)
Alternatively, this can be done with a LEFT JOIN and only selecting the non-matching rows:
SELECT p.[user]
FROM payment p
LEFT JOIN payment p2
ON p2.[user] = p.[user]
AND p2.type = 1
WHERE p2.[user] IS NULL

SQL to find mutual friends from the same table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to find the mutual friends with SQL from a single table
I have a simple table with 2 fields user, friends as int. Data values as are as below
Expected Output
user friend mutual friends
1 2 2
1 3 1
This is the query i tried
select ex1.user,ex2.friend, count(distinct ex3.friend) from (select distinct user from exer1) ex1
join exer1 ex2, exer1 ex3
where ex1.user=ex2.user and
ex2.user<>ex3.user and ex2.friend<>ex3.friend
group by ex1.user,ex2.friend order by 1,2
The output i got was
enter image description here
The desired output I am looking for is
enter image description here
You can use a self-join and aggregation:
select f1.user, f2.user, count(*)
from friends f1 join
friends f2
on f1.friend = f2.friend and
f1.user < f2.user
group by f1.user, f2.user;

DESC command in SQL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What is the use of DESC command in SQL?
"desc" by itself isn't a command. I suppose you mean "ORDER BY field DESC"
the "desc" means that the results obtained from the query will be showed in descending order.
Example:
Suppose you have a bills table with an id field and a total of 5 rows in that table. Those five rows have id: 1 (row 1), 2 (row 2) and so on. Well with a query of this type:
SELECT id FROM bills ORDER BY id DESC
You will get as query result:
5
4
3
2
1
Why? because you asked for descending order in the query instruction (from highest number to lowest number) in this case.

MYSQL SUM Value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 table :
table structure for "kegiatanrenja"
idkegiatan tahun koderekening detail
renja001 2001 1.03.03.04.1 A
renja002 2001 1.03.03.03.2 B
renja003 2001 1.08.08.05.3 C
renja004 2001 1.08.08.05.2 D
table structure for "sumberdana"
idkegiatan nilai kodesumberdana
renja001 100 1
renja002 200 2
renja003 50 1
renja004 100 1
I want to create a query that displays the value of all the columns "nilai" if MID (koderekening, 9,2) the same value, along with the value of another column, where each row in the group by where each row in the group by MID (koderekening, 9,2).
kode rekening nilai_group(SUM Of value the same kode MID(koderekening,9,2)
1.03.03.04 300
1.08.08.05 150
please help, every suggestion will be very appreciated.
SELECT LEFT(koderekeneing,0,10), sum (nilal)
FROM kegiatanrenja k
JOIN sumberdana s ON s.idkegiatan = k.idkegiatan
GROUP BY MID(koderekening,9,2)
UNION
SELECT 'ALL', sum (nilal)
FROM kegiatanrenja k
JOIN sumberdana s ON s.idkegiatan = k.idkegiatan
Please note, this is VERY easy, so if you are asking this sort of question on stack lot, you are going to receive a negative response. Please get yourself a good SQL book and do some studying.

How to find rows that has values that exists with criteria 'a' in a table but does not exist with criteria 'b'? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an aggregated table like:
No Field1 Type
12 -1 FR2
12 45 FR1
11 -1 FR2
11 30 FR2
11 20 FR1
Is it possible to select Nos according to types where they possess negative values in Field1 and no other entries with positive values?
Is it possible to find the negation of that selection?
Hence the output should be:
No
11
Explanation:
I am trying to check for people who have failed a list of tests and have not retaken and passed them. Those who never failed as well as those who retook and passed is the result.
In the result output, 12 is not returned because it has negative values for FR2 and no subsequent positive values for FR2.
Thanks.
SELECT No
FROM
( SELECT No, MAX(Field1) AS MaxField1
FROM tableX
GROUP BY No, Type
) AS tmp
GROUP BY No
HAVING MIN(MaxField1) > 0 ;
Tested at SQL-Fiddle
select distinct No from yourtable
group by No,Type
having max(Field1) >0
and min(Field1)<0