0

Hi everyone, i would like some help with a couple of mysql queries.
I have a table of data which looks like this :
grid1

And represents a transaction log for players of a game.

The ID is the primary key (auto incremented).
DATETIME is the date and time of the transaction.
USERNAME is useless. :D
PCNAME is a description of the physical computer in which the player was when the trasaction was made. MAX number of PCs is 30 so values can be PC1-PC30.
PLAYER is the name of the player.
CREDIT_A, B, C, D are game values, all Unsigned integers.
GAMEDAY is a value representing the virtual "in-game" DAY.

What i want to ask the Database is :
Given a list of player names (PLAYER column) , lets say (NICK, PETER), calculate the SUM of each credit type (CREDIT_A,CREDIT_B,CREDIT_C,CREDIT_D) for the last 3 game days (GAMEDAY) and show it seperately for each of those gamedays.
So the resulting table will look like :
grid2

Can i achive it somehow?

Thanks in advance!

2
Contributors
13
Replies
16
Views
4 Years
Discussion Span
Last Post by pritaeas
Featured Replies
  • 1

    Off the top of my head: SELECT player, SUM(credit_a) AS SumA, SUM(credit_b) AS SumB, SUM(credit_c) AS SumC, SUM(credit_d) AS SumD, gameday FROM TransactionLog WHERE player IN ('NICK', 'PETER') AND gameday IN ( SELECT gameday FROM TransactionLog ORDER BY gameday DESC LIMIT 3 ) GROUP BY player, gameday ORDER BY player, … Read More

  • 1

    Is this what you want? SELECT customer, SUM(trans_In) AS SumA, vardia, (SELECT pcnum FROM trans t2 WHERE t2.Customer=t1.Customer AND t2.vardia=t1.vardia ORDER BY date DESC LIMIT 1) AS pcnum FROM trans t1 WHERE customer IN ('Thanos', 'Kostas') AND vardia IN ( SELECT * FROM( SELECT DISTINCT vardia FROM trans ORDER BY … Read More

1

Off the top of my head:

SELECT 
    player, 
    SUM(credit_a) AS SumA, 
    SUM(credit_b) AS SumB, 
    SUM(credit_c) AS SumC, 
    SUM(credit_d) AS SumD, 
    gameday
FROM TransactionLog
WHERE player IN ('NICK', 'PETER')
AND gameday IN (
    SELECT gameday
    FROM TransactionLog 
    ORDER BY gameday DESC
    LIMIT 3
)
GROUP BY player, gameday
ORDER BY player, gameday DESC

If incorrect, am sure it will get you started.

0

Wooow that was fast!!!!

Though i got a weird error at once:
Error Code: 1235. This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

After a fast search i changed it to: (Don't ask me why i wouldn't know!)

SELECT 
    player, 
    SUM(credit_a) AS SumA, 
    SUM(credit_b) AS SumB, 
    SUM(credit_c) AS SumC, 
    SUM(credit_d) AS SumD, 
    gameday
FROM TransactionLog
WHERE player IN ('NICK', 'PETER')
AND gameday IN (
    SELECT * FROM(
        SELECT gameday
        FROM TransactionLog 
        ORDER BY gameday DESC LIMIT 3) 
    alias)
GROUP BY player, gameday
ORDER BY player, gameday DESC

This one worked BUT result set was only for the last gameday so i added the DISTINCT keyword and voila!

SELECT 
    player, 
    SUM(credit_a) AS SumA, 
    SUM(credit_b) AS SumB, 
    SUM(credit_c) AS SumC, 
    SUM(credit_d) AS SumD, 
    gameday
FROM TransactionLog
WHERE player IN ('NICK', 'PETER')
AND gameday IN (
    SELECT * FROM(
        SELECT DISTINCT gameday
        FROM TransactionLog 
        ORDER BY gameday DESC LIMIT 3) 
    alias)
GROUP BY player, gameday
ORDER BY player, gameday DESC

Thanks very much!
I admire the way database developers think. I couldn't figure it out although i know all of the functions you used here...
:)

I have another one quite simpler, but i will try it myself first and then maybe i'll come back with another question!
^_^

0

Ah yes, of course. I forgot about the DISTINCT. There are of course much more gameday's... I have no database here to do a quick test, that explains me forgetting the subselect table alias.

0

I would like a little more help regarding the above query.
:)

I want to add the PCNAME column in the SELECT statement, so as to be able to see in which PC was the player logged in the last time he played.

If i just add the PCNAME in the SELECT statement, it does not correspond to that.
On the contrary, i get a "kind of random" PCNAME in which the player was logged. By random i mean one of the PCs he was logged but not the last one, in respect to the GAMEDAY column.

Is there any way to achieve that?

Below is a query response in accordance to the data table i provided above:

data3

The marked in red cell is wrong. Although NICK was logged both in PC23 and PC16, it was PC16 the last one he did on gameday 5.

So my question is how does mysql pick the PCNAME and how can i change it.

I hope i made it clear enough, thanks in advance!

0

That's because of the group by most likely. You can try to do a left join between what you have and the original table.

0

All right i tried this one :

SELECT 
    t1.PCNAME,
    t1.player, 
    SUM(t1.credit_a) AS SumA, 
    SUM(t1.credit_b) AS SumB, 
    SUM(t1.credit_c) AS SumC, 
    SUM(t1.credit_d) AS SumD, 
    t1.gameday
FROM TransactionLog AS t1 
LEFT JOIN TransactionLog AS t2
    ON t1.PCNAME=t2.PCNAME
WHERE t1.player IN ('NICK', 'PETER')
AND t1.gameday IN (
    SELECT * FROM(
        SELECT DISTINCT gameday
        FROM TransactionLog 
        ORDER BY gameday DESC LIMIT 3) 
    alias)
GROUP BY t1.player, t1.gameday
ORDER BY t1.player, t1.gameday DESC

But it is completely wrong...
:S
The sums are wrong and the PCNAME is shown in the same way as before.

0

Tried:

LEFT JOIN TransactionLog AS t2
    ON t1.gameday=t2.gameday AND t2.player=t1.player

But no luck. Same results.

Sorry for being boring!
:S

0

Can you provide an sql script that creates the table and inserts the data from your original post? (I'm not going to type all that.)

0

Of course!
This is the TransactionLog table but the names are quite different.

pcname = PCNum
gameday = Vardia
player = Customer
for credit_a use trans_in and ommit the other SUMS just one is enough to check the validity

Attachments
CREATE DATABASE  IF NOT EXISTS `viptest` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `viptest`;
-- MySQL dump 10.13  Distrib 5.5.16, for Win32 (x86)
--
-- Host: www.bullos.com    Database: viptest
-- ------------------------------------------------------
-- Server version	5.1.58-1ubuntu1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `trans`
--

DROP TABLE IF EXISTS `trans`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `trans` (
  `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Date` datetime DEFAULT NULL,
  `User` varchar(45) DEFAULT NULL,
  `PCNum` varchar(45) DEFAULT NULL,
  `Customer` varchar(45) NOT NULL,
  `trans_In` int(11) DEFAULT NULL,
  `trans_Out` int(11) DEFAULT NULL,
  `trans_Gift` int(11) DEFAULT NULL,
  `trans_Card` int(11) DEFAULT NULL,
  `Vardia` int(11) DEFAULT NULL,
  `trans_petalouda` int(11) DEFAULT '0',
  `trans_mgift` int(11) DEFAULT '0',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `ID_UNIQUE` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1058 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `trans`
--

LOCK TABLES `trans` WRITE;
/*!40000 ALTER TABLE `trans` DISABLE KEYS */;
INSERT INTO `trans` VALUES (18,'2013-01-19 00:01:40','aptent','PC4','Alekos',2,19,16,7,2,1,3),(19,'2013-01-19 00:03:21','mi','PC8','Thanos',0,3,7,3,2,3,2),(20,'2013-01-19 00:05:02','netus','PC6','Thanos',19,20,8,11,2,0,6),(21,'2013-01-19 00:06:43','Lorem','PC4','Thanos',2,9,2,14,2,1,2),(22,'2013-01-19 00:08:24','velit','PC2','Vasilis',0,3,1,18,2,0,10),(23,'2013-01-19 00:10:05','metus','PC8','Dimitris',5,18,4,12,2,1,4),(24,'2013-01-19 00:11:46','ante','PC7','Vasilis',10,10,5,1,2,3,9),(25,'2013-01-19 00:13:27','nulla','PC9','Petros',7,4,19,18,2,1,7),(26,'2013-01-19 00:15:07','Mauris','PC7','Apostolos',8,10,10,2,2,2,8),(27,'2013-01-19 00:16:48','ut','PC10','Lakis',1,6,17,3,2,1,1),(28,'2013-01-19 00:18:29','ut','PC8','Markos',8,9,16,18,2,2,7),(29,'2013-01-19 00:20:10','Suspendisse','PC10','Kostas',20,3,0,3,2,2,9),(30,'2013-01-19 00:21:51','magna','PC2','Haris',12,9,2,14,2,1,10),(31,'2013-01-19 00:23:32','lorem','PC9','Vasilis',11,20,4,14,2,1,3),(32,'2013-01-19 00:25:13','auctor','PC7','Alekos',0,3,3,2,2,2,3),(33,'2013-01-19 00:26:54','congue','PC10','Markos',13,13,19,12,2,2,9),(34,'2013-01-19 00:28:35','facilisi','PC4','Haris',19,8,13,19,2,2,7),(35,'2013-01-19 00:30:15','cubilia','PC6','Giannis',7,13,6,4,2,0,6),(36,'2013-01-19 00:31:56','natoque','PC2','Lakis',18,20,10,15,2,2,4),(37,'2013-01-19 00:33:37','rutrum','PC8','Alekos',19,0,5,13,2,3,4),(38,'2013-01-19 00:35:18','amet','PC9','Markos',15,17,9,7,2,1,4),(39,'2013-01-19 00:36:59','feugiat','PC7','Petros',0,17,16,16,2,3,4),(40,'2013-01-19 00:38:40','sagittis','PC7','Kostas',15,5,18,7,2,0,7),(41,'2013-01-19 00:40:21','Sed','PC4','Kostas',15,9,19,16,2,3,9),(42,'2013-01-19 00:42:02','pede','PC5','Alekos',5,0,6,19,2,2,0),(43,'2013-01-19 00:43:43','condimentum','PC3','Kostas',2,5,3,18,2,0,2),(44,'2013-01-19 00:45:23','Fusce','PC6','Apostolos',2,2,4,16,2,2,6),(45,'2013-01-19 00:47:04','libero','PC3','Alekos',8,4,11,5,2,3,6),(46,'2013-01-19 00:48:45','mus','PC7','Petros',1,0,8,7,2,3,6),(47,'2013-01-19 00:50:26','purus','PC2','Apostolos',1,1,9,13,2,0,4),(48,'2013-01-19 00:52:07','convallis','PC11','Alekos',11,16,15,1,2,3,10),(49,'2013-01-19 00:53:48','aliquam','PC11','Giannis',17,9,6,6,2,1,9),(50,'2013-01-19 00:55:29','rhoncus','PC6','Petros',1,16,8,10,2,0,4),(51,'2013-01-19 00:57:10','in','PC2','Haris',14,16,12,3,2,1,1),(52,'2013-01-19 00:58:51','molestie','PC5','Giannis',3,3,12,6,2,0,0),(53,'2013-01-19 01:00:31','elementum','PC2','Giannis',20,11,17,18,2,3,2),(54,'2013-01-19 01:02:12','dolor','PC1','Lakis',10,10,17,7,2,1,2),(55,'2013-01-19 01:03:53','dictum','PC9','Kostas',19,15,17,4,2,1,9),(56,'2013-01-19 01:05:34','primis','PC2','Dimitris',11,3,7,1,2,0,4),(57,'2013-01-19 01:07:15','molestie','PC9','Petros',9,9,17,17,2,0,2),(58,'2013-01-19 01:08:56','enim','PC11','Demis',18,13,1,7,2,3,1),(59,'2013-01-19 01:10:37','montes','PC10','Petros',12,3,16,3,2,3,6),(60,'2013-01-19 01:12:18','metus','PC10','Petros',4,12,19,0,2,3,9),(61,'2013-01-19 01:13:59','Nulla','PC5','Giannis',20,12,9,8,2,2,0),(62,'2013-01-19 01:15:39','Fusce','PC7','Petros',13,4,2,14,2,2,1),(63,'2013-01-19 01:17:20','elit','PC9','Lakis',7,15,17,11,2,0,4),(64,'2013-01-19 01:19:01','neque','PC8','Alekos',15,17,6,15,2,1,10),(65,'2013-01-19 01:20:42','pellentesque','PC10','Vasilis',0,4,8,4,2,1,9),(66,'2013-01-19 01:22:23','mi','PC6','Giannis',10,3,11,15,2,0,6),(67,'2013-01-19 01:24:04','at','PC7','Takis',0,1,4,6,2,0,10),(68,'2013-01-19 01:25:45','quis','PC1','Kostas',9,9,9,5,3,1,4),(69,'2013-01-19 01:27:26','magna','PC3','Giannis',15,20,0,10,3,3,3),(70,'2013-01-19 01:29:07','velit','PC5','Kostas',1,11,9,14,3,1,6),(71,'2013-01-19 01:30:47','placerat','PC9','Kostas',19,9,20,10,3,0,2),(72,'2013-01-19 01:32:28','diam','PC10','Petros',10,16,0,15,3,1,2),(73,'2013-01-19 01:34:09','interdum','PC10','Lakis',10,19,12,14,3,2,7),(74,'2013-01-19 01:35:50','pellentesque','PC9','Apostolos',18,18,5,12,3,1,3),(75,'2013-01-19 01:37:31','Duis','PC10','Apostolos',1,4,1,10,3,1,10),(76,'2013-01-19 01:39:12','Mauris','PC8','Thanos',10,0,20,18,3,0,2),(77,'2013-01-19 01:40:53','nisi','PC2','Vasilis',17,20,5,19,3,0,0),(78,'2013-01-19 01:42:34','tincidunt','PC3','Petros',8,6,16,17,3,1,4),(79,'2013-01-19 01:44:15','morbi','PC5','Petros',12,17,6,17,3,3,1),(80,'2013-01-19 01:45:55','condimentum','PC10','Giannis',14,10,17,17,3,1,10),(81,'2013-01-19 01:47:36','nibh','PC2','Vasilis',11,8,19,2,3,1,2),(82,'2013-01-19 01:49:17','Vestibulum','PC11','Demis',1,6,17,1,3,1,7),(83,'2013-01-19 01:50:58','dolor','PC8','Haris',11,19,9,3,3,0,4),(84,'2013-01-19 01:52:39','eros','PC6','Vasilis',15,9,9,3,3,3,9),(85,'2013-01-19 01:54:20','natoque','PC5','Giannis',20,13,15,5,3,0,7),(86,'2013-01-19 01:56:01','Vivamus','PC3','Haris',20,17,11,11,3,2,0),(87,'2013-01-19 01:57:42','parturient','PC7','Vasilis',3,14,0,7,3,2,5),(88,'2013-01-19 01:59:23','torquent','PC3','Alekos',20,5,8,1,3,2,3),(89,'2013-01-19 02:01:03','Ut','PC6','Kostas',19,0,8,12,3,3,10),(90,'2013-01-19 02:02:44','metus','PC7','Markos',17,6,8,16,3,1,4),(91,'2013-01-19 02:04:25','ligula','PC9','Dimitris',12,13,17,6,3,1,5),(92,'2013-01-19 02:06:06','feugiat','PC4','Markos',16,13,19,6,3,3,10),(93,'2013-01-19 02:07:47','nonummy','PC10','Lakis',4,15,1,9,3,0,2),(94,'2013-01-19 02:09:28','hendrerit','PC11','Giannis',11,11,12,3,3,0,8),(95,'2013-01-19 02:11:09','dolor','PC2','Giannis',0,4,14,18,3,2,5),(96,'2013-01-19 02:12:50','habitant','PC7','Giannis',11,0,2,16,3,2,2),(97,'2013-01-19 02:14:31','scelerisque','PC2','Alekos',16,12,17,14,3,0,1),(98,'2013-01-19 02:16:11','Aenean','PC2','Vasilis',11,8,10,2,3,3,2),(99,'2013-01-19 02:17:52','ullamcorper','PC7','Kostas',5,1,14,12,3,2,3),(100,'2013-01-19 02:19:33','aliquet','PC9','Kostas',14,13,17,3,3,3,1),(101,'2013-01-19 02:21:14','nec','PC6','Thanos',2,20,12,18,3,0,7),(102,'2013-01-19 02:22:55','leo','PC9','Takis',18,20,15,9,3,0,10),(103,'2013-01-19 02:24:36','torquent','PC11','Thanos',15,4,11,9,3,2,3),(104,'2013-01-19 02:26:17','Aenean','PC8','Apostolos',12,9,15,16,3,0,0),(105,'2013-01-19 02:27:58','Curabitur','PC2','Takis',17,19,2,19,3,3,7),(106,'2013-01-19 02:29:39','lorem','PC1','Apostolos',15,0,8,15,3,0,10),(107,'2013-01-19 02:31:19','Aliquam','PC6','Vasilis',8,10,4,3,3,3,10),(108,'2013-01-19 02:33:00','parturient','PC1','Vasilis',7,15,5,7,3,0,7),(109,'2013-01-19 02:34:41','faucibus','PC5','Takis',12,7,9,0,3,1,2),(110,'2013-01-19 02:36:22','convallis','PC6','Giannis',9,17,19,1,3,0,8),(111,'2013-01-19 02:38:03','Fusce','PC10','Thanos',11,18,20,13,3,3,6),(112,'2013-01-19 02:39:44','libero','PC11','Petros',13,5,14,1,3,2,1),(113,'2013-01-19 02:41:25','orci','PC6','Petros',11,20,20,10,3,3,3),(114,'2013-01-19 02:43:06','Sed','PC2','Thanos',19,14,20,16,3,2,10),(115,'2013-01-19 02:44:47','faucibus','PC9','Petros',12,6,4,20,3,1,7),(116,'2013-01-19 02:46:27','morbi','PC6','Apostolos',16,16,3,12,3,2,9),(117,'2013-01-19 02:48:08','fames','PC11','Vasilis',13,11,16,10,3,3,10),(118,'2013-01-19 02:49:49','Class','PC6','Petros',17,11,17,11,3,0,8),(119,'2013-01-19 02:51:30','leo','PC9','Takis',14,17,3,5,4,1,3),(120,'2013-01-19 02:53:11','tempor','PC3','Demis',13,9,3,20,4,0,7),(121,'2013-01-19 02:54:52','diam','PC2','Haris',5,7,3,6,4,1,2),(122,'2013-01-19 02:56:33','mus','PC10','Alekos',9,14,9,19,4,0,5),(123,'2013-01-19 02:58:14','luctus','PC4','Vasilis',17,2,9,9,4,0,9),(124,'2013-01-19 02:59:55','Nam','PC4','Kostas',16,2,4,17,4,0,8),(125,'2013-01-19 03:01:35','ornare','PC2','Thanos',7,4,13,4,4,2,10),(126,'2013-01-19 03:03:16','tincidunt','PC2','Vasilis',5,15,11,10,4,1,2),(127,'2013-01-19 03:04:57','magnis','PC10','Lakis',6,17,6,9,4,1,9),(128,'2013-01-19 03:06:38','Quisque','PC11','Giannis',5,1,12,6,4,3,2),(129,'2013-01-19 03:08:19','natoque','PC5','Takis',7,7,12,19,4,3,5),(130,'2013-01-19 03:10:00','Cras','PC6','Demis',4,8,10,14,4,1,3),(131,'2013-01-19 03:11:41','magna','PC10','Lakis',1,20,13,16,4,2,7),(132,'2013-01-19 03:13:22','nunc','PC3','Takis',0,14,11,3,4,3,2),(133,'2013-01-19 03:15:03','scelerisque','PC5','Dimitris',2,0,7,9,4,2,3),(134,'2013-01-19 03:16:43','lorem','PC10','Demis',1,4,18,0,4,3,6),(135,'2013-01-19 03:18:24','tincidunt','PC7','Giannis',15,9,16,18,4,1,8),(136,'2013-01-19 03:20:05','malesuada','PC8','Lakis',18,1,0,17,4,3,6),(137,'2013-01-19 03:21:46','aliquam','PC7','Lakis',8,10,4,14,4,0,2),(138,'2013-01-19 03:23:27','arcu','PC4','Demis',19,8,18,3,4,0,8),(139,'2013-01-19 03:25:08','felis','PC7','Petros',20,10,8,9,4,0,6),(140,'2013-01-19 03:26:49','hendrerit','PC
1

Is this what you want?

SELECT 
    customer, 
    SUM(trans_In) AS SumA, 
    vardia,
    (SELECT pcnum 
     FROM trans t2 
     WHERE t2.Customer=t1.Customer 
     AND t2.vardia=t1.vardia 
     ORDER BY date DESC LIMIT 1) AS pcnum
FROM trans t1
WHERE customer IN ('Thanos', 'Kostas')
AND vardia IN (
    SELECT * FROM(
        SELECT DISTINCT vardia
        FROM trans 
        ORDER BY vardia DESC LIMIT 3) 
    alias)
GROUP BY customer, vardia
ORDER BY customer, vardia DESC
0

That's it!
I can't thank you enough pritaeas!

Not important now but, as far as i understand this could be done also with LEFT JOIN as you mentioned before right?
I' ll try it myself just for practice... I won't bother you anymore!!!
:)

Thanks again!

0

Yes, you could left join it to a sub-select. I didn't understand when I wrote it that player/game was connected to different users and dates, messing up the result.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.