Oracle SQL Developer query not sorting alphabetically - sql

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
;

Related

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';

Multiple values in same cell

I'm trying to make a description of a track including all details related to it.
SELECT
(SELECT
UPPER(Track.Name) || ' is a ' ||
CAST(Track.Milliseconds/1000 AS INT) || ' seconds long track in the album ' ||
UPPER(Album.Title) || ' of ' ||
Artist.Name || ' composed by ' ||
CASE WHEN Track.Composer IS NULL THEN 'an unknown composer' ELSE Track.Composer END ||
'. ' || 'It is available as a ' ||
MediaType.Name || ' for $' ||
Track.UnitPrice || ', and it can be found in the following playlists: ' ||
Playlist.Name )
AS 'Track Description' FROM Track
LEFT JOIN Album
ON Track.AlbumId=Album.AlbumId
INNER JOIN Artist
ON Artist.ArtistId=Album.ArtistId
INNER JOIN MediaType
ON Track.MediaTypeId=MediaType.MediaTypeId
INNER JOIN PlaylistTrack
ON PlaylistTrack.TrackId = Track.TrackId
INNER JOIN Playlist
ON Playlist.PlaylistId = PlaylistTrack.PlaylistId
ORDER BY RANDOM()
LIMIT 1;
I'm up to working with playlist.name. One track can be in more than one playlist and currently it only outputs the top playlist. I want to know how i can output all playlists the track is in separated by commas after and it can be found in the following playlists: ' ||
One strategy would be to use GROUP_CONCAT to aggregate all the playlist names in which a given track appears. To do this, you can GROUP BY every column associated with a track except for the playlist name. The subquery computes your output in pretty format from the inner query.
SELECT UPPER(t.trackName) || ' is a ' ||
CAST(t.trackMillis/1000 AS INT) || ' seconds long track in the album ' ||
UPPER(t.albumTitle) || ' of ' ||
t.artistName || ' composed by ' ||
CASE WHEN t.trackComposer IS NULL THEN 'an unknown composer' ELSE t.trackComposer END ||
'. ' || 'It is available as a ' ||
t.mediaTypeName || ' for $' ||
t.trackUnitPrice || ', and it can be found in the following playlists: ' || t.playlistNames
FROM
(
SELECT Track.Name AS trackName, Track.Milliseconds AS trackMillis,
Album.Title AS albumTitle, Artist.Name AS artistName,
Track.Composer AS trackComposer, MediaType.Name AS mediaTypeName,
Track.UnitPrice AS trackUnitPrice, GROUP_CONCAT(Playlist.Name) AS playlistNames
FROM Track
LEFT JOIN Album
ON Track.AlbumId = Album.AlbumId
INNER JOIN Artist
ON Artist.ArtistId = Album.ArtistId
INNER JOIN MediaType
ON Track.MediaTypeId = MediaType.MediaTypeId
INNER JOIN PlaylistTrack
ON PlaylistTrack.TrackId = Track.TrackId
INNER JOIN Playlist
ON Playlist.PlaylistId = PlaylistTrack.PlaylistId
GROUP BY Track.Name, Track.Milliseconds, Album.Title, Artist.Name,
Track.Composer, MediaType.Name, Track.UnitPrice
) t

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

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

Filtering subquery results

I'm having a problems filtering the duplicate values from my query. This is my query
SELECT cid.IDS_NO,cid.SUB_TITLE,cid.E_SUB_NAME, (cid.SUB_TITLE || ' '|| cid.E_SUB_NAME),
(SELECT(INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME FROM street_table where street_code = d.INST_ST_CODE))
FROM del d where d.IDS_NO = cid.IDS_NO and ROWNUM = 1),
(SELECT INST_ST_LEVEL || '-' || INST_STUNIT FROM detail d where d.IDS_NO = cid.IDS_NO and rownum = 1),
('COUNTRY' ||' ' || pl_post),pl_post
FROM SUBSCRIBER cid where pl_postal_district = 15 and rownum < 3001
and hi_property_type IN ('CONDO','COMMERCIAL BUILDING');
These are the parts I'm having problems with:
(SELECT(INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME FROM street_table where street_code = d.INST_ST_CODE))
FROM del d where d.IDS_NO = cid.IDS_NO and ROWNUM = 1),
(SELECT INST_ST_LEVEL || '-' || INST_STUNIT FROM detail d where d.IDS_NO = cid.IDS_NO and rownum = 1);
These parts always get multiple records, and I want to get only 1 record with the lowest sequence number, So here's what I did:
(SELECT(INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME FROM street_table where street_code = d.INST_ST_CODE))
FROM del d where d.IDS_NO = cid.IDS_NO and ROWNUM = 1 ORDER BY d.SEQ_NO DESC),
(SELECT INST_ST_LEVEL || '-' || INST_STUNIT FROM detail d where d.IDS_NO = cid.IDS_NO and rownum = 1 ORDER BY d.SEQ_NO DESC);
But I always get an error saying missing parenthesis, but when I remove the order by, it works fine. What should I do to get my query running correctly?
Formatting your query to make it easier to read goes a long way to begin with.
But you also have a rather tricky problem, since you ..
want to get only 1 record with the lowest sequence number.
Should work like this:
SELECT s.ids_no
,s.sub_title
,s.e_sub_name
,(s.sub_title || ' '|| s.e_sub_name) AS title_sub_name
,d.some_col_name
,t.inst_name
,('COUNTRY' ||' ' || s.pl_post) AS country_post
,s.pl_post
FROM subscriber s
LEFT JOIN (
SELECT d.ids_no
,(d.inst_blk_hse || ' ' || st.e_street_name) AS some_col_name
,ROW_NUMBER() OVER (PARTITION BY d.ids_no ORDER BY d.seq_no) AS rn
FROM del d
LEFT JOIN e_street_name st ON st.street_code = d.inst_st_code
) d ON d.ids_no = s.ids_no AND d.rn = 1
LEFT JOIN (
SELECT ids_no
,(inst_st_level || '-' || inst_stunit) AS inst_name
,ROW_NUMBER() OVER (PARTITION BY d.ids_no ORDER BY d.seq_no) AS rn
FROM detail
) t ON t.ids_no = s.ids_no AND t.rn = 1
WHERE s.pl_postal_district = 15
AND s.rownum < 3001
AND s.hi_property_type IN ('CONDO','COMMERCIAL BUILDING');
More details on how the subqueries work under this related question:
Select first row in each GROUP BY group?
The core features is ROW_NUMBER() to get the record with the lowest sequence number.
I rewrote your correlated subqueries, because they generally suck performance-wise. They are also hard to read.
I use LEFT JOIN, so you still get a row in case the subquery shouldn't find anything to match.
Subqueries must return only one row. So you have to add rownum = 1 to the subquery that is inside the subquery FROM del d. Something like:
SELECT
cid.IDS_NO,
cid.SUB_TITLE,
cid.E_SUB_NAME,
(cid.SUB_TITLE || ' '|| cid.E_SUB_NAME),
(SELECT (INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME
FROM street_table
where street_code = d.INST_ST_CODE
and rownum = 1)) <<<<<<< here
FROM del d
where d.IDS_NO = cid.IDS_NO
and ROWNUM = 1),
(SELECT INST_ST_LEVEL || '-' || INST_STUNIT
FROM detail d
where d.IDS_NO = cid.IDS_NO
and rownum = 1),
('COUNTRY' ||' ' || pl_post),
pl_post
FROM SUBSCRIBER cid
where pl_postal_district = 15
and rownum < 3001
and hi_property_type IN ('CONDO','COMMERCIAL BUILDING');
Or, JOIN all the tables like this:
SELECT
cid.IDS_NO,
cid.SUB_TITLE,
cid.E_SUB_NAME,
cid.SUB_TITLE || ' '|| cid.E_SUB_NAME,
d.INST_BLK_HSE || ' ' || s.E_STREET_NAME,
d.INST_ST_LEVEL || '-' || d.INST_STUNIT
'COUNTRY' ||' ' || pl_post),
pl_post
FROM SUBSCRIBER cid
INNER JOIN del d ON d.IDS_NO = cid.IDS_NO
INNER JOIN detail d2 ON d2.IDS_NO = cid.IDS_NO
INNER JOIN street_table s ON s.street_code = d.INST_ST_CODE
WHERE cid.pl_postal_district = 15
and cid.rownum < 3001
and cid.hi_property_type IN ('CONDO','COMMERCIAL BUILDING')
and s.rownum = 1
and d.rownum = 1
and d2.rownum = 1;