Consider that my SQLite table has the following schema,
|'''''''''''''''''''''''''''''''''''''
| unique_id(TXT) | object_data(BLOB) |
|'''''''''''''''''''''''''''''''''''''
| | |
| | |
| | |
''''''''''''''''''''''''''''''''''''''
Assume that the row_id property is enabled, and so my question is, for a given unique_id, is it possible to fetch the next 50 records from the table,
using a single SQL query? (Using the row_id property)
Try getting result through this method.
Select * from table_name [WHERE conditions] Limit 50 offset (Select row_id from table_name where unique_id = x);
I have such kind of a data:
table1
id | part | price
1 | ox900 | 100
2 | ox980 | 200
and
table2
id | part | price
1 | ox560 | 560
2 | ox980 | 120
as result I want to get such schema:
id | part | priceTable1 | priceTable2 | minPrice
1 | ox900 | 100 | | 100
1 | ox980 | 200 | 120 | 120
1 | ox560 | | 560 | 560
to simplify it can be without minPrice column...
now I have such query:
SELECT *
FROM (select part, price from supportContacts
union all
select part, price from supportContacts2)
group by part
but it's not exactly what I want to achieve.
Is it possible somehow to do, what I've described above?
Also a fiddle: http://sqlfiddle.com/#!7/f7401/7/0
SQLite does not support full outer joins, so get a list of all parts first, and then look up their prices with left outer joins:
SELECT part,
table1.price AS priceTable1,
table2.price AS priceTable2,
min(ifnull(table1.price, 'inf'),
ifnull(table2.price, 'inf')) AS minPrice
FROM (SELECT part FROM table1
UNION
SELECT part FROM table2)
LEFT JOIN table1 USING (part)
LEFT JOIN table2 USING (part);
(fiddle)
I have a table ABC with duplicate records. I want to Insert only the duplicate records into another table ABC_DUPE in same schema using Bteq.
Any suggestions ?
Thanks,
Mukesh
You can use QUALIFY statement to identify and output duplicates:
Since you didn't share your table... Then consider the following ABC table:
+----+----+----+
| f1 | f2 | f3 |
+----+----+----+
| 1 | a | x |
| 1 | b | y |
| 2 | a | z |
| 2 | b | w |
| 2 | a | n |
+----+----+----+
Where a unique record is determined by using fields f1 and f2. In this example the record where f1=2 and f2='a' is a duplicate with f3 values z and n. To output these we use qualify:
SELECT *
FROM ABC
QUALIFY COUNT(*) OVER (PARTITION BY f1, f2) > 1;
QUALIFY uses Window functions to determine which records to include in the outputted record set. Here we use window function COUNT(*) partitioning by our unique composite key f1, f2. We keep only records where the Count(*) over that partition is greater than 1.
This will output:
+----+----+----+
| f1 | f2 | f3 |
+----+----+----+
| 2 | a | z |
| 2 | a | n |
+----+----+----+
You can use this in a CREATE TABLE statement like:
CREATE TABLE ABC_DUPE AS
(
SELECT *
FROM ABC
QUALIFY COUNT(*) OVER (PARTITION BY f1, f2) > 1
) PRIMARY INDEX (f1, f2);
I'm trying to get a result from two sqlite tables that contain the same columns but have no other relation. Both have the date and amount columns and all I want is a new table as a result to show dates and amounts from both tables.
Table A
+----------+-------+
| date | amount|
+----------+-------+
|10-01-2013| 3.8 |
|12-23-2104| 4.2 |
+----------+-------+
and
Table B
+----------+-------+
| date | amount|
+----------+-------+
|10-03-2013| 2.4 |
|12-28-2014| 3.5 |
+----------+-------+
And the desired table would be
+----------+----------+---------+
| date | A.amount | B.amount|
+----------+----------+---------+
|10-01-2013| 3.8 | NULL |
|10-03-2013| NULL | 2.4 |
|12-23-2104| 4.2 | NULL |
|12-28-2014| NULL | 3.5 |
+----------+----------+---------+
I tried many posts in the forum but I couldn't find any that match my need.
Could you help?
What you need is not a JOIN is a UNION.
see:
UNIONS in SQLITE
Something like:
SELECT Date, Amount as Amount1, NULL AS Amount2
FROM TableA
UNION ALL
SELECT Date, NULL AS Amount1, Amount as Amount2
FROM TableB
I'd like to get a table which shows parameters and values they receive for all of their parametervalues in one query.
This is my table structure:
tbl_parameter
parameterid
parametername
tbl_parametervalues
parameterid
parametervalue
Actual Structure in tbl_parameter
--------------------------------
----------------------------------
| parameterid | parametername |
|----------------------------
| TYPE | Type |
| TEMP | Temp(Deg.C) |
| TIME | Time |
| DATE | Date |
| TECHNICIAN | Technician |
| TESTLENGTH | Test Length |
| TESTRESULT | Test Result |
-----------------------------------
Actual Structure in tbl_parametervalues
------------------------------------
| parameterid | parametervalue |
|----------------------------
| TYPE | DW1 |
| TEMP | 21 |
| TIME | 10:45 PM |
| DATE | 14/09/2011 |
| TECHNICIAN | Test1 |
| TESTLENGTH | 12 |
| TESTRESULT | Pass |
| TYPE | DW2 |
| TEMP | 22 |
| TIME | 11:45 PM |
| DATE | 15/09/2011 |
| TECHNICIAN | Test2 |
| TESTLENGTH | 12 |
| TESTRESULT | Pass
-----------------------------------
I want the result set to look like this:
-----------------------------------------------------------------------------
| SL NO | Type | Temp | Time | Date | Technician | Test |Test |
| | Length |Result |
---------------------------------------------------------------------------
| 1 | DW1 | 21 |10:45 PM|14/09/2011| Test1 | 12 | Pass |
| 2 | DW2 | 22 |11.45 | 15/09/2011| Test2 | 12 | Pass |
|------------------------------------------------------------------------------
How can I accomplish this in SQLite?
I could not find a definition of how to detect sets of parameter values, i.e. which "TEMP" belongs to which "TYPE". So I assume that the sets of parameter values are always entered into the database consecutively and in the order given in the question. The comment by OP seems to allow this assumption. It is not very complicated (though a little) to immplement some robustness against shuffled orders (by associating via a detour by the parameterids), but I hope it is not necessary.
I also could not find information of what "SL NO" means and where the value is coming from. So I fake it by dividing the rowid of the TESTRESULT value by 7 (the number of different parameter names, which I consider to be the size of a parameter set). It should not be hard to dig the correct values up from your database. It is not required that the rowids are on multiples of 7, as long as the parameters are entered consecutively. Just the "SL NO" might skip a few numbers, if the rowids of e.g. "TYPE" are multiples of e.g. 8, otherwise the query tolerates gaps between parameter sets.
You can find the non-query part of my MCVE at the end of this answer.
Query:
select
'SL NO',
TYPE.parametername,
TEMP.parametername,
TIME.parametername,
DATE.parametername,
TECHNICIAN.parametername,
TESTLENGTH.parametername,
TESTRESULT.parametername
from
parameter TYPE,
parameter TEMP,
parameter TIME,
parameter DATE,
parameter TECHNICIAN,
parameter TESTLENGTH,
parameter TESTRESULT
where TYPE.parameterid='TYPE'
and TEMP.rowid=TYPE.rowid+1
and TIME.rowid=TYPE.rowid+2
and DATE.rowid=TYPE.rowid+3
and TECHNICIAN.rowid=TYPE.rowid+4
and TESTLENGTH.rowid=TYPE.rowid+5
and TESTRESULT.rowid=TYPE.rowid+6
UNION ALL
select
TESTRESULT.rowid/7,
TYPE.parametervalue,
TEMP.parametervalue,
TIME.parametervalue,
DATE.parametervalue,
TECHNICIAN.parametervalue,
TESTLENGTH.parametervalue,
TESTRESULT.parametervalue
from
parametervalues TYPE,
parametervalues TEMP,
parametervalues TIME,
parametervalues DATE,
parametervalues TECHNICIAN,
parametervalues TESTLENGTH,
parametervalues TESTRESULT
where TYPE.parameterid='TYPE'
and TEMP.rowid=TYPE.rowid+1
and TIME.rowid=TYPE.rowid+2
and DATE.rowid=TYPE.rowid+3
and TECHNICIAN.rowid=TYPE.rowid+4
and TESTLENGTH.rowid=TYPE.rowid+5
and TESTRESULT.rowid=TYPE.rowid+6
;
make one table on the fly for each pivot column
select from each of those tables the entry for one column
associate entries for the same line via assumptions (as stated above) on rowids
Output:
SL NO Type Temp(Deg.C) Time Date Technician Test Length Test Result
1 DW1 21 10:45 PM 14/09/2011 Test1 12 Pass
2 DW2 22 11.45 15/09/2011 Test2 12 Pass
MCVE (.dump):
BEGIN TRANSACTION;
CREATE TABLE parametervalues(parameterid varchar(30), parametervalue varchar(30) );
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TYPE','DW1');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TEMP','21');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TIME','10:45 PM');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('DATE','14/09/2011');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TECHNICIAN','Test1');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TESTLENGTH','12');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TESTRESULT','Pass');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TYPE','DW2');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TEMP','22');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TIME','11.45');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('DATE',' 15/09/2011');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TECHNICIAN','Test2');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TESTLENGTH','12');
INSERT INTO parametervalues(parameterid,parametervalue) VALUES('TESTRESULT','Pass');
CREATE TABLE parameter (parameterid varchar(30), parametername varchar(30));
INSERT INTO parameter(parameterid,parametername) VALUES('TYPE','Type');
INSERT INTO parameter(parameterid,parametername) VALUES('TEMP','Temp(Deg.C)');
INSERT INTO parameter(parameterid,parametername) VALUES('TIME','Time');
INSERT INTO parameter(parameterid,parametername) VALUES('TATE','Date');
INSERT INTO parameter(parameterid,parametername) VALUES('TECHNICIAN','Technician');
INSERT INTO parameter(parameterid,parametername) VALUES('TESTLENGTH','Test Length');
INSERT INTO parameter(parameterid,parametername) VALUES('TESTRESULT','Test Result');
COMMIT;
Note1:
I skipped the line breaks for the headers of the "Test *" parameter names. That seems irrelevant.
Note2:
My MCVE contains values to match the desired output, not always identical to the sample input. If the values are taken from sample input, the output looks different - and more convincing. It is not important which side of the inconsistence is correct and which is the typo. I chose to implement the version which requires more flexibility.