I want to make sure that this conversion of a DECODE function into a SELECT statement joining it to a mapping table would run properly and I'm not using coding or format that works in SQL Server but is different in Oracle SQL
About the code: it is using the DECODE function to map a series of four digit medical taxonomy codes into two digit provider specialty codes. The primary table is PRVDR.TXNMY_CD, the outcome would be a column PRFRM_PRVDR_SPCLTY_CD.
Original code:
SELECT
DECODE (SUBSTR(PRVDR.TXNMY_CD, 1, 4),
'261Q', '70','347E', '59','332H', '96','332B', 'A6','1711', 'Y9','2257', 'Y9','106H', '62','103K', '26','101Y', '26','367A', '42','207K', '03', '3416', '59','367H', '32','207L', '05','211D', '48','231H', '64','2376', '64','111N', '35','291U', '69','103G', '86','364S', '89','208C', '28', '172V', '60','251S'
) END AS PRFRM_PRVDR_SPCLTY_CD
FROM
NPS_CLM_HDR
My conversion attempt:
First, I'd separately create this table called MAPPING with the following columns
| TXNMY_CD_MAP | PRFRM_PRVDR_SPCLTY_CD |
| 1711 | Y9 |
| 2257 | Y9 |
| 106H | 62 |
| 367A | 42 |
etc.
Then I would use the following query:
SELECT PRFRM_PRVDR_SPCLTY_CD
FROM REF.MAPPING AS M
JOIN PRVDR.TXNMY_CD AS P ON P.TXNMY_CD = M.TXNMY_CD_MAP
Does this look correct or have I used terminology from SQL Server that does not work with Oracle SQL?
Hmmm . . . I am expecting the two columns to be:
TXNMY_CD4 PRFRM_PRVDR_SPCLTY_CD
'261Q' '70'
'347E' '59'
'332H' '96'
. . .
This may be what your table looks like, but these are the values at the beginning of the table.
And then:
SELECT m.PRFRM_PRVDR_SPCLTY_CD
FROM PRVDR.TXNMY_CD P LEFT JOIN
REF.MAPPING M
ON LEFT(P.TXNMY_CD, 4) = M.TXNMY_CD_MAP
Except for the LEFT() vs. SUBSTR(), this should work in both databases.
Note that this uses LEFT JOIN to ensure that no rows are lost, even if there are no matches.
decode() is an Oracle function, that is not available in SQL Server. If you wanted to translate your decode to SQL Server, you would use case:
case left(prvdr.txnmy_cd, 4)
when '261Q' then '70'
when '347E' then '59'
...
end as prfrm_prvdr_spclty_cd
from nps_clm_hdr
Note that substr() is not supported in SQL Server - here we cause left() instead.
That said, using a mapping table is a better approach: it scales better, and makes it easy to maintain the mapping (there is no need to modify the code of the query, just the data).
You would phrase the query as:
select prfrm_prvdr_spclty_cd
from prvdr.txnmy_cd as p
left join ref.mapping as m on left(p.txnmy_cd, 4) = m.txnmy_cd_map
The left join allows unmapped values.
Not all database support left(), nor substr() (it is sometimes called substring(), as in SQL Server). I think that the most portable approach uses like and concat():
left join ref.mapping as m on p.txnmy_cd like concat(m.txnmy_cd_map, '%')
I am using MS Access and I have a rather complex situation.
I have Respondents who are linked to varying numbers of different Companies via 2 connecting tables. I want to be able to create a list of distinct customers which excludes any customer associated with Company X.
Here is a pic of the relationships that are involved with the query.
And here is an example of what I'm trying to achieve.
RespondentRef | Respondent Name
8 Joe Bloggs
.
RespondentRef | GroupRef
8 2
.
GroupRef | CompanyRef
2 10
.
CompanyRef | CompanyName
10 Ball of String
I want a query where I enter in 'Ball of String' for the company name, and then it produces a list of all the Respondents (taken from Tbl_Respondent) which completely excludes Respondent 8 (as he is linked to CompanyName: Ball of String).
Tbl_Respondent
RespondentRef | Respondent Name
... ...
7 Bob Carlyle
9 Anton Boyle
I have tried many combinations of subqueries with <> and NOT EXISTS and NOT IN and nothing seems to work. I suspect the way these tables are linked may have something to do with it.
Any help you could offer would be very much appreciated. If you have any questions let me know. (I have made best efforts, but please accept my apologies for any formatting conventions or etiquette faux-pas I may have committed.)
Thank you very much.
EDIT:
My formatted version of Frazz's code is still turning resulting in a syntax error. Any help would be appreciated.
SELECT *
FROM Tbl_Respondent
WHERE RespondentRef NOT IN (
SELECT tbl_Group_Details_Respondents.RespondentRef
FROM tbl_Group_Details_Respondents
JOIN tbl_Group_Details ON tbl_Group_Details.GroupReference = tbl_Group_Details_Respondents.GroupReference
JOIN tbl_Company_Details ON tbl_Company_Details.CompanyReference = tbl_Group_Details.CompanyReference
WHERE tbl_Company_Details.CompanyName = "Ball of String"
)
This should do what you need:
SELECT *
FROM Tbl_Respondent
WHERE RespondentRef NOT IN (
SELECT gdr.RespondentRef
FROM Tbl_Group_Details_Respondent gdr
JOIN Tbl_Group_Details gd ON gd.GroupRef=gdr.GroupRef
JOIN Tbl_Company_Details cd ON cd.CompanyRef=gd.CompanyRef
WHERE cd.CompanyName='Ball of String'
)
I'm trying to make this query but it's returning too much rows
SELECT
Denuncia.codigoAsociado,
Involucrado.nombreCompleto
FROM
Denuncia
RIGHT JOIN
Involucrado ON Denuncia.ID = Involucrado.idDenuncia
I would like to get one codigoAsociado and one nombreCompleto. I have tried using DISTINCT but it's the same.
This is the result (check the link) Sorry can't post images
http://oi62.tinypic.com/2l9gwnp.jpg
I need it to look like this
codigoAsociado | nombreCompleto
341130402 | Juan Carlos Espinoza López
341131290 | Carlos Queirolo Rochabrun
.
.
.
341131600 | Enrique Froemel
341131949 | Raúl Muñoz
Thanks in advance
I use Oracle DB but in Access should work this:
SELECT
TOP 1 Denuncia.codigoAsociado,
Involucrado.nombreCompleto
FROM
Denuncia
RIGHT JOIN
Involucrado ON Denuncia.ID = Involucrado.idDenuncia
The TOP number tell you how many rows will be returned. You should also use TOP 10 PERCENT and it returns first 10% of records.
If you need only unique records, try to use this code
SELECT
DISTINCT Denuncia.codigoAsociado,
Involucrado.nombreCompleto
FROM
Denuncia
RIGHT JOIN
Involucrado ON Denuncia.ID = Involucrado.idDenuncia
DISTINCT in Access has been also discussed in this post how to use distinct in ms access.
I have a Hive table with the numeric version of an IP address. I have another table with start, end, location where start and end define a range of numeric IPs associated with a location.
Example
Numeric: 29
start | end | location
----------------------
1 | 11 | 666
12 | 30 | 777
31 | 40 | 888
Output: 29 - 777
I need to use the IP from table 1 to lookup the location from table 2. I'm new to Hive and have discovered that I can't use BETWEEN or < > in join statements. I've been trying to figure out some way of making this happen using Hive SQL and can't figure it out. Is there a way? I'm somewhat familiar with UDFs as well if one of those is needed. I'm open to the idea that this isn't possible in Hive and I need to do with Pig or a Java Map/Reduce job, I just don't know enough about things at this point to say.
Any help is appreciated. Thanks.
Hive and Pig do not support such inequality join. You can use cross join and where to do it. But it's inefficient. A simple example:
SELECT t1.ip, t2.location_ip FROM t1 JOIN t2
WHERE t1.ip >= t2.start_ip and t1.ip<=t2.start_ip ;
However, it seems you want to do cross join a big table and a small table. If so, maybe the following statement is more efficient:
SELECT /*+ MAPJOIN(t2) */ t1.ip, t2.location_ip FROM t1 JOIN t2
WHERE t1.ip >= t2.start_ip and t1.ip<=t2.start_ip ;
everyone;
I am working on a database where path information are stored, one simplified table is shown below
path_id | path value
1 //a/b/c/d
2 //a/b/e
3 //a/b
4 //a/bcd
So here is the question, how can I get information where has '//a/b' as the prefix? In this case, the result should be:
path_id | path value
1 //a/b/c/d
2 //a/b/e
3 //a/b
I am seeking for a more elegant and optimized query, other than using logic operators like 'OR'.
Thanks.
SELECT * FROM YourTable WHERE path_value like '//a/b/%' OR path_value = '//a/b'
Note the extra slash before the wild card in the first part of the WHERE statement. This will exclude //a/bcd.
the % acts as a wild card so it anything can follow that part.
SELECT * FROM tableName WHERE path_value like '//a/b/%' OR path_value = '//a/b'