Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert Excel formula to SQL


Convert Excel formula to SQL

Author
Message
davidmckee
davidmckee
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 22
I connect to the server/table using phpMyAdmin I believe it is a MySQL database

The menu gives me the following options...

Browse
Structure
SQL
Search
Insert
Export
Import
Operations
Triggers

Thanks in advance if you have time later

Cheers,
David
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44968 Visits: 39863
davidmckee (8/19/2014)
I connect to the server/table using phpMyAdmin I believe it is a MySQL database


Wellllll.... crud. I came up with something fun but now I don't know if it'll work for you. I'll still post it anyway...

Here's the test data if anyone wants to "play"...


SELECT HomeTeam, HomeScore, AwayTeam, AwayScore
INTO #Games
FROM (
SELECT 'B',2,'D',0 UNION ALL
SELECT 'G',17,'D',71 UNION ALL
SELECT 'I',25,'F',46 UNION ALL
SELECT 'E',26,'D',38 UNION ALL
SELECT 'I',26,'G',41 UNION ALL
SELECT 'H',26,'E',54 UNION ALL
SELECT 'J',33,'F',51 UNION ALL
SELECT 'E',34,'G',28 UNION ALL
SELECT 'J',34,'G',35 UNION ALL
SELECT 'E',35,'F',45 UNION ALL
SELECT 'E',37,'A',42 UNION ALL
SELECT 'C',39,'J',29 UNION ALL
SELECT 'B',41,'F',16 UNION ALL
SELECT 'H',42,'G',45 UNION ALL
SELECT 'D',43,'F',20 UNION ALL
SELECT 'A',44,'F',41 UNION ALL
SELECT 'E',46,'J',27 UNION ALL
SELECT 'I',47,'H',63 UNION ALL
SELECT 'C',50,'B',57 UNION ALL
SELECT 'C',50,'G',33 UNION ALL
SELECT 'A',51,'D',48 UNION ALL
SELECT 'B',52,'J',26 UNION ALL
SELECT 'B',57,'H',29 UNION ALL
SELECT 'A',58,'J',14 UNION ALL
SELECT 'A',61,'C',24 UNION ALL
SELECT 'C',61,'H',47 UNION ALL
SELECT 'C',62,'I',37 UNION ALL
SELECT 'B',66,'I',20 UNION ALL
SELECT 'A',75,'H',21
) d (HomeTeam, HomeScore, AwayTeam, AwayScore)
;




Notice that the test data does NOT have a Winner/Loser column, calculated or otherwise.

Here's the code I was getting ready to post and then saw the "MySQL" thingy... dunno if it'll still work for you or not.


WITH
cteWinLose AS
( --=== Calculates the wins/losses using a CROSS APPLY to "unpivot" the data.
-- Means we only need to make a single table scan and there are no JOINs.
SELECT ca.Team
,Wins = SUM(ca.Win)
,Losses = SUM(CASE WHEN ca.Win = 0 THEN 1 ELSE 0 END)
FROM #Games
CROSS APPLY (
SELECT HomeTeam, CASE WHEN Homescore >= AwayScore THEN 1 ELSE 0 END UNION ALL
SELECT AwayTeam, CASE WHEN AwayScore >= Homescore THEN 1 ELSE 0 END
) ca (Team,Win)
GROUP BY ca.Team
)
,cteCalcs AS
( --=== Once we know the wins/losses, we can calculate the win average and Games Behind
SELECT *
,PCT = CAST(Wins/(Wins+Losses+0.0) AS DECIMAL(6,3))
,GamesBehind = CAST(
( MAX(Wins) OVER (PARTITION BY (SELECT NULL)) - Wins
+ Losses - MIN(Losses) OVER (PARTITION BY (SELECT NULL))
) / 2.0
AS DECIMAL(6,3))
FROM cteWinLose
) --=== Last but not least, calculation the position of each team and display all
SELECT Place = DENSE_RANK() OVER (ORDER BY GamesBehind),*
FROM cteCalcs
ORDER BY Place, Team
;





Here's the output...


Place Team Wins Losses PCT GamesBehind
----- ---- ---- ------ ----- -----------
1 A 6 0 1.000 0.000
1 B 6 0 1.000 0.000
2 C 4 2 0.667 2.000
3 D 3 2 0.600 2.500
4 E 3 3 0.500 3.000
4 F 3 3 0.500 3.000
4 G 3 3 0.500 3.000
5 H 1 5 0.167 5.000
6 I 0 5 0.000 5.500
7 J 0 6 0.000 6.000

(10 row(s) affected)


--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
davidmckee
davidmckee
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 22
This is the exact result...except it seems to be in MSSql...the web server runs MySql

I did some research after copying the code into the query and receiving errors...maybe this is MS SQL and not MySql

Does anyone know how to convert this from MSSQL to MySql?

Again assuming this is the difference....


This was the error when I run it

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WITH
cteWinLose AS
( --=== Calculates the wins/losses using a CROSS APPLY to "' at line 1
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44968 Visits: 39863
davidmckee (8/19/2014)
This is the exact result...except it seems to be in MSSql...the web server runs MySql

I did some research after copying the code into the query and receiving errors...maybe this is MS SQL and not MySql

Does anyone know how to convert this from MSSQL to MySql?

Again assuming this is the difference....


This was the error when I run it

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WITH
cteWinLose AS
( --=== Calculates the wins/losses using a CROSS APPLY to "' at line 1


It would appear that you need to convert the CTEs (the WITHs) to FROMs as "derived tables".

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
mickyT
mickyT
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1253 Visits: 3309
Jeff Moden (8/19/2014)
davidmckee (8/19/2014)
This is the exact result...except it seems to be in MSSql...the web server runs MySql

I did some research after copying the code into the query and receiving errors...maybe this is MS SQL and not MySql

Does anyone know how to convert this from MSSQL to MySql?

Again assuming this is the difference....


This was the error when I run it

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WITH
cteWinLose AS
( --=== Calculates the wins/losses using a CROSS APPLY to "' at line 1


It would appear that you need to convert the CTEs (the WITHs) to FROMs as "derived tables".

I've had a bit of a play with this in SQLFiddle and found a couple of views worked well for me. Here's what I came up with, thanks to Jeff for the setup.
SQL Fiddle
davidmckee
davidmckee
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 22
This is very close but something else is slightly off...the third place team should be 2 games behind not 3

This was the SQLfiddle statement:

   (ABS((SELECT MAX(Wins ) FROM WinLossSum) - Wins )) +
   (ABS(Losses - (SELECT MIN(Losses) FROM WinLossSum))) / 2.0 AS GamesBehind

(6-4) + (2-0) / 2
2+2/2=2 not 3

The Result should be 2 so I am not sure how it is coming to 3?
davidmckee
davidmckee
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 22
Actually I got it!!!


I adjusted the SQLfiddle statement needed a couple extra brackets to complete the addition before dividing by 2:

This now works!!!
((ABS((SELECT MAX(Wins ) FROM WinLossSum) - Wins )) +
(ABS(Losses - (SELECT MIN(Losses) FROM WinLossSum)))) / 2.0 AS GamesBehind

(6-4) + (2-0) / 2
2+2/2=2

You all are awesome!! Thanks so much for this!
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44968 Visits: 39863
Our pleasure. Evven though it's MySQL, any chance of seeing the final code you ended up with including the views? Thanks.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
davidmckee
davidmckee
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 22
Here you go!

Final MySQL:

SELECT Team, Wins, Losses, CAST(IF(Wins + Losses = 0,0.0,Wins / CAST(Wins + Losses AS Decimal(6,3))) AS Decimal(6,3)) Percentage, ((ABS((SELECT MAX(Wins ) FROM WinLossSum) - Wins )) + (ABS(Losses - (SELECT MIN(Losses) FROM WinLossSum)))) / 2.0 AS GamesBehind
FROM WinLossSum
ORDER BY `Percentage` DESC,`Wins` DESC,`Losses` ASC

Results
TEAM......WINS......LOSSES......PERCENTAGE......GAMESBEHIND
..A............   6..............0................1......................0.........
..B............6..............0................1......................0.........
..C............   4..............2................0.667................2.........
..D............   3..............2................0.6...................2.5.......
..E............   3..............3................0.5...................3.........
..F............   3..............3................0.5...................3.........
..G............   3..............3................0.5...................3.........
..H............1..............5................0.167................5.........
..I.............0..............5................0......................5.5......
..J.............0..............6................0......................6........
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44968 Visits: 39863
davidmckee (8/20/2014)
Here you go!

Final MySQL:

SELECT Team, Wins, Losses, CAST(IF(Wins + Losses = 0,0.0,Wins / CAST(Wins + Losses AS Decimal(6,3))) AS Decimal(6,3)) Percentage, ((ABS((SELECT MAX(Wins ) FROM WinLossSum) - Wins )) + (ABS(Losses - (SELECT MIN(Losses) FROM WinLossSum)))) / 2.0 AS GamesBehind
FROM WinLossSum
ORDER BY `Percentage` DESC,`Wins` DESC,`Losses` ASC

Results
TEAM......WINS......LOSSES......PERCENTAGE......GAMESBEHIND
..A............   6..............0................1......................0.........
..B............6..............0................1......................0.........
..C............   4..............2................0.667................2.........
..D............   3..............2................0.6...................2.5.......
..E............   3..............3................0.5...................3.........
..F............   3..............3................0.5...................3.........
..G............   3..............3................0.5...................3.........
..H............1..............5................0.167................5.........
..I.............0..............5................0......................5.5......
..J.............0..............6................0......................6........


Thanks, David. What's the code for the WinLossSum view look like?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search