I have something very odd happening and nothing seems to work. I have a SP in MySql that returns some results. When I run the SP in MySql workbench everything is correct. The query is quite long. But this is the LEFT JOIN is somehow creating the issues. I have other inner joins/left joins but they are fine.
SELECT DISTINCT Id.ReelTag
, Id.ECSPartNo
, WM.ShortDescription AS Description
, Id.ReelTagSerial
, group_concat(DISTINCT RA.UniqueID) AS UniqueID
, group_concat(DISTINCT coalesce(RA.OrdNo, Std.OrdNo)) AS OrdNo
, Id.Received
, IFNULL(Std.ReelQuantity,0) AS OriginalQuantity
, IFNULL(Id.Quantity,0) AS CurrentQuantity
, IFNULL(yest.Quantity,0) AS YesterdayQuantity
, IFNULL(cuts.Quantity,0) AS QuantityChanged
, cuts.OrdNo AS OrdNoChange
, IFNULL(CC.ShipQuantity,0) AS ShipQuantity
, CC.OrdNo AS OrdNo_Allocated
, IFNULL(Id.Quantity,0) - IFNULL(yest.Quantity,0) AS changeAOF_Yesterday
FROM InventoryDtl Id .....
LEFT JOIN (
SELECT
SourceReel
, SUM(CASE WHEN Action = 'Insert' THEN TotalQuantity
WHEN Action = 'Delete' THEN -TotalQuantity
ELSE 0 END) AS Quantity
, group_concat(DISTINCT CASE WHEN Action = 'Insert' THEN concat(OrdNo,'(Cut)') ELSE concat(OrdNo,'(UnCut)') END) AS OrdNo
FROM (
SELECT
Action
, SourceReel
, OrdNo
, SUM(Quantity) AS TotalQuantity
FROM CableCuts_Log CD
WHERE 1=1
AND 1 = CASE
WHEN SourceReel IS NOT NULL AND OrdNo LIKE 'E9%' AND Quantity > 0 AND DaTediff(Now(),LogDate) = 0 THEN 1
ELSE 0 END
GROUP BY Action, SourceReel, OrdNo
) CC
WHERE 1=1
GROUP BY SourceReel
) cuts ON Id.ReelTag = cuts.SourceReel
Again, When I run this in MySql workbench it's fine and loads in a second if that makes a difference.
But when I call my API to call the SP using ...
let inventoryReport = await models.sequelize
.query(
`call rpt_DailyInventoryReport($location, $byECSPartNo);`,
{ bind: {location: req.body.location, byECSPartNo: null} },
)
for(i=0;i<inventoryReport.length;i++) {
if(inventoryReport[i].ReelTagSerial == '6906' || inventoryReport[i].ReelTagSerial == '6858') {
console.log(inventoryReport[i]);
}
}
and exporting that into an Excel using ExcelJS, the 2 "cuts" columns from the query are essentially NULLs, because the return value of the "cuts" select are coming up NULLS, which is why it's giving the IFNULL value instead. Again this work in MySql workbench.
These are the values the API throws out.
{
"ReelTagSerial": 6858,
"CurrentQuantity": 700,
"YesterdayQuantity": 2500,
"QuantityChanged": 0,
"OrdNoChange": null,
"ShipQuantity": 0,
"OrdNo_Allocated": null,
"changeAOF_Yesterday": -1800
},
{
"ReelTagSerial": 6906,
"CurrentQuantity": 2730,
"YesterdayQuantity": 3330,
"QuantityChanged": 0,
"OrdNoChange": null,
"ShipQuantity": 0,
"OrdNo_Allocated": null,
"changeAOF_Yesterday": -600
},
Here are the 2 rows that their values should be.
{
"ReelTagSerial": 6906,
"CurrentQuantity": 2730,
"YesterdayQuantity": 3330,
"QuantityChanged": 600,
"OrdNoChange": E92021(Cut),
"ShipQuantity": 0,
"OrdNo_Allocated": null,
"changeAOF_Yesterday": -600
}
{
"ReelTagSerial": 6858,
"CurrentQuantity": 700,
"YesterdayQuantity": 2500,
"QuantityChanged": 1800,
"OrdNoChange": E912345(Cut),
"ShipQuantity": 0,
"OrdNo_Allocated": null,
"changeAOF_Yesterday": -1800
},
I have tried creating a temp table and defining the field type of varchar, text, mediumint for the QuantityChanged field.
At first I thought maybe ExcelJs was not liking the key/value pairs but then I simply console logged the 2 rows and they are returning like that from Sequelize. I have tried casting the 2 fields with every datatype possible.
, CAST(cuts.OrdNo AS char) AS OrdNoChange
, CAST(cuts.OrdNo AS binary) AS OrdNoChange
Now I am just immediately sending the return result as a response back to Postman and seeing all the rows to make sure for whatever reason those values are not being set in other rows. But they seem to be all good.
If I simply rearrange the columns so that the two bad fields get values of other fields, they do populate with their values, so that's about as far as I got. Or if I put just 1000 or a string type it returns correctly, so IT MUST be something to do with those 2 data types from the "cuts" query.
I have tried returning simply returning these from the "cuts" join query
SELECT
DISTINCT SourceReel
#, CAST(SUM(CASE WHEN Action = 'Insert' THEN TotalQuantity
# WHEN Action = 'Delete' THEN -TotalQuantity
# ELSE 0 END) AS UnSigned) AS Quantity
, SUM(TotalQuantity) AS Quantity
, OrdNo
#, group_concat(DISTINCT CASE WHEN Action = 'Insert' THEN concat(OrdNo,'(Cut)') ELSE concat(OrdNo,'(UnCut)') END) AS OrdNo
Nothing is working. Spent hours troubleshooting and searching...
Need some help please :)
Thanks in advance.
I was able to get around this by creating a table and inserting into it during the SP call. Then in NodeJs I created a model of that table and just used to find everything in the table immediately after I called the SP.
let inventoryReport = await models.Rpt_DailyInventory_Temp.findAll();
Then just TRUNCATE the table every time the SP is called.
Is it possible to have a calculated field where some of the columns are not calculated?
with not calculated I mean that some of the data needs to be calculated and some of the data needs to be put in by hand
I have already tried it with an IIF statement like this
IIf( [Uitkomst] = 0 , [Lengte] * [Breedte] * [Hoogte] , [Uitkomst])
If you by "not calculated" mean Null, you can use Nz for these empty fields:
IIf([Uitkomst] = 0, Nz([Lengte]) * Nz([Breedte]) * Nz([Hoogte]), [Uitkomst])
Query:
Select
*, IIf([Uitkomst] = 0, [Lengte] * [Breedte] * [Hoogte], [Uitkomst])
From
YourTable
I'm trying to use AND with OR's inside my WHERE clause but I'm not getting expected results. This is my query:
select *
from Pitches
where BatterID = #playerID
and YEAR(GameDate) in (2019)
and (BattedBallID is not null or BattedBallID <> 0);
The problem is I'm getting BattedBallID's that have 0 in the rows but none of them have null. I want it so my results have BattedBallID's that aren't 0 and aren't null;
I want it so my results have BattedBallID's that aren't 0 and aren't null;
You want:
and BattedBallID is not null and BattedBallID <> 0;
Instead of:
and (BattedBallID is not null or BattedBallID <> 0);
Your original expression will actually always evaluate as true, since both conditions cannot be false at the same time : if something is null, then it is not equal to 0, and if something is equal to 0, then it is not null.
I have an MDX query which gets me how many Completed/Booked/Unbooked Maintenance Jobs there are for each Vehicle
SELECT NON EMPTY { [Measures].[Completed],
[Measures].[Unbooked],
[Measures].[Booked]} ON COLUMNS,
NON EMPTY { (
[Job Code].[Code].[Code].ALLMEMBERS*
[Vehicle].[Vehicle Number].[Vehicle Number].ALLMEMBERS*
[Job Group Target Completion Date].[Date].[Date].ALLMEMBERS
) } ON ROWS
FROM ( SELECT ( STRTOMEMBER(#FromDate, CONSTRAINED) : STRTOMEMBER(#ToDate, CONSTRAINED) )
FROM [Standard Reports - Depot Work Analysis])
Where #FromDate and #ToDate are members from my [Job Group Target Completion Date] dimension. Jobs are grouped into "Job Groups" when they share Job Code and Target Date.
How can I expand this so that I either get all Job Groups between the target dates (as it is now) OR all Job Groups in which not all jobs are completed (i.e. [Measures].[Unbooked] > 0 or [Measures].[Booked] > 0) but were targeted to be completed before the given date range.
I expect you want something like the following:
With
Member [Measures].[Targeted] as
IIF(
[Measures].[Unbooked] > 0
and
[Measures].[Booked] > 0
and
[Measures].[Completed] > 0,
1,
Null
)
Select
Non Empty {
[Measures].[Completed],
[Measures].[Unbooked],
[Measures].[Booked]
} on 0,
Non Empty {
{
[Job Code].[Code].[Code].AllMembers *
[Vehicle].[Vehicle Number].[Vehicle Number].AllMembers *
{StrToMember(#FromDate, CONSTRAINED):StrToMember(#ToDate, CONSTRAINED)}
{
+
NonEmpty(
{
[Job Code].[Code].[Code].AllMembers *
[Vehicle].[Vehicle Number].[Vehicle Number].AllMembers *
{NULL:StrToMember(#FromDate, CONSTRAINED).Lag(1)}
},
[Measures].[Targeted]
)
} on 1
From [Standard Reports - Depot Work Analysis])
I'm trying to figure out why this query SELECTs just fine, but I can't use it to create a temporary table... The IF = '', 0 stuff didn't used to exist in the original query so I added it to make sure that I wasn't trying to sum anything that could potentially be an empty string, but it didn't fix the problem. This select works and returns the correct result set:
SELECT
SUM(((IF(reserve_transactions.Revenue = '',
0,
reserve_transactions.Revenue) * IF(reserve_transactions.Confidence = '',
0,
reserve_transactions.Confidence)) / 100)) AS rawadjusted
FROM
(store_locations
LEFT JOIN reserve_transactions ON ((store_locations.ID = location_sales)))
GROUP BY store_locations.Loc_Long
But this does not:
CREATE TEMPORARY TABLE tmptable_which_doesnt_work AS SELECT
SUM(((IF(reserve_transactions.Revenue = '',
0,
reserve_transactions.Revenue) * IF(reserve_transactions.Confidence = '',
0,
reserve_transactions.Confidence)) / 100)) AS rawadjusted
FROM
(store_locations
LEFT JOIN reserve_transactions ON ((store_locations.ID = location_sales)))
GROUP BY store_locations.Loc_Long
The error is: Error Code: 1292. Truncated incorrect DECIMAL value: ''
I have other queries where the situation is the same, actually. Sometimes with other truncated incorrect types. What am I missing here?
I think that the problem could be data and the logic of selecting only the numeric entries - You could try to define a functions as follows:
CREATE FUNCTION IsNumeric (sIn varchar(1024))
RETURNS tinyint
RETURN sIn REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
and apply it in the where clause for the two arguments, eventually trim unnecessary white spaces (as per How do I check to see if a value is an integer in MySQL? )