0

I have 4 different tables:

  1. Contest
  2. Object
  3. Contest_Obj
  4. Winning

CONTEST has many objects (through contest_obj) AND winnings. A contest contains start and end dates.

OBJECT has many contests (through contest_obj)

CONTEST_OBJ contains object_id, contest_id, votes (most votes after end date of contest is a winner)

WINNING has contest_id, amount, and amount_type (dollars and euros)


I would like to be able to access this data in as few SQL calls as possible. More specifically, I am not currently storing the winner of a contest, so I have been calculating this on the fly.

I was hoping to get some help writing the SQL for:

  1. total_winning_dollars (return total dollars won in past contests)
  2. total_winning_euros (return total dollars won in past contests)
  3. all_won_contests (return all contests that object is the winner along with winnings for that contest as both dollars and euros)

Code I have to determine winner of a contest:

SELECT "objects".* 
     FROM "objects" 
     INNER JOIN "contest_objs" ON "objects"."id" = "contest_objs"."object_id" 
     WHERE "contest_objs"."contest_id" = ? 
     ORDER BY votes DESC 
     LIMIT 1

sample table data:

CREATE TABLE `contests` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`start_date` DATETIME,
`end_date`  DATETIME,
`user_id`  INT,
`title` STRING
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `objects` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` STRING
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `winnings` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`contest_id` INT NOT NULL,
`comment` TEXT,
`amount_type` STRING NOT NULL,
`amount` INT NOT NULL,
`user_id`  DATETIME 
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `contest_objs` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`contest_id` INT NOT NULL,
`object_id` INT NOT NULL,
`votes` INT 
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO `objects` VALUES
(1, "Foo"),
(2, "Bar")

INSERT INTO `contests` VALUES
(1, "2012-09-16 23:30:16.220991", "2012-09-17 23:30:16.220991", 1, "Contest X"),
(2, "2012-09-17 23:30:16.220991", "2016-09-17 23:30:16.220991", 2, "Contest Y")

INSERT INTO `winnings` VALUES
(1, 1, "Giving $5", 5, "dollars", 1),
(2, 1, "Giving 2 euros", 2, "euros", 1),
(3, 1, "Giving 4 euros", 4, "euros", 2),
(4, 2, "Giving 2 euros to different contest", 2, "euros", 1)

INSERT INTO `contest_objs` VALUES
(1, 1, 1, 10),
(2, 1, 2, 12),
(3, 2, 2, 0),
(4, 2, 2, 0)

In the above example:

Contest X (over) has 2 objects in it (foo and bar). Bar is the winner of Contest X with 12 votes. Bar has won $5 dollars and 6 euros total from this contest.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Marc
  • 1,033
  • 2
  • 10
  • 28
  • i have SQL to determine the winner of a contest. not sure how to include this into another statement to figure out the above. (updated above) – Marc Sep 11 '12 at 00:02
  • can you state their relationship with each other? do you want the total winning for the previous constest only right? – John Woo Sep 11 '12 at 00:23
  • can you also provide some records with desired result? – John Woo Sep 11 '12 at 00:26
  • Can you share your CREATE TABLE statements and some sample data in form of INSERT statements like in [this question](http://stackoverflow.com/questions/12358842/how-to-get-distinct-rows-with-max-value)? – Iain Samuel McLean Elder Sep 11 '12 at 00:36
  • sorry for the delay! i added some data by hand hopefully in an understandable format. let me know if you have any questions – Marc Sep 17 '12 at 23:57
  • Is this MySQL or PostgreSQL? I am pretty sure those create table statements will run on MySQL but not PostgreSQL. – Chris Travers Apr 07 '13 at 13:30

0 Answers0