I'm somewhat new to PowerApps and I'm stuck at trying to reproduce a JOIN using the AddColumns function and a LookUp function as I saw on this link: Syntax for joining tables. This JOIN will go inside of a Data Table.
I have two SQL tables I'm trying to JOIN using that method
mT_SalesAttributeDB_FamilyMaterialGroupMaterials: ([ID], [MaterialGroupID], [MaterialID], [Comment], [Created], [Updated])
mT_SalesAttributeDB_Materials: ([ID], [MaterialName], [Description], [ShortDesc], [Abbr], [Superseded], [HasNoDrawing], [Created], [Updated])
What I'm wanting to accomplish is this:
SELECT
*
FROM
mT_SalesAttributeDB_FamilyMaterialGroupMaterials AS Grps
INNER JOIN
mT_SalesAttributeDB_Materials AS Mats ON Grps.MaterialID = Mats.ID
WHERE
Grps.MaterialGroupID = tblMaterialGroups_Families.Selected.ID
I think this is what that should look like:
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
Text(tblMaterialGroups_Families.Selected.ID) = MaterialGroupID
),
"MaterialName",
LookUp(
mT_SalesAttributeDB_Materials,
ID = mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID],
MaterialName
)
)
However, the equality inside of the LookUp, ID = mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID] gives an error on the equal sign saying I can't compare a number to a table
As a reference, I've tried this and it works fine
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
Text(tblMaterialGroups_Families.Selected.ID) = MaterialGroupID
)
Is the problem that I'm trying to use a preview object that isn't fully released? Or am I joining them incorrectly?
EDIT
I've also tried this and still get the same error about not being able to compare a number to a Table
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialGroupID] = Text(tblMaterialGroups_Families.Selected.ID)
),
"SpecName",
LookUp(
mT_SalesAttributeDB_Materials,
mT_SalesAttributeDB_Materials[#ID] = mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID],
mT_SalesAttributeDB_Materials[#MaterialName]
)
)
NOTE: I also wanted to mention that I attempted the exact same AddColumns with a List Box to make sure it wasn't something to do with Data Tables being in preview, and I still get the error that I can't compare a Number to a Table
EDIT 2
I seem to be getting close. I can get the error to stop and it to populate "SpecName", however, it just grabs the first MaterialName from mT_SalesAttributeDB_Materials instead of doing a proper LookUp
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
MaterialGroupID = Text(tblMaterialGroups_Families.Selected.ID)
),
"MaterialName",
LookUp(
mT_SalesAttributeDB_Materials,
ID in mT_SalesAttributeDB_FamilyMaterialGroupMaterials.MaterialID,
MaterialName
)
)
I changed the "=" to an "in" and it seems that mT_SalesAttributeDB_FamilyMaterialGroupMaterials.MaterialID is the same as mT_SalesAttributeDB_FamilyMaterialGroupMaterials[#MaterialID]
Okay, so I finally got it with some help from Google. I had a few things that were likely causing me problems with my tables.
_Materials.MaterialID was changed to .MaterialName so I wouldn't have two fields with the same name but different meanings. I think that helped when PowerApps was using context.
_Materials.ID is stored as an INT however, for some reason I decided to store _FamilyMaterialGroupMaterials as a VARCHAR. So I changed that to an INT so MaterialID and ID would be exactly the same thing in each table. (I assume this helps with the JOIN)
Once I had that updated, this is the correct syntax to JOIN two tables. You can see I even added in a second Column once I got it working
AddColumns(
Filter(
mT_SalesAttributeDB_FamilyMaterialGroupMaterials,
MaterialGroupID = tblMaterialGroups_Families.Selected.ID
),
"MaterialName",
LookUp(
mT_SalesAttributeDB_Materials,
ID = MaterialID
).MaterialName,
"ShortDesc",
LookUp(
mT_SalesAttributeDB_Materials,
ID = MaterialID
).ShortDesc
)
Hopefully my full day wasted trying to figure that out will help someone else.
Related
I have been working on this SQL code for a bit and I cannot get it to display like I want. I have an operation that we send parts outside of our business but there is no time stamp on when that operation sent out.
I am taking the previous operation's last labor date and the purchase order creation date to try and find out how long it takes that department to issued a purchase order.
I have tried LAST_Value to add to my query. I have even played with LAG and couldn't get a anything but errors.
SELECT
JobOpDtl.JobNum,
JobOpDtl.OprSeq,
JobOpDtl.OpDtlDesc,
LastValue.ClockInDate,
LastValue.LastValue
FROM Erp.JobOpDtl
LEFT OUTER JOIN Erp.LaborDtl ON
LaborDtl.JobNum = JobOpDtl.JobNum
and LaborDtl.OprSeq = JobOpDtl.OprSeq
LEFT OUTER JOIN (
Select
LaborDtl.JobNum,
LaborDtl.OprSeq,
MAX(LaborDtl.ClockInDate) as ClockInDate,
LAST_VALUE (LaborDtl.ClockInDate) OVER (PARTITION BY OprSeq ORDER BY JobNum) as LastValue
FROM Erp.LaborDtl
GROUP BY
LaborDtl.JobNum,
LaborDtl.OprSeq,
LaborDtl.ClockInDate
) as LastValue ON
JobOpDtl.JobNum = LastValue.JobNum
and JobOpDtl.OprSeq = LastValue.OprSeq
WHERE JobOpDtl.JobNum = 'PA8906'
GROUP BY
JobOpDtl.JobNum,
LastValue.OprSeq,
JobOpDtl.OpDtlDesc,
JobOpDtl.OprSeq,
LastValue.ClockInDate,
LastValue.LastValue
No errors, just not displaying how I am wanting it.
I would like it to display the OperSeq with the previous OperSeq last transaction date.
The basic function you want is LAG (as you suggested) but you need to wrap it in a COALESCE. Here is a sample code that illustrates the concept
SELECT * INTO #Jobs
FROM (VALUES ('P1','Step1', '2019-04-01'), ('P1','Step2', '2019-04-02')
, ('P1','Step3', '2019-04-03'), ('P1','Step4', NULL),
('P2','Step1', '2019-04-01'), ('P2','Step2', '2019-04-03')
, ('P2','Step3', '2019-04-06'), ('P2','Step4', NULL)
) as JobDet(JobNum, Descript, LastDate)
SELECT *
, COALESCE( LastDate, LAG(LastDate,1)
OVER(PARTITION BY JobNum
ORDER BY COALESCE(LastDate,GETDATE()))) as LastValue
FROM #Jobs
ORDER BY JobNum, Descript
DROP TABLE #Jobs
To apply it to your specific problem, I'd suggest using a COMMON TABLE EXPRESSION that replaces LastValue and using that instead of the raw table for your queries.
Your example picture doesn't match any tables you reference in your code (it would help us significantly if you included code that created temp tables matching those referenced in your code) so this is a guess, but it will be something like this:
;WITH cteJob as (
SELECT JobNum, OprSeq, OpDtlDesc, ClockInDate
, COALESCE( LastValue, LAG(LastValue,1)
OVER(PARTITION BY JobNum
ORDER BY COALESCE(LastValue,GETDATE()))) as LastValue
FROM Erp.JobOptDtl
) SELECT *
FROM cteJob as J
LEFT OUTER JOIN LaborDtl as L
on J.JobNum = JobNum
AND J.OprSeq = L.OprSeq
BTW, if you clean up your question to provide a better example of your data (i.e. SELECT INTO sttements like in the start of my answer that produce tables that correspond to the tables in your code instead of an image of an excel file) I might be able to get you closer to what you need, but hopefully this is enough to get you on the right track and it's the best I can do with what you've provided so far.
I am facing a strange behavior with a merge into statement in Oracle. Actually,few days ago, my statement used to work perfectly and the requested column is being updated. However, as of today I found out it is no longer updating the column. I am getting confused if this is environment related issue or a problem related the query it self. Can you advise please ?
Query :
merge into Currency_table CURR
using (
SELECT CURR.rowid as RID, M_SYSTEM
FROM Currency_table CURR, Trade_ext TRN_EXT, TradeGen TRN, CONTRACT CNT
WHERE CNT.M_REFERENCE= 123
AND CNT.M_REFERENCE = TRN.M_CONTRACT
AND TRN.M_NumB = TRN_EXT.M_C_REF
AND TRN_EXT.M_C_REF = CURR.M_NumBer
)T
on ( CURR.rowid = T.RID)
when matched then
update set CURR.MSYSTEM = 'S1' ;
I have two tables:
FINAL_VIEW:
and tblUnits:
I need to check that every Value in FINAL_VIEW, if present in Units or if LIKE any field in Correct_Unit, is a case sensitive match for that Correct_Unit(presumably using strComp(Value,Correct_Unit,0)). I'm using this as a criteria for selection.
For instance, if Value was BarG it would be LIKE barg but would then evaluate to false on the strComp().
The stage after this is to return the relevant Correct_Unit (in this case barg) in another field.
I have tried using a subquery within the strComp() but that doesn't work and I have no idea where ot go from here. Any help would be appreciated!
My question can't have been particularly clear but I worked out a solution for both parts.
The key was to link Value with Unit and then perform the query, whilst having my subStr() in the WHERE clause and selecting the Correct_Unit.
Essentially, the linking did the work.
Here's my query for reference:
INSERT INTO tbluoms
SELECT final_view.controller AS Controller,
final_view.compound AS Compound,
final_view.contained_name AS contained_name,
final_view.strategytagname AS StrategyTagname,
final_view.tagname AS Tagname,
final_view.block_cont_name AS Block_Cont_Name,
final_view.name AS Name,
Cstr(final_view.value) AS [Value],
tblunits.[correct unit] AS Correct_Value
FROM tblunits
INNER JOIN final_view
ON tblunits.unit = Cstr(final_view.value)
WHERE NOT (( [value] = [correct unit] ))
OR Strcomp([value], [correct unit], 0)
AND tagname NOT IN (SELECT [tagname]
FROM [tbluoms])
AND name LIKE "ei*"
AND controller IS NOT NULL
AND compound IS NOT NULL;
I'm currently working in a Netezza environment on Aginity Workbench and I was planning on using some of the columns from the Management View _V_QRYSTAT to populate a graph in MicroStrategy.
Unfortunately, I cannot get MicroStrategy to recognize any of the columns in _V_QRYSTAT. I don't think that it can read columns from views, and I figured that the best way around this would be to find out which table the _V_QRYSTAT view is getting its data from, but I can't figure out a way to find the source table of a view in Netezza. Does anyone know a method that can be used in Netezza on Aginity Workbench for locating the source table of a view (specifically _V_QRYSTAT)?
I'm very new to SQL, Netezza and MicroStrategy, so I apologize if I am being unclear. Let me know if further elaboration is needed.
I'm pretty sure that MicroStrategy will recognize and work with views, but to answer your question directly, you can see the view definition by querying the _V_VIEW system view.
select definition from _v_view where viewname = '_V_QRYSTAT';
DEFINITION
---------
SELECT
QS.QS_SESSIONID,
QS.QS_PLANID ,
QS.QS_CLIENTID ,
CASE
WHEN ((VU.OBJID NOTNULL
)
OR ("CURRENT_USEROID"() = 4900
)
)
THEN QS.QS_CLIIPADDR
ELSE "NAME"(NULL::"VARCHAR")
END AS QS_CLIIPADDR,
CASE
WHEN ((VU.OBJID NOTNULL
)
OR ("CURRENT_USEROID"() = 4900
)
)
THEN QS.QS_SQL
ELSE TEXT(NULL::"VARCHAR")
END AS QS_SQL ,
QS.QS_STATE ,
QS.QS_TSUBMIT ,
QS.QS_TSTART ,
QS.QS_PRIORITY,
QS.QS_PRITXT ,
QS.QS_ESTCOST ,
QS.QS_ESTDISK ,
QS.QS_ESTMEM ,
QS.QS_SNIPPETS,
QS.QS_CURSNIPT,
QS.QS_RESROWS ,
QS.QS_RESBYTES
FROM
((DEFINITION_SCHEMA."_T_QRYSTAT" QS
LEFT JOIN DEFINITION_SCHEMA."_T_SESSCTX" SS ON (
(QS.QS_SESSIONID = SS.SESSION_ID
)
))
LEFT JOIN DEFINITION_SCHEMA."_V_USER" VU ON (
(SS.SESSION_USERNAME = VU.USERNAME
)
));
(1 row)
This will almost certainly take you a couple of recursions down, as the view you are interested in is based on either views as well as tables.
I'm fairly new to Access so this is driving me a little crazy.
I'm creating an inventory database and want to count the number of items in stock to update an ordering form. Received items are assigned an order code, and I want to count the number of instances of each order code found within the master table. I have a make table query which does this just fine:
SELECT PrimerList.PrimerName
, First(Primer_Master.FR) AS FR
, Primer_Master.OrderCode
, Count(Primer_Master.OrderCode) AS InStock
INTO PrimerOrder
FROM PrimerList
LEFT JOIN Primer_Master ON PrimerList.ID = Primer_Master.PrimerName
GROUP BY PrimerList.PrimerName
, Primer_Master.OrderCode
, Primer_Master.PrimerName
, Primer_Master.FR
, Primer_Master.Finished
HAVING ((([Primer_Master]![Finished])=No));
I want to use PrimerOrder to update an order list table PrimerOrderList which has all of the different possible order codes, updating the InStock value for records with matching OrderCode:
UPDATE PrimerOrderList
SET PrimerOrderList.InStock = PrimerOrder.InStock
WHERE (((PrimerOrderList.OrderCode)=[PrimerOrder].[OrderCode]));
However, when I try to run it I get parameter boxes which pop-up asking for PrimerOrder.OrderCode and PrimerOrderList.OrderCode. Even if I put in a valid value for each, I get a type conversion failure. I've checked the data types for both tables and don't see how there could be a type conversion failure - both are set to text.
Any insight would be greatly appreciated! Thanks in advance!
You haven't included the PrimerOrder table in your query. Should be:
UPDATE PrimerOrderList INNER JOIN PrimerOrder
ON PrimerOrderList.OrderCode = PrimerOrder.OrderCode
PrimerOrderList.InStock = PrimerOrder.InStock