Fox Pro Error on getting values using MAX(date) - php

I'm in a bind with a deadline and I cannot seem to figure this out.
I am trying to query the table to get the values for the corresponding max shipdate. My query is below. This is for Fox Pro using an ODBC driver.
SELECT
so1.sono,
so1.custno,
so1.item,
so1.shipdate as last_shipdate,
so1.price as last_price
FROM sotran01 so1
INNER JOIN (
SELECT
custno,
item,
MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item
) so2 ON (so1.custno = so2.custno AND so1.item = so2.item AND so1.shipdate = so2.last_shipdate)
WHERE so1.item IN (
SELECT item
FROM arpric01
)
ORDER BY so1.custno, so1.item, so1.shipdate
This is what I get (using ADOdb):
ADODB_vfp Object
(
[databaseType] => vfp
[fmtDate] => {^Y-m-d}
[fmtTimeStamp] => {^Y-m-d, h:i:sA}
[replaceQuote] => '+chr(39)+'
[true] => .T.
[false] => .F.
[hasTop] => top
[_bindInputArray] =>
[sysTimeStamp] => datetime()
[sysDate] => date()
[ansiOuter] => 1
[hasTransactions] =>
[curmode] =>
[dataProvider] => odbc
[hasAffectedRows] => 1
[binmode] => 1
[useFetchArray] =>
[_genSeqSQL] => create table %s (id integer)
[_autocommit] => 1
[_haserrorfunctions] => 1
[_has_stupid_odbc_fetch_api_change] => 1
[_lastAffectedRows] => 0
[uCaseTables] => 1
[_dropSeqSQL] => drop table %s
[database] =>
[host] => DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=dbf;SOURCEDB=C:\Sites\hub.fieldfresh.dev\_cache\VP10\PRAXIS\;EXCLUSIVE=NO;
[user] =>
[password] =>
[debug] =>
[maxblobsize] => 262144
[concat_operator] => +
[substr] => substr
[length] => length
[random] => rand()
[upperCase] => upper
[nameQuote] => "
[charSet] =>
[metaDatabasesSQL] =>
[metaTablesSQL] =>
[uniqueOrderBy] =>
[emptyDate] =>
[emptyTimeStamp] =>
[lastInsID] =>
[hasInsertID] =>
[hasLimit] =>
[readOnly] =>
[hasMoveFirst] =>
[hasGenID] =>
[genID] => 0
[raiseErrorFn] =>
[isoDates] =>
[cacheSecs] => 3600
[memCache] =>
[memCacheHost] =>
[memCachePort] => 11211
[memCacheCompress] =>
[sysUTimeStamp] =>
[arrayClass] => ADORecordSet_array
[noNullStrings] =>
[numCacheHits] => 0
[numCacheMisses] => 0
[pageExecuteCountRows] => 1
[uniqueSort] =>
[leftOuter] =>
[rightOuter] =>
[autoRollback] =>
[poorAffectedRows] =>
[fnExecute] =>
[fnCacheExecute] =>
[blobEncodeType] =>
[rsPrefix] => ADORecordSet_
[autoCommit] => 1
[transOff] => 0
[transCnt] => 0
[fetchMode] => 2
[null2null] => null
[bulkBind] =>
[_oldRaiseFn] =>
[_transOK] =>
[_connectionID] => Resource id #8
[_errorMsg] => [Microsoft][ODBC Visual FoxPro Driver]Syntax error.
[_errorCode] => 37000
[_queryID] =>
[_isPersistentConnection] =>
[_evalAll] =>
[_affected] =>
[_logsql] =>
[_transmode] =>
[_error] =>
)
The error doesn't say much. I can copy and paste the code into MySQL and it runs fine and returns what I expect. Hopefully another set of eyes, with more Fox Pro experience, can see what the issue is here.
Thanks for any assistance.

Your subquery is incorrect. This is the subquery:
INNER JOIN (
SELECT custno, item, MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item, last_shipdate
-------------------------------^
)
That is an aggregation column. Remove it:
INNER JOIN (
SELECT custno, item, MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item
)

Related

php array_diff not working on this compare

I have two arrays:
(
[lineCode] => C
[serviceRequest] => Ext, Warr auth.#19091100102 for $650.64 to be paid by c/card(fax#817-785-6700). Cust owes balance
[skillCode] => 90SB
[opCode] => 90SB
[jobType] => CUSTOMER
[techNo] =>
[lineStatus] => C
)
(
[id] => 755350
[rid] => 252178
[lineCode] => C
[serviceRequest] => Ext, Warr auth.#19091100102 for $650.64 to be paid by c/card(fax#817-785-6700). Cust owes balance
[skillCode] => 90SB
[opCode] => 90SB
[jobType] => CUSTOMER
[techNo] =>
[lineStatus] => W
[timeA] => 1575497139
[timeC] => 0
)
When i perform $diff = array_diff($arry1, $arry2);, it does not find the lineStatus to be different. Could it be because of service Request line with special characters? Although as a test, I set both lines to blanks, and it still did not see the difference.
Any help would be great. Stumped for the day.
UPDATE w/ MORE INFO
As this is part of a larger loop through mulitple arrays, the diff check before this one above is below:
$arry1= Array
(
[lineCode] => B
[serviceRequest] =>
[skillCode] => 15
[opCode] => 15
[jobType] => CUSTOMER
[techNo] => A05
[lineStatus] => C
)
$arry2= Array
(
[id] => 755362
[rid] => 252184
[lineCode] => B
[serviceRequest] =>
[skillCode] => 15
[opCode] => 15
[jobType] => CUSTOMER
[techNo] => A05
[lineStatus] => W
[timeA] => 1575504138
[timeC] => 0
)
$diff= Array
(
[lineStatus] => C
)
the code for the diff is $diff=array_diff($arry1,$arry2);
If it works correctly for this one, why would it not for the next.
Why does it not find it the way you originally did it...?
array_diff() does not care about key-value combinations, it only compares the values in the different arrays that are compared.
To better understand what's happening: if in your updated data you introduce an array element [dummy] => "C" into $arry2 you will no longer get [lineStatus] => "C" returned into $diff. As the value C is now found in both $arry1 and $arry2.
Have a look at this demo

MySQL join if address value not equal default

Data:
[ID] => 4
[Create_date] => 2017-10-07
[Client] => 17
[Address] => default
[GST] => Regular
[Rate_group] => D
[Sales_man] => 0
[CGST] => 91.12
[SGST] => 91.12
[IGST] => 0
[Discount] => 10
[Total] => 1397.25
[LR_number] => 0
[LR_date] => 0000-00-00
[Transport] => 5
[Transport_branch] => default
[Remark] => Hi, my name is Rushabh Shah.
[Payment_mode] => Cash
[Status] => Open
[Customer_name] => Rushabh Infotech
[Transport_name] => Shah Infotech
MYSQL Query:
SELECT `sale`.*,
`customer`.`english_company_name` AS `Customer_name`,
`transport`.`english_company_name` AS `Transport_name`
FROM `sale`
LEFT JOIN `customer`
ON `customer`.`id` = `sale`.`client`
LEFT JOIN `transport`
ON `transport`.`id` = `sale`.`transport`
MySQL join with Transport_branch if Transport_branch != 'default'. Can anyone please tell me how can I achieve this.
You could use a case when
SELECT `sale`.*,
`customer`.`english_company_name` AS `Customer_name`,
`transport`.`english_company_name` AS `Transport_name` ,
case when `transport`.`transport_branch` = 'default'
THEN `transport`.`english_company_name`
ELSE `transport_brach`.`transport_btanch__company_name`
end AS my_transport_name
FROM `sale`
LEFT JOIN `customer`
ON `customer`.`id` = `sale`.`client`
LEFT JOIN `transport`
ON `transport`.`id` = `sale`.`transport`
LEFT JOIN `transport_branch`
ON `transport_branch`.`id` = `transport`.`id`

PHP - select value by key (date), then return values sum

I may have stupid question but I do not know how to solve my problem.
What I want to achieve.
I need to divide the field by date (key) (specifically, I'm interested in the year) and figure out the total amount (value) for each year.
my array:
Array
(
[2017-04-20 15:26:08] => 0.00
[2016-10-11 13:52:05] => 0.00
[2016-05-26 13:04:14] => 200000.00
[2016-05-24 14:32:34] => 0.00
[2016-05-24 11:32:17] => 0.00
[2016-03-29 19:02:24] => 79800.00
[2016-02-16 07:54:38] => 0.00
[2015-12-16 19:01:55] => 149900.00
[2015-01-30 15:07:38] => 402103.00
[2014-11-03 15:13:29] => 0.00
[2014-10-15 15:58:44] => 129000.00
[2014-10-06 12:44:52] => 0.00
[2014-07-28 10:47:45] => 0.00
[2014-07-23 15:50:24] => 133333.33
[2014-05-16 12:39:25] => 0.00
[2014-04-29 13:21:01] => 5045524.00
[2014-03-04 15:28:03] => 0.00
[2014-01-13 09:41:47] => 0.00
[2013-11-26 08:40:20] => 70000.00
[2013-07-22 12:17:24] => 0.00
[2012-10-05 13:51:06] => 0.00
[2012-03-08 15:45:44] => 149880.00
[2012-03-02 13:11:19] => 0.00
[2012-02-14 09:26:43] => 0.00
[2011-12-05 10:44:23] => 0.00
[2011-12-05 10:33:53] => 1500000.00
[2011-07-01 14:40:22] => 0.00
[2011-05-30 09:54:22] => 1680000.00
[2011-01-13 08:59:14] => 72000.00
)
my code:
$min_year = date('Y',strtotime(min(array_column($MY_ARRAY,'published_at'))));
$max_year = date('Y',strtotime(max(array_column($MY_ARRAY,'published_at'))));
for($year = $max_year; $year >= $min_year; $year--){
$amount_array = array_column($MY_ARRAY,'contract_amount','published_at');
echo 'Year: ' .$year . ', Total amount for this year: ' . ???? . ' €';
}
how should I do it?
edit// part of original array
Array
(
[0] => stdClass Object
(
[id] => 2898917
[contract_identifier] => 155/AD00/2017
[contracting_authority_name] => some company
[contracting_authority_formatted_address] => address
[contracting_authority_cin] => 123456789
[supplier_name] => another company
[supplier_formatted_address] =>
[supplier_cin] => n123456789mber
[subject] => contract title
[subject_description] =>
[signed_on] => 2017-04-19
[effective_note] =>
[contract_price_amount] => 0.00
[contract_price_total_amount] => 0.00
[note] =>
[published_at] => 2017-04-20 15:26:08
[changed_at] => 2017-04-20 15:10:09
[change_note] =>
[internal_id] => 155
[internal_note] =>
[confirmation_file_name] =>
[confirmed_on] =>
[source_id] => 2
[description] =>
[reference] =>
[effective_from] => 2017-04-21
[effective_to] =>
[crz_department_name] => department
[crz_status_name] => 1
[crz_type_name] => 1
[crz_kind_name] => 2
[crz_confirmation_status_name] => 5
[crz_attachments_title] => 11111
[crz_attachments_file_name] =>
[crz_attachments_file_size] =>
[crz_attachments_scan_file_name] => 2898918_dokument.pdf
[crz_attachments_scan_file_size] => 373463
)
[1] => stdClass Object
(
[id] => 2635819
[contract_identifier] => 241
[contracting_authority_name] => ...
...
...
I would do something like this:
Loop through the whole stuff.
Create another array.
Get the date part and parse the year.
Cumulatively add the years as the key and value as the sum.
Display.
Code:
<?php
$arr = array(
"2017-04-20 15:26:08" => 0.00,
"2016-10-11 13:52:05" => 0.00,
"2016-05-26 13:04:14" => 200000.00,
"2016-05-24 14:32:34" => 0.00,
"2016-05-24 11:32:17" => 0.00,
"2016-03-29 19:02:24" => 79800.00,
"2016-02-16 07:54:38" => 0.00,
"2015-12-16 19:01:55" => 149900.00,
"2015-01-30 15:07:38" => 402103.00,
"2014-11-03 15:13:29" => 0.00,
"2014-10-15 15:58:44" => 129000.00,
"2014-10-06 12:44:52" => 0.00,
"2014-07-28 10:47:45" => 0.00,
"2014-07-23 15:50:24" => 133333.33,
"2014-05-16 12:39:25" => 0.00,
"2014-04-29 13:21:01" => 5045524.00,
"2014-03-04 15:28:03" => 0.00,
"2014-01-13 09:41:47" => 0.00,
"2013-11-26 08:40:20" => 70000.00,
"2013-07-22 12:17:24" => 0.00,
"2012-10-05 13:51:06" => 0.00,
"2012-03-08 15:45:44" => 149880.00,
"2012-03-02 13:11:19" => 0.00,
"2012-02-14 09:26:43" => 0.00,
"2011-12-05 10:44:23" => 0.00,
"2011-12-05 10:33:53" => 1500000.00,
"2011-07-01 14:40:22" => 0.00,
"2011-05-30 09:54:22" => 1680000.00,
"2011-01-13 08:59:14" => 72000.00
);
$sum = array();
foreach ($arr as $year => $value) {
$year = date('Y', strtotime($year));
if (!isset($sum[$year]))
$sum[$year] = 0.0;
$sum[$year] += $value;
}
?>
Output
array(7) {
[2017]=>
float(0)
[2016]=>
float(279800)
[2015]=>
float(552003)
[2014]=>
float(5307857.33)
[2013]=>
float(70000)
[2012]=>
float(149880)
[2011]=>
float(3252000)
}
Would be way more easier to do something like:
$sum = array();
foreach ($MY_ARRAY as $date => $value) {
$year = substr($date, 0, 4);
$sum[$year] = (isset($sum[$year]) ? $sum[$year] : 0) + $value;
}
print_r($sum);

Post array incomplete when receiving a post

I'm on a Drupal7 site, and I'm not used to Drupal. When I edit a node (standard page), and try to save it, the menu disappears. Not all node's are like this, only the ones that uses a field group of heatmaps, probably a custom field group (legacy).
System specs are:
CentOS 6.6
Apache 2.2
Mysql 5.5
Php 7
At first, I thought it was a bug in Drupal 7, and I tried solutions as Menu items disappearing in Drupal 7. But the suggested solutions didn't work. So I started to suspect post_max_size or memory_limit, because the form with the custom field grows very large when it uses the field or the field group. So I've maxed the memory settings and it looks good but is still not working.
The field group array tends to be very large and I tried to find some info about nestling level too big in a post but found no hints.
The post size is:
post_max_size in bytes = 536870912
post CONTENT_LENGTH = 1020347
The field group contains Heatmaps with Geolocations and endless of data:
[field_heatmap_data] => Array
(
[und] => Array
(
[0] => Array
(
[tablefield] => Array
(
[cell_0_0] => X
[cell_0_1] => Y
[cell_0_2] => Plastic
[cell_0_3] => Paper
[cell_0_4] => Glass
[cell_0_5] => Metal
[cell_0_6] => Organiskt
[cell_0_7] =>
[cell_0_8] =>
[cell_0_9] => Other
[cell_1_0] => 14.1741233638
[cell_1_1] => 57.7797089972
[cell_1_2] => 0
[cell_1_3] => 0
[cell_1_4] =>
[cell_1_5] =>
[cell_1_6] =>
[cell_1_7] => 1
[cell_1_8] =>
[cell_1_9] => 2
[cell_2_0] => 14.1784435935
[cell_2_1] => 57.7797106709
[cell_2_2] => 0
[cell_2_3] => 0
[cell_2_4] =>
[cell_2_5] =>
[cell_2_6] =>
[cell_2_7] =>
[cell_2_8] =>
[cell_2_9] =>
[cell_3_0] => 14.1656472109
[cell_3_1] => 57.7831198751
[cell_3_2] => 1
[cell_3_3] => 2
[cell_3_4] => 1
[cell_3_5] => 1
[cell_3_6] =>
[cell_3_7] =>
[cell_3_8] =>
[cell_3_9] =>
[cell_4_0] => 14.1753179083
[cell_4_1] => 57.7826699822
[cell_4_2] => 0
[cell_4_3] => 5
[cell_4_4] =>
[cell_4_5] => 3
[cell_4_6] =>
[cell_4_7] => 9
[cell_4_8] => 4
[cell_4_9] =>
[cell_5_0] => 14.1602465906
[cell_5_1] => 57.7824661754
[cell_5_2] => 2
[cell_5_3] => 0
[cell_5_4] => 1
[cell_5_5] =>
[cell_5_6] =>
[cell_5_7] => 4
[cell_5_8] =>
[cell_5_9] => 1
[cell_6_0] => 14.1552312791
[cell_6_1] => 57.7788985858
[cell_6_2] => 0
[cell_6_3] => 1
[cell_6_4] =>
[cell_6_5] => 1
[cell_6_6] =>
[cell_6_7] => 4
[cell_6_8] =>
[cell_6_9] =>
[cell_7_0] => 14.1631063952
[cell_7_1] => 57.7813178687
[cell_7_2] => 1
[cell_7_3] => 0
[cell_7_4] =>
[cell_7_5] =>
[cell_7_6] =>
[cell_7_7] => 2
[cell_7_8] => 3
[cell_7_9] =>
[cell_8_0] => 14.1742044644
[cell_8_1] => 57.7827544419
[cell_8_2] => 0
[cell_8_3] => 0
[cell_8_4] =>
[cell_8_5] =>
[cell_8_6] =>
[cell_8_7] => 4
[cell_8_8] => 1
[cell_8_9] =>
[cell_9_0] => 14.157952438
[cell_9_1] => 57.7818974962
[cell_9_2] => 2
[cell_9_3] => 4
[cell_9_4] => 5
[cell_9_5] => 1
[cell_9_6] =>
[cell_9_7] => 8
[cell_9_8] => 2
[cell_9_9] =>
[cell_10_0] => 14.1706946744
[cell_10_1] => 57.7815507326
[cell_10_2] => 0
[cell_10_3] => 0
[cell_10_4] =>
And so on....
So I've figured out that there is a flaw in the architecture of the node because it cant clearly not handle that much data in a field group and should been handled as an separate node, but since this is a legacy project I don't want to mess things up.
If I var_dump the $_POST variable on different pages when editing, I can clearly see that the $_POST variable stops after the $_POST['field_heatmap'] element where there is data, while the pages that doesn't contain data in that field group the $_POST array continues after the $_POST['field_heatmap'] element.
So my question is, should I continue to try to find a bug in Drupal or should I investigate further some php settings (Or maybe apache). I've tried debugging with cachegrind but can't find any unusual. Or any hints are greatly appreciated!
Finally! The max_input_vars was set to 1000
Changed it to max_input_vars = 10000 and it worked!

What is wrong with my SQL statement? How do I use JOIN properly?

so I'm working on a tool for fantasy baseball. I constructed this SQL statement that I thought worked initially but turned out to not do so after I added more rows.
Here is the statement:
SELECT
p.player_id, position, last_name, first_name, player_type, team, s.season_gp, s.season_runs, s.season_hits, s.season_doubles, s.season_triples, s.season_home_runs, s.season_runs_batted_in, s.season_base_on_balls, s.season_strikeouts, s.season_stolen_bases, w.week_gp, w.week_runs, w.week_hits, w.week_doubles, w.week_triples, w.week_home_runs, w.week_runs_batted_in, w.week_base_on_balls, w.week_strikeouts, w.week_stolen_bases
FROM
player AS p
LEFT JOIN
(SELECT
player_id, COUNT(date) AS week_gp, SUM(runs) AS week_runs, SUM(hits) AS week_hits, SUM(doubles) AS week_doubles, SUM(triples) AS week_triples, SUM(home_runs) AS week_home_runs, SUM(runs_batted_in) AS week_runs_batted_in, SUM(base_on_balls) AS week_base_on_balls, SUM(strikeouts) AS week_strikeouts, SUM(stolen_bases) AS week_stolen_bases
FROM
game_mlb
WHERE
date >= CURDATE() - INTERVAL 1 WEEK) w
ON p.player_id = w.player_id
LEFT JOIN
(SELECT
player_id, COUNT(date) AS season_gp, SUM(runs) AS season_runs, SUM(hits) AS season_hits, SUM(doubles) AS season_doubles, SUM(triples) AS season_triples, SUM(home_runs) AS season_home_runs, SUM(runs_batted_in) AS season_runs_batted_in, SUM(base_on_balls) AS season_base_on_balls, SUM(strikeouts) AS season_strikeouts, SUM(stolen_bases) AS season_stolen_bases
FROM
game_mlb
WHERE
date >= YEAR(CURDATE())) s
ON p.player_id = s.player_id
WHERE
team_id = 1
Here are the PHP arrays for the tables being used.
game_mlb:
$game_mlb = array(
array('game_id' => '9','player_id' => '1','date' => '2013-03-31','at_bats' => '4','runs' => '0','hits' => '0','doubles' => '0','triples' => '0','home_runs' => '0','runs_batted_in' => '0','stolen_bases' => '0','base_on_balls' => '0','strikeouts' => '0','innings_pitched' => NULL,'wins' => NULL,'complete_games' => NULL,'shutouts' => NULL,'saves' => NULL,'earned_runs' => NULL,'strikeouts_pitched' => NULL,'holds' => NULL),
array('game_id' => '10','player_id' => '2','date' => '2013-04-01','at_bats' => '4','runs' => '0','hits' => '0','doubles' => '0','triples' => '0','home_runs' => '0','runs_batted_in' => '0','stolen_bases' => '0','base_on_balls' => '1','strikeouts' => '1','innings_pitched' => NULL,'wins' => NULL,'complete_games' => NULL,'shutouts' => NULL,'saves' => NULL,'earned_runs' => NULL,'strikeouts_pitched' => NULL,'holds' => NULL)
);
player:
$player = array(
array('player_id' => '1','player_league' => 'MLB','first_name' => 'Adrian','last_name' => 'Beltre','player_type' => 'MLB_BATTER','position' => '3B','team' => 'TEX','yahoo_id' => '6039','minors_type' => 'MLB_BR','minors_id' => 'beltre001adr','injury' => NULL,'team_id' => '1'),
array('player_id' => '2','player_league' => 'MLB','first_name' => 'Albert','last_name' => 'Pujols','player_type' => 'MLB_BATTER','position' => '1B','team' => 'LAA','yahoo_id' => '6619','minors_type' => 'MLB_BR','minors_id' => 'pujols001jos','injury' => NULL,'team_id' => '1')
);
And here is the result I'm getting:
$player = array(
array('player_id' => '1','position' => '3B','last_name' => 'Beltre','first_name' => 'Adrian','player_type' => 'MLB_BATTER','team' => 'TEX','season_gp' => '2','season_runs' => '0','season_hits' => '0','season_doubles' => '0','season_triples' => '0','season_home_runs' => '0','season_runs_batted_in' => '0','season_base_on_balls' => '1','season_strikeouts' => '1','season_stolen_bases' => '0','week_gp' => '2','week_runs' => '0','week_hits' => '0','week_doubles' => '0','week_triples' => '0','week_home_runs' => '0','week_runs_batted_in' => '0','week_base_on_balls' => '1','week_strikeouts' => '1','week_stolen_bases' => '0'),
array('player_id' => '2','position' => '1B','last_name' => 'Pujols','first_name' => 'Albert','player_type' => 'MLB_BATTER','team' => 'LAA','season_gp' => NULL,'season_runs' => NULL,'season_hits' => NULL,'season_doubles' => NULL,'season_triples' => NULL,'season_home_runs' => NULL,'season_runs_batted_in' => NULL,'season_base_on_balls' => NULL,'season_strikeouts' => NULL,'season_stolen_bases' => NULL,'week_gp' => NULL,'week_runs' => NULL,'week_hits' => NULL,'week_doubles' => NULL,'week_triples' => NULL,'week_home_runs' => NULL,'week_runs_batted_in' => NULL,'week_base_on_balls' => NULL,'week_strikeouts' => NULL,'week_stolen_bases' => NULL)
);
This is incorrect. Beltre and Pujols should each have 1 game played with their respective stats, but Beltre is getting credited with everything.
Any help would be appreciated, I've been stuck on this for a while now.
You need group by statements in your subquery:
SELECT p.player_id, position, last_name, first_name, player_type, team,
s.season_gp, s.season_runs, s.season_hits, s.season_doubles, s.season_triples, s.season_home_runs, s.season_runs_batted_in, s.season_base_on_balls, s.season_strikeouts, s.season_stolen_bases, w.week_gp, w.week_runs, w.week_hits, w.week_doubles, w.week_triples, w.week_home_runs, w.week_runs_batted_in, w.week_base_on_balls, w.week_strikeouts, w.week_stolen_bases
FROM player p LEFT JOIN
(SELECT player_id, COUNT(date) AS week_gp, SUM(runs) AS week_runs, SUM(hits) AS week_hits, SUM(doubles) AS week_doubles, SUM(triples) AS week_triples, SUM(home_runs) AS week_home_runs, SUM(runs_batted_in) AS week_runs_batted_in, SUM(base_on_balls) AS week_base_on_balls, SUM(strikeouts) AS week_strikeouts, SUM(stolen_bases) AS week_stolen_bases
FROM game_mlb
WHERE date >= CURDATE() - INTERVAL 1 WEEK
group by player_id
) w
ON p.player_id = w.player_id LEFT JOIN
(SELECT player_id, COUNT(date) AS season_gp, SUM(runs) AS season_runs, SUM(hits) AS season_hits, SUM(doubles) AS season_doubles, SUM(triples) AS season_triples, SUM(home_runs) AS season_home_runs, SUM(runs_batted_in) AS season_runs_batted_in, SUM(base_on_balls) AS season_base_on_balls, SUM(strikeouts) AS season_strikeouts, SUM(stolen_bases) AS season_stolen_bases
FROM game_mlb
WHERE date >= YEAR(CURDATE())
group by player_id
) s
ON p.player_id = s.player_id
WHERE team_id = 1;
In most versions of SQL, your code would have returned an error. However, MySQL allows columns in the select list that are not in the group by -- and this has caused your problem.

Categories