SQL Server 'FOR XML PATH' in PHP/MySQL - sql

I have a SQL Server query that I need to convert to MySQL. I've never used SQL Server/T-SQL before, so I have no experience with FOR XML PATH. There's surprisingly little documentation on this sort of thing. If I remove the FOR XML PATH statement, MySQL returns the error "Operand should contain at least 1 column(s)."
It seems like the nested SELECT statements return strings containing raw XML data, but I don't have the original SQL Server database available to test that theory. I could emulate this effect easily if I knew the return schema.
The original query is below:
SELECT
har_autos.*, har_automodels.Model, har_automodels.MakeID, har_automakes.Make, har_autotypes.AutoType,
(SELECT har_notes.*, har_notelinks.Value
FROM har_notes
JOIN har_notelinks on (har_notes.ID = har_notelinks.NoteID)
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 1)
AS 'UserNotes',
(SELECT har_notes.*, har_notelinks.Value
FROM har_notes
JOIN har_notelinks on (har_notes.ID = har_notelinks.NoteID)
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 2)
AS 'EngineeringNotes'
FROM har_autos
LEFT JOIN har_automodels ON (har_autos.ModelID = har_automodels.ID)
LEFT JOIN har_automakes ON (har_automodels.MakeID = har_automakes.ID)
LEFT JOIN har_autotypes ON (har_autos.AutoTypeID = har_autotypes.ID)
UPDATE:
I rebuilt the query in its entirety and it now returns "Not unique table/alias: 'har_automakes'."
SELECT
har_autos.*,
har_automodels.Model,
har_automodels.MakeID,
har_automakes.Make,
har_autotypes.AutoType,
(SELECT
har_notes.ID,
har_notes.NoteTypeID,
har_notes.Text001,
har_notelinks.Value,
har_notelinks.AutoID,
har_autos.ID
FROM
har_notes
INNER JOIN har_notelinks ON (har_notes.ID = har_notelinks.NoteID),
har_autos
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 1)
AS UserNotes,
(SELECT
har_notes.ID,
har_notes.NoteTypeID,
har_notes.Text001,
har_notelinks.Value,
har_notelinks.AutoID,
har_autos.ID
FROM
har_notes
INNER JOIN har_notelinks ON (har_notes.ID = har_notelinks.NoteID), har_autos
WHERE har_autos.ID = har_notelinks.AutoID AND har_notes.NoteTypeID = 2)
AS EngineeringNotes
FROM
har_autos
LEFT OUTER JOIN har_automodels ON (har_autos.ModelID = har_automodels.ID)
LEFT OUTER JOIN har_autotypes ON (har_autos.AutoTypeID = har_autotypes.ID),
har_automakes
LEFT OUTER JOIN har_automakes ON (har_automakes.MakeID = har_automakes.ID)
WHERE
ID = 1
This is the original T-SQL query (really sorry about all of these):
SELECT a.*, mo.Model, mo.MakeID, ma.Make, at.AutoType,
(SELECT n.*, nl.Value
FROM dbo.Notes n
JOIN dbo.NoteLinks nl on (n.ID = nl.NoteID)
WHERE a.ID = nl.AutoID AND n.NoteTypeID = 1
FOR XML PATH('Note'), TYPE
) AS 'UserNotes',
(SELECT n.*, nl.Value
FROM dbo.Notes n
JOIN dbo.NoteLinks nl on (n.ID = nl.NoteID)
WHERE a.ID = nl.AutoID AND n.NoteTypeID = 2
FOR XML PATH('Note'), TYPE
) AS 'EngineeringNotes'
FROM dbo.Autos a
LEFT JOIN dbo.AutoModels mo ON (a.ModelID = mo.ID)
LEFT JOIN dbo.AutoMakes ma ON (mo.MakeID = ma.ID)
LEFT JOIN dbo.AutoTypes at ON (a.AutoTypeID = at.ID)

Little Documentation:
Using PATH Mode
Basic Syntax of the FOR XML Clause
Examples: Using PATH Mode
Constructing XML Using FOR XML
All of the above are from the following search: http://social.technet.microsoft.com/Search/en-US/?Refinement=129&Query=for+xml+path.

Related

How can I transform this Code with Joins instead of using +(=) or =

I was trying Hours to transform this code(I think It is Oracle) with joins in SQL Server instead of
using +(=)/= but somehow i dont get the same result as using += or =. Can please someone help me out ?
SELECT KNVV.MANDT, KNVV.KUNNR,
KNVV.VTWEG, KNVV.BZIRK, KNVP.KUNN2
FROM KNA1, KNVV, KNVP, CDHDR
WHERE
AND `(KNVV.MANDT = KNA1.MANDT`
AND `KNVV.KUNNR = KNA1.KUNNR)`
AND `(KNVP.MANDT (+) = KNVV.MANDT`
AND `KNVP.KUNNR (+) = KNVV.KUNNR`
AND `KNVP.VTWEG (+) = KNVV.VTWEG)`
AND `(CDHDR.MANDANT (+) = KNVV.MANDT`
AND `CDHDR.OBJECTID (+) = KNVV.KUNNR)`
AND `(KNVV.VTWEG = KNVP.VTWEG)`
AND `KNVP.PARVW (+) = 'RG'`
AND CDHDR.OBJECTCLAS (+) = 'DEBI'
Without sample data its hard to test but it appears you want:
SELECT KNVV.MANDT, KNVV.KUNNR,
KNVV.VTWEG, KNVV.BZIRK, KNVP.KUNN2
FROM KNA1
INNER JOIN KNVV
ON ( KNVV.MANDT = KNA1.MANDT
AND KNVV.KUNNR = KNA1.KUNNR
)
INNER JOIN KNVP
ON ( KNVP.MANDT = KNVV.MANDT
AND KNVP.KUNNR = KNVV.KUNNR
AND KNVP.VTWEG = KNVV.VTWEG
AND KNVV.VTWEG = KNVP.VTWEG -- This duplicated condition makes it an
-- INNER JOIN and not a LEFT OUTER JOIN as
-- you are not using (+) in Oracle's legacy
-- syntax.
AND KNVP.PARVW = 'RG'
)
LEFT OUTER JOIN CDHDR
ON ( CDHDR.MANDANT = KNVV.MANDT
AND CDHDR.OBJECTID = KNVV.KUNNR
AND CDHDR.OBJECTCLAS = 'DEBI'
)
What you probably want is to make the second join a LEFT OUTER JOIN instead of an INNER JOIN but that is not what your current query is doing as you are using (KNVV.VTWEG = KNVP.VTWEG) rather than (KNVV.VTWEG = (+) KNVP.VTWEG) in your Oracle query.
You query can be converted to:
select
KNVV.MANDT,
KNVV.KUNNR,
KNVV.VTWEG,
KNVV.BZIRK,
KNVP.KUNN2
from KNA1
join KNVV on KNVV.MANDT = KNA1.MANDT
and KNVV.KUNNR = KNA1.KUNNR
left join KNVP on KNVP.MANDT = KNVV.MANDT -- left join defeated!
and KNVP.KUNNR = KNVV.KUNNR
and KNVP.VTWEG = KNVV.VTWEG
and KNVP.PARVW = 'RG'
left join CDHDR on CDHDR.MANDANT = KNVV.MANDT
and CDHDR.OBJECTID = KNVV.KUNNR
and CDHDR.OBJECTCLAS = 'DEBI'
where KNVV.VTWEG = KNVP.VTWEG -- silently converts left join into inner join
NOTE: the original query is malformed
Even though the original query tried to perform a left join against the table KNVP this join is silently converted into an inner join by the engine. Are you sure this query is working as expected?
Did you try inner JOIN?
SELECT KNVV.MANDT, KNVV.KUNNR,
KNVV.VTWEG, KNVV.BZIRK, KNVP.KUNN2
FROM KNA1, KNVV, KNVP, CDHDR
INNER JOIN KNVV ON KNA1.MANDT=KNVV.MANDT;
Something in this sense its more convenient.
documentation bellow
https://www.w3schools.com/sql/sql_join.asp

Speed up SQL query performance with nested queries

Could anyone help me speed this query up? It currently take 17 minutes to run but does return the correct data and it populates a subform in MS Access. Functions in the rest of the VBA are declared as long to try to speed up more.
Here's the full query:
SELECT lots of things
FROM (((((((((((((((ngstest
INNER JOIN patients
ON ngstest.internalpatientid = patients.internalpatientid)
INNER JOIN referral
ON ngstest.referralid = referral.referralid)
INNER JOIN checker
ON ngstest.bookby = checker.check1id)
INNER JOIN ngspanel
ON ngstest.ngspanelid = ngspanel.ngspanelid)
LEFT JOIN ngspanel AS ngspanel_1
ON ngstest.ngspanelid_b = ngspanel_1.ngspanelid)
INNER JOIN status
ON ngstest.statusid = status.statusid)
INNER JOIN dbo_patient_table
ON patients.patientid = dbo_patient_table.patienttrustid)
LEFT JOIN dna
ON ngstest.dna = dna.dnanumber)
INNER JOIN status AS status_1
ON patients.s_statusoverall = status_1.statusid)
LEFT JOIN gw_gendertable
ON dbo_patient_table.genderid = gw_gendertable.genderid)
LEFT JOIN ngswesbatch
ON ngstest.wesbatch = ngswesbatch.ngswesbatchid)
LEFT JOIN checker AS checker_1
ON ngstest.check1id = checker_1.check1id)
LEFT JOIN checker AS checker_2
ON ngstest.check2id = checker_2.check1id)
LEFT JOIN checker AS checker_3
ON ngstest.check3id = checker_3.check1id)
LEFT JOIN ngspanel AS ngspanel_2
ON ngstest.ngspanelid_c = ngspanel_2.ngspanelid)
LEFT JOIN checker AS checker_4
ON ngstest.check4id = checker_4.check1id
WHERE ((ngstest.referralid IN
(SELECT referralid FROM referral
WHERE grouptypeid = 14)
AND ngstest.ngstestid IN
(SELECT ngstest.ngstestid
FROM ngsanalysis
INNER JOIN ngstest
ON ngsanalysis.ngstestid = ngstest.ngstestid
WHERE ngsanalysis.pedigree = 3302) )
AND status.statusid = 1202218800)
ORDER BY ngstest.priority,
ngstest.daterequested;
The two nested queries are strings from elsewhere in the code so are called in the vba as " & includereferralls & " And " & ParentsStatusesFilter & "
They are:
ParentsStatusesFilter = "NGSTest.NGSTestID in
(SELECT NGSTest.NGSTestID
FROM NGSAnalysis
INNER JOIN NGSTest
ON NGSAnalysis.NGSTestID = NGSTest.NGSTestID
WHERE NGSAnalysis.Pedigree IN (3302,3303,3304)"
And
includereferrals = "NGSTest.ReferralID
(SELECT referralid FROM referral WHERE referral.grouptypeid = 14)"
The query needs to remain readable (and therefore editable) so can't use things like Distinct, Group By or contain any Unions. Have tried Exists instead of In for the nested queries but that stops it from actually filtering the results.
WHERE EXISTS (SELECT NGSTest.NGSTestID
FROM NGSAnalysis
INNER JOIN NGSTest
ON NGSAnalysis.NGSTestID = NGSTest.NGSTestID
WHERE NGSAnalysis.Pedigree IN (3302,3303,3304)
So the exist clause you have there isn't tied to the outer query which would run similar to just added 1 = 1 to the where clause. I took your where clause and converted it. It should look something like this...
WHERE EXISTS (
SELECT referralid
FROM referral
WHERE grouptypeid = 14 AND ngstest.referralid = referral.referralid)
AND EXISTS (
SELECT ngsanalysis.ngstestid
FROM ngsanalysis
WHERE ngsanalysis.pedigree IN (3302,3303,3304) AND ngstest.ngstestid = ngsanalysis.ngstestid
)
AND status.statusid = 1202218800
Adding exists will speed it up a bit, but the the bulk of the slowness is the left joins. Access does not handle the left joins as well as SQL Server does. Change all your joins to inner joins and you will see the query runs very fast. This is obviously not ideal since some relationships are optional. What I have done to get around this is add a default record that replaces a null relationship.
Here is what that looks like for you: In the checker table you could add a record that represents a null value. So put a record into the checker table with check1id of -1 or 0. Then default check1id, check2id, check3id on ngstest to -1 or 0. You will need to do that type of thing for all tables you need to left join on.

JOIN syntax and order for multiple tables

SQL Gurus,
I have a query that uses the "old" style of join syntax as follows using 7 tables (table and column names changed to protect the innocent), as shown below:
SELECT v1_col, p1_col
FROM p1_tbl, p_tbl, p2_tbl, p3_tbl, v1_tbl, v2_tbl, v3_tbl
WHERE p1_code = 1
AND v1_code = 1
AND p1_date >= v1_date
AND p_uid = p1_uid
AND p2_uid = p1_uid AND p2_id = v2_id
AND p3_uid = p1_uid AND p3_id = v3_id
AND v2_uid = v1_uid
AND v3_uid = v1_uid
The query works just fine and produces the results it is supposed to, but as an academic exercise, I tried to rewrite the query using the more standard JOIN syntax, for example, below is one version I tried:
SELECT V1.v1_col, P1.p1_col
FROM p1_tbl P1, v1_tbl V1
JOIN p_tbl P ON ( P.p_uid = P1.p1_uid )
JOIN p2_tbl P2 ON ( P2.p2_uid = P1.p1_uid AND P2.p2_id = V2.v2_id )
JOIN p3_tbl P3 ON ( P3.p3_uid = P1.p1_uid AND P3.p3_id = V3.v3_id )
JOIN v2_tbl V2 ON ( V2.v2_uid = V1.v1_uid )
JOIN v3_tbl V3 ON ( V3.v3_uid = V1.v1_uid )
WHERE P1.p1_code = 1
AND V1.v1_code = 1
AND P1.p1_date >= V1.v1_date
But, no matter how I arrange the JOINs (using MS SQL 2008 R2), I keep running into the error:
The Multi-part identifier "col-name" could not be bound,
where "col-name" varies depending on the order of the JOINs I am attempting...
Does anyone have any good examples on how use the JOIN syntax with this number of tables??
Thanks in advance!
When you use JOIN-syntax you can only access columns from tables in your current join or previous joins. In fact it's easier to write the old syntax, but it's more error-prone, e.g. you can easily forget a join-condition.
This should be what you want.
SELECT v1_col, p1_col
FROM p1_tbl
JOIN v1_tbl ON p1_date >= v1_date
JOIN v2_tbl ON v2_uid = v1_uid
JOIN v3_tbl ON v3_uid = v1_uid
JOIN p_tbl ON p_uid = p1_uid
JOIN p2_tbl ON p2_uid = p1_uid AND p2_id = v2_id
JOIN p3_tbl ON p3_uid = p1_uid AND p3_id = v3_id
WHERE p1_code = 1
AND v1_code = 1
You are not naming the tables in your join such that it doesn't know which column is from which table. Try something like:
SELECT a.v1_col, b.p1_col
FROM p1_tbl b
JOIN p_tbl a ON b.p_uid = a.p1_uid
WHERE b.p1_code = 1
From your query above, I am assuming a naming convention of p2_uid comes from p2_tbl. Below id my best interpretation of WHERE joins to using INNER joins.
SELECT
v1_col, p1_col
FROM
p1_tbl
INNER JOIN p1_tbl
ON p1_tbl.p1_date >= v1_tbl.v1_date
INNER JOIN p_tbl
ON p_tbl.p_uid = p1_tbl.p1_uid
INNER JOIN p2_tbl
ON p2_tbl.p2_uid = p1_tbl.p1_uid
INNER JOIN v2_tbl
ON p2_tbl.p2_id = v2_tbl.v2_id
INNER JOIN p3_tbl
ON p3_tbl.p3_uid = p1_tbl.p1_uid
INNER JOIN v3_tbl
ON p3_tbl.p3_id = v3_tbl.v3_id
INNER JOIN v1_tbl
ON v1_tbl.v1_uid = v2_tbl.v2_uid
AND v1_tbl.v1_uid = v3_tbl.v2_uid
WHERE
p1_code = 1
AND
v1_code = 1
Some general points I have found useful in SQL statements with many joins.
Always fully qualify the names. I.e dont use ID , rahter use
TableName.ID
Dont use aliases unless there is meaning. (I.e. joining a table to
its self where aliasing is needed.)

Select from named query

I have issue with following query in MS Access 2013:
SELECT *
FROM (((
(SELECT Stop.TAGeoID AS TAGeoID,
Trip.TripNo AS TripNo
FROM ((((Trip
INNER JOIN BTStopTimes ON Trip.TripNo = BTStopTimes.TripNumber)
INNER JOIN TripsTxt ON (Trip.TripNo = TripsTxt.trip_id
AND Left(TripsTxt.shape_id, 3) <> 'ELD'))
INNER JOIN PatternDetail ON Trip.PatternID = PatternDetail.PatternID
AND BTStopTimes.Sequence = PatternDetail.StopSortOrder)
INNER JOIN Stop ON Stop.GeoID = PatternDetail.GeoID)) AS t3
LEFT JOIN
(SELECT F10,
F16
FROM PatternStopsRaw
WHERE F16 <> ''
GROUP BY F10,
F16)R ON R.F10 = t3.TAGeoID)
LEFT JOIN Timepoint ON Timepoint.PlaceID = R.F16)
LEFT JOIN
(SELECT *
FROM t3
INNER JOIN TripDetail ON t3.TripNo = TripDetail.TripNo)TripTripDetail ON (Timepoint.TimePointID = TripTripDetail.TimepointID))
It says that there is syntax error
The Microsoft Access database engine cannot find the input table or query t3. Make sure it exists and that its name is spelled correctly
It seems that Access does not allow selecting from the named query in the same query.
Any suggestions?
PS: * are used only for testing purposes, I'll remove them once I have working query.
Yes, it appears that Access does not recognize the [t3] alias at that level. Perhaps you could try creating the query...
SELECT Stop.TAGeoID AS TAGeoID,
Trip.TripNo AS TripNo
FROM ((((Trip
INNER JOIN BTStopTimes ON Trip.TripNo = BTStopTimes.TripNumber)
INNER JOIN TripsTxt ON (Trip.TripNo = TripsTxt.trip_id
AND Left(TripsTxt.shape_id, 3) <> 'ELD'))
INNER JOIN PatternDetail ON Trip.PatternID = PatternDetail.PatternID
AND BTStopTimes.Sequence = PatternDetail.StopSortOrder)
INNER JOIN Stop ON Stop.GeoID = PatternDetail.GeoID)
...as a saved query in Access, name it [t3], and then just reference that in your main query.

SQL Server update with joins

I am trying to update a date in a table, based off of a MAX(date) in another table. To get the correct data to link up, I have to do 2 inner joins and 2 left outer joins.
I can select the correct data, it returns a Guid (PersonId) and the Date.
I have to use this information to update my original table. I am having trouble getting this to work, I still getting syntax errors.
update tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
set assign.dtmQAPCLed = dtmTaken
from
(
select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken from tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as types on types.intcerttypeid = certs.intcerttypeid
where types.intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid as data
)
where data.guidpersonid = team.guidpersonid
Assuming you are using SQL Server for this, then this should work:
UPDATE A
SET A.dtmQAPCLed = dtmTaken
FROM tblqualityassignments AS A
INNER JOIN tblrequirementteams as T
ON A.guidmemberid = T.guidmemberid
INNER JOIN (select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken
from tblqualityassignments as assign
inner join tblrequirementteams as team
on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg
on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs
on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as [types]
on [types].intcerttypeid = certs.intcerttypeid
where [types].intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid) data
ON T.guidpersonid = data.guidpersonid