Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 July SQL Puzzle: Find Correlation Coefficients for the Run of Cricket Players

SQL Puzzle: Find Correlation Coefficients for the Run of Cricket Players

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to find the Correlation Coefficients for the run of Cricket Players.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PlayerName Runs MatchYear
---------- ----------- -----------
Dhoni 1200 2014
Dhoni 1300 2015
Dhoni 1100 2016
Dhoni 1800 2017
Dhoni 2000 2018
Kohli 2200 2018
Kohli 1100 2017
Kohli 1300 2016
Kohli 1500 2015
Kohli 1800 2014
Yuvraj 1000 2014
Yuvraj 900 2015
Yuvraj 1200 2016
Yuvraj 1300 2017
Yuvraj 700 2018

Expected Output:

1
2
3
4
5
Player1 Player2 PlayersCoefficientRuns
---------- ---------- ----------------------
Dhoni Kohli 0.332662790481163
Dhoni Yuvraj -0.311842111156846
Kohli Yuvraj -0.915321318268559

Sample table:

1
2
3
4
5
6
7
8
CREATE TABLE tbl_Players (PlayerName VARCHAR(10), Runs INT, MatchYear INT)
 
INSERT INTO tbl_Players VALUES
('Dhoni',1200, 2014),('Kohli',1800, 2014),('Yuvraj',1000, 2014)
,('Dhoni',1300, 2015),('Kohli',1500, 2015),('Yuvraj',900, 2015)
,('Dhoni',1100, 2016),('Kohli',1300, 2016),('Yuvraj',1200, 2016)
,('Dhoni',1800, 2017),('Kohli',1100, 2017),('Yuvraj',1300, 2017)
,('Dhoni',2000, 2018),('Kohli',2200, 2018),('Yuvraj',700, 2018)

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
select
Player1
,Player2
,(Avg(Runs1 * Runs2) - (Avg(Runs1) * Avg(Runs2))) /
(StDevP(Runs1) * StDevP(Runs2)) as PlayersCoefficientRuns
from
(
select
a.PlayerName as Player1
,a.Runs as Runs1
,b.PlayerName as Player2
,b.Runs as Runs2
,a.MatchYear
from tbl_Players a
cross join tbl_Players b
where b.PlayerName>a.PlayerName and a.MatchYear=b.MatchYear
) as t
group by Player1, Player2

Please try the different solution for this puzzle and share it via comment...

Jul 2, 2018Anvesh Patel
SQL Server: Extra Tabs and Spaces of object occupies more disk areaSQL Server: Script to check the file exists or not (Using xp_fileexist )
Comments: 3
  1. dinesh danny
    July 4, 2018 at 4:57 am

    Hi sir,
    I had seen your sql puzzle. really its very interesting. And more thing i need to learn Postgresql administration part can you please explain and post postgres.conf file information, vacuum analyze, MVCC concepts, replication concepts and all concepts.
    Because the way of explaining topics is very clear and very easy to understand. Can you please me.

    regards,
    Dinesh.A

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      July 4, 2018 at 6:03 pm

      Sure, will help you. You can share more detail via contact me page and I will get back to you.

      ReplyCancel
  2. abraham
    October 23, 2018 at 5:59 pm

    with avg_tbl as(
    select avg(runs) a, playername from tbl_players group by playername
    ),
    cte as (
    select playername, runs, matchyear, row_number() over (partition by matchyear order by playername) r from tbl_players
    ),
    cte2 as (
    select tb.playername, tb.runs ru1, tb.matchyear,p,ru ru2,av.a a1,av2.a a2, tb.runs-av.a a , ru-av2.a b,
    (tb.runs-av.a)*( ru-av2.a) axb,(tb.runs-av.a)*(tb.runs-av.a) asquared, ( ru-av2.a)*( ru-av2.a) bsquared from cte as tb
    cross apply (select playername p, runs ru, r from cte
    where matchyear=tb.matchyear and playername tb.playername and r>tb.r ) as v

    join avg_tbl av on av.playername= tb.playername
    join avg_tbl av2 on av2.playername= p
    ) select sum(axb),sum(asquared), sum(bsquared), sum(axb)/(sqrt(sum(asquared))*sqrt(sum(bsquared))) from cte2 group by playername,p

    ReplyCancel

Leave a Reply to dinesh danny Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

July 2, 2018 3 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....