SQLITE : near from syntax error: - sqlite

I stucked on following error if i'm executing this SQLlite command:
Error: near "FROM": syntax error
I check code of the query few times but i cannot able to locate error.
Could somebody tell me what is wrong in my code?
Thanks for any help.
SQL query:
SELECT d.date AS DATE,
IFNULL(DIALS_CNT, 0) AS DIALS_CNT,
IFNULL(APPT_CNT, 0) AS APPT_CNT,
IFNULL(CONVERS_CNT, 0) AS CONVERS_CNT,
FROM
(SELECT DATE('2014-01-01', '+' || (t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) || ' days') date
FROM
(SELECT 0 i
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9) t0,
(SELECT 0 i
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9) t1,
(SELECT 0 i
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9) t2,
(SELECT 0 i
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9) t3,
(SELECT 0 i
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9) t4) d
LEFT JOIN
(SELECT substr(m.date, 1, 10) AS my_date,
COUNT(m.ID) AS 'DIALS_CNT',
(SELECT COUNT(*)
FROM dialed_calls subq
WHERE subq.call_result = 'APPT'
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) AS 'APPT',
(SELECT COUNT(*)
FROM dialed_calls subq
WHERE subq.call_result = 'CONV_NO_APPT'
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) AS 'CONV_NO_APPT',
(SELECT COUNT(*)
FROM dialed_calls subq
WHERE subq.call_result = 'CANNOT_REACH'
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) AS 'CANNOT_REACH'
FROM dialed_calls m
GROUP BY my_date) t ON d.date = t.my_date
WHERE d.date BETWEEN '2014-09-30' AND '2014-09-20'
ORDER BY d.date;

A simple typo;
IFNULL(CONVERS_CNT, 0) AS CONVERS_CNT, <-- extraneous comma
FROM

Related

Oracle left join with Union gives duplicate records

Have two tables payment, payment_info , trying with left join and union to get the ordnums, it gives me duplicate records, below is my tables data structure.
Payment Table:
id
invoice
cardpay
gpay
phonepe
1
4567
0000123
null
null
2
4567
null
dummy#dummy
null
3
4567
null
null
P#dummy
4
4568
0000124
null
null
Payment_info Table:
ordnum
payment_method
payment_value
101
C
0000123
102
G
dummy#dummy
103
C
0000124
Query:
select pinfo.ordnum from
payment p
left join payment_info pinfo
on (select payment_info.ordnum
from payment_info
where payment_info.payment_method = 'C'
and payment_info.payment_value = p.cardpay
union
select payment_info.ordnum
from payment_info
where payment_info.payment_method = 'G'
and payment_info.payment_value = p.gpay
union
select payment_info.ordnum
from payment_info
where payment_info.payment_method = 'P'
and payment_info.payment_value = p.phonepe ) = pinfo.ordnum
where p.invoice = '4567'
Result: It gives 3 duplicate records.
ordnum
101
101
101
Expected result value is : 101,102, null
Can you please explain me on why it is generating duplicate records, also please let me know how can I solve this, it works good with "OR", but that would cause performance issue, any other solution that that would be really helpful.
Also in sql server it works good.
How about something simpler?
Sample data:
SQL> with
2 payment (id, invoice, cardpay, gpay, phonepe) as
3 (select 1, 4567, '0000123', null , null from dual union all
4 select 2, 4567, null , 'dummy#dummy', null from dual union all
5 select 3, 4567, null , null , 'P#dummy' from dual union all
6 select 4, 4568, '0000124', null , null from dual
7 ),
8 payment_info (ordnum, payment_method, payment_value) as
9 (select 101, 'C', '0000123' from dual union all
10 select 102, 'G', 'dummy#dummy' from dual union all
11 select 103, 'C', '0000124' from dual
12 )
Query begins here: outer join it is, but simplified as coalesce returns the first non-null value. This should be OK because there can be only one payment "method" (cardpay, gpay or phonepe) per ID.
13 select p.id, i.ordnum
14 from payment p left join payment_info i on
15 coalesce(p.cardpay, p.gpay, p.phonepe) = i.payment_value
16 where p.invoice = 4567;
ID ORDNUM
---------- ----------
1 101
2 102
3
SQL>
You can use a single LEFT OUTER JOIN:
SELECT i.ordnum
FROM payment p
LEFT OUTER JOIN payment_info i
ON ( (i.payment_method, i.payment_value) IN (
('C', p.cardpay),
('G', p.gpay),
('P', p.phonepe)
) )
WHERE p.invoice = 4567;
or:
SELECT i.ordnum
FROM payment p
LEFT OUTER JOIN payment_info i
ON ( (i.payment_method = 'C' AND i.payment_value = p.cardpay)
OR (i.payment_method = 'G' AND i.payment_value = p.gpay)
OR (i.payment_method = 'P' AND i.payment_value = p.phonepe)
)
WHERE p.invoice = 4567;
Which, for the sample data:
CREATE TABLE payment (id, invoice, cardpay, gpay, phonepe) AS
SELECT 1, 4567, '0000123', NULL , NULL FROM DUAL UNION ALL
SELECT 2, 4567, NULL , 'dummy#dummy', NULL FROM DUAL UNION ALL
SELECT 3, 4567, NULL , NULL , 'P#dummy' FROM DUAL UNION ALL
SELECT 4, 4568, '0000124', NULL , NULL FROM DUAL;
CREATE TABLE payment_info (ordnum, payment_method, payment_value) AS
SELECT 101, 'C', '0000123' FROM DUAL UNION ALL
SELECT 102, 'G', 'dummy#dummy' FROM DUAL UNION ALL
SELECT 103, 'C', '0000124' FROM DUAL;
Both output:
ORDNUM
101
102
null
fiddle

how to join all the tables without using any condition

all the below results are not related to each other wheras we cannot use any condition.
ID
----------
1
2
3
4
5
6
7
7 rows selected.
NAME
-----------------
SRUJAN
DEERAJ
VINEETH
CHANIKYA
LAVANYA
KAVITHA
BUNNY
7 rows selected.
AGE
----------
23
24
26
25
29
28
24
7 rows selected.
ADDRESS
-------------
NAGARAM
BANDLAGUDA
UPPAL
KUKATPALLY
HB COLONY
MOULALI
BOUDHA NAGAR
7 rows selected.
SALARY
----------
12000
13000
14000
15000
16000
17000
18000
7 rows selected.
I USED
SQL>select id,name,age,address,salary from table1,table2,table3,table4,table5;
but it showing 16807 rows selected
i want to get only one table.
please suggest a query.
The only possible join between 2 tables having only 1 columns and both having no relation is cross join. You cannot avoid it. And the same you are getting when you tried to join. The best way for you is to create a sequence as ID and then call it in your select statement of table2.
CREATE SEQUENCE TEST_SEQ
START WITH 1
MAXVALUE 9999999999999999999999999999
MINVALUE 1
NOCYCLE;
select TEST_SEQ.nextval ID,col1 NAME
from table2;
Here is one way, using inner joins. This solution matches the lowest id with the first name in alphabetical order, the lowest age, etc.
If instead of this ordered matching you need random matching, that is easy to do as well: in the over... clause of the prep tables, change the order by clause to order by dbms_random.value() (in all places).
In the solution below I use only the first three tables, but the same works for any number of input tables.
with
tbl_id ( id ) as (
select 1 from dual union all
select 2 from dual union all
select 3 from dual union all
select 4 from dual union all
select 5 from dual union all
select 6 from dual union all
select 7 from dual
),
tbl_name ( name ) as (
select 'SRUJAN' from dual union all
select 'DEERAJ' from dual union all
select 'VINEETH' from dual union all
select 'CHANIKYA' from dual union all
select 'LAVANYA' from dual union all
select 'KAVITHA' from dual union all
select 'BUNNY' from dual
),
tbl_age ( age ) as (
select 23 from dual union all
select 24 from dual union all
select 26 from dual union all
select 25 from dual union all
select 29 from dual union all
select 28 from dual union all
select 24 from dual
),
prep_id ( id, rn ) as (
select id, row_number() over (order by id) from tbl_id
),
prep_name ( name, rn ) as (
select name, row_number() over (order by name) from tbl_name
),
prep_age ( age , rn ) as (
select age, row_number() over (order by age) from tbl_age
)
select i.id, n.name, a.age
from prep_id i inner join prep_name n on i.rn = n.rn
inner join prep_age a on i.rn = a.rn
;
Output:
ID NAME AGE
---------- -------- ----------
1 BUNNY 23
2 CHANIKYA 24
3 DEERAJ 24
4 KAVITHA 25
5 LAVANYA 26
6 SRUJAN 28
7 VINEETH 29
7 rows selected

linq to sql: 2 counts as result

I want to do the following in LINQ to SQL:
Select count(*) as count_1,
(select count(*) from tableName2) as count_2 FROM tableName
Where x = y
The result should be
Column 1 | column 2
--------------------
50 34
What you need to do is something like this:
select
(select count(*)
from tableName
where x = y) as count_1,
(select count(*)
from tableName2) as count_2

sqlite select minimum and maximum from two tables

I have two tables
table1
c1t1 c2t1
1 saanu
3 abc
table2
c1t2 c2t2
2 val2
4 val4
I have to find out the values of c2t1 and c2t2 for the minimum and maximum value of c1t1 and c1t2 with one line command.
For the above example I have to find saanu and val4
I had a very similar problem and solved it with UNION ALL. The minimum of the aColumn column in tables aTable1, ... , aTableN can be computed as:
SELECT Min(aColumn)
FROM (
SELECT aColumn FROM aTable1 UNION ALL
SELECT aColumn FROM aTable2 UNION ALL
...
SELECT aColumn FROM aTableN) t;
You should be able to do Min in each of the inner selects, but I haven't found out how to do that!
One approach:
select max(case c1 when min1 then c2 end) c2_first,
max(case c1 when max1 then c2 end) c2_last
from (select c1t1 c1, c2t1 c2 from table1
union all
select c1t2 c1, c2t2 c2 from table2) u
cross join
(select min(min11, min12) min1, max(max11, max12) max1 from
(select min(c1t1) min11, max(c1t1) max11 from table1) t1
cross join
(select min(c1t2) min12, max(c1t2) max12 from table2) t2) m
SQLFiddle here.
1)
SELECT c2t1
FROM table1
ORDER BY c1t1 ASC LIMIT 1
2)
SELECT c2t2
FROM talbe2
ORDER BY c1t2 DESC LIMIT 1

Select numbers between a range (1 to 100) in sqlite

I know some of the solutions in SQL but couldn't find any of them from SQlite.
I just want to execute a select query that returns a resultset of numbers ranging from 1 to 100.
Numbers
1
2
3
4
......
5
A correction: I don't actually have a table at all. (however a solution is encouraged with a virtual table like dual in MySQL)
Thanks sgmentzer!
Inspired by your answer I went ahead and also found this:
WITH RECURSIVE
cnt(x) AS (
SELECT 1
UNION ALL
SELECT x+1 FROM cnt
LIMIT 100000
)
SELECT x FROM cnt;
How about
SELECT * FROM myTable WHERE myNumber >= 1 AND myNumber <= 100;
?
Example subquery to generate the series 1 <= n <= 100000 in SQLite. No table is created or used.
select 1+e+d*10+c*100+b*1000+a*10000 as n from
(select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as b union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as c union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as d union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as e union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9);
I don't think SQLite has a clean way to do this, so you'll need to use a virtual table interface. SQLite ships one for 'C', and apsw has one for python as I'll demonstrate below. Here's documentation for the APSW Virtual Table interface.
#!/usr/bin/python
import apsw,tempfile
### Opening/creating database
filename=tempfile.mktemp() #insecure - do not use in production code
connection=apsw.Connection(filename)
cursor=connection.cursor()
# This gets registered with the Connection
class Source:
def Create(self, db, modulename, dbname, tablename, *args):
schema="create table foo( dummy integer )"
return schema,Table()
Connect=Create
# Represents a table
class Table:
def __init__(self):
pass
def BestIndex(self, constraints, orderbys):
used = []
self.constraints = []
ucount = 0
for c in constraints:
if c[1] in (
apsw.SQLITE_INDEX_CONSTRAINT_GT,
apsw.SQLITE_INDEX_CONSTRAINT_GE,
apsw.SQLITE_INDEX_CONSTRAINT_LT,
apsw.SQLITE_INDEX_CONSTRAINT_LE,
apsw.SQLITE_INDEX_CONSTRAINT_EQ,
):
used.append( ucount ) #tell sqlite we want to use this one
self.constraints.append( c[1] ) #save some for later
else:
used.append( None ) #skip anything we don't understand
ucount += 1
return ( used, # used constraints list
0, # index number - no biggie we only support one right now
)
def Open(self):
return Cursor(self)
def Disconnect(self):
pass
Destroy=Disconnect
# Represents a cursor
class Cursor:
def __init__(self, table):
self.table=table
def Filter(self, indexnum, indexname, constraintargs):
start = 0
self.end = 4000000000
#map constraint arguments to start and end of generation
for tc, ca in zip( self.table.constraints, constraintargs ):
if tc == apsw.SQLITE_INDEX_CONSTRAINT_EQ:
start = ca
self.end = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_LE:
if self.end > ca:
self.end = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_LT:
if self.end >= ca:
self.end = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_GE:
if start < ca:
start = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_GT:
if start >= ca:
start = ca
self.pos = start
def Eof(self):
return self.pos > self.end
def Rowid(self):
return self.pos
def Next(self):
self.pos+=1
def Close(self):
pass
# Register the module as intsource, you can make a bunch if needed
connection.createmodule("intsource", Source())
# Create virtual table to use intsource
cursor.execute("create virtual table uints using intsource()")
# Do some counting
for i in cursor.execute("SELECT rowid FROM uints WHERE rowid BETWEEN 1 AND 100"):
print i
This implements a virtual-table type named "intsource", which by default counts from 0 to 4*10^9. It supports directly filtering by equality and comparison, but any other constraints will still be filtered out by sqlite. Virtual tables are a very powerful concept you can do a lot with, this is probably one of the simplest uses for them. Also, thank you for a good excuse to try out a new virtual table API.
SELECT * FROM Numbers limit 1, 100;
If your goal is to select actual records from a table with values between 1 and 100, use BETWEEN as shown by the other respondents.
If your goal is to generate a sequence of numbers from 1 to 100 without having a table to base it on, I don't believe SQLite has a feature that does this.
SELECT * FROM myTable WHERE myNumber BETWEEN 1 AND 100;
This is more efficient than using 2 WHERE clauses.

Resources