How to Convert the SQL Server Query to Equivelent SQLite Query? - sql

I have this query which works in SQL server:
SELECT
T_Cbt.cbtName,
T_HDD.hddName,
T_HDD.ip,
T_HDD.compName,
T_Cbt.mobRack,
T_HDD.size,
T_OS.osName + ' ' + T_OS.osVersion + ' - ' + T_OS.osLanguage AS Expr1,
T_OS.osBit,
T_SWList.swName + ' ' + T_SWDetails.swVersion + ' - ' + T_SWDetails.swLang + ' ' + T_SWDetails.swArch AS Expr2
FROM T_OS INNER JOIN
T_Cbt INNER JOIN
T_HDD ON T_Cbt.cbtId = T_HDD.cbtId ON T_OS.id = T_HDD.osId INNER JOIN
T_Division ON T_HDD.division = T_Division.divId LEFT OUTER JOIN
T_SWDetails INNER JOIN
T_SW ON T_SWDetails.sdPk = T_SW.swdid INNER JOIN
T_SWList ON T_SWDetails.swdId = T_SWList.swId ON T_HDD.hddid = T_SW.hddid
WHERE T_Cbt.cbtName <> 'FREE'
ORDER BY T_Cbt.purpose
While i am executing this query in Sqlite i get error like Error while executing query: near "ON": syntax error How to fix it?

SQLite does not allow nesting JOIN and ON arbitrarily. Reordering your JOINs to not use nesting should allow the query to execute without a problem;
SELECT
T_Cbt.cbtName, T_HDD.hddName, T_HDD.ip, T_HDD.compName,
T_Cbt.mobRack, T_HDD.size,
T_OS.osName || ' ' || T_OS.osVersion || ' - ' || T_OS.osLanguage AS Expr1,
T_OS.osBit, T_SWList.swName || ' ' || T_SWDetails.swVersion || ' - ' ||
T_SWDetails.swLang || ' ' || T_SWDetails.swArch AS Expr2
FROM T_OS
INNER JOIN T_HDD ON T_OS.id = T_HDD.osId
INNER JOIN T_Cbt ON T_Cbt.cbtId = T_HDD.cbtId
INNER JOIN T_Division ON T_HDD.division = T_Division.divId
INNER JOIN T_SW ON T_HDD.hddid = T_SW.hddid
LEFT OUTER JOIN T_SWDetails ON T_SWDetails.sdPk = T_SW.swdid
INNER JOIN T_SWList ON T_SWDetails.swdId = T_SWList.swId
WHERE (T_Cbt.cbtName <> 'FREE')
ORDER BY T_Cbt.purpose

Related

Oracle SQL Developer query not sorting alphabetically

My query:
SELECT
pmmr.REQUEST_NO, pmmr.item_code itemCode, Pmmr.Form_No Form_No,
NVL(Pmf.Form_Name, Pmmr.Form_No) formName, pmmr.MRN,
NVL(p.FIRST_NAME || DECODE(p.FAMILY_NAME, NULL, '', ' ' || p.FAMILY_NAME),pmmr.MRN) PATIENT_NAME,
pmmr.ASSIGNED_TO, pmmr.DRUG_GENERIC_NAME, pmmr.LAST_STATUS,
NVL(initcap(( hr1.FIRST_NAME || ' ' || hr1.LAST_NAME)), pmmr.LAST_PERFORMER_ID) LastActionBy,
NVL(hr2.DEPARTMENT || ' - ' || hr2.SECTION_NAME,'') ORGANIZATION_UNIT,
NVL(initcap(( hr2.FIRST_NAME || ' ' || hr2.LAST_NAME)), pmmr.REQUESTER_ID) RequesterName,
pmmr.REQUEST_DATE
FROM
inhouse_apps.PHRM_MFRP_MEDICATION_REQUEST pmmr
LEFT OUTER JOIN
inhouse_apps.Hr_Employee hr1 ON Pmmr.Last_Performer_Id = Hr1.Employee_Number
LEFT OUTER JOIN
inhouse_apps.Hr_Employee hr2 ON inhouse_apps.Pmmr.Requester_Id = Hr2.Employee_Number
LEFT OUTER JOIN
EAPPTMT.PATIENT p ON inhouse_apps.Pmmr.Mrn = P.Mrn
LEFT OUTER JOIN
inhouse_apps.Phrm_Mfrp_Form pmf ON Pmmr.Form_No = Pmf.Form_No;
WHERE
LAST_STATUS IN ('Approved')
AND Pmmr.Form_No = 2
ORDER BY
pmmr.DRUG_GENERIC_NAME ASC
I need the DRUG_GENERIC_NAME sorted alphabetically, but it's not returning the result sorted ..
EDIT: as mentioned below the semicolon needed to be removed, that's all
You have semicolon ";" before your "Where clause". You need to remove it from there to make Oracle take account of both your "Where clause" and your "Order by clause"
SELECT pmmr.REQUEST_NO, pmmr.item_code itemCode, Pmmr.Form_No Form_No, nvl(Pmf.Form_Name, Pmmr.Form_No) formName, pmmr.MRN, nvl (p.FIRST_NAME || DECODE(p.FAMILY_NAME, NULL, '', ' ' || p.FAMILY_NAME),pmmr.MRN) PATIENT_NAME,pmmr.ASSIGNED_TO,
pmmr.DRUG_GENERIC_NAME,pmmr.LAST_STATUS, nvl(initcap(( hr1.FIRST_NAME || ' ' || hr1.LAST_NAME)),pmmr.LAST_PERFORMER_ID) LastActionBy,
nvl(hr2.DEPARTMENT || ' - ' || hr2.SECTION_NAME,'') ORGANIZATION_UNIT, nvl(initcap(( hr2.FIRST_NAME || ' ' || hr2.LAST_NAME)),pmmr.REQUESTER_ID) RequesterName, pmmr.REQUEST_DATE
FROM inhouse_apps.PHRM_MFRP_MEDICATION_REQUEST pmmr
left outer join inhouse_apps.Hr_Employee hr1 on Pmmr.Last_Performer_Id = Hr1.Employee_Number
left outer join inhouse_apps.Hr_Employee hr2 on inhouse_apps.Pmmr.Requester_Id = Hr2.Employee_Number
left outer join EAPPTMT.PATIENT p on inhouse_apps.Pmmr.Mrn = P.Mrn
left outer join inhouse_apps.Phrm_Mfrp_Form pmf on Pmmr.Form_No = Pmf.Form_No
WHERE LAST_STATUS IN ('Approved')
and Pmmr.Form_No = 2
order by pmmr.DRUG_GENERIC_NAME ASC
;

ORA-00920 [42000]: invalid relational operator

Here is the SQL Source Code for database selection that generates the following
error: "Query execution failed, Reason: SQL Error [920] [42000]:
ORA-00920: opérateur relationnel non valide.
Please Help!
WITH adr_siege as (
SELECT Nvl(adr.libadr, ' ') AS libadr, Nvl(adr.adress, ' ') AS adress,
Nvl(adr.codpos, ' ') AS codpos, Nvl(adr.locali, ' ') AS locali,
Nvl(adr.codpay, ' ') AS codpay
FROM gnx.tie
JOIN gnx.adr
ON adr.codsoc = tie.codsoc
AND adr.typtie = tie.typtie
AND adr.sigadr = tie.sigtie
AND typadr = 'COM'
WHERE tie.typtie = 'DEP'
AND tie.codsoc=1
AND tie.sigtie = '00000137'
)
SELECT Nvl(a.codpro, ' ') AS codpro, Nvl(a.nompro, ' ') AS nompro,
Nvl(ll.prix_achat, 0) AS prix_achat, Nvl(ll.qte, 0) AS qte,
Nvl(l.nom_fournisseur, ' ') AS nom_fournisseur, Nvl(m.raison, ' ') AS raison,
Nvl(concat(m.adresse1,' ' || m.adresse2), ' ') AS adresse_mag,
Nvl(m.cp, ' ') AS cp, Nvl(m.ville, ' ') AS ville,
Nvl(p.libelle, ' ') AS libelle,
Nvl(To_Char(c.date_validation, 'DD/MM/YYYY'), ' ') AS date_validation,
Nvl((
SELECT sum(fll.prix_achat * fll.qte)
FROM fourniture.frnt_livraison_ligne fll
WHERE fll.noliv
), 0) AS total_prix,
To_Char(SYSDATE, 'DD/MM/YYYY') AS date_edition,
Nvl(f.libadr, ' ') AS fou_lib_adr,
Nvl(f.tel, ' ') AS fou_tel,
Nvl(f.adress, ' ') AS fou_adr, Nvl(f.codpos, ' ') AS fou_cp,
Nvl(f.locali, ' ') AS fou_ville, Nvl(pf.libelle, ' ') AS fou_pays,
Nvl(to_char(p.date_livraison_s1, 'DD/MM/YYYY'), ' ') AS date_livraison_s1,
Nvl(to_char(p.date_livraison_s2, 'DD/MM/YYYY'), ' ') AS date_livraison_s2,
Nvl((
SELECT tbl.lib1
FROM gnx.tie
LEFT JOIN gnx.tbl
ON tbl.codtbl = 'mrg'
AND tbl.lib1 IS NOT NULL
AND tbl.codsoc=1
AND tbl.cletbl = tie.modrgl
WHERE tie.typtie = 'FOU'
AND tie.sigtie = l.code_fournisseur
AND tie.codsoc =1
), ' ') AS mode_paiement,
l.num_sous_periode,
adr_siege.*,
Nvl(m.nummag, ' ') AS nummag,
Nvl(m.tel1, ' ') AS tel1,
l.ref_gnx,
Nvl(tva.taux_tva, 0)/100 AS taux_tva,
Nvl(f.code, ' ') AS code_fournisseur,
(
CASE WHEN (m.lundi_ouverture != '0' OR m.mardi_ouverture != '0' OR m.mercredi_ouverture != '0'
OR m.jeudi_ouverture != '0' OR m.vendredi_ouverture != '0' OR m.samedi_ouverture != '0') THEN 1
ELSE 0
END
) AS AFFICH_HORRAIRE,
a.conditionnement,
a.refpro as REFPRO
FROM adr_siege, fourniture.frnt_livraison l
INNER JOIN fourniture.frnt_livraison_ligne ll ON ll.noliv = l.noliv
INNER JOIN fourniture.frnt_article a ON a.codpro = ll.codpro
INNER JOIN fourniture.frnt_commande c ON c.nocde = l.nocde
INNER JOIN polymag.magasin m ON m.nummag = c.nummag
INNER JOIN polymag.pays p ON p.code = m.codepays
LEFT JOIN fourniture.frnt_fournisseur f ON f.code =l.code_fournisseur
INNER JOIN polymag.pays pf ON pf.code = f.codpay
INNER JOIN fourniture.frnt_periode p ON p.noperiode = c.noperiode
LEFT JOIN fourniture.frnt_tva tva ON tva.code_pays_fou = f.codpay AND tva.code_pays_mag = m.codepays
WHERE l.type_livraison = 'FOU'
(we are selecting in schema named 'fournisseur')
Thank you in advance for your help.
problem is at line 25 because of WHERE fll.noliv that's without any relational operator like <,>,!,= after this statement.
TOTAL_PRIX seems to be invalid; look at its WHERE clause:
(
SELECT SUM(fll.prix_achat * fll.qte)
FROM fourniture.frnt_livraison_ligne fll
WHERE fll.noliv ), 0) AS total_prix,

select in select inner join

I try to make an inner join on select. My select statement is like below
SELECT con.fpd_no,
con.contract_no,
cm.cust_name,
(select cm.cust_name, TRIM(cm.address) || ', ' || TRIM(cm.zipcode) || ', ' || TRIM(klm.kelurahan_desc) || ', ' || TRIM(kcm.kecamatan_desc) || ', ' || TRIM(kbm.kabupaten_desc) || ', ' || TRIM(prm.propinsi_desc)
from customer_master cm
inner join kelurahan_master klm on klm.kelurahan_id=cm.kelurahan_id
inner join kecamatan_master kcm on kcm.kecamatan_id=klm.kecamatan_id
inner join kabupaten_master kbm on kbm.kabupaten_id=kcm.kabupaten_id
inner join propinsi_master prm on prm.propinsi_id=kbm.propinsi_id;) as address,
con.principal_amount
FROM contract con
JOIN customer_master cm ON cm.cust_no = con.cust_no
WHERE con.deal_code = 'A005'
AND con.fpd_date BETWEEN '01-01-2016' AND '31-01-2016'
AND con.fpd_no = '0103006116'
AND con.objt_group = 'MOTOR';
but I get this error:
ORA-00911: invalid character.
try this:
remove semicolon on subselect and sure about select returns only one row
SELECT con.fpd_no, con.contract_no, cm.cust_name, (select distinct cm.cust_name, TRIM(cm.address) || ', ' || TRIM(cm.zipcode) || ', ' || TRIM(klm.kelurahan_desc) || ', ' || TRIM(kcm.kecamatan_desc) || ', ' || TRIM(kbm.kabupaten_desc) || ', ' || TRIM(prm.propinsi_desc)
from customer_master cm
inner join kelurahan_master klm on klm.kelurahan_id=cm.kelurahan_id
inner join kecamatan_master kcm on kcm.kecamatan_id=klm.kecamatan_id
inner join kabupaten_master kbm on kbm.kabupaten_id=kcm.kabupaten_id
inner join propinsi_master prm on prm.propinsi_id=kbm.propinsi_id) as address, con.principal_amount
FROM contract con
JOIN customer_master cm ON cm.cust_no = con.cust_no
WHERE con.deal_code = 'A005'
AND con.fpd_date BETWEEN '01-01-2016' AND '31-01-2016'
AND con.fpd_no = '0103006116'
AND con.objt_group = 'MOTOR';

Concatenating 2 columns in sql and applying like operation on the result

SELECT
CONCAT(SG.firstname +' ', SG.lastName) AS 'Name',
SG.entityId 'ID',
dbo.GetTitleDescriptionByEntityID(SG.entityId) 'Title',
CASE ISNULL(EC.address2, '')
WHEN '' THEN EC.address1
ELSE EC.address1 +', ' + EC.address2
END +', ' + EC.city + ', ' + LDState.ItemName + ', ' + EC.zip 'Address',
CR.clinicalRate
FROM
StaffGeneral SG WITH (NOLOCK)
JOIN
LookupDetails LD WITH (NOLOCK) ON SG.employeeStatus = LD.lookupDetailsId AND LD.ItemAbbreviation = 'SCSSC'
JOIN
othersRating or1 WITH (nolock) ON SG.entityId = or1.entityId AND dateRated = (SELECT TOP 1 MAX(dateRated) FROM OthersRating r1 WITH (nolock) WHERE r1.entityid = SG.entityId)
JOIN
LookupDetails LDRating with (nolock) ON or1.rating = LDRating.lookupDetailsId AND LDRating.ItemAbbreviation NOT IN ('RTNP', 'RTDNU', 'RTPENDING', 'RTDELETE', 'RTTERMI')
JOIN
EntityContacts EC with (nolock) ON SG.entityId = EC.entityId AND (eC.contactId = 'General' or EC.contactId = 'General1') AND EC.deleted = 0
JOIN
LookupDetails LDState WITH (NOLOCK) ON EC.state = LDState.lookupDetailsId
JOIN
ContractorRate CR ON SG.entityId = CR.entityId AND CR.deleted = 0
WHERE
SG.isActive = 0 AND SG.deleted = 0
AND ((SG.entityId = #subContractorID) OR (#subContractorID IS null))
AND ((#subContractorName IS NULL) OR ((EC.firstName + ' ' + EC.lastName) LIKE #subContractorName + '%'))
In the above query I wanted to concat the first and last name with space and query it against the input to the stored procedure #subcontractorname.
When the subcontractorname is null that is fine but I am not able to get results when it jumps to Or clause .
Tried the SQL Server Profiler but does not show anything how the query is formed
I tried executing the SQL by putting the whole SQL in a var but I am hitting the problem of varchar(max).
Please advise

attempt to index global variable OE (a nil value) Lua with sql

mk = conn:query({live = true, sql = "SELECT DISTINCT OE.OderNumber,(SELECT R.ReportBody + ' CRLF ' +'Modified By: ' + UM.FirstName + ' at ' + UM.LastName + ' CRLF '+ 'Authored By: ' + UC.FirstName + ' at ' + UC.LastName + ' CRLF ' +'CRLF' AS [text()] FROM dbo.OrderReports R INNER JOIN dbo.Orders O ON R.OrderId = O.OrderId INNER JOIN dbo.Users UM ON R.ModifiedById = UM.UserId INNER JOIN dbo.Users UC ON R.CreatedById = UC.UserId WHERE O.OrderNumber = '"..OE.OderNumber.."' ORDER BY R.CreateDateFOR XML PATH('')) 'OrderReport' FROM dbo.vw_OrderLevelEvents OE"})
This query works just fine in SQL but while I have it with Lua, it says attempt to index global variable OE(a nil value)
How can i fix this
From your code :
WHERE O.OrderNumber = '"..OE.OderNumber.."' ORDER BY R.CreateDateFOR
Here you're using the OderNumber key of the OE table and its value is nil. You'll need to fix that before using its key.
EDIT : you really should rename it to OrderNumber, too! ;)