Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I made a query two retrieve address information from the database, the problem is that there are 2 columns, 'cd_cidade' and 'cd_cidade_comercial', which are city codes in the table 'prestador'. and we have a table named 'cidade' where have the 'cd_cidade' column as primary key and the column 'nm_cidade' that is the name for each code in the table.

How can I retrive the name from table 'cidade' to use it in place of 'cd_cidade' and 'cd_cidade_comercial'.

By the way the query is:

SQL
SELECT  COUNT (a.cd_atendimento),
        p.cd_prestador,
        p.nm_prestador,
        p.ds_endereco,
        p.nr_endereco,
        p.ds_bairro,
        p.ds_complemento,
        p.nr_cep,
        p.cd_cidade,
        p.ds_endereco_comercial,
        p.nr_endereco_comercial,
        p.ds_bairro_comercial,
        p.ds_complemento_comercial,
        p.ds_cep_comercial,
        p.cd_cidade_comercial,
        esp.ds_especialid,
        decode (p.cd_tip_presta, 1, 'Médico', 2, 'Odontologo')

FROM    dbamv.prestador p,
        dbamv.atendime a,
        dbamv.esp_med e,
        dbamv.especialid esp

WHERE   p.cd_prestador = a.cd_prestador
AND     p.cd_prestador = e.cd_prestador
AND     e.cd_especialid = esp.cd_especialid
AND     p.cd_tip_presta in (1,2)
AND     A.tp_atendimento = 'I'
AND     A.dt_atendimento between '01/jan/2012' and '31/dez/2012'
AND     A.dt_alta is not null

GROUP BY    p.cd_prestador,
            p.nm_prestador,
            p.ds_endereco,
            e.cd_especialid,
            esp.ds_especialid,
            p.tp_prestador,
            p.cd_tip_presta,
            p.nr_endereco,
            p.ds_bairro,
            p.ds_complemento,
            p.nr_cep,
            p.cd_cidade,
            p.ds_endereco_comercial,
            p.nr_endereco_comercial,
            p.ds_bairro_comercial,
            p.ds_complemento_comercial,
            p.ds_cep_comercial,
            p.cd_cidade_comercial



Thank you all. Any questions feel free to reply.
Posted

Without wading through your query (and possibly messing it up - I'll leave that bit to you) you need to add a JOIN. If we just get a couple of fields:
SQL
SELECT p.cd_prestador, c.nm_cidade 
FROM prestador p
JOIN cicade c
ON p.cd_cidade=c.cd_cicade
Try that and you should see what I mean.
 
Share this answer
 
Comments
Ricardo Santos da Silva 26-Nov-13 13:05pm    
Used it and it works apart. but the query is complete, what I need is the city name instead the code in the original query.
OriginalGriff 26-Nov-13 14:03pm    
So add the join to your query.
I'd do it myself, but it's a big query, and you wrote it! :laugh:
You should in any case not mix ANSI joins with implicit joins.
Try this:
SQL
SELECT  COUNT (a.cd_atendimento),
        p.cd_prestador,
        p.nm_prestador,
        p.ds_endereco,
        p.nr_endereco,
        p.ds_bairro,
        p.ds_complemento,
        p.nr_cep,
        p.cd_cidade,
        p.ds_endereco_comercial,
        p.nr_endereco_comercial,
        p.ds_bairro_comercial,
        p.ds_complemento_comercial,
        p.ds_cep_comercial,
        p.cd_cidade_comercial,
        esp.ds_especialid,
        decode (p.cd_tip_presta, 1, 'Médico', 2, 'Odontologo'),
        c1.nm_cidade cidade,
        c2.nm_cidade cidade_comercial
        
        
FROM    dbamv.prestador p,
        dbamv.atendime a,
        dbamv.esp_med e,
        dbamv.especialid esp,
        cidade c1
        cidade c2
        
WHERE   p.cd_prestador = a.cd_prestador
AND     p.cd_prestador = e.cd_prestador
AND     e.cd_especialid = esp.cd_especialid
AND     p.cd_cidade=c1.cd_cidade
AND     p.cd_cidade_comercial=c2.cd_cidade
AND     p.cd_tip_presta in (1,2)
AND     A.tp_atendimento = 'I'
AND     A.dt_atendimento between '01/jan/2012' and '31/dez/2012'
AND     A.dt_alta is not null
 
GROUP BY    p.cd_prestador,
            p.nm_prestador,
            p.ds_endereco,
            e.cd_especialid,
            esp.ds_especialid,
            p.tp_prestador,
            p.cd_tip_presta,
            p.nr_endereco,
            p.ds_bairro,
            p.ds_complemento,
            p.nr_cep,
            p.cd_cidade,
            p.ds_endereco_comercial,
            p.nr_endereco_comercial,
            p.ds_bairro_comercial,
            p.ds_complemento_comercial,
            p.ds_cep_comercial,
            p.cd_cidade_comercial,
            c1.nm_cidade,
            c2.nm_cidade
 
Share this answer
 
v3
Comments
Ricardo Santos da Silva 27-Nov-13 9:52am    
It works, but only give the name of the city for 'p.cd_cidade' and still need the codes for the column 'p.cd_cidade_comercial'.

I just changed it on excel and sent the report, but if anyone have a way to do that direct on the query for both column I'll be very glad. Thank you guys.
Jörgen Andersson 28-Nov-13 4:17am    
Updated solution.
Ricardo Santos da Silva 28-Nov-13 11:35am    
Nice, worked pretty well. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900