I have one table 'positions' with columns:
id | session_id | keyword_id | position
and some rows in it:
10 rows with session_id = 1
and 10 with session_id = 2.
As a result of the query I need a table like this:
id | keyword_id | position1 | position2
where 'position1' is a column with values that had session_id = 1 and 'position2' is a column with values that had session_id = 2.
The result set should contain 10 records.
Sorry for my bad English.
Data examle:
id | session_id | keyword_id | position
1 | 1 | 1 | 2
2 | 1 | 2 | 3
3 | 1 | 3 | 0
4 | 1 | 4 | 18
5 | 2 | 5 | 9
6 | 2 | 1 | 0
7 | 2 | 2 | 14
8 | 2 | 3 | 2
9 | 2 | 4 | 8
10 | 2 | 5 | 19
Assuming that you wish to combine positions with the same id, from the two sessions, then the following query should to the trick:
SELECT T1.keyword_id
, T1.position as Position1
, T2.position as Position2
FROM positions T1
INNER JOIN positions T2
ON T1.keyword_id = T2.keyword_id -- this will match positions by [keyword_id]
AND T1.session_id = 1
AND T2.session_id = 2
Related
I have a SQLite table just like this:
the table name is 'surat'
But i want to make id_ayat to be split into different rows using SQLite query, and expected result just like this:
_id|id_surat|id_ayat
---+--------+-------
3 | 2 | 112
3 | 2 | 213
3 | 3 | 19
3 | 3 | 83
3 | 3 | 85
3 | 3 | 102
is that possible? what query that i can use in SQLite format?
With a recursive CTE:
with recursive cte as (
select _id, id_surat, id_ayat,
id_ayat + 0 col
from tablename
union all
select _id, id_surat, trim(substr(id_ayat, length(col) + 2)),
trim(substr(id_ayat, length(col) + 2)) + 0
from cte
where instr(id_ayat, ',')
)
select _id, id_surat, col id_ayat
from cte
order by _id, id_surat
See the demo.
Results:
| _id | id_surat | id_ayat |
| --- | -------- | ------- |
| 3 | 2 | 112 |
| 3 | 2 | 213 |
| 3 | 3 | 19 |
| 3 | 3 | 83 |
| 3 | 3 | 85 |
| 3 | 3 | 102 |
Assuming the following table and using sqlite I have the following question:
Node |Loadcase | Fx | Cluster
---------------------------------
1 | 1 | 50 | A
2 | 1 | -40 | A
3 | 1 | 60 | B
4 | 1 | 80 | C
1 | 2 | 50 | A
2 | 2 | -50 | A
3 | 2 | 80 | B
4 | 2 | -100 | C
I am trying to write a query which fetches the maximum absolute value of Fx and the Load case for each Node 1-4.
An additional requirement is that Fx having the same Cluster shall be summed up before making this query .
In the example above I would expect the following results:
Node | Loadcase | MaxAbsClusteredFx
-----|-----------|-------------------
1 | 1 | 10
2* | |
3 | 2 | 80
4 | 2 | 100
N/A because summed up with node one. Both belonging to cluster A
Query:
For Node 1 I would execute a query similar to this
SELECT Loadcase,abs(Fx GROUP BY Cluster) FROM MyTable WHERE abs(Fx GROUP BY Cluster) = max(abs(Fx GROUP BY Cluster)) AND Node = 1
I keep getting " Error while executing query: near "Forces": syntax error " or alike.
Thankful for any help!
I have these tables in my database:
TOTAL TABLE:
total_table_id | person_id | car_id | year
----------------|-----------|-----------|------
0 | 1 | 4 | 2015
1 | 1 | 2 | 2017
2 | 2 | 0 | 2017
3 | 3 | 3 | 2017
4 | 3 | 4 | 2015
PERSON TABLE:
person_id | name | age
------------|-------|-----
0 | John | 26
1 | Anna | 41
2 | Sam | 33
3 | Tim | 33
CAR TABLE:
car_id | model | color
--------|-------|-------
0 | A | red
1 | B | blue
2 | B | white
3 | D | red
4 | C | black
And what I want after select the year in a dropdown is to get something like this:
2017:
color | age | cars_count
--------|-------|------------
red | 33 | 2
white | 41 | 1
This is the query that I have for the moment:
from a in total
join b in person
on a.person_id equals b.person_id
join c in car
on a.car_id equals c.car_id
select new
{
color = c.color,
age = b.age,
cars_count = ? // <--------This is what I don't know how to get it
}).ToList();
Any tips?
You should use group by statement
var answer = (from total in totalTable
where total.year == 2017
join car in carTable on total.car_id equals car.car_id
join person in personTable on total.person_id equals person.person_id
group person by car.color into sub
select new
{
color = sub.Key,
age = sub.Max(x => x.age),
//or age = sub.Min(x => x.age),
//or age = sub.First().age,
count = sub.Count()
}).ToList();
I have two tables
table1- ---
file_id ,est_edit_id,cal_head_code,cal_spec_code,cal_item_head_code
table 2--
file_id ,est_edit_id,cal_head_code,cal_spec_code,cal_item_head_code,cal_item_code
i want to delete rows from table 2 that does not match values( file_id ,est_edit_id,cal_head_code,cal_spec_code,cal_item_head_code) in table 1
any help on the same
eg
table A contains
-----------
file_id |est_edit_id,| cal_head_code| cal_spec_code| cal_item_head_code
1 | 2 | 3 | 4 | 5
--
table B contains
file_id |est_edit_id ,| cal_head_code | cal_spec_code | cal_item_head_code |cal_item_code
1 | 2 | 3 | 4 | 5 | 20
1 | 2 | 3 | 4 | 5 | 50
7 | 8 | 9 | 10 | 11 |21
i want to delete row containg value 7 | 8 | 9 | 10 | 11 from table B because 7 | 8 | 9 | 10 | 11 is not present in table A
This should do it:
delete from table2 t2
where not exists (select 1
from table1 t1
where t1.file_id = t2.file_id
and t1.est_edit_id = t2.est_edit_id
and t1.cal_head_code = t2.cal_head_code
and t1.cal_spec_code = t2.cal_spec_code);
This assumes that none of the columns contain null values.
SQLFiddle example: http://sqlfiddle.com/#!15/e9e9b/1
i have a dataset in sqlite db which has ids and image urls. It is as follows:
ID | URL
1 | http://img.url1.com
2 |
3 | http://img.url2.com
4 | http://img.url3.com
5 | http://img.url4.com
6 |
7 | http://img.url5.com
8 |
9 | http://img.url6.com
10 |
11 | http://img.url7.com
12 | http://img.url8.com
13 |
I want to sort this accordingly in such a way that the ids with blank values appears at the bottom and the ids with nearest with img urls get appeared at the top with nearest values in the sorted order of ids as follows:
ID | URL
1 | http://img.url1.com
3 | http://img.url2.com
4 | http://img.url3.com
5 | http://img.url4.com
7 | http://img.url5.com
9 | http://img.url6.com
11 | http://img.url7.com
12 | http://img.url8.com
2 |
6 |
8 |
10 |
13 |
Can you suggest me what kind of query I need to put ?
SELECT * FROM T
ORDER BY (URL IS NULL),id
SQLFiddle demo