-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_query_scripts.sql
660 lines (575 loc) · 21.6 KB
/
project_query_scripts.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
-- Find all references to publications where --- is first author.
---Find publications under "Physics" and display the result university wise.
-- Find the followers of authors whose publication that has maximum citations for a subtopic "Systems"
-- Find a researcher who is very active on Intelliqurom. Being active can be defined by following parameters
--- 1. Whose publication has maximum citations * 0.3
----2. Number of followers * 0.1
----3. Maximum number of profile views * 0.2
----4. Contribution to answers and questions * 0.4
--- Find all questions and answers posted under "Artificial Intelligence"
------------------------------------------------------------------------------------
/*
1;"Artificial intelligence'";3
trial 1
update subtopics set name = 'Artificial intelligence' where subtopicid = 1
select q.discussionforumid, s.name as subtopic_name, t.name as topic_name,
q.question, q.description, a.numberofupvotes as up_voters, a.numberofdownvotes as down_voters,
u.firstname || ' ' || u.lastname as questioner_name,
a.answer, a.date as answer_date, a.replierid,
u.firstname || ' ' || u.lastname as replier_name
from question q
join subtopics s on q.subtopics_subtopicid = s.subtopicid
join topics t on s.topics_topicid = t.topicid
join researcher r on q.researcher_researcherid = r.researcherid
join answerthread a on a.replierid = r.researcherid
join users u on u.userid = r.researcherid
wher e s.name ilike 'Artificial Intelligence' and(a.question_discussionforumid = q.discussionforumid)
trial 2
select
q.discussionforumid,
q.question,
q.description,
a.numberofupvotes,
a.numberofdownvotes,
a.answerid,
a.immediateparentanswerid,
--a1.answer as Immediate_answer,
a.answer,
a.date as answer_date,
s.name as subtopic_name,
t.name as topic_name,
q.researcher_researcherid,
u.firstname || ' ' || u.lastname as questioner_name,
a.replierid,
u1.firstname || ' ' || u1.lastname as replier_name
from question q
join answerthread a on a.question_discussionforumid = q.discussionforumid
join subtopics s on q.subtopics_subtopicid = s.subtopicid
join topics t on s.topics_topicid = t.topicid
join researcher r on q.researcher_researcherid = r.researcherid
join users u on u.userid = r.researcherid
join researcher r1 on a.replierid = r1.researcherid
join users u1 on u1.userid = r1.researcherid
--left outer join answerthread a1 on a1.immediateparentanswerid = a1.answerid
where s.name ilike 'Artificial Intelligence'
--select a1.answer from answerthread left outer join answerthread a1 on a1.immediateparentanswerid = a1.answerid
--and (a.replierid = r.researcherid)
--where q.subtopics_subtopicid = 1
--and q.subtopics_subtopicid = 1
join subtopics s on q.subtopics_subtopicid = s.subtopicid
join topics t on s.topics_topicid = t.topicid
where (a.question_discussionforumid = q.discussionforumid)
and q.subtopics_subtopicid = 1
*/
select * from answerthread where answerid = 45174
delete stored precedure find_question_answers_subtopicwise
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
create function find_question_answers_subtopicwise (text)
returns setof record as $$
with forumdiscussion as
(
select
s.name as subtopic_name,
t.name as topic_name,
q.discussionforumid,
u.firstname || ' ' || u.lastname as questioner_name,
q.question,
q.description,
a.answerid,
a.answer,
u1.firstname || ' ' || u1.lastname as replier_name,
a.date as answer_date,
a.numberofupvotes as NumberOfUpVotes,
a.numberofdownvotes as NumberOfDownVotes,
a.immediateparentanswerid
from question q
join answerthread a on a.question_discussionforumid = q.discussionforumid
join subtopics s on q.subtopics_subtopicid = s.subtopicid
join topics t on s.topics_topicid = t.topicid
join researcher r on q.researcher_researcherid = r.researcherid
join users u on u.userid = r.researcherid
join researcher r1 on a.replierid = r1.researcherid
join users u1 on u1.userid = r1.researcherid
where s.name ilike $1
)
select
fd.subtopic_name,
fd.topic_name,
fd.discussionforumid,
fd.questioner_name,
fd.question,
fd.description,
fd.answerid,
fd.answer,
fd.replier_name,
fd.answer_date,
fd.NumberOfUpVotes,
fd.NumberOfDownVotes,
fd.immediateparentanswerid,
ans.answer
from forumdiscussion fd
left outer join answerthread ans on fd.immediateparentanswerid = ans.answerid
$$ language SQL
-------------------------------------------------------------------------------------
-- Write a stored procedure
create function find_question_answers_subtopic_wise (text)
returns setof record as $$
select
s.name as subtopic_name,
t.name as topic_name,
q.discussionforumid,
u.firstname || ' ' || u.lastname as questioner_name,
q.question,
q.description,
a.answerid,
a.answer,
u1.firstname || ' ' || u1.lastname as replier_name,
a.date as answer_date,
a.numberofupvotes as NumberOfUpVotes,
a.numberofdownvotes as NumberOfDownVotes,
a.immediateparentanswerid
from question q
join answerthread a on a.question_discussionforumid = q.discussionforumid
join subtopics s on q.subtopics_subtopicid = s.subtopicid
join topics t on s.topics_topicid = t.topicid
join researcher r on q.researcher_researcherid = r.researcherid
join users u on u.userid = r.researcherid
join researcher r1 on a.replierid = r1.researcherid
join users u1 on u1.userid = r1.researcherid
where s.name ilike $1
$$ language SQL
------------------------------------------------------------------------------------
-- execute query
select * from find_question_answers_subtopicwise ('Artificial intelligence')
as
(
subtopic_name character varying(100),
topic_name character varying(100),
discussionforumid integer,
questioner_name character varying(1000),
question character varying(1000),
description character varying(2000),
answerid integer,
answer character varying(3000),
replier_name character varying(1000),
answer_date date,
numberofupvotes integer,
numberofdownvotes integer,
immediateparentanswerid integer,
replyanswer varchar
)
-----------------------------------------------------------------------------------------------------
-- Find all references to publications where --- is first author.
"PHYLLIS BERGER"
-- find only reference publications
with authors_publications as
(
select --*
r.researcherid,
u.firstname || ' ' || u.lastname as author_name,
rhp.publications_publicationid as publication_id,
pub.title
from researcher r
join researcher_has_publications rhp on r.researcherid = rhp.researcher_researcherid
join users u on u.userid = r.researcherid
join publications pub on pub.publicationid = rhp.publications_publicationid
where rhp.publication_rank = 1 and (u.firstname ilike 'PHYLLIS') and (u.lastname ilike 'BERGER')
)
select ap.researcherid, ap.author_name, ap.publication_id, ap.title,
pp.publications_referencepublicationid as reference_publication_id,
p.title as reference_title
from authors_publications ap
join publications_has_reference_to_publications pp
on pp.publications_publicationid = ap.publication_id
join publications p on p.publicationid = pp.publications_referencepublicationid
----------------------------------------------------------------------------------------
-- Find only other references (non publication references)
with authors_publications as
(
select --*
r.researcherid,
u.firstname || ' ' || u.lastname as author_name,
rhp.publications_publicationid as publication_id,
pub.title
from researcher r
join researcher_has_publications rhp on r.researcherid = rhp.researcher_researcherid
join users u on u.userid = r.researcherid
join publications pub on pub.publicationid = rhp.publications_publicationid
where rhp.publication_rank = 1 and (u.firstname ilike 'PHYLLIS') and (u.lastname ilike 'BERGER')
)
select ap.researcherid, ap.author_name, ap.publication_id,
ap.title,
ref.referenceid,
r.type as reference_type,
r.text as reference_name
from authors_publications ap
left outer join publications_has_references ref on ref.publicationid = ap.publication_id
left outer join reference r on ref.referenceid = r.referenceid
-------------------------------------------------------------------------------------------
-- Find both types of references
with authors_publications as
(
select --*
r.researcherid,
u.firstname || ' ' || u.lastname as author_name,
rhp.publications_publicationid as publication_id,
pub.title
from researcher r
join researcher_has_publications rhp on r.researcherid = rhp.researcher_researcherid
join users u on u.userid = r.researcherid
join publications pub on pub.publicationid = rhp.publications_publicationid
where rhp.publication_rank = 1 and (u.firstname ilike 'PHYLLIS') and (u.lastname ilike 'BERGER')
),
reference_publications as
(
select ap.researcherid, ap.author_name, ap.publication_id, ap.title,
pp.publications_referencepublicationid as reference_publication_id,
p.title as reference_title
from authors_publications ap
join publications_has_reference_to_publications pp
on pp.publications_publicationid = ap.publication_id
join publications p on p.publicationid = pp.publications_referencepublicationid
)
select rp.researcherid, rp.author_name, rp.publication_id,
rp.title, rp.reference_publication_id,
rp.reference_title,
ref.referenceid,
r.type as reference_type,
r.text as reference_name
from reference_publications rp
left outer join publications_has_references ref on ref.publicationid = rp.publication_id
left outer join reference r on ref.referenceid = r.referenceid
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
-- helper queries
insert into publications_has_references values(2, 67160)
insert into publications_has_references values(3, 67160)
insert into publications_has_references values(4, 67160)
select* from publications_has_reference_to_publications where
publications_publicationid = 67160
select * from publications_has_references where publicationid = 67160
----------------------------------------------------------------------------------------------------
-- profile views daily/weekly/monthly for a particular researcher with name xxx
-- between two dates
select
r.researcherid,
u.firstname || ' ' || u.lastname as researcher_name,
ps.visitorid,
u1.firstname || ' ' || u1.lastname as visitor_name,
ps.visitingdate
from researcher r
join researcher_has_publications rhp on r.researcherid = rhp.researcher_researcherid
join users u on u.userid = r.researcherid
join profilestats ps on ps.researcherid = r.researcherid
join users u1 on u1.userid = ps.visitorid
where (u.firstname ilike 'ATKINSON') and (u.lastname ilike 'WILDER')
and (ps.visitingdate >= ('2012-08-07')::date) and (ps.visitingdate < ('2013-01-10')::date)
-- stored procedure weekly view
create function weekly_stats (text,text,text)
returns setof record as $$
select
r.researcherid,
u.firstname || ' ' || u.lastname as researcher_name,
ps.visitorid,
u1.firstname || ' ' || u1.lastname as visitor_name,
ps.visitingdate
from researcher r
join researcher_has_publications rhp on r.researcherid = rhp.researcher_researcherid
join users u on u.userid = r.researcherid
join profilestats ps on ps.researcherid = r.researcherid
join users u1 on u1.userid = ps.visitorid
where (u.firstname ilike $1) and (u.lastname ilike $2)
and (ps.visitingdate >= ($3)::date) and (ps.visitingdate < (($3)::date + 7))
$$ language SQL
select * from weekly_stats ('ATKINSON', 'WILDER', '2012-10-07')
as
(researcherid integer,
researcher_name varchar,
visitorid integer,
visitor_name varchar,
visitingdate date
)
--- stored procedure monthly view
create function monthly_stats (text,text,text,text)
returns setof record as $$
select
r.researcherid,
u.firstname || ' ' || u.lastname as researcher_name,
ps.visitorid,
u1.firstname || ' ' || u1.lastname as visitor_name,
ps.visitingdate
from researcher r
join researcher_has_publications rhp on r.researcherid = rhp.researcher_researcherid
join users u on u.userid = r.researcherid
join profilestats ps on ps.researcherid = r.researcherid
join users u1 on u1.userid = ps.visitorid
where (u.firstname ilike $1) and (u.lastname ilike $2)
and (date_part('Month',ps.visitingdate)::text = $3) and (date_part('Year',ps.visitingdate)::text = $4)
$$ language SQL
select * from monthly_stats ('ATKINSON', 'WILDER', '10','2012')
as
(researcherid integer,
researcher_name varchar,
visitorid integer,
visitor_name varchar,
visitingdate date
)
------------------------------------------------------------------------------------------------------
---Find publications under "Physics" and display the result university wise.
with publications_under_topic as
(
select
pub.publicationid,
pub.title,
pub.subtopicid,
sub.name as subtopic_name,
t.topicid,
t.name as topic_name
from publications pub
join subtopics sub on sub.subtopicid = pub.subtopicid
join topics t on t.topicid = sub.topics_topicid
where t.name ilike 'Physics'
),
publication_authors as
(
select
pt.publicationid,
rhp.researcher_researcherid as primary_author_id,
rhp.publication_rank,
pt.title,
pt.subtopicid,
pt.subtopic_name,
pt.topicid,
pt.topic_name
from publications_under_topic pt
join researcher_has_publications rhp on rhp.publications_publicationid = pt.publicationid
where rhp.publication_rank = 1
)
select
pa.publicationid,
pa.title,
pa.primary_author_id,
us.firstname || ' ' || us.lastname as primary_author_name,
pa.subtopic_name,
pa.topic_name,
r.universityid,
u.name as university_name
from publication_authors pa
join researcher r on r.researcherid = pa.primary_author_id
join university u on r.universityid = u.universityid
join users us on us.userid = pa.primary_author_id
order by universityid
------------------------------------------------------------------------------------------------------
--- Select all Subtopics with the topic to which they belong.
select s.name as subtopic_name,t.name as topic_name
from subtopics s join topics t
on s.topics_topicid=t.topicid
order by t.name
--Show all publications under topic Computer Science
select pub.title,pub.abstract,pub.publicationviews,s.name,t.name from
publications pub join subtopics s on pub.subtopicid=s.subtopicid
join topics t on s.topics_topicid=t.topicid
where t.name='Computer Science'
-- Researchers with their total number of publications
--all authors with their publication
with all_first_authors as
(select u.firstname ||' ' || u.lastname as researchername ,
pub.title from researcher r
join researcher_has_publications rpub on rpub.researcher_researcherid=r.researcherid
join publications pub on pub.publicationid=rpub.publications_publicationid
join users u on u.userid=r.researcherid
order by researchername)
select count(researchername) as cnt,researchername from all_first_authors
group by researchername
having count(researchername) =5
-- Find all references for publications by author Taylor Rowes.
with all_publications_by_author as
(select pub.publicationid from researcher r
join researcher_has_publications rpub on rpub.researcher_researcherid=r.researcherid
join publications pub on pub.publicationid=rpub.publications_publicationid
join users u on u.userid=r.researcherid
where (u.firstname ||' ' || u.lastname ) ='TAYLOR ROWE' ),
referencedpublication as
(select publications_referencepublicationid as pubids from publications_has_reference_to_publications
where publications_publicationid in (select * from all_publications_by_author))
select pub.title,pub.abstract,pub.publicationviews,s.name,t.name from
publications pub join subtopics s on pub.subtopicid=s.subtopicid
join topics t on s.topics_topicid=t.topicid join
referencedpublication refpub on pub.publicationid=refpub.pubids
-- Find number of citations of publications by author OLLIE BOYLE.
with all_publications_by_author as
(select pub.publicationid from researcher r
join researcher_has_publications rpub on rpub.researcher_researcherid=r.researcherid
join publications pub on pub.publicationid=rpub.publications_publicationid
join users u on u.userid=r.researcherid
where (u.firstname ||' ' || u.lastname ) ='OLLIE BOYLE' ),
cited as
(select publications_publicationid as pubids from publications_has_reference_to_publications
where publications_referencepublicationid in (select * from all_publications_by_author))
select distinct count(*) from
publications pub join
cited refpub on pub.publicationid=refpub.pubids
--- Find researcher with maximum citations
-- Working on this
with all_publications_by_author as
(select rpub.publications_publicationid,
r.researcherid as researcher_id
from researcher r
join researcher_has_publications rpub on rpub.researcher_researcherid=r.researcherid),
/*select * from researcher_has_publications where publications_publicationid = 3
select rpub.publications_publicationid,
r.researcherid as researcher_id
from researcher r
join researcher_has_publications rpub on rpub.researcher_researcherid=r.researcherid
where publications_publicationid=3*/
cited as
(select pp.publications_publicationid as pubids,allp.researcher_id from publications_has_reference_to_publications pp
join all_publications_by_author allp on pp.publications_publicationid=allp.publicationid),
all_researchers as
(select count(*) as count_max,researcher_id from
publications pub join
cited refpub on pub.publicationid=refpub.pubids
group by researcher_id)
select max(count_max) from all_researchers al join
users u on u.userid=al.researcher_id
-------------------------------------------------------------------------------------------------------------------------
-- Find the followers of authors whose publication has maximum citations for a subtopic "Systems"
-- folowers of Author with maximum citations for a subtopic
with subtopic_publications as
(
select
pub.publicationid,
pub.title,
pub.subtopicid,
sub.name
from publications pub
join subtopics sub on pub.subtopicid = sub.subtopicid
and sub.name ilike 'Systems'
)
,
reference_publications as
(
select
sp.publicationid,
sp.title,
sp.subtopicid,
sp.name,
pref.publications_referencepublicationid as reference_pubplication_id
from subtopic_publications sp
join publications_has_reference_to_publications pref on
pref.publications_publicationid = sp.publicationid
),
citations as
(
select
rp.publicationid,
rp.title,
rp.subtopicid,
rp.name,
rp.reference_pubplication_id,
rpub.researcher_researcherid as researcher
from reference_publications rp
join researcher_has_publications rpub on
rp.reference_pubplication_id = rpub.publications_publicationid
),
count_researcher as
(
select count(*) as count_max,
researcher
from citations
group by researcher),
max_researcher as
(
select researcher
from count_researcher
where count_max = (select max(count_max) from count_researcher)
group by researcher
)
select
mr.researcher as researcher_id,
u.firstname || ' ' || u.lastname as researcher_name,
rf.followerid,
u1.firstname || ' ' || u1.lastname as follower_name
from max_researcher mr
join researcher_has_followers rf on rf.researcherid = mr.researcher
join users u on u.userid = mr.researcher
join users u1 on u1.userid = rf.followerid
--select * from researcher_has_followers where researcherid = 30514;
--select * from researcher_has_followers where researcherid = 131280;
-- followers for authors of maximum cited publications for a subtopic
with subtopic_publications as
(
select
pub.publicationid,
pub.title,
pub.subtopicid,
sub.name
from publications pub
join subtopics sub on pub.subtopicid = sub.subtopicid
and sub.name ilike 'Systems'
)
,reference_publications as
(
select
sp.publicationid,
sp.title,
sp.subtopicid,
sp.name,
pref.publications_referencepublicationid as reference_pubplication_id
from subtopic_publications sp
join publications_has_reference_to_publications pref on
pref.publications_publicationid = sp.publicationid
)
,count_publications as
(
select count(*) as count_max,
reference_pubplication_id
from reference_publications
group by reference_pubplication_id
)
,max_publication as
(
select reference_pubplication_id
from count_publications
where count_max = (select max(count_max) from count_publications)
group by reference_pubplication_id
)
,max_author as
(
select
mp.reference_pubplication_id,
rpub.researcher_researcherid as researcher
from max_publication mp
join researcher_has_publications rpub on
mp.reference_pubplication_id = rpub.publications_publicationid
)
select
ma.researcher as researcher_id,
u.firstname || ' ' || u.lastname as researcher_name,
rf.followerid,
u1.firstname || ' ' || u1.lastname as follower_name
from max_author ma
join researcher_has_followers rf on rf.researcherid = ma.researcher
join users u on u.userid = ma.researcher
join users u1 on u1.userid = rf.followerid
/*
select count(c.researcher) from citations c
select count(*) from citations c
select count(distinct c.researcher) from citations c
select * from citations where researcher = 30514
select * from citations
select c.researcher, count(*) as refval from citations c
where c.reference_pubplication_id = 30514
group by c.researcher
select count(distinct c.researcher) from citations c
insert into publications_has_reference_to_publications values (10849,30514)
where rp.publicationid = 9266
select * from researcher_has_publications where publications_publicationid = 30514
select * from publications_has_reference_to_publications where publications_publicationid = 9266
select publications_publicationid,
publications_referencepublicationid,
count(*)as refcount
from publications_has_reference_to_publications
group by publications_referencepublicationid, publications_publicationid
*/