How to calculate with multiple value in SQL [closed] - mysql

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 6 years ago.
Improve this question
How to dividing field "budget" with multiple value
Look like "SELECT fName, (budget/ 2,3,4) FROM Table"
fName budget
---------------
A 100 >>divide with 2
B 100 >>divide with 4
C 100 >>divide with 6
The result is
fName budget
---------------
A 50
B 25
C 16.67

SELECT fname,CASE WHEN fname ='A' THEN budget/2
WHEN fname ='B' THEN budget/4
WHEN fname ='C' THEN budget/6
END
FROM tableName

Although it's very unclear what you're asking, I guess you may need something like this:
create temporary table and put your desired values in it. then make a cross join to your main table:
SELECT
fName
, Table.budget / tempTable.ColumnName
FROM
Table CROSS JOIN tempTable

Related

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;

SQL Display student record having maximum marks from other table [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 4 years ago.
Improve this question
student(sID,sNAME,sCLASS);
result(sID,subMARKS);
Actually, in MS-ACCESS, i am trying to this with equi join but getting wrong result. i'm writting my query like
SELECT stud.sID
, stud.sNAME
, stud.sCLASS
, result.sID
FROM student
, result
WHERE(SELECT MAX(subMARKS) FROM result)
It should display Ali record only because he is having maximum marks. but i am getting this kind of output as shown in picture below.
sID sNAME sCLASS
1 Ali BSC
2 Ahmad FSC
3 Asgar ICS
4 Akram BSC
SELECT T1.SID, T1.sname FROM student T1
LEFT JOIN resultT2 ON t1.sid=t2.sid
WHERE t2.submarks = (SELECT Max(submarks) FROM result);

SQL Query conditional row [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 8 years ago.
Improve this question
I have a table called 'list' that is like this:
system item1 exception
------------------------
A John 1
A Sarah 0
A Tim 1
A Blake 0
B Nikki 1
B Rick 0
C Jimbo 1
I am trying to build a query and I'm so terrible at it and I'm stuck. I want to return all rows unconditionally UNLESS system = 'A'. If system = 'A' then only return the rows where exception is true.
Thanks for your time.
What about this?
SELECT * FROM table WHERE system != 'A' OR exception = 1
SQL FIDDLE DEMO
select *
from list
where (system = 'A' and exception = 1)
or system <> 'A'
You can group conditions in the WHERE clause by enclosing each block in ()
http://www.techonthenet.com/sql/and_or.php

MySQL - Return Distinct Rows and Count of their Duplicates [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 8 years ago.
Improve this question
MySQL isn't my strong suit, can someone recommend an efficient query for pulling all distinct values from a column and counting all duplicates of each distinct value?
So for example the following table
s_ip
---------
10.1.25.4
10.8.25.8
10.1.25.4
10.1.25.4
10.8.25.8
10.1.48.1
10.1.25.4
Would return
s_ip | Count
----------|------
10.1.25.4 | 4
10.8.25.8 | 2
10.1.48.1 | 1
Thanks in advance
SELECT s_ip , COUNT(*) as count FROM table_name GROUP BY (s_ip)
select
s_ip, count(*) as `count`
from table
group by s_ip
DEMO

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.