I have two table. one with 50 records in table A and other with 100 in table B.
Both table have customer id and date on it.
Both table have duplication but there are less number of unique customer in table with 50.
Now i wish to put date of customer in table B in front of customer in table A. a customer can appear more than 2 time in table B.
So number of time it appear, number of column appended with date in front of the customer.
Id Date
1 1/01/2016
2 2/01/2016
2 3/01/2016
3 1/01/2016
4 4/01/2016
5 5/01/2016
6 1/01/2016
7 3/01/2016
8 4/01/2016
9 1/01/2016
1 6/01/2016
10 1/01/2016
For eg. this is table A
and Table B is
Id Date
1 1/01/2016
2 2/01/2016
2 3/01/2016
3 1/01/2016
4 4/01/2016
5 5/01/2016
6 1/01/2016
7 3/01/2016
8 4/01/2016
9 1/01/2016
1 6/01/2016
10 1/01/2016
1 15/01/2016
2 16/01/2016
2 17/01/2016
3 18/01/2016
4 19/01/2016
5 20/01/2016
6 21/01/2016
7 16/01/2016
8 20/01/2016
9 21/01/2016
1 16/01/2016
10 18/01/2016
1 19/01/2016
2 2/02/2016
2 30/01/2016
3 16/01/2016
4 31/01/2016
5 21/01/2016
6 18/01/2016
7 19/01/2016
8 16/01/2016
9 2/02/2016
1 18/01/2016
10 19/01/2016
I wish to have output given under
Id Date Date1 Date2
1 1/01/2016 15/01/2016 19/01/2016
2 2/01/2016 16/01/2016 2/02/2016
2 3/01/2016 17/01/2016 30/01/2016
3 1/01/2016 18/01/2016 16/01/2016
4 4/01/2016 19/01/2016 31/01/2016
5 5/01/2016 20/01/2016 21/01/2016
6 1/01/2016 21/01/2016 18/01/2016
7 3/01/2016 16/01/2016 19/01/2016
8 4/01/2016 20/01/2016 16/01/2016
9 1/01/2016 21/01/2016 2/02/2016
1 6/01/2016 16/01/2016 18/01/2016
10 1/01/2016 18/01/2016 19/01/2016
Any help is much much appreciable. Thanks
This should explain how to use joins. Once you learn about that you can write SQL yourself.
http://www.w3schools.com/sql/sql_join.asp
try understanding LEFT and Inner Joins
try this (not easy)
with tmp1 as (
select row_number() over(order by (select null) ) - 1 newid , (row_number() over(order by (select null)) - 1) %12 rg1 , f1.*
from table2 f1
),
tableranked as (
select DENSE_RANK() over(partition by rg1 order by newid) rg2, f2.*
from tmp1 f2
)
select f1.id, f1.date, f4.date1, f4.date2
from tableranked f1 inner join (select f2.id, f2.date as date1, f3.date as date2 , f2.rg1
from tableranked f2
inner join tableranked f3 on f2.id=f3.id and f2.rg2+1=f3.rg2 and f2.rg1=f3.rg1
where f2.newid>=12 and f3.newid>=12
) f4 on f1.id=f4.id and f1.rg1=f4.rg1
where f1.newid<12
Related
i have table structure like this
id
user_id
parent_id
club
1
1
club1
2
2
1
club1
3
3
1
club1
4
4
2
club1
5
5
2
club1
6
6
3
club1
7
7
3
club1
8
8
4
club1
9
9
4
club1
10
10
5
club1
11
11
5
club1
12
12
6
club1
13
13
6
club1
14
14
7
club1
15
15
7
club1
i want to select user_id whose child is less then 2.
refer to above table user 1,2,3,4,5,6 and 7 two child complete so the query should return 8,9,10,11,12,13,14,15
Refer to above table here user_id 1 have two child 2 and 3 , user_id 2 have child 4 and 5 and so on.
i need to select user_id whose child is less than 2
You can do it as follows :
SELECT user_id
FROM club_tree
WHERE user_id NOT IN (
SELECT parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
)
and club = 'club1';
We Select users that are not in the list of users with more than two childs.
This is to get users with more than two childs :
SELECT parent_id
from club_tree
group by parent_id
HAVING count(1) >= 2
Here you can test it : http://sqlfiddle.com/#!9/eb2c70/3
Can you try this query using left join :
SELECT c.user_id
from club_tree c
left join
(
select parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
) as c2 on c.user_id = c2.parent_id
where c.club = 'club1' and c2.parent_id is null;
Try it here : http://sqlfiddle.com/#!9/eb2c70/17
I have a table of matches of a game in a tournement.
Here is the mysql table:
id
player1
player1_score
player1_rating
player2
player2_score
player2_rating
tourney_id
round
1
1
9
1.05
2
3
5.34
5
1
2
3
4
5.21
4
9
3.34
5
1
3
5
9
3.52
6
2
5.24
5
1
4
1
9
6.23
3
0
4.74
5
2
5
2
8
9.43
4
9
1.23
5
2
6
3
9
3.41
5
7
6.23
5
2
7
1
9
5.22
4
2
2.43
5
3
8
2
3
4.21
3
9
5.22
5
3
9
5
1
7.31
6
9
3.43
5
3
How can I get the average player ratings for each individual player in a particular tourney?
Please note, there's two columns with player ids (player1, player2)
Based on your requirements (Even though you do not give us your expected results), I assume your code should be like this:
SELECT (SELECT SUM(player1_rating) / COUNT(DISTINCT player1)
FROM your_table GROUP BY player1) as avg_player1,
(SELECT SUM(player2_rating) / COUNT(DISTINCT player2) FROM your_table
GROUP BY player2) as avg_player2
FROM your_table
I revised the query to:
SELECT player_id
, tourney_id
, avg_player_rating = SUM(player_rating) / SUM(cnt)
FROM (
SELECT player_id = player1
, tourney_id
, player_rating = SUM(player1_rating)
, cnt = COUNT(*)
FROM tournament
GROUP BY tourney_id, player1
UNION ALL
SELECT player_id = player2
, tourney_id
, player_rating = SUM(player2_rating)
, cnt = COUNT(*)
FROM tournament
GROUP BY tourney_id, player2
) AS a
GROUP BY player_id, tourney_id
and this will give the result:
player_id tourney_id avg_player_rating
---------- ---------- --------------------
1 5 4.1666
2 5 6.3266
3 5 4.6450
4 5 2.3333
5 5 5.6866
6 5 4.3350
I hope that this is the result that you are looking for.
I have table structure like where Loanid is Foreign Key
TranID LOANID TRANSDATE
2 2 2013-05-01
13 2 2013-05-10
14 2 2013-05-15
6 5 2013-05-01
7 5 2013-06-10
8 5 2013-06-14
9 5 2013-07-01
10 5 2013-07-10
i need a query to calculate Days between like below .
TranID LOANID TRANSDATE DAYS_BETWEEN
2 2 2013-05-01 9
13 2 2013-05-10 5
14 2 2013-05-15 0
6 5 2013-05-01 41
7 5 2013-06-10 4
8 5 2013-06-14 17
9 5 2013-07-01 9
10 5 2013-07-10 0
Possibly a self join, using MIN to get the next date.
SELECT t1.tranid,
t1.loanid,
t1.transdate
DATEDIFF(IFNULL(MIN(t2.transdate), t1.transdate), t1.transdate) AS days
FROM some_table t1
LEFT OUTER JOIN some_table t2
ON t1.loanid = t2.loan_id
AND t1.transdate < t2.transdate
GROUP BY t1.tranid,
t1.loanid,
t1.transdate
I have a MySql table with 3 coluomn : Nip, Bidang and Total.
I want to count min and max value of total with the same bidang. but i don't want to count min and max value for all coloumns.
Sample data:
NIP Bidang Total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
And the result column:
MIN
1
1
1
4
4
8
8
Simply do this:
SELECT * FROM
(
(SELECT Bidang FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(Total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
BIDANG MINVAL MAXVAL
A 1 10
A 1 10
A 1 10
B 4 7
B 4 7
C 8 9
C 8 9
See result in SQL Fiddle.
EDIT
To see the Total column also, add Total to first query.
SELECT * FROM
(
(SELECT NIP,Bidang,Total FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
NIP BIDANG TOTAL MINVAL MAXVAL
1 A 10 1 10
2 A 5 1 10
3 A 1 1 10
4 B 4 4 7
5 B 7 4 7
6 C 8 8 9
7 C 9 8 9
See result in SQL Fiddle.
Select bidang, min(total) MyMin, Max(total) myMax
From tableName
group by bidang
NIP bidang total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
should return
A 1 10
B 4 7
C 8 9
Good day everyone..!:-)
I have this table tab where totalUsed is equal to the sum of all used values referenced to name
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 0
7 a 5 0
i have this code but it only copy totalUsed's adjacent used value
UPDATE tab
SET totalUsed=
(
SELECT SUM(used)
)
cid name used total
1 a 1 1
2 a 3 3
3 a 6 6
4 b 3 3
5 b 7 7
6 b 10 10
7 a 5 5
if used is set as 10 for cid 6, totalUsed should be 20
and for cid 7 it should be 15.
how to do it in mysql?
it should look like this.
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 20
7 a 5 15
thanks for help
:-)
In most dialects of SQL, you can do this using an update with a correlated subquery:
UPDATE tab
SET totalUsed = (SELECT SUM(used)
from tab tab2
where tab2.name = tab.name and
tab2.cid <= tab.cid
);
EDIT:
The above will not work in MySQL. You can do this instead:
UPDATE tab join
(select tab2.cid,
(SELECT SUM(used)
from tab tab3
where tab3.name = tab2.name and
tab3.cid <= tab2.cid
) as cum_used
from tab tab2
) tab2
on tab.cid = tab2.cid
SET tab.totalUsed = tab2.cum_used;