Commit f83808a5 authored by Petra's avatar Petra
Browse files

MSSQL create schema and computed tables for user polarization

parent ec71edf4
-- facebook user polarization
--
-- by Petra Kralj Novak
-- Petra.Kralj.Novak@ijs.si
-- - count number of posts per category
--#####################################################
--USE facebook_norm;
SELECT cat.name, count(p.id)
FROM category1 cat
JOIN page1 pg
ON pg.category_id = cat.id
JOIN post1 p
ON p.page_id = pg.id
group by cat.id, cat.name
-- facebook user polarization
--
-- by Petra Kralj Novak
-- Petra.Kralj.Novak@ijs.si
-- - create table user_comments as count number of comments per category by user
--#####################################################
--USE facebook_norm;
SELECT u.fb_code, science_count, conspiracy_count, troll_count, hoaxbusters_count
INTO user_comments
FROM
user1 u
JOIN
(
SELECT u.fb_code usr, u.name, count(*) science_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN comment1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 1
GROUP BY all u.fb_code, u.name
) science
ON science.usr = u.fb_code
JOIN
(
SELECT u.fb_code usr, count(*) conspiracy_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN comment1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 2
GROUP BY all u.fb_code, u.name
) conspiracy
ON conspiracy.usr = u.fb_code
JOIN
(
SELECT u.fb_code usr, count(*) hoaxbusters_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN comment1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 3
GROUP BY all u.fb_code, u.name
) hoaxbusters
ON hoaxbusters.usr = u.fb_code
JOIN
(
SELECT u.fb_code usr, count(*) troll_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN comment1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 4
GROUP BY all u.fb_code, u.name
) troll
ON troll.usr = u.fb_code;
delete
FROM [egregorem].[dbo].[user_comments]
where ([science_count]+[conspiracy_count]+[troll_count]+[hoaxbusters_count]) =0;
select count(*)
FROM [egregorem].[dbo].[user_comments] ;
\ No newline at end of file
-- facebook user polarization
--
-- by Petra Kralj Novak
-- Petra.Kralj.Novak@ijs.si
-- - create table user_likes as count number of likes per category by category
--#####################################################
--USE facebook_norm;
SELECT u.fb_code, science_count, conspiracy_count, troll_count, hoaxbusters_count
INTO user_likes1
FROM
user1 u
JOIN
(
SELECT u.fb_code usr, u.name, count(*) science_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN like1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 1
GROUP BY all u.fb_code, u.name
) science
ON science.usr = u.fb_code
JOIN
(
SELECT u.fb_code usr, count(*) conspiracy_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN like1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 2
GROUP BY all u.fb_code, u.name
) conspiracy
ON conspiracy.usr = u.fb_code
JOIN
(
SELECT u.fb_code usr, count(*) hoaxbusters_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN like1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 3
GROUP BY all u.fb_code, u.name
) hoaxbusters
ON hoaxbusters.usr = u.fb_code
JOIN
(
SELECT u.fb_code usr, count(*) troll_count
FROM page1 pg
JOIN post1 p
ON pg.id = p.page_id
JOIN like1 l
ON p.id = l.post_id
JOIN user1 u
ON u.fb_code = l.user_fb_code
WHERE pg.category_id = 4
GROUP BY all u.fb_code, u.name
) troll
ON troll.usr = u.fb_code;
delete
FROM [egregorem].[dbo].[user_likes]
where ([science_count]+[conspiracy_count]+[troll_count]+[hoaxbusters_count]) =0;
select count(*)
FROM [egregorem].[dbo].[user_likes]
\ No newline at end of file
B/****** Script for SelectTopNRows command from SSMS ******/
use egregorem;
CREATE TABLE "category" (
"id" int NOT NULL ,
"name" varchar(255) ,
PRIMARY KEY ("id")
) ;
CREATE TABLE "page" (
"id" int NOT NULL ,
"fb_code" varchar(255) UNIQUE ,
"name" varchar(63) ,
"last_update" datetime ,
"category_id" int ,
PRIMARY KEY ("id"),
) ;
CREATE TABLE "post" (
"id" int NOT NULL ,
"page_id" int ,
"message" text,
"creating_time" datetime ,
"type" varchar(255) ,
"link" varchar(511) ,
"fb_code" varchar(255) UNIQUE,
"like1_count" int ,
"comment_count" int ,
"share_count" int ,
"last_update" datetime ,
PRIMARY KEY ("id"),
) ;
CREATE TABLE "comment" (
"id" int NOT NULL ,
"post_id" int ,
"user_fb_code" bigint,
"message" text,
"creating_time" datetime ,
"fb_code" varchar(511) UNIQUE ,
"like1_count" int ,
"last_update" datetime ,
PRIMARY KEY ("id"),
) ;
CREATE TABLE "like1" (
"id" int NOT NULL ,
"post_id" int ,
"user_fb_code" bigint ,
"last_update" datetime ,
PRIMARY KEY ("id"),
) ;
CREATE TABLE "user" (
"fb_code" bigint NOT NULL,
"name" varchar(255) ,
PRIMARY KEY ("fb_code")
) ;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment