I want to create a Crystal report that takes values from 3 MySQL tables but I want to write my custom query because when I select tables in visual studio it shows a query that doesn't meet what I expected.
I tested query on localhost phpMyAdmin and it worked.
note 1: I am using visual studio 2019 and installed Crystal report SP26
note 2: when visual studio wrote the query it added "1" to the table name
like boq_table make it boq_table1.
code below represents what query I want to execute:
SELECT
ubc.boq_table.itemNum,
ubc.boq_table.descriptionOfWork,
ubc.boq_table.unit,
ubc.boq_table.contractualQuantity,
ubc.boq_table.priceNum,
ubc.summary.executedQuantLastSummary,
ubc.summary.priceLastWorks,
ubc.summary.executedQuantBetw2Sum,
ubc.submittal.priceCurrentWorks
FROM
ubc.boq_table
LEFT OUTER JOIN ubc.summary ON
ubc.boq_table.itemNum = ubc.summary.itemNum
LEFT OUTER JOIN ubc.submittal ON
ubc.boq_table.itemNum = ubc.submittal.itemNum
WHERE
ubc.boq_table.projectName ='proj'
UNION
SELECT
ubc.boq_table.itemNum,
ubc.boq_table.descriptionOfWork,
ubc.boq_table.unit,
ubc.boq_table.contractualQuantity,
ubc.boq_table.priceNum,
ubc.summary.executedQuantLastSummary,
ubc.summary.priceLastWorks,
ubc.summary.executedQuantBetw2Sum,
ubc.submittal.priceCurrentWorks
FROM
ubc.summary
LEFT OUTER JOIN ubc.boq_table ON
ubc.summary.itemNum = ubc.boq_table.itemNum
LEFT OUTER JOIN ubc.submittal ON
ubc.summary.itemNum = ubc.submittal.itemNum
WHERE
ubc.summary.projectName = 'proj'
UNION
SELECT
ubc.boq_table.itemNum,
ubc.boq_table.descriptionOfWork,
ubc.boq_table.unit,
ubc.boq_table.contractualQuantity,
ubc.boq_table.priceNum,
ubc.summary.executedQuantLastSummary,
ubc.summary.priceLastWorks,
ubc.summary.executedQuantBetw2Sum,
ubc.submittal.priceCurrentWorks
FROM
ubc.submittal
LEFT OUTER JOIN ubc.boq_table ON
ubc.submittal.itemNum = ubc.boq_table.itemNum
LEFT OUTER JOIN ubc.summary ON
ubc.submittal.itemNum = ubc.summary.itemNum
WHERE
ubc.submittal.projectName = 'proj'
ORDER BY
itemNum;
and this code was written by the visual studio after I selected tables
SELECT boq_table1.itemNum, boq_table1.descriptionOfWork, boq_table1.unit, boq_table1.contractualQuantity, boq_table1.priceNum, submittal1.priceCurrentWorks, summary1.executedQuantLastSummary, summary1.executedQuantBetw2Sum, summary1.priceLastWorks
FROM (ubc.boq_table boq_table1 INNER JOIN ubc.submittal submittal1 ON boq_table1.ID=submittal1.ID) INNER JOIN ubc.summary summary1 ON boq_table1.ID=summary1.ID
ORDER BY boq_table1.itemNum
the last code read-only I can't edit
You can do 2 things if you want to get data to crystal reprots, you look again in the database selector, there is an option for adding a command or use the connector and a datareader to build a dataset that you can use.
The first is ok, if you don't need to compute the data heavily.
Related
I have a MySQL statement which when executed in MySQL workbench, successfully returns a result. The statement in question is below:
SELECT report.*, client.`Client`, 3rdpartyreport.`Status` FROM report
LEFT JOIN client ON report.ClientID = client.ID
LEFT JOIN 3rdpartyreport ON (report.`Serial No` = 3rdpartyreport.`Serial No` AND report.`AssignedTo` = 3rdpartyreport.`Received By`)
WHERE (report.Status <> 'Deleted')
ORDER BY report.`Serial No` DESC
I am joining a column from the client table and the 3rdparty table to the report table. However, when I input this exact query into Visual Studio 2013 Dataset designer, it gives the error:
Error in join expression. Unable to parse query text.
As soon as I remove one of the LEFT JOIN lines, it works ok. Why doesn't Visual Studio allow multiple left joins? Am I doing something wrong, and if not then is there another way I can achieve the same result?
Figured it out. I needed to have 3rdpartyreport written as '3rdpartyreport', because it starts with a digit. Workbench doesn't need this apparently.
I am issuing an SQL query in my visual studio application to search a database. I am having trouble joining two tables into one query search. I am looking to combine the First and Last name of an employee and match it up to a phone number. In one table I have an abbreviated name, and on the other table I have the full name.
Example
Code
cmdTxt.Append("SELECT partner.staffid, staff.Forename,staff.surname FROM tblpartner LEFT JOIN tblstaff ON staff.StaffID = partner.staffid ORDER BY staff.forename , staff.surname ");
I would Like to have a field that shows the Forename , surename, and phone number in one query search
You are using "partner" and "staff" as table aliases, but you aren't assigning those aliases to the table names. If you want to use those instead of the full table names, you need:
SELECT partner.staffid, staff.Forename,staff.surname, partner.phone
FROM tblpartner partner
LEFT JOIN tblstaff staff ON staff.StaffID = partner.staffid
ORDER BY staff.forename , staff.surname
As general advice, get your query working outside of your string literal; enter it into SQL Server Management Studio or something similar. Had you done that, it should have clearly showed you what the problem was. Then when you have your query set how you want, you can copy it into your Command Text in your Visual Studio application.
I recently asked a question on StackOverflow (MySQL Returns All Rows When field = 0) regarding a query statement not working in MySQL. I now have a very similar problem, this time using OleDB where I am trying to use a join to include fields that have 0 as an entry, but not select every field in the table as a result.
The new look MySQL query posted in the above question as the accepted answer works without a hitch. However the OleDB counterpart I have written to do almost the same does not. It's a bit messy as the tables are not named very well (I didn't create this database) and I'm getting a simple syntax error;
myQuery.CommandText = "SELECT s.scm_num, s.scm_name, c.cr3_text, q.qsp_value, s.scm_bprefix, s.scm_nxbnum FROM qspreset q INNER JOIN sdccomp s LEFT OUTER JOIN cntref3 c ON s.scm_cntref3=c.cr3_id AND q.qsp_relat=s.scm_invtype AND q.qsp_what=13";
I'm querying another table here as well as the two involved in the LEFT OUTER JOIN and I believe that is where I am making the mistake.
Join conditions need to be with the join
myQuery.CommandText =
"SELECT s.scm_num, s.scm_name, c.cr3_text, q.qsp_value, s.scm_bprefix, s.scm_nxbnum
FROM qspreset q
INNER JOIN sdccomp s
on q.qsp_relat = s.scm_invtype AND q.qsp_what = 13
LEFT OUTER JOIN cntref3 c
ON s.scm_cntref3 = c.cr3_id";
q.qsp_what = 13 can be moved to a where
I happen to like this style
In the case of MSSQL T-SQL and some queries with a lot of joins I have gotten more efficient query plan by moving a where condition up into a join. The filter happened early rather that last.
If you don't believe you can put a hard value in a join see SQLfiddle
I am trying to update my mysql table "upproj" using Visual Studio 2010 (c#).
I want to execute the following query :
string Query = " update tlog.upproj set summ=(select sum(grade) from tlog.upproj group by ams having count(*) =2); ";
But I get error
"You can't specify target table 'upproj' for update in FROM clause".
When I execute the previous query on Mysql Query browser I don't get any error, but my project needs to execute this query from visual studio.
Is there any way to fix that?
As the error hints at, you can't specify the table you're updating in the UPDATE statement's FROM clause (in the sub-query); the same is true if you were to try to use it in a WHERE clause.
You can accomplish the task of "using the table you're updating" if you use a JOIN though.
Based on the schema inferred by your existing query, the ams field is unique to a set grouping and you can, therefore, add that as part of your clause.
Try the following (untested):
UPDATE
tlog.upproj AS u1
JOIN (SELECT ams, SUM(grade) AS grade_sum FROM tlog.upproj GROUP BY ams HAVING COUNT(*) = 2) AS u2
ON u1.ams = u2.ams
SET
summ = u2.grade_sum;
This should update each record for ams with the total sum of grade for that same ams. If a specific ams doesn't have 2 entries, it isn't updated.
1) First issue I'm having is if you do an include and then an order by the SQL generated generates an inner join and an outer join
var query = from l in Lead.Include("Contact")
orderby l.Contact.FirstName
select l;
Which generates the following inner join and outer join on the same table
INNER JOIN [dbo].[Contact] AS [Extent2]
ON [Extent1].[ContactId] = [Extent2].[ContactId]
LEFT OUTER JOIN [dbo].[Contact] AS [Extent3]
ON [Extent1].[ContactId] = [Extent3]. [ContactId]
ORDER BY [Extent2].[FirstName] ASC
Which makes for a slightly inefficient query
2) if I do multiple includes it always does the second one as an outer join so like
Lead.Include("OneToOne").Include("OtherOneToOne") <- in this scenario
OtherOneToOne is an outer
join and OneToOne is an inner
join
Lead.Include("OtherOneToOne").Include("OneToOne") <- in this scenario
OneToOne is an outer join
and OtherOneToOne is an
inner join
is that just how it works?
I found another post where someone was talking about this and they said that it was fixed in the June CTP release
http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx
But I installed and setup that to be used and it still doesn't work..
alright it won't let me answer my own question
so
EDIT:
Alright I setup an isolated test and found that http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx seems to have resolved these
But since I'm using RIA I'm out of luck since the june ctp doesn't support RIA :-/
A solution is to do the include yourself:
var query = from l in Lead
select new { l, l.Contact } into row
orderby row.Contact.FirstName
select row;