Input:
Table1->Student details
sno sname scourse sjoindate
1 Ram bsc 11/26/2011
2 Sham bcom 10/06/2010
3 Krishna ba 06/16/2012
Table2->Student marks
sno id marks
1 1 67
1 2 77
1 3 80
1 4 60
1 5 90
Table3->Subjectnames
id subjectname
1 Computerscience
2 Maths
3 Satatistics
4 English
5 Hindi
Table4->Student_feedback_QuestionandAnswer
sno Questions Answers
1 Q1 A1
1 Q2 A2
1 Q3 A3
1 Q4 A4
Expected Output:
sno sname scourse sjoindate Computerscience Maths Statistics English Hindi Questions Answers
1 Ram bsc 11/26/2011 67 77 80 60 90 Q1 A1
Q2 A2
Q3 A3
Q4 A4
In the above tables the rows of Table2 and Table3 can increase and decrease depending on the studentname.
I need tabled to combine the data from all four tables into one result set but I don't know how to use pivot with dynamic header with dynamic column values:
Data from Table3.Subjectname column values should be header column names e.g. Computerscience, Hindi.
Data from Table2.marks should be the value under the header column name e.g. marks for id=1 under Computerscience.
the Questions, Answers column from Table4 should come in result set depending on the value of `Table1.sno'
If you can provide a hint for creating a table using pivot with dynamic header with dynamic column values ,yeah using two tables.
Please let me know for more clarification.
Related
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
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
This is in relation to my survey application for our team. I have 3 tables in my database related to this problem.
I apologize if the database is not fully normalized.
TBL_CHURCH columns:
1 FAM_CHURCH_SACRMNT_NUM (Primary Key) Int(15)
2 RSPONDNT_NUM
3 SURVYR_NUM
4 QN_NUMBER
5 CHRCHFAMLY_NAME
6 CHRCHFAMLY_ISBAPTIZED
Sample row based on order of columns above:
1 2 3 4 5 6
6422164 76826499 5712 362 Serio Tecson Jr. Yes
TBL_INTRVW columns:
1 QN_NUMBR (Primary Key)
2 SURVYR_NUM
3 ZONE_NUM
4 RSPONDNT_NUM
Sample row based on order of columns above:
1 2 3 4
362 5712 11 76826499
TBL_AREA columns:
1 BRGY_ZONE_NUM (Primary Key)
2 BRGY_CODE
Sample row based on order of columns above:
1 2
11 2A
21 2A
31 2A
The field CRCHFAMLY_ISBAPTIZED has only two values. A "Yes" or a "No" and each row has a QN_NUMBR value that is referenced to TBL_INTRVW and each QN_NUMBR on TBL_INTRVW has a unique ZONE_NUM that is referenced to TBL_AREA and that ZONE_NUM has a corresponding BRGY_CODE. Each BRGY_CODE have at least 2 ZONE_NUM values
My problem is that I want to count the number of people baptized in a given area.
The output more or less should look like this:
(The output is collected from the 3 different ZONE_NUM)
Zone Name Num of People Baptized
2A 20
I'm having what trouble what to use in my SQL statements. Should I use a WHERE within an INNER JOIN? And how do I go about in my SELECT statements?
SELECT c.BRGY_ZONE_NUM,count(a.CHRCHFAMLY_ISBAPTIZED) as [Num of People Baptized]
from TBL_CHURCH a
left join
TBL_INTRVW b
on a.QN_NUMBER=b.QN_NUMBER
left join
TBL_AREA c
on b.ZONE_NUM=cRGY_ZONE_NUM
where a.CHRCHFAMLY_ISBAPTIZED='Yes'
group by c.BRGY_ZONE_NUM
I dont see Zone Name column on the three table, so i used BRGY_ZONE_NUM
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
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.