I am trying to retrieve data from a MySQL Database using RODBC with the
commands odbcConnect and sqlQuery. There are different tables in the
database and in some cases it works without any difficulties. Nevertheless I
get an error with some of them and it seems to be related to date format. Since I'm not familiar with MySQL I hope to get some ideas here.
My code looks as follows:
library(RODBC)
mycon<-odbcConnect("nicer_64")
> data1<-sqlQuery(mycon,paste("select PID, NAME, FIRSTNAME1, SEXE from a_patient;"))
> head(data1)
PID NAME FIRSTNAME1 SEXE
1 22145 PILLONEL GASTON ANTONIN 1
2 22146 PORCHER HENRIETTE MARIE 2
3 22147 PUGIN AUGUSTE JOSEPH 1
4 21437 HEIZ MARIE ANTOINETTE 2
5 21439 KALT OTTO 1
6 2144 BRUNNER MARIE 2
> data2<-sqlQuery(mycon,paste("select PID, NAME, FIRSTNAME1, BIRTHDATE, SEXE from a_patient;"))
Error in as.POSIXlt.character(x, tz, ...) :
la chaîne de caractères n'est pas dans un format standard non ambigu
> head(data2)
Error in head(data2) : object 'data2' not found
Many thanks in advance!
Related
I need help in identifying bad data(PERSON_NAME) from below select query due to which query is failing as it is not able to parse FIRST_NAME & LAST_NAME.
SELECT
PERSON_NAME
,Trim(OReplace(PERSON_NAME,StrTok(PERSON_NAME,' ',1),'')) AS AGENT_LAST_NAME
,StrTok(PERSON_NAME,' ',1) AS AGENT_FIRST_NAME
FROM TABLENAME
WHERE CAST(RECORD_START_TS AS DATE) = '2022-11-01';
*** Failure 6706 The string contains an untranslatable character.
Actual Output should look like below:
PERSON_NAME AGENT_LAST_NAME AGENT_FIRST_NAME
Abraham Gomezfitzgerald Gomezfitzgerald Abraham
Adam Tregoning Tregoning Adam
Ajiel Marino Marino Ajiel
Alexander Ford III Ford III Alexander
Fernanda Garvey Hernandez Garvey Hernandez Fernanda
I have a table like this :
Clients Cities
1 NY
1 NY | WDC | LA
1 NY | WDC
2 LA
So, I have duplicate clients with different cities (not in order, but with different length at each line). What I want is to display for each user the longest cities string. So, I should get something like this :
Clients Cities
1 NY | WDC | LA
2 LA
I am a beginner in SQL (I use Spark SQL but it's mainly the same thing), so can you please how can I fix this problem please ??
Thanks !
You can use max():
select client, max(cities)
from t
group by client;
Then you should fix your data model, so you are not storing lists of cities in a string. That is not a good way to store the data in a relational database.
I think you should handle that query (in MYSQL) by using SELECT DISTINCT statement,
As inside a table contains many duplicate values, I hope it will make it work!
For instance,
SELECT DISTINCT city_name FROM cities;
And continue.... this is my hint to lead you to the desired and great answer
I am relatively new to SQL and I am having a really hard time getting this query figured out. I need to show which shipments (shipment_no) were delivered by multiple trucks drivers.
Here are the only two columns in the table (named Package) that I believe I need as well as the entire other table (truck) I am joining it with. As you can see, shipment_no 1775 is the only one that has been delivered by more than one truck/driver.
Package table = Shipment_No - 1770,1771,1772,1773,1774,1774,1774,1775,1775,1775,1776,1777
and Truck_no = 100,103,105,102,108,108,108,101,109,109,100,100 (Respectively)
Truck table = Truck_NO 100,101,102,103,104,105,106,107,108,109
and drivername = JONES,DAVIS,GOMEZ,THOMPSON,HERSHEY,FERRIS,SHAVER,LEE,TOPI,ACKERMAN (Respectively)
This is what I've got so far
select shipment_no, drivername
from package, truck
where package.truck_no=truck.truck_no
group by shipment_no, drivername
My results look like this
- Shipment_no =
1770
1771
1772
1773
1774
1775
1775
1776
1777
- Drivername =
JONES
THOMPSON
FERRIS
GOMEZ
TOPI
ACKERMAN
DAVIS
JONES
JONES
All I need to display is the shipping number in the end so it would look like this.
-Shipment_no
-1775
I've been trying for hours and any help is appreciated.
Thanks a lot!
Select shipment_no
From Package
Group BY shipment_no
Having Count(Distinct Truck_No) > 1
Try this:
SELECT Shipment_no
FROM package
GROUP BY Shipment_no
HAVING COUNT(DISTINCT Truck_no) > 1
I have this strange behaviour in a query using Oracle 10g database.
I invoke this stored procedure contained in a package:
SQL> VAR RC REFCURSOR
SQL> EXEC MyPackage.MyProcedure('ATLANTICO', :RC )
PL/SQL procedure successfully completed.
SQL> PRINT RC
--prints the data
LURUACO
TUBARA
CGTO SALGAR
ALPES DE SEVILLA
MANATI
SOLEDAD
USIACURI
CGTO SANTA CRUZ
PONEDERA
CGTO JUAN MINA
PALMAR DE VARELA
PIOJO
SANTA LUCIA
BARRANQUILLA
POLONUEVO
REPELON
SANTO TOMAS
SUAN
JUAN DE ACOSTA
BARANOA
MALAMBO
PUERTO COLOMBIA
SABANALARGA
CGTO PATILLA
GALAPA
SABANAGRANDE
CAMPO DE LA CRUZ
EDUARDO SANTOS (LA PLAYA)
This is the definition of the stored procedure in the package:
PROCEDURE MyProcedure(iDEPTO IN VARCHAR2,oCURSOR OUT MYREFCUR) IS
BEGIN
OPEN oCURSOR FOR
select distinct city from MyTable where state=iDEPTO;
END;
If I execute the query outside of the package, this is what I'm getting:
SQL> select distinct city from MyTable where state='ATLANTICO';
--data retrieved
ALPES DE SEVILLA
BARANOA
BARRANQUILLA
CAMPO DE LA CRUZ
CGTO JUAN MINA
CGTO PATILLA
CGTO SALGAR
CGTO SANTA CRUZ
EDUARDO SANTOS (LA PLAYA)
GALAPA
JUAN DE ACOSTA
LURUACO
MALAMBO
MANATI
PALMAR DE VARELA
PIOJO
POLONUEVO
PONEDERA
PUERTO COLOMBIA
REPELON
SABANAGRANDE
SABANALARGA
SANTA LUCIA
SANTO TOMAS
SOLEDAD
SUAN
TUBARA
USIACURI
28 rows selected.
As you can see, I am not applying sorting neither in the stored procedure nor in the SQL query. So, why does the cursor return disordered data, using the same query? Is there a "scientific" explanation for this behaviour?
Neither query has an ORDER BY clause so the order in which Oracle happens to return the data is arbitrary. It is entirely valid for Oracle to return the data in any order. And there is no guarantee that you'll get the same order over time. If you care about the order in which data is returned, you must include an ORDER BY clause.
Getting a bit further into the details, I would wager that if you looked at the query plans for these two queries, you'd see that Oracle is taking a different approach to removing the duplicate rows. In the query using the bind variable, my guess is that it is doing a hash. In the query using the literal, my guess is that it is doing a sort. Of course, that's just a guess. You'd need to post the query plans for anyone to be certain.
Suppose we have some data that looks like so
R_Id Nm Base Dest Proj Cust_id 201203 201202 201201
MRBR Bob LONDON UK Project1 1 0 0 0
MRBU Frank LONDON London Project2 2 11.68 0 248.93
MRBU Frank LONDON UK Project3 1 7.4 4.8 0
MRGB Barry GUILDFORD Hull Project4 1 50.36 12.85 48.92
MRGB Barry GUILDFORD Project5 1 0 177.31 0
MRGB Barry GUILDFORD INTL Project6 3 0 331.08 0
And suppose we have a lot more columns than above, but we've limited to a few for now.
I want to be able to use a where statment to only show rows where the row needs further investigation. This is done by saying "Where are there more than two large numbers next to each other in a row?" So I need to count the number of rows where the number is large.
The output should look like such, where I've explained what filtering I'm doing.
R_Id Nm Base Dest Proj Cust_id 201203 201202 201201
MRBR Bob LONDON UK Project1 1 "Numbers not Large"
MRBU Frank LONDON London Project2 2 11.68 248.93 0
MRBU Frank LONDON UK Project3 1 "Numbers not Large"
MRGB Barry GUILDFORD Hull Project4 1 50.36 12.85 48.92
MRGB Barry GUILDFORD Project5 1 "Too few adjacent numbers"
MRGB Barry GUILDFORD INTL Project6 3 "Too few adjacent numbers"
It's the case where there are too few adjacent numbers I'm trying to filter for. I need to count the numer of adjacent (or every other!) numbers in those specific columns.
I've looked at this question: Multiple Column Conditional Count SQL, but I don't think I can use Count(*) as I get this error: You tried to execute a query thaty does not include the speicified expression 'AT_RISK?' as part of aggregate function. At risk is a column that just stores Yes/No and lives to the left of R_Id (not included above for brevity)
Can anyone help or at least point me in the right direction please? I'd really appreciate it. I've read the question above, and I've looked at how to use count in general, but this is really stumping me.
Well I can think of a somewhat ugly solution to this question, but it involves the use of a custom VBA function.
Concatenate and Test String
Your SQL statement should be something like:
SELECT * FROM tblName
WHERE IsSeqHigh([201203] & ";" & [201202] & ";" & ..., 1000);
And then, in a VBA module we define:
Public Function IsSeqHigh(seq As String, thres As Double) As Boolean
IsSeqHigh = False
Dim valStrs() As String
valStrs = Split(seq, ";")
For n = 1 To UBound(valStrs) - 1
If (valStrs(n) >= thres) And (valStrs(n + 1) >= thres) Then
IsSeqHigh = True
Exit For
End If
Next n
End Function
Another approach
Alternatively, if your schema is fixed and unlikely to change - and you have a primary key value, you can write a VBA function which takes the primary key value and scans the columns for the specific condition you are looking for.
In short, there is no good SQL-only solution that I can think of.
You said "orders over 1000", which should exclude Pjt_Id = 1 from the output. If you actually want orders at least 1000, change > to >= in this query.
SELECT
p.Pjt_Id,
p.OrderPriceQ1,
p.OrderPriceQ2,
p.OrderPriceQ3,
p.Customer
FROM TblPureferret AS p
WHERE
(p.OrderPriceQ1 > 1000 AND p.OrderPriceQ2 > 1000)
OR (p.OrderPriceQ2 > 1000 AND p.OrderPriceQ3 > 1000);
Try this
select
Pjt_Id ,OrderPriceQ1 ,OrderPriceQ2 ,OrderPriceQ3 ,Customer
from
table
where
(
(OrderPriceQ1>=1000 and OrderPriceQ2 >=1000) or
(OrderPriceQ1>=1000 and OrderPriceQ3 >=1000) or
(OrderPriceQ2>=1000 and OrderPriceQ3 >=1000)
)