Add AUTO INCREMENT column to Sqlite View - sql

I have a table lets called it myTable
tID Name Degree
1 A 23
2 B 55
3 C 77
4 D 45
I want to select only pass Degrees in New View lets call it MyView
The result will be
tID Name Degree
--- ---- --------
2 B 55
3 C 77
But I want myView have sequences start from one by one as follows:
index Name Degree
----- ---- ------
1 B 55
2 C 77
It's possible?
I need to do this because I have a the original table have:
tID sID Name Lesson TryNo Degree
1 1 A a1 1 23
2 1 A a1 2 66
3 2 A b1 1 55
4 2 A b1 2 77
I want to select max tries degree for every lesson exams for each student. ex:
tID sID Name Lesson TryNo Degree
2 1 A a1 2 66
4 2 A b1 2 77
Thanks

I think this is not possible directly since View is just a mirror representation of your tables.
View is created using Select statements, as far as I remember, it is not possible to have a select statement with variables.

Let me know if this works:
CREATE or replace VIEW myView AS
SELECT rownum id,
marks,
name
FROM studentinfo
WHERE degree>50

Related

How to: For each unique id, for each unique version, grab the best score and organize it into a table

Just wanted to preface this by saying while I do have a basic understanding, I am still fairly new to using Bigquery tables and sql statements in general.
I am trying to make a new view out of a query that grabs all of the best test scores for each version by each employee:
select emp_id,version,max(score) as score from `project.dataset.table` where type = 'assessment_test' group by version,emp_id order by emp_id
I'd like to take the results of that query, and make a new table comprised of employee id's with a column for each versions best score for that rows emp_id. I know that I can manually make a table for each version by including a "where version = a", "where version = b", etc.... and then joining all of the tables at the end but that doesn't seem like the most elegant solution plus there is about 20 different versions in total.
Is there a way to programmatically create a column for each unique version or at the very least use my initial query as maybe a subquery and just reference it, something like this:
with a as (
select id,version,max(score) as score
from `project.dataset.table`
where type = 'assessment_test' and version is not null and score is not null and id is not null
group by version,id
order by id),
version_a as (select score from a where version = 'version_a')
version_b as (select score from a where version = 'version_b')
version_c as (select score from a where version = 'version_c')
select
a.id as id,
version_a.score as version_a,
version_b.score as version_b,
version_c.score as version_c
from
a,
version_a,
version_b,
version_c
Example Picture: left table is example data, right table is expected output
Example Data:
id
version
score
1
a
88
1
b
93
1
c
92
2
a
89
2
b
99
2
c
78
3
a
95
3
b
83
3
c
89
4
a
90
4
b
90
4
c
86
5
a
82
5
b
78
5
c
98
1
a
79
1
b
97
1
c
77
2
a
100
2
b
96
2
c
85
3
a
83
3
b
87
3
c
96
4
a
84
4
b
80
4
c
77
5
a
95
5
b
77
Expected Output:
id
a score
b score
c score
1
88
97
92
2
100
99
85
3
95
87
96
4
90
90
86
5
95
78
98
Thanks in advance and feel free to ask any clarifying questions
Use below approach
select * from your_table
pivot (max(score) score for version in ('a', 'b', 'c'))
if applied to sample data in your question - output is
In case if versions is not known in advance - use below
execute immediate (select '''
select * from your_table
pivot (max(score) score for version in (''' || string_agg(distinct "'" || version || "'") || "))"
from your_table
)

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3
table 1
ID name: status : age
1 john F 28
2 peter G 20
3 Roger K 67
Table 2:
ID name: status : age
1 john Y 28
2 peter J 20
3 Roger K 67
4 trump U 120
5 Donald F 450
Table 3 should contain the updated values
1 john Y 28
2 peter J 20
3 Roger K 67
I need to get the updated status of IDs present in table 1 in table 3 how can I do that.
Note: I am using exacttarget SQL activity and update and many more functionalities does not work so I need some work around> I have tried this but this does not work.
UPDATE
1C-C1-MatchStatus_72hoursSubscribers
SET
1C-C1-MatchStatus_72hoursSubscribers.current_status = B.current_status
FROM
1C-C1-MatchStatus_72hoursSubscribers A
INNER JOIN
a_query B
ON
A.current_status = B.current_status

Repetation of column when using join between two table

As per using select query in postgres along 8 or 9 table using join found output as
1. A 2 34
2. A 2 56
3. B 3 34
4. B 3 56
whereas i required output in two form either
1. A 2 34
2. A 2 34
3. B 3 56
4. B 3 56
or
A 2 34
B 3 56
what can i do?
Using distinct?
select distinct * from table

How to create a view using relation from a different table

There are two tables STUDENT and RELATION
Student
Roll No | Name | Marks
1 Peter 40
2 Daniel 45
3 Emma 43
4 Drake 47
5 John 49
Relation
Roll No | Younger Sibling
1 2
2 NULL
3 NULL
4 3
5 NULL
Now i want to create a view which shows data like this
Roll No | Marks | Sibling Marks
1 40 45
2 45 0
3 43 0
4 47 43
5 49 0
Assume that there can only be max two siblings in the table and Roll No is the primary key for both tables. Relation between siblings is only one way (younger sibling). I am really new to SQL, any help would be much appreciated.
Try this:
SELECT
R.RolNo,
COALESCE(S1.Marks, 0) Marks,
COALESCE(S2.Marks, 0) SiblingMarks
FROM Relation R
LEFT JOIN Student S1
ON R.RolNo = S.RolNo
LEFT JOIN Student S2
ON R.YoungerSibling = S2.RolNo

How to get the last non empty value of a hierarchy?

I've got a hierarchy with the appropriate value linked to each level, let's say :
A 100
A1 NULL
A2 NULL
B
B1 NULL
B2 1000
B21 500
B22 500
B3 NULL
This hierarchy is materialized in my database as a parent-child hierarchy
Hierarchy Table
------------------------
Id Code Parent_Id
1 A NULL
2 A1 1
3 A2 3
4 B NULL
5 B1 4
6 B2 4
7 B21 6
8 B22 6
9 B3 4
And here is my fact table :
Fact Table
------------------------
Hierarchy_Id Value
1 100
6 1000
7 500
8 500
My question is : do you know/have any idea of how to get only the last non empty value of my hiearchy?
I know that there an MDX function which could do this job but I'd like to do this in an another way.
To be clear, the desired output would be :
Fact Table
------------------------
Hierarchy_Id Value
1 100
7 500
8 500
(If necessary, the work of flatten the hierarchy is already done...)
Thank you in advance!
If the codes for your hierarchy are correct, then you can use the information in the codes to determine the depth of the hierarchy. I think you want to filter out any "code" where there is a longer code that starts with it.
In that case:
select f.*
from fact f join
hierarchy h
on f.hierarchyId = h.hierarchyId
where not exists (select 1
from fact f2 join
hierarchy h2
on f2.hierarchyId = h2.hierarchyId
where h2.code like concat(h.code, '%') and
h2.code <> h.code
)
Here I've used the function concat() to create the pattern. In some databases, you might use + or || instead.