Sugar crm suctom field sql select error - sql

I've added few additional fields to Accounts module. Also added same fields to detailed search. One field is decimal, another text, filled from drop down menu. After running a test search everything looked fine. But not long time ago I needed to see all the items in the search. Search was made using my new added fields as parameters. As you may already know, search in sugar crm shows only first 20 items. So, after trying to access another 20 items i've got an empty list.
sugarcrm.log showed me sql query error:
11/12/12 00:09:33 [6300][ca4960aa-6cce-065a-be1d-4fa7b40db052][FATAL] Query Failed:SELECT TOP 21 * FROM
(SELECT ROW_NUMBER()
OVER (ORDER BY accounts.name) AS row_number,
accounts.id ,
**accounts_cstm.print_srv_prov_c**,
accounts.name ,
accounts.billing_address_city ,
accounts.billing_address_country ,
accounts.phone_office ,
LTRIM(RTRIM(ISNULL(jt0.first_name,'')+N' '+ISNULL(jt0.last_name,''))) assigned_user_name ,
jt0.created_by assigned_user_name_owner ,
N'Users' assigned_user_name_mod,
accounts.annual_revenue ,
accounts.account_type ,
**accounts_cstm.print_srv_prov_c** ,
accounts.assigned_user_id
FROM accounts
LEFT JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c
LEFT JOIN users jt0 ON accounts.assigned_user_id=jt0.id AND jt0.deleted=0 AND jt0.deleted=0
where ((accounts.account_type in (N'potencial_client') ) AND ( accounts_cstm.print_srv_prov_c in (N'itel'))) AND accounts.deleted=0
) AS a
WHERE row_number > 20::: [Microsoft][SQL Server Native Client 10.0][SQL Server]The column 'print_srv_prov_c' was specified multiple times for 'a'.
All fields were added using Studio tool, and source code has been intact.
sql error is happening, because column accounts_cstm.print_srv_prov_c is used two times. Why this is happening and how to fix it.
I am Using SugarCRM CE 6.4.4

Related

SQL code using 'as' resulting in not being able to run

SQL microsoft server
I’m quite close with this piece of code… having said that I haven’t got it to run successfully yet… there seems to be issues with me naming ‘tableabc’ (At the end of the first ‘from’).
Ie the line: WHERE R.PE.d_R_month = (SELECT MAX(d_R_month) FROM R.PE)) AS tableabc
The error I am getting is: “The column 'id' was specified multiple times for 'tableabc'.”
Just for a bit of context, the code does the following:
‘tableabc’ left joins to A.PS table, based on different conditions (As listed).
(I have named the following output as ‘tableabc’. A new column in the R.PAS table is added (and is a summation of the ‘date and month’ creating a column called ‘newdate.’) It then right joins to another table (R.PE) and there are additional conditions applied (ie the maximum dates are only shown)
Select
A.PS.id,
A.PS.name,
A.PS.current_phase,
A.PS.asset_id,
A.PS.production_category,
tableabc.*
FROM
(select R.PAS.*, R.PE.*, DATEADD(month, [R].[PAS].[months_accelerated], [R].[PE].[d_date]) as 'newdate'
from R.PAS
Right join R.PE
On [R].[PAS].[id] = [R].[PE].[id]
WHERE R.PE.d_R_month = (SELECT MAX(d_R_month) FROM R.PE)) AS tableabc
LEFT JOIN A.PS
ON tableabc.id = A.PS.id
Where A.PS.prod = 'Include' OR A.PS.R_category <> 'Can' or A.PS.R_category <> 'Hold';

Most recent transaction date against a Works Order?

Apologies in advance for what will probably be a very stupid question but I've been using Google to teach myself SQL after making the move from years of using Crystal Reports.
We have Works Orders which can have numerous transactions against them. I want to find the most recent one and have it returned against the Works Order number (which is a unique ID)? I attempted to use MAX but that just returns whatever the Transaction Date for that record is.
I think my struggles may be caused by a lack of understanding of grouping in SQL. In Crystal it was just 'choose what to group by' but for some reason in SQL I seem to be forced to group by all selected fields.
My ultimate goal is to be able to compare the planned end date of the Works Order ("we need to finish this job by then") vs when the last transaction was booked against the Works Order, so that I can create an OTIF KPI.
I've attached an image of what I'm currently seeing in SQL Server 2014 Management Studio and below is my attempt at the query.
SELECT wip.WO.WO_No
, wip.WO.WO_Type
, stock.Stock_Trans_Log.Part_No
, stock.Stock_Trans_Types.Description
, stock.Stock_Trans_Log.Qty_Change
, stock.Stock_Trans_Log.Trans_Date
, wip.WO.End_Date
, wip.WO.Qty - wip.WO.Qty_Stored AS 'Qty remaining'
, MAX(stock.Stock_Trans_Log.Trans_Date) AS 'Last Production Receipt'
FROM stock.Stock_Trans_Log
INNER JOIN production.Part
ON stock.Stock_Trans_Log.Part_No = production.Part.Part_No
INNER JOIN wip.WO
ON stock.Stock_Trans_Log.WO_No = wip.WO.WO_No
INNER JOIN stock.Stock_Trans_Types
ON stock.Stock_Trans_Log.Tran_Type = stock.Stock_Trans_Types.Type
WHERE (stock.Stock_Trans_Types.Type = 10)
AND (stock.Stock_Trans_Log.Store_Code <> 'BI')
GROUP BY wip.WO.WO_No
, wip.WO.WO_Type
, stock.Stock_Trans_Log.Part_No
, stock.Stock_Trans_Types.Description
, stock.Stock_Trans_Log.Qty_Change
, stock.Stock_Trans_Log.Trans_Date
, wip.WO.End_Date
, wip.WO.Qty - wip.WO.Qty_Stored
HAVING (stock.Stock_Trans_Log.Part_No BETWEEN N'2Z' AND N'9A')
Query + results
If my paraphrase is correct, you could use something along the following lines...
WITH
sequenced_filtered_stock_trans_log AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY WO_No
ORDER BY Trans_Date DESC) AS reversed_sequence_id
FROM
stock.Stock_Trans_Log
WHERE
Type = 10
AND Store_Code <> 'BI'
AND Part_No BETWEEN N'2Z' AND N'9A'
)
SELECT
<stuff>
FROM
sequenced_filtered_stock_trans_log AS stock_trans_log
INNER JOIN
<your joins>
WHERE
stock_trans_log.reversed_sequence_id = 1
First, this will apply the WHERE clause to filter the log table.
After the WHERE clause is applied, a sequence id is calculated. Restarting from one for each partition (each WO_No), and starting from the highest Trans_Date.
Finally, that can be used in your outer query with a WHERE clause that specifies that you only want the records with sequence id one, this it the most recent row per WO_No. The rest of the joins on to that table would proceed as normal.
If there is any other filtering that should be done (through joins or any other means) that should all be done before the application of the ROW_NUMBER().

How to get a query definition from Cognos?

Is it possible to view the SQL used in Cognos's queries?
e.g. To get the XML definition of a report you can use the below SQL (copied from https://stackoverflow.com/a/24335760/361842):
SELECT CMOBJNAMES.NAME AS ObjName
, CMOBJECTS.PCMID
, CMCLASSES.NAME AS ClassName
, cast(CMOBJPROPS7.spec as xml) ReportDefinition
FROM CMOBJECTS
INNER JOIN CMOBJNAMES ON CMOBJECTS.CMID = CMOBJNAMES.CMID
INNER JOIN CMCLASSES ON CMOBJECTS.CLASSID = CMCLASSES.CLASSID
LEFT OUTER JOIN CMOBJPROPS7 ON CMOBJECTS.CMID = CMOBJPROPS7.CMID
WHERE CMOBJECTS.CLASSID IN (10, 37)
ORDER BY CMOBJECTS.PCMID;
... and from that XML you can often find sqltext elements giving the underlying SQL. However, where existing queries are being used it's hard to see where that data's coming from.
I'd like the equivalent of the above SQL to find Query definitions; though so far have been unable to find any such column.
Failing that, is there a way to find this definition through the UI? I looked under Query Studio and found the query's lineage which gives some information about the query columns, but doesn't make the data's source clear.
NB: By query I'm referring to those such as R5BZDDAN_GRAPH in the below screenshot from Query Studio:
... which would be referred to in a Cognos report in a way such as:
<query name="Q_DEMO">
<source>
<model/>
</source>
<selection autoSummary="false">
<dataItem aggregate="none" name="REG_REG" rollupAggregate="none">
<expression>[AdvRepData].[Q_R5BZDDAN_GRAPH].[REG_REG]</expression>
</dataItem>
<dataItem aggregate="none" name="REG_ORG" rollupAggregate="none">
<expression>[AdvRepData].[Q_R5BZDDAN_GRAPH].[REG_ORG]</expression>
</dataItem>
<!-- ... -->
UPDATE
For the benefit of others, here's an amended version of the above code for pulling back report definitons:
;with recurse
as (
select Objects.CMID Id, ObjectClasses.Name Class, ObjectNames.NAME Name
, cast('CognosObjects' as nvarchar(max)) ObjectPath
from CMOBJECTS Objects
inner join CMOBJNAMES ObjectNames
on ObjectNames.CMID = Objects.CMID
and ObjectNames.IsDefault = 1 --only get 1 result per object (could filter on language=English (LocaleId=24 / select LocaleId from CMLOCALES where Locale = 'en'))
inner join CMCLASSES ObjectClasses on ObjectClasses.CLASSID = Objects.CLASSID
where Objects.PCMID = objects.CMID --cleaner than selecting on root since not language sensitive
--where ObjectClasses.NAME = 'root'
union all
select Objects.CMID Id, ObjectClasses.Name Class, ObjectNames.NAME Name
, r.ObjectPath + '\' + ObjectNames.NAME ObjectPath --I use a backslash rather than forward slash as using this to build a windows path
from recurse r
inner join CMOBJECTS Objects
on objects.PCMID = r.Id
and Objects.PCMID != objects.CMID --prevent ouroboros
inner join CMOBJNAMES ObjectNames
on ObjectNames.CMID = Objects.CMID
and ObjectNames.IsDefault = 1 --only get 1 result per object (could filter on language=English (LocaleId=24 / select LocaleId from CMLOCALES where Locale = 'en'))
inner join CMCLASSES ObjectClasses
on ObjectClasses.CLASSID = Objects.CLASSID
)
select *
from recurse
where Class in ('report','query')
order by ObjectPath
Terminology:
Query Subject can be considered a table
Query Item can be considered a column
For your example the SQL might be defined in the R5BZDDAN_GRAPH query subject which is in turn defined in the Framework Manager model. The framework manager model is defined in a .cpf file which isn't in the content store at all. (it is an XML file though). This file is 'published' to Cognos to make packages.
There is also a cached version of the framework manager file on the actual cognos server (a .cqe file) although it is generally not recommended to rely on this
I say your SQL might be defined. If the query subject is a SQL query subject then that is where it is defined. If If the query subject is a model query subject then it is just a list of query items from other query subjects. These might be from many other query subjects which then have joins defined in Framework Manager. So there is no actual SQL defined there - it gets generated at run time
I'm not sure of your end requirement but there are three other ways to get SQL:
In Report Studio you can 'show generated SQL' on each query
In Framework Manager you can select one or more query subjects and show generated SQL
You can use a monitoring tool on your database to see what SQL is being submitted
If you just want to know how numbers are generated in your report, the most direct solution is to monitor your database.
Lastly keep in mind that in some rare cases, SQL defined in Framework Manager might be altered by the way the report is written

Type conversion failure in update query

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

sql query is too complex

Please help me to simplify this complex query statement because when I run the code it will always end up with "Query is too complex" error
SELECT tblTimeReport.DBIDTimeReport, tblTimeReport.DBIDHumanResource
, tblHumanResource.FullName, tblHumanResource.Title, tblTimeReport.DBIDProject
, tblProject.ProjectID, tblProject.ProjectName, tblTimeReport.DBIDActivity
, tblActivity.ActivityID, tblActivity.ActivityName, tblTimeReport.DateTR AS TRDate
, tblTimeReport.StartTime, tblTimeReport.Duration, tblTimeReport.HourlyRate
, Sum([Duration]*[HourlyRate]) AS Cost, tblTimeReport.Comments, tblTimeReport.Deleted
, tblTimeReport.InputBy, tblHumanResourceInput.[FullName] AS InputByFullName
, tblTimeReport.InputDate
FROM (((tblTimeReport
LEFT JOIN tblHumanResource
ON tblTimeReport.DBIDHumanResource = tblHumanResource.DBIDHumanResource)
LEFT JOIN tblHumanResource AS tblHumanResourceInput
ON tblTimeReport.InputBy = tblHumanResourceInput.DBIDHumanResource)
LEFT JOIN tblProject
ON tblTimeReport.DBIDProject = tblProject.DBIDProject)
LEFT JOIN tblActivity ON tblTimeReport.DBIDActivity = tblActivity.DBIDActivity
%WhereCondition%
GROUP BY tblTimeReport.DBIDTimeReport, tblTimeReport.DBIDHumanResource
, tblHumanResource.FullName, tblHumanResource.Title, tblTimeReport.DBIDProject
, tblProject.ProjectID, tblProject.ProjectName, tblTimeReport.DBIDActivity
, tblActivity.ActivityID, tblActivity.ActivityName, tblTimeReport.DateTR
, tblTimeReport.StartTime, tblTimeReport.Duration, tblTimeReport.HourlyRate
, tblTimeReport.Comments, tblTimeReport.Deleted, tblTimeReport.InputBy
, tblHumanResourceInput.FullName, tblTimeReport.InputDate
Thanks in advance
If this is for a report, as the names of your columns imply, you my be experiencing this:
Reports create temporary queries for each section of the report,
including the report header, page header, group header, detail
section, group footer, page footer, and report footer. All of the
temporary queries for each report are combined into a segmented
virtual table (SVT). The final output must be compiled within the 64K
segment limit.
http://support.microsoft.com/kb/103429
So even if your query is compiled within the limits, its size is multiplied by number of sections. The article implies newer versions of Access have increased dynamic limit, which might help.
You could try creating a view for the group by aggregation, and leave out all of the name columns, and only include the critical IDs. Then use a join against the view to pull in names. I'm not sure though if this would help because I don't know if the contents of the view get pulled in whenever Access compiles the query.