I have the following query;
SELECT TOP 1
T0.custmrName,
T0.callID,
T0.BpShipAddr,
T1.Name,
T0.createDate,
T0.subject,
T0.manufSN,
T0.internalSN,
T0.itemCode,
T0.itemName,
CAST(T0."resolution" AS varchar(MAX)),
T5.DocNum,
T7.DocNum AS 'OrderNum'
FROM OSCL T0
LEFT JOIN OSCT T1 ON T0.callType = T1.callTypeID
LEFT JOIN OHEM T2 ON T0.technician = T2.empID
LEFT JOIN SCL4 T3 ON T3.SrcvCallID = T0.callID
LEFT JOIN QUT1 T4 ON T3."Object" = T4."ObjType" AND T3."DocAbs" = T4."DocEntry"
LEFT JOIN OQUT T5 ON T5.DocEntry = T4.DocEntry
LEFT JOIN RDR1 T6 ON T3.Object = T6.ObjType AND T3.DocAbs = T6.DocEntry
LEFT JOIN ORDR T7 ON T7.DocEntry = T6.DocEntry
WHERE T0.callID = 8235
ORDER BY T5.DocNum DESC, T7.DocNum DESC, T3.Object DESC
In SAP b1 this works perfectly fine, only gives me the 1 record relating to the service call I am after. If it has a linked quotation, gives me the info with the quote number, if only a linked sales order then all the info with the S/O number. If both exist then gives me the one record with the quotation number and no sales order number (which is fine).
Now the problem lies in getting this to show in Crystal Report. None of the fields are showing up in here (only the labels) and i cant seem to work out why. All my joins are left joins and it works perfectly fine in SAP B1.
It seems to only happen like this in CR if its TOP 1, DISTINCT works but that will give me multiple records.
If anyone can shed some light on why this might be that would be super helpful.
(note the where condition for the call id is used as an example, this is controlled by a parameter in Crystal Reports)
Thank you.
I managed to fix this issue by removing the parameter from Crystal Reports and placing it directly within the SQL Query.
Related
I have a filtered table (filtered by t1 and t3 values) with 6 columns (it has event number and t2 - t6 visible) in my SSRS report. I am using report builder.
I am having a problem because the t6 (Reason_Note) variable doesn't always have a value. And I don't mean it has a blank value. It appears as if that test does NOT EXIST.
For example: Over a timeframe, There are 5 rows of data that should appear in report. But for 2 of the 5, t6 has NO data (test doesn't exist), so the report only shows 3 rows. But I still want it to show all 5 rows it should, just leaving t6 blank. And I still want the t6 value to correspond to the correct rows (lined up with other columns). Is it possible to force t6 to be blank if the test doesn't exist?
I have tried IsNothing tests, but it doesn't work. It doesn't even recognize the test, so it seems like it doesn't even test for something/nothing.
Any help would be appreciated. Here is my current query:
select e.event_num, event_status, e.event_ID, t1.result as Quality_issue, t2.result as
Reason, t3.result as Storage, t4.result as Grade, t5.result as Roll_Bulk, t6.result as
Reason_Note, prodstatus_desc_local P from events e
left outer join tests t1 on (e.timestamp = t1.result_on)
left outer join tests t2 on (e.timestamp = t2.result_on)
left outer join tests t3 on (e.timestamp = t3.result_on)
left outer join tests t4 on (e.timestamp = t4.result_on)
left outer join tests t5 on (e.timestamp = t5.result_on)
left outer join tests t6 on (e.timestamp = t6.result_on)
left outer join Production_status P on (e.Event_Status = P.ProdStatus_Id)
Where (e.pu_id =2 and t1.var_id = 24777 and t2.var_id = 16 and t3.var_id = 312 and t4.var_id
= 1249 and t5.var_id = 1130 and t6.var_id = 3657 )
and t1.result_on >=#StartTime
and t1.result_on <#EndTime
enter image description here
That is the data I get currently (Not sure if that picture worked). I would expect to have three more rows of data, but there is no value for t6 in those 3 rows, so it's not pulled in.
The only parameters are start time and end time.
Source Data
This one is probably really simple but im not seeing the solution. In SAP B1 I have a service call that has multiple sales orders linked to it. I only want the results from the most recent sales order however its throwing at me all the line items from every sales order attached to it. I can see in my code why its doing this but the solution just hasnt appeared before me.
I thought i could fix it by using a MAX(T4.DocNum) and grouping the select information however not only did that not work it also got rid of one of the lines i didnt want it to.
The code;
SELECT T1."callID",T3."ItemCode", T3."Dscription", T3.DocEntry,T2.SrcvCallId,T3.Quantity,T4.DocNum
FROM OSCL T1
LEFT JOIN SCL4 T2 ON T1."callID" = T2."SrcvCallID"
INNER JOIN RDR1 T3 ON T2."Object" = T3."ObjType" AND T2."DocAbs" = T3."DocEntry" AND T2."Object" = '17'
INNER JOIN ORDR T4 ON T4.DocEntry = T3.DocEntry
WHERE T1.callID = 11255
ORDER BY T4.DocDate DESC
The results;
As you can see the last result there comes from another associated linked document, i dont want this to appear.
Thanks for any help on this.
Looks like i figured it out. Solution below for anyone else that may run into this problem.
I added an extra section to the RDR1 join to compare the SCL4.DocPstDate to the MAX docdate from a linked sales order which seems to have done the job.
SELECT T1."callID",
T3."ItemCode" AS 'OrderCode',
T3."Dscription" AS 'OrderDesc',
T3.DocEntry,
T2.SrcvCallId,
T3.Quantity AS 'OrderQty',
T4.DocNum
FROM OSCL T1
LEFT JOIN SCL4 T2 ON T1."callID" = T2."SrcvCallID"
INNER JOIN RDR1 T3 ON T2."Object" = T3."ObjType"
AND T2."DocAbs" = T3."DocEntry"
AND T2."Object" = '17'
AND T2.DocPstDate =
(SELECT MAX(A.DocDate) FROM ORDR A
INNER JOIN RDR1 B ON B.DocEntry = A.DocEntry
INNER JOIN SCL4 C ON C.DocAbs = B.DocEntry AND C.Object = '17'
WHERE C.SrcvCallID = T1.callID)
INNER JOIN ORDR T4 ON T4.DocEntry = T3.DocEntry
WHERE T1.callID = 11255
ORDER BY T4.DocDate DESC
select t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
from capd_a t1
right outer join capd_b t2
on (t2.s_id = t1.p_id)
join capd_c t3
on ((t3.e_student=t1.p_id) and (t3.e_reference='D /YR2A2/12'))
I receive this error:
Syntax Error (Missing Operator) on (t2.s_id = t1.p_id)
join capd_c t3
on ((t3.e_student=t1.p_id) and (t3.e_reference='D /YR2A2/12'))
Any idea what is wrong with my query?
Access has a pervert version of SQL and not the ISO/ANSI standard. It really likes (read: requires) parenthesis when joining more than two tables. There are also restrictions on what types of joins (LEFT, RIGHT, INNER) are allowed inside parentheses:
from
( --- required
capd_a t1
INNER join capd_b t2 --- RIGHT join turned into INNER
on (t2.s_id = t1.p_id)
) --- required
INNER join capd_c t3
on ((t3.e_student=t1.p_id) and (t3.e_reference='D /YR2A2/12'))
Try building your query in the Access query designer. Coming from a different database system, you may not like the query designer much, but I'm suggesting you try it anyway because it will help you create SQL which the Access db engine can accept.
Among the differences you're facing are:
Access requires parentheses when your query includes more that 2 data sources, and is very demanding about their positions.
The db engine does not recognize JOIN as a synonym for INNER JOIN. You must always specify the join type (INNER; LEFT; or RIGHT).
The engine will accept RIGHT OUTER JOIN, but the query designer will change it to just RIGHT JOIN. So it doesn't really make a difference, but I mentioned it only to avoid confusion.
If you can't tolerate the query designer, I'll offer this as a starting point.
select t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
from (capd_a t1
inner join capd_b t2
on t2.s_id = t1.p_id)
inner join capd_c t3
on t3.e_student=t1.p_id
where t3.e_reference='D /YR2A2/12'
I user inner join both times because in certain situations Access balks with LEFT or RIGHT joins and INNER joins in the same query. I'm uncertain whether your RIGHT join would be a problem in this case, but both as INNER will surely work. So first get all the rest of the query syntax working correctly, then revise your join types as needed.
I moved t3.e_reference='D /YR2A2/12' from the ON to a WHERE clause. That's my preference, but I don't think it will make a difference either way.
As your query includes t3.e_reference='D /YR22/A12' in ON statement scope which should be excluded from ON statement scope and write it seperately outside the ON statement scope......
You can do like this;
select t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
from capd_a t1
right outer join capd_b t2
on (t2.s_id = t1.p_id)
join capd_c t3
on (t3.e_student=t1.p_id)
and (t3.e_reference='D /YR2A2/12')
Or, you can do use WHERE clause in query:
SELECT t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
FROM capd_a t1
RIGHT OUTER JOIN capd_b t2
ON (t2.s_id = t1.p_id)
JOIN capd_c t3
ON (t3.e_student = t1.p_id)
WHERE t3.e_reference='D /YR2A2/12'
I'm in the process of moving some Mysql queries over to Postgresql and I ran across this one that doesn't work.
select (tons of stuff)
from trip_publication
left join trip_collection AS "tc" on
tc.id = tp.collection_id
left join
trip_author ta1, (dies here)
trip_person tp1,
trip_institution tai1,
trip_location tail1,
trip_rank tr1
ON
tp.id = ta1.publication_id
AND tp1.id = ta1.person_id
AND ta1.order = 1
AND tai1.id = ta1.institution_id
AND tail1.id = tai1.location_id
AND ta1.rank_id = tr1.id
The query seems to be dying on the "trip_author ta1" line, where I marked it above. The actual error message is:
syntax error at or near ","
LINE 77: (trip_author ta1, trip_person tp1, ...
I went through the docs, and it seems to be correct. What exactly am I doing wrong here? Any feedback would be much appreciated.
I don't know postgres, but in regular SQL you would need to a series of LEFT JOIN statements rather than your comma syntax. You seemed to have started this then stopped after the first two.
Something like:
SELECT * FROM
table1
LEFT JOIN table2 ON match1
LEFT JOIN table3 ON match2
WHERE otherFilters
The alternative is the older SQL syntax of:
SELECT cols
FROM table1, table2, table3
WHERE match AND match2 AND otherFilters
There's a couple of other smaller errors in your SQL, like the fact you forgot your tp alias on your first table, and have tried including a where clause (ta1.order = 1) as a joining constraint.
I think this is what you are after:
select (tons of stuff)
from trip_publication tp
left join trip_collection AS "tc" on tc.id = tp.collection_id
left join trip_author ta1 on ta1.publication_id = tp.id
left join trip_person tp1 on tp1.id = ta1.person_id
left join trip_institution tai1 on tai1.id = ta1.institution_id
left join trip_location tail1 on tail1.id = tai1.location_id
left join trip_rank tr1 on tr1.id = ta1.rank_id
where ta1.order = 1
Your left joins are one per table you are joining
left join trip_author ta1 on ....
left join trip_person tp1 on ....
left join trip_institution on ...
...and so on
I have this problem regarding sql which i will be using in a webservice if I can get this right. What I wanted to do is create a summary report of a transaction. The transaction have header and lines. In a transaction, we are going to input a many planks.
After the inputing, I would like to produce a report that would add all the plank that belongs to a category, then multiply all the planks by the price of that supplier so that I can have the total amount. Here is a pic:
below is my sql which produce wrong output:
select
t1.transaction_num,
wo1.wood_classification_desc,
wo2.wood_specie_desc,
sum(t2.board_foot) as total_board_foot,
su1.price,
sum(t2.board_foot*su1.price) as total_amount
from
"transaction_hdr" t1
left join "transaction_lne" t2 on (t1.transaction_id = t2.transaction_id)
left join "supplier" su2 on (t1.supplier_id = su2.supplier_id)
left join "supplier_price" su1 on (t2.price = su1.price)
left join "wood_classification" wo1 on (t2.wood_classification_id = wo1.wood_classification_id)
left join "wood_specie" wo2 on (wo1.wood_specie_id = wo2.wood_specie_id)
group by
t1.transaction_num,wo1.wood_classification_desc,su1.price,su2.supplier_name,wo2.wood_specie_desc
order by transaction_num,wo2.wood_specie_desc
Evrytime I run that sql, it produces somthing like this:
the transaction that i test only have five planks. 4 planks under Mahogany 6" wider - 7ft. up and 1 Mahogany 5" wider - 7ft. up.
I would guess, that in one of the left joins you have more than one record, which means it give you wrong sum (group by will affect the set after the left join).
Just run the sql without the grouping and check what you get.
So it's probably not the multiplication that causes the problem, it's the sum.
Do you have more than one record in supplier_price with price = 13.33 ? That would be my guess, as all the other joins appear to be on primary keys..
EDIT:
Your problem is that you're joining to supplier_price on the price field, which is not a valid key. Given that your output doesn't take anything from the supplier_price table, I'd be inclined to remove it from the query altogether as below:
select
t1.transaction_num,
wo1.wood_classification_desc,
wo2.wood_specie_desc,
sum(t2.board_foot) as total_board_foot,
t2.price,
sum(t2.board_foot*t2.price) as total_amount
from
"transaction_hdr" t1
left join "transaction_lne" t2 on (t1.transaction_id = t2.transaction_id)
left join "supplier" su2 on (t1.supplier_id = su2.supplier_id)
left join "wood_classification" wo1 on (t2.wood_classification_id = wo1.wood_classification_id)
left join "wood_specie" wo2 on (wo1.wood_specie_id = wo2.wood_specie_id)
group by
t1.transaction_num,wo1.wood_classification_desc,t2.price,su2.supplier_name,wo2.wood_specie_desc
order by transaction_num,wo2.wood_specie_desc
This may solve the issue. But it would help if you provided the relationships between the tables (and the tables' primary keys):
select
t1.transaction_num,
wo1.wood_classification_desc,
wo2.wood_specie_desc,
sum(t2.board_foot)
as total_board_foot,
( SELECT DISTINCT su1.price
FROM "supplier_price" su1
WHERE t2.price = su1.price
) AS price,
sum(t2.board_foot) *
( SELECT DISTINCT su1.price
FROM "supplier_price" su1
WHERE t2.price = su1.price
)
as total_amount
from
"transaction_hdr" t1
left join "transaction_lne" t2
on (t1.transaction_id = t2.transaction_id)
left join "supplier" su2
on (t1.supplier_id = su2.supplier_id)
left join "wood_classification" wo1
on (t2.wood_classification_id = wo1.wood_classification_id)
left join "wood_specie" wo2
on (wo1.wood_specie_id = wo2.wood_specie_id)
group by
t1.transaction_num
, wo1.wood_classification_desc
, su2.supplier_name
, wo2.wood_specie_desc
order by transaction_num
, wo2.wood_specie_desc