Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Convert Excel formula to SQL Expand / Collapse
Author
Message
Posted Tuesday, August 19, 2014 6:12 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 9:24 AM
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
Post #1605255
Posted Tuesday, August 19, 2014 8:12 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 37,099, Visits: 31,649
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1605262
Posted Tuesday, August 19, 2014 8:53 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 9:24 AM
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
Post #1605267
Posted Tuesday, August 19, 2014 9:19 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 37,099, Visits: 31,649
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1605275
Posted Tuesday, August 19, 2014 10:49 PM This worked for the OP Answer marked as solution
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:02 AM
Points: 1,025, Visits: 3,072
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
Post #1605282
Posted Wednesday, August 20, 2014 7:39 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 9:24 AM
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?
Post #1605433
Posted Wednesday, August 20, 2014 7:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 9:24 AM
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!
Post #1605439
Posted Wednesday, August 20, 2014 8:45 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 37,099, Visits: 31,649
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1605471
Posted Wednesday, August 20, 2014 9:08 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 9:24 AM
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........
Post #1605486
Posted Wednesday, August 20, 2014 9:11 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 37,099, Visits: 31,649
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1605487
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse