BigQuery SQL: Execute query over rolling time window - sql

Using BigQuery and Standard SQL I am trying to calculate the retention rate for users seen in one period, compared to users seen in period after. I want to calculate this daily, using the same period offset which will slide as time passes by.
The data used is Google Analytics data, which include these fields:
https://support.google.com/analytics/answer/3437719?hl=en
I have the query to calculate retention, and I know how to set this up to run daily, but I would like to create some history to start with, and then I need to simulate that this query has been running daily for some time.
So my question is how can I create retention rate numbers, for each day, given that the first period is e.g. 60-31 days ago and the following period is 30-1 day(s) ago, all relative to each day.
The query I have:
WITH
users_seen_on_start AS (
SELECT DISTINCT
fullVisitorId AS users
FROM `project.view.ga_sessions_*`, UNNEST(hits) as hits
WHERE _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 60 DAY))
AND FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 31 DAY))
),
num_users AS (
SELECT
count(*) AS num_users_in_cohort
FROM users_seen_on_start
),
engaged_user_by_day AS (
SELECT
COUNT (DISTINCT fullVisitorId) as num_engaged_users
FROM `project.view.ga_sessions_*`, UNNEST(hits) as hits INNER JOIN users_seen_on_start ON users = fullVisitorId
WHERE _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY))
AND FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
)
SELECT
num_engaged_users,
num_users_in_cohort ,
ROUND((num_engaged_users / num_users_in_cohort), 3) as retention_rate
FROM engaged_user_by_day CROSS JOIN num_users
Output from query:
num_engaged_users num_users_in_cohort retention_rate
100871 130632 0.772
Desired output:
date num_engaged_users num_users_in_cohort retention_rate
20190101 100871 130632 0.772
20190102 102356 128044 0.799
Sample data:
users_seen_on_start:
CREATE TABLE users_seen_on_start(
users INTEGER NOT NULL PRIMARY KEY
);
INSERT INTO users_seen_on_start(users) VALUES (6854940999573646134);
INSERT INTO users_seen_on_start(users) VALUES (9215697890064860396);
INSERT INTO users_seen_on_start(users) VALUES (5595285367064974856);
INSERT INTO users_seen_on_start(users) VALUES (2054889847396937366);
INSERT INTO users_seen_on_start(users) VALUES (2159837518531156200);
INSERT INTO users_seen_on_start(users) VALUES (2297077047785095499);
INSERT INTO users_seen_on_start(users) VALUES (15934479773952228986);
INSERT INTO users_seen_on_start(users) VALUES (18388188973174323198);
INSERT INTO users_seen_on_start(users) VALUES (13527051077114159514);
INSERT INTO users_seen_on_start(users) VALUES (10527965347657532651);
INSERT INTO users_seen_on_start(users) VALUES (10056509199904199853);
INSERT INTO users_seen_on_start(users) VALUES (721447367663373337);
INSERT INTO users_seen_on_start(users) VALUES (7418392997259835212);
INSERT INTO users_seen_on_start(users) VALUES (1739158781654194388);
INSERT INTO users_seen_on_start(users) VALUES (13485010633919602577);
INSERT INTO users_seen_on_start(users) VALUES (11647513515368913077);
INSERT INTO users_seen_on_start(users) VALUES (14723171573825482124);
INSERT INTO users_seen_on_start(users) VALUES (316809625899342248);
INSERT INTO users_seen_on_start(users) VALUES (736697877724685769);
INSERT INTO users_seen_on_start(users) VALUES (1069762618672583190);
INSERT INTO users_seen_on_start(users) VALUES (6216571959193109764);
INSERT INTO users_seen_on_start(users) VALUES (8276320148745358024);
INSERT INTO users_seen_on_start(users) VALUES (4390033140354437765);
INSERT INTO users_seen_on_start(users) VALUES (4691956767605638049);
INSERT INTO users_seen_on_start(users) VALUES (8853050929187030210);
INSERT INTO users_seen_on_start(users) VALUES (4866380534293592106);
INSERT INTO users_seen_on_start(users) VALUES (9336123194114580988);
INSERT INTO users_seen_on_start(users) VALUES (9102157575556710064);
INSERT INTO users_seen_on_start(users) VALUES (5656668438554927436);
INSERT INTO users_seen_on_start(users) VALUES (1488391481235428518);
INSERT INTO users_seen_on_start(users) VALUES (2840931994944989396);
INSERT INTO users_seen_on_start(users) VALUES (2881922818148829205);
INSERT INTO users_seen_on_start(users) VALUES (15266979732129081227);
INSERT INTO users_seen_on_start(users) VALUES (17452034639473427980);
INSERT INTO users_seen_on_start(users) VALUES (16885946609916150102);
INSERT INTO users_seen_on_start(users) VALUES (11414196691107747488);
INSERT INTO users_seen_on_start(users) VALUES (11428367061145620067);
INSERT INTO users_seen_on_start(users) VALUES (11589939716097939663);
INSERT INTO users_seen_on_start(users) VALUES (9471966568512958356);
INSERT INTO users_seen_on_start(users) VALUES (10302973548806993195);
INSERT INTO users_seen_on_start(users) VALUES (11655856328192191298);
INSERT INTO users_seen_on_start(users) VALUES (13935768668138194306);
INSERT INTO users_seen_on_start(users) VALUES (12094331062677811830);
INSERT INTO users_seen_on_start(users) VALUES (10077917656361210181);
INSERT INTO users_seen_on_start(users) VALUES (12524832796889539656);
INSERT INTO users_seen_on_start(users) VALUES (12545063140779927439);
INSERT INTO users_seen_on_start(users) VALUES (12842029924433102779);
INSERT INTO users_seen_on_start(users) VALUES (642899976804427792);
INSERT INTO users_seen_on_start(users) VALUES (6445850403127955479);
INSERT INTO users_seen_on_start(users) VALUES (6564816699382875533);
INSERT INTO users_seen_on_start(users) VALUES (4991902095596735494);
INSERT INTO users_seen_on_start(users) VALUES (9240481697386039624);
INSERT INTO users_seen_on_start(users) VALUES (7462479064488338261);
INSERT INTO users_seen_on_start(users) VALUES (7954751513116206324);
INSERT INTO users_seen_on_start(users) VALUES (7916442053878140133);
INSERT INTO users_seen_on_start(users) VALUES (5943783673017806941);
INSERT INTO users_seen_on_start(users) VALUES (8019839094524452470);
INSERT INTO users_seen_on_start(users) VALUES (1325025305488677572);
INSERT INTO users_seen_on_start(users) VALUES (2757934917480873578);
INSERT INTO users_seen_on_start(users) VALUES (2953784252203011629);
INSERT INTO users_seen_on_start(users) VALUES (15663806630564163334);
INSERT INTO users_seen_on_start(users) VALUES (15822234287772625947);
INSERT INTO users_seen_on_start(users) VALUES (16086946171320332009);
INSERT INTO users_seen_on_start(users) VALUES (18326627563023885086);
INSERT INTO users_seen_on_start(users) VALUES (10177146105583960910);
INSERT INTO users_seen_on_start(users) VALUES (11536897925313534298);
INSERT INTO users_seen_on_start(users) VALUES (9521017502744452252);
INSERT INTO users_seen_on_start(users) VALUES (13846940074652198876);
INSERT INTO users_seen_on_start(users) VALUES (12072437921316824606);
INSERT INTO users_seen_on_start(users) VALUES (12130911952369094625);
INSERT INTO users_seen_on_start(users) VALUES (9944467373343765193);
INSERT INTO users_seen_on_start(users) VALUES (10130381296105130198);
INSERT INTO users_seen_on_start(users) VALUES (14503197259750222085);
INSERT INTO users_seen_on_start(users) VALUES (14514330935424697592);
INSERT INTO users_seen_on_start(users) VALUES (14700594671656063689);
INSERT INTO users_seen_on_start(users) VALUES (14735848995356133105);
INSERT INTO users_seen_on_start(users) VALUES (14880164794899465972);
INSERT INTO users_seen_on_start(users) VALUES (12844072088150040888);
INSERT INTO users_seen_on_start(users) VALUES (13244940156815171079);
INSERT INTO users_seen_on_start(users) VALUES (260647987138229776);
INSERT INTO users_seen_on_start(users) VALUES (4223941834652936903);
INSERT INTO users_seen_on_start(users) VALUES (7094513577121476923);
INSERT INTO users_seen_on_start(users) VALUES (9277783379267650134);
INSERT INTO users_seen_on_start(users) VALUES (5996874826262341487);
INSERT INTO users_seen_on_start(users) VALUES (6070125918500272373);
INSERT INTO users_seen_on_start(users) VALUES (1530161613058767114);
INSERT INTO users_seen_on_start(users) VALUES (3564084216977083409);
INSERT INTO users_seen_on_start(users) VALUES (2096791516261012274);
INSERT INTO users_seen_on_start(users) VALUES (17168509252900308876);
INSERT INTO users_seen_on_start(users) VALUES (17616873481220648376);
INSERT INTO users_seen_on_start(users) VALUES (17998058763193684336);
INSERT INTO users_seen_on_start(users) VALUES (16355698068697852664);
INSERT INTO users_seen_on_start(users) VALUES (18429432702234588790);
INSERT INTO users_seen_on_start(users) VALUES (11376613349708048591);
INSERT INTO users_seen_on_start(users) VALUES (11409024415220391296);
INSERT INTO users_seen_on_start(users) VALUES (11497048558563286896);
INSERT INTO users_seen_on_start(users) VALUES (11461240236069178124);
INSERT INTO users_seen_on_start(users) VALUES (10315048118076394592);
INSERT INTO users_seen_on_start(users) VALUES (10534194857330671443);
INSERT INTO users_seen_on_start(users) VALUES (13087206783728054302);
Sample data engaged_user_by_day:
CREATE TABLE engaged_user_by_day(
users INTEGER NOT NULL PRIMARY KEY
);
INSERT INTO engaged_user_by_day(users) VALUES (6854940999573646134);
INSERT INTO engaged_user_by_day(users) VALUES (9215697890064860396);
INSERT INTO engaged_user_by_day(users) VALUES (5595285367064974856);
INSERT INTO engaged_user_by_day(users) VALUES (2054889847396937366);
INSERT INTO engaged_user_by_day(users) VALUES (2159837518531156200);
INSERT INTO engaged_user_by_day(users) VALUES (2297077047785095499);
INSERT INTO engaged_user_by_day(users) VALUES (15934479773952228986);
INSERT INTO engaged_user_by_day(users) VALUES (18388188973174323198);
INSERT INTO engaged_user_by_day(users) VALUES (13527051077114159514);
INSERT INTO engaged_user_by_day(users) VALUES (10527965347657532651);
INSERT INTO engaged_user_by_day(users) VALUES (10056509199904199853);
INSERT INTO engaged_user_by_day(users) VALUES (721447367663373337);
INSERT INTO engaged_user_by_day(users) VALUES (7418392997259835212);
INSERT INTO engaged_user_by_day(users) VALUES (1739158781654194388);
INSERT INTO engaged_user_by_day(users) VALUES (13485010633919602577);
INSERT INTO engaged_user_by_day(users) VALUES (11647513515368913077);
INSERT INTO engaged_user_by_day(users) VALUES (14723171573825482124);
INSERT INTO engaged_user_by_day(users) VALUES (316809625899342248);
INSERT INTO engaged_user_by_day(users) VALUES (736697877724685769);
INSERT INTO engaged_user_by_day(users) VALUES (1069762618672583190);
INSERT INTO engaged_user_by_day(users) VALUES (6216571959193109764);
INSERT INTO engaged_user_by_day(users) VALUES (8276320148745358024);
INSERT INTO engaged_user_by_day(users) VALUES (4390033140354437765);
INSERT INTO engaged_user_by_day(users) VALUES (4691956767605638049);
INSERT INTO engaged_user_by_day(users) VALUES (8853050929187030210);
INSERT INTO engaged_user_by_day(users) VALUES (4866380534293592106);
INSERT INTO engaged_user_by_day(users) VALUES (9336123194114580988);
INSERT INTO engaged_user_by_day(users) VALUES (9102157575556710064);
INSERT INTO engaged_user_by_day(users) VALUES (5656668438554927436);
INSERT INTO engaged_user_by_day(users) VALUES (1488391481235428518);
INSERT INTO engaged_user_by_day(users) VALUES (2840931994944989396);
INSERT INTO engaged_user_by_day(users) VALUES (2881922818148829205);
INSERT INTO engaged_user_by_day(users) VALUES (15266979732129081227);
INSERT INTO engaged_user_by_day(users) VALUES (17452034639473427980);
INSERT INTO engaged_user_by_day(users) VALUES (16885946609916150102);
INSERT INTO engaged_user_by_day(users) VALUES (11414196691107747488);
INSERT INTO engaged_user_by_day(users) VALUES (11428367061145620067);
INSERT INTO engaged_user_by_day(users) VALUES (11589939716097939663);
INSERT INTO engaged_user_by_day(users) VALUES (9471966568512958356);
INSERT INTO engaged_user_by_day(users) VALUES (10302973548806993195);
INSERT INTO engaged_user_by_day(users) VALUES (11655856328192191298);
INSERT INTO engaged_user_by_day(users) VALUES (13935768668138194306);
INSERT INTO engaged_user_by_day(users) VALUES (12094331062677811830);
INSERT INTO engaged_user_by_day(users) VALUES (10077917656361210181);
INSERT INTO engaged_user_by_day(users) VALUES (12524832796889539656);
INSERT INTO engaged_user_by_day(users) VALUES (12545063140779927439);
INSERT INTO engaged_user_by_day(users) VALUES (12842029924433102779);
INSERT INTO engaged_user_by_day(users) VALUES (642899976804427792);
INSERT INTO engaged_user_by_day(users) VALUES (6445850403127955479);
INSERT INTO engaged_user_by_day(users) VALUES (6564816699382875533);
INSERT INTO engaged_user_by_day(users) VALUES (4991902095596735494);
INSERT INTO engaged_user_by_day(users) VALUES (9240481697386039624);
INSERT INTO engaged_user_by_day(users) VALUES (7462479064488338261);

Rolling data in SQL can be implemented using a self-join.
Please see an example of this below that finds data from the past 30 days relative to each day:
from atable a1
left join atable a2 on a1.custid=a2.custid
and datediff(day,a1.dt,a2.dt)<=30 and datediff(day,a1.dt,a2.dt)>0

There are some links that address a time window:
The BigQuery LAG function.
Query: Daily retention
Retention, Using BigQuery and a
Simple Data Model
However, you use case sounds more like Custom Retention Cohorts Using BigQuery and Google Analytics. Even when the link describe a use case for Firebase, the queries explained in there address your main inquiry. For example, the following query address the retention across every three days:
WITH analytics_data AS (
SELECT user_pseudo_id, event_timestamp, event_name, device.mobile_model_name,
UNIX_MICROS(TIMESTAMP("2018-08-01 00:00:00", "-7:00")) AS start_day,
3600*1000*1000*24*3 AS one_week_micros -- Note the *3 at the end here
FROM `firebase-public-project.analytics_153293282.events_*`
WHERE _table_suffix BETWEEN '20180731' AND '20180829'
)

Related

SQL Constraint only if column has a specific value

I would like to add a unique constraint to a sql table but only if the column has a specific value. In my case, I have Geographic IDs and Status as fields. The IDs can have multiple status but there should only be one Current (C) status per ID. Is there a way to involve a where clause to have unique values only in the case of C values?
I tried adding a UNIQUE CONSTAINT as well as a filtered index, to no avail.
In SQL Server you can create filtered indexes, e.g.:
create table dbo.Demo (
GeographicID int,
[Status] char(1)
);
create unique index IXUF_Demo_GeographicID_Current
on dbo.Demo (GeographicID)
where [Status]='C';
insert dbo.Demo (GeographicID, [Status]) values (1, 'A');
insert dbo.Demo (GeographicID, [Status]) values (1, 'A');
insert dbo.Demo (GeographicID, [Status]) values (1, 'B');
insert dbo.Demo (GeographicID, [Status]) values (1, 'B');
insert dbo.Demo (GeographicID, [Status]) values (1, 'C'); --Succeeds
insert dbo.Demo (GeographicID, [Status]) values (1, 'C'); --Fails

Removing count column from query output

select top (5) t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice, count(t2.TruckID) as RentedAmount
from [IndividualTruck-PB] t1
inner join [TruckRental-PB] t2 on t1.TruckID = t2.TruckID
inner join [TruckModel-PB] t3 on t1.TruckModelID = t3.ModelID
group by t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice
order by RentedAmount desc
Bsically, I'm trying to get the top 5 most rented but don't want the actual count column as output only as a means of ordering the output. Is this possible?
You can try remove the count column and give the formula to order by part:
select top (5) t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice
from [IndividualTruck-PB] t1
inner join [TruckRental-PB] t2 on t1.TruckID = t2.TruckID
inner join [TruckModel-PB] t3 on t1.TruckModelID = t3.ModelID
group by t3.Model, t3.Manufacturer, t1.Colour, t1.RegistrationNumber, t1.DailyRentalPrice
order by count(t2.TruckID) desc
My test:
create table A (
col1 varchar(255)
);
insert into A (col1) values ('A');
insert into A (col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('A');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('B');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('C');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('D');
insert into A(col1) values ('E');
insert into A(col1) values ('E');
insert into A(col1) values ('E');
Select for MS SQL Server 2017:
select top(2) col1 from A group by col1 order by count(col1) desc;
Output:
col1
A
B

How do I include certain conditions in SQL Count

`CREATE TABLE PERSON
(
Person_id NUMBER(3) PRIMARY KEY,
Surname VARCHAR2(20),
First_name VARCHAR2(20),
Sex CHAR(1),
Birth_date DATE,
Street VARCHAR2(40),
Town CHAR(20),
Postcode NUMBER(4),
Next_of_kin NUMBER(3)
);
CREATE TABLE STAFF
(
Person_id NUMBER(3) PRIMARY KEY,
Start_date DATE,
Staff_type VARCHAR2(15),
Charges NUMBER(10,2),
Resign_date DATE,
FOREIGN KEY (Person_id) references PERSON(Person_id)
);
CREATE TABLE WARD (
Ward_code CHAR(3) PRIMARY KEY,
Ward_name VARCHAR2(20),
Bed_count NUMBER(4),
Opened_date DATE,
Last_painted_date DATE,
Daily_charge NUMBER(10,2)
);
CREATE TABLE OPERATION_TYPE (
Op_code CHAR(3) PRIMARY KEY,
Operation_name VARCHAR2(50),
Theatre_fee NUMBER(10,2),
Days_in NUMBER(5)
);
CREATE TABLE ADMISSION (
Admission_id NUMBER(3) PRIMARY KEY,
Patient_id NUMBER(3),
Admission_date DATE NOT NULL,
Expected_op CHAR(3),
Admitted_by NUMBER(3),
Ward_code CHAR(3),
Discharge_date DATE,
FOREIGN KEY (Patient_id) references PERSON(Person_id),
FOREIGN KEY (Expected_op) references OPERATION_TYPE(Op_code),
FOREIGN KEY (Admitted_by) references PERSON(Person_id),
FOREIGN KEY (Ward_code) references WARD(Ward_code)
);
CREATE TABLE OPERATION (
Operation_id NUMBER(3) PRIMARY KEY,
Actual_op CHAR(3),
Admission_id NUMBER(3),
Op_date DATE,
Surgeon NUMBER(3),
Anaesthetist NUMBER(3),
FOREIGN KEY (Surgeon) references PERSON(Person_id),
FOREIGN KEY (Anaesthetist) references PERSON(Person_id),
FOREIGN KEY (Actual_op) references OPERATION_TYPE(Op_code),
FOREIGN KEY (Admission_id) references ADMISSION(Admission_id)
);
CREATE TABLE OBSERVATION(
Admission_id NUMBER(3),
Observ_date DATE,
Observ_time NUMBER(4),
Observ_type CHAR(10),
Observ_value NUMBER(4),
Staff_id NUMBER(3),
PRIMARY KEY (Admission_id,Observ_date,Observ_time,Observ_type),
FOREIGN KEY (Admission_id) references ADMISSION(Admission_id),
FOREIGN KEY (Staff_id) references STAFF(Person_id)
);
REM***********************
REM PERSON TABLE
REM***********************
INSERT INTO PERSON VALUES (101,'Black','Barry','M','31/12/1959','11 High St.','Cooma',2630,102);
INSERT INTO PERSON VALUES (102,'Black','Mary','F','11/04/1965','11 High St.','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (103,'Strathclyde','Albert','M','15/5/1955','3 The Mews','Hawthorne',3171,104);
INSERT INTO PERSON VALUES (104,'Strathclyde','Alice','F','17/7/1955','3 The Mews','Hawthorne',3171,103);
INSERT INTO PERSON VALUES (105,'Green','Gill','F','16/6/1966','124 Main St.','Young',2594,106);
INSERT INTO PERSON VALUES (106,'Green','Graham','M','24/4/1967','124 Main St.','Young',2594,105);
INSERT INTO PERSON VALUES (107,'Gray','Lesley','F','12/9/1972','130 Main St.','Young',2594,109);
INSERT INTO PERSON VALUES (109,'Gray','John','M','14/4/1972','130 Main St.','Young',2594,107);
INSERT INTO PERSON VALUES (110,'Samuelson','Thomas','M','1/1/1964','17 The Mews','Hawthorne',3171,NULL);
INSERT INTO PERSON VALUES (111,'Abrahams','Mary','F','15/5/1967','2177A The Esplanade','Ivanhoe',3878,NULL);
INSERT INTO PERSON VALUES (112,'Aumann','Monica','F','25/5/1955','29 The Esplanade','Ivanhoe',3878,NULL);
INSERT INTO PERSON VALUES (113,'Brown','Melissa','F','8/8/1984','11 East St.','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (114,'Napier','Mary','F','1/1/1971','163 New Rd.','Henty',2658,NULL);
INSERT INTO PERSON VALUES (115,'Nelson','Nigel','M','2/2/1972','165 Young Rd.','Temora',2666,NULL);
INSERT INTO PERSON VALUES (116,'Newman','Olive','F','3/3/1973','21 Olympic Way','Henty',2658,NULL);
INSERT INTO PERSON VALUES (117,'Gray','Lesley','M','31/12/1989','130 Andres St.','Young',2594,105);
INSERT INTO PERSON VALUES (118,'Hon','Tasuku','M','13/3/1953','21 Silcon Height','Henty',2658,NULL);
INSERT INTO PERSON VALUES (119,'Livingstone','Frank','M','3/3/2003','21 Sun Height','Henty',2658,122);
INSERT INTO PERSON VALUES (120,'Giggle','Frank','M','23/3/1975','21 Albrige Close','Cooma',2630,121);
INSERT INTO PERSON VALUES (121,'Giggle','Felicia','F','3/3/1980','21 Albrige Close','Cooma',2630,120);
INSERT INTO PERSON VALUES (122,'Black','Frank Jr','M','13/3/2011','21 Stun Height','Henty',2658,123);
INSERT INTO PERSON VALUES (123,'Black','Frances','F','12/12/2005','21 Stun Height','Henty',2658,122);
INSERT INTO PERSON VALUES (124,'Smith','Buddy','M','11/12/1979','101 High St.','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (125,'Smith','Maxime','F','31/12/1979','101 High St.','Cooma',2630,124);
INSERT INTO PERSON VALUES (126,'Smith','Issac','M','1/12/2007','101 High St.','Cooma',2630,124);
INSERT INTO PERSON VALUES (127,'Smith','Ronny','M','3/12/2009','101 High St.','Cooma',2630,124);
INSERT INTO PERSON VALUES (128,'Giggle','Fanny','F','3/12/2007','121 Close Rose','Hillo',2330,120);
INSERT INTO PERSON VALUES (129,'Murad','Nadia','F','3/3/2000','121 Close Rose', 'Hillo',2330,130);
INSERT INTO PERSON VALUES (130,'Murad','Tange','M','3/3/1999','7711 Albrige Close','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (131,'Rome','Paula','F','23/9/1965','21 Height Close','Cooma',2630,132);
INSERT INTO PERSON VALUES (132,'Rome','Paul','M','13/3/1966','21 Height Close','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (133,'Rome','Fay','M','3/3/2017','21 Height Close','Cooma',2630,132);
INSERT INTO PERSON VALUES (134,'Murad','Michelle','F','3/3/2001','1 Height Close','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (135,'Trump','Donald','M','13/3/1956','222 White House Avenue','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (136,'Trump','Melania','F','3/3/1992','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (137,'Trump','Baron','M','3/6/2005','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (138,'Johnson','Boris','M','23/9/1965','10 Downing Street','Wagga Wagga',2999,NULL);
INSERT INTO PERSON VALUES (139,'Cordeiro','Wayne','M','3/3/1965','777 Hawaii Close Rose', 'Hillo',7770, NULL);
INSERT INTO PERSON VALUES (140,'Cordeiro','Anne','F','23/4/1968','777 Hawaii Close Rose', 'Hillo',7770, 139);
INSERT INTO PERSON VALUES (141,'Thatcher','Margaret','F','21/1/1955','120 Main Sq.','Wagga Wagga',2999,142);
INSERT INTO PERSON VALUES (142,'Thatcher','Denis','M','23/9/1955','120 Main Sq.','Wagga Wagga',2999,NULL);
INSERT INTO PERSON VALUES (143,'Thatcher','Carols','F','1/9/1985','120 Main Sq.','Wagga Wagga',2999,142);
INSERT INTO PERSON VALUES (144,'Nelson','Nigel','M','22/2/1992','15 Young Rd.','Temora',2666,NULL);
INSERT INTO PERSON VALUES (145,'Neon','Gela','F','2/2/1972','1465 Main Rd.','Temora',2666,NULL);
INSERT INTO PERSON VALUES (146,'Twain','Shane','F','21/1/1995','A-129 Main Rose Sq.','Wagga Wagga',2650,147);
INSERT INTO PERSON VALUES (147,'Twain','Dens','M','23/9/1985','A-129 Main Rose Sq.','Wagga Wagga',2650,NULL);
INSERT INTO PERSON VALUES (148,'Trump','Ivanka','F','5/3/1985','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (149,'Trump','Eric','M','3/12/1975','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (150,'Gates','Bill','M','5/3/1975','2 Rosey Lane','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (151,'Bucket','Eric','M','3/1/1985','11 Oserey Avenue','Cooma',2630,NULL);
REM*******************************************************
REM STAFF TABLE
REM nursing services will not be charged to the patients
REM*******************************************************
INSERT INTO STAFF VALUES (103,'1/1/2009','Surgeon',4525,NULL);
INSERT INTO STAFF VALUES (110,'5/5/2009','Surgeon',5600,NULL);
INSERT INTO STAFF VALUES (118,'1/5/2016','Surgeon',7890,NULL);
INSERT INTO STAFF VALUES (111,'1/1/2009','Anaesthetist',5900,NULL);
INSERT INTO STAFF VALUES (112,'3/3/2009','Anaesthetist',4788,NULL);
INSERT INTO STAFF VALUES (114,'1/1/2009','Senior Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (115,'2/2/2016','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (116,'3/3/2014','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (125,'23/3/2014','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (135,'1/1/2019','Senior Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (136,'2/2/2016','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (138,'23/3/2019','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (139,'23/3/2020','Senior Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (150,'1/11/2019','Anaesthetist',6900,NULL);
INSERT INTO STAFF VALUES (151,'31/1/2019','Anaesthetist',5900,NULL);
REM***********************
REM WARD TABLE
REM***********************
INSERT INTO WARD VALUES ('C','Covid Bay',90,'31/12/2019',NULL,150.00);
INSERT INTO WARD VALUES ('A','Abraham',80,'1/12/2015','12/05/2011',350.00);
INSERT INTO WARD VALUES ('N','Nightingale',50,'1/12/2017','12/05/2012',450.00);
INSERT INTO WARD VALUES ('F','Flemming',75,'15/11/2009',NULL,230.00);
INSERT INTO WARD VALUES ('L','Lister',80,'1/1/2009','20/12/2013',200.00);
INSERT INTO WARD VALUES ('P','Pasteur',60,'1/12/2009','12/12/2011',250.00);
REM***********************
REM OPERATION_TYPE TABLE
REM***********************
INSERT INTO OPERATION_TYPE VALUES ('LO','Lobotomy',700.00,10);
INSERT INTO OPERATION_TYPE VALUES ('CS','Caesarean',5700.00,3);
INSERT INTO OPERATION_TYPE VALUES ('CT','Cataract',670.00,1);
INSERT INTO OPERATION_TYPE VALUES ('AP','Appendicectomy',500.00,5);
INSERT INTO OPERATION_TYPE VALUES ('HB','Heart Bypass',2000.00,14);
INSERT INTO OPERATION_TYPE VALUES ('HT','Heart Transplant',5000.00,30);
INSERT INTO OPERATION_TYPE VALUES ('HY','Hysterectomy',800.00,7);
INSERT INTO OPERATION_TYPE VALUES ('LA','Leg Amputation',1500.00,10);
INSERT INTO OPERATION_TYPE VALUES ('TS','Tonsillectomy',700.00,7);
INSERT INTO OPERATION_TYPE VALUES ('LP','Laparoscopy',500.00,1);
INSERT INTO OPERATION_TYPE VALUES ('AR','Arthroscopy ',700.00,17);
REM***********************
REM ADMISSION TABLE
REM***********************
INSERT INTO ADMISSION VALUES (205,101,'2/2/2011','HB',114,'P','21/2/2011');
INSERT INTO ADMISSION VALUES (275,101,'1/9/2010','HY',115,'L','1/11/2010');
INSERT INTO ADMISSION VALUES (286,101,'3/5/2016','AR',116,'A','3/7/2016');
INSERT INTO ADMISSION VALUES (303,101,'3/4/2018','LA',125,'F', '13/5/2018');
INSERT INTO ADMISSION VALUES (298,103,'23/1/2016','TS',114,'L','24/04/2016');
INSERT INTO ADMISSION VALUES (299,103,'23/3/2018','AP',114,'L','23/4/2018');
INSERT INTO ADMISSION VALUES (305,103,'23/4/2018','HT',125,'F','29/5/2018');
INSERT INTO ADMISSION VALUES (321,103,'13/8/2018','AR',125,'F', '23/10/2018');
INSERT INTO ADMISSION VALUES (283,105,'3/12/2015','AR',116,'A','5/12/2015');
INSERT INTO ADMISSION VALUES (278,105,'1/1/2011','HB',115,'P','30/1/2011');
INSERT INTO ADMISSION VALUES (307,105,'3/4/2018','TS',125,'F', '13/5/2018');
INSERT INTO ADMISSION VALUES (276,106,'24/8/2010','LA',114,'P','15/9/2010');
INSERT INTO ADMISSION VALUES (287,106,'3/5/2016','TS',114,'A','3/6/2016');
INSERT INTO ADMISSION VALUES (274,109,'1/9/2019','AP',114,'P','9/9/2019');
INSERT INTO ADMISSION VALUES (288,109,'23/5/2016','LO',114,'F','3/07/2016');
INSERT INTO ADMISSION VALUES (301,112,'13/4/2018','AP',125,'F','16/4/2018');
INSERT INTO ADMISSION VALUES (304,112,'23/4/2019','LO',114,'L','23/5/2019');
INSERT INTO ADMISSION VALUES (279,113,'3/9/2010','TS',115,'F','10/9/2010');
INSERT INTO ADMISSION VALUES (284,113,'3/12/2015','HY',116,'A','03/01/2016');
INSERT INTO ADMISSION VALUES (285,113,'3/5/2016','HT',116,'A','3/6/2016');
INSERT INTO ADMISSION VALUES (300,113,'23/4/2018','AR',114,'L','25/6/2018');
INSERT INTO ADMISSION VALUES (306,113,'13/8/2018','AP',125,'L', '13/9/2018');
INSERT INTO ADMISSION VALUES (277,114,'20/9/2010','AP',115,'P','30/9/2010');
INSERT INTO ADMISSION VALUES (289,115,'11/4/2016','LO',114,'L','3/6/2016');
INSERT INTO ADMISSION VALUES (290,115,'5/7/2016','TS',114,'L','3/09/2016');
INSERT INTO ADMISSION VALUES (308,115,'23/3/2018','AR',114,'L','25/3/2018');
INSERT INTO ADMISSION VALUES (280,117,'13/9/2010','AP',115,'F','25/9/2010');
INSERT INTO ADMISSION VALUES (281,117,'3/9/2014','HB',116,'A','21/9/2014');
INSERT INTO ADMISSION VALUES (282,117,'3/12/2015','LA',116,'A','14/12/2015');
INSERT INTO ADMISSION VALUES (309,126,'23/3/2018','TS',125,'L', '13/5/2018');
INSERT INTO ADMISSION VALUES (310,127,'13/5/2018','AP',125,'L', '28/5/2018');
INSERT INTO ADMISSION VALUES (311,124,'3/5/2018','LO',125,'A', '23/5/2018');
INSERT INTO ADMISSION VALUES (312,127,'21/6/2019','LO',125,'L','22/8/2019');
INSERT INTO ADMISSION VALUES (313,124,'22/6/2019','AP',125,'A','22/7/2019');
INSERT INTO ADMISSION VALUES (314,109,'21/6/2019','LO',125,'L','22/7/2019');
INSERT INTO ADMISSION VALUES (315,126,'12/6/2019','AP',125,'A','22/9/2019');
INSERT INTO ADMISSION VALUES (316,114,'22/7/2019','HB',125,'A','12/12/2019');
INSERT INTO ADMISSION VALUES (318,128,'3/5/2019','LA',116,'A','4/6/2019');
INSERT INTO ADMISSION VALUES (319,129,'23/3/2019','TS',125,'L', '13/4/2019');
INSERT INTO ADMISSION VALUES (320,130,'3/5/2019','LA',116,'F','4/6/2019');
INSERT INTO ADMISSION VALUES (328,119,'3/3/2019','TS',115,'N', '3/4/2019');
INSERT INTO ADMISSION VALUES (322,132,'3/5/2019','LA',116,'A','4/6/2019');
INSERT INTO ADMISSION VALUES (323,133,'23/3/2019','TS',125,'L', '13/4/2019');
INSERT INTO ADMISSION VALUES (324,131,'3/5/2018','LA',116,'F','4/5/2018');
INSERT INTO ADMISSION VALUES (325,118,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (326,102,'3/5/2018','LA',116,'F','4/5/2018');
INSERT INTO ADMISSION VALUES (327,104,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (339,107,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (329,110,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (330,111,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (331,116,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (332,121,'21/6/2019','TS',125,'L','22/7/2019');
INSERT INTO ADMISSION VALUES (333,123,'22/7/2019','AP',125,'A','22/9/2019');
INSERT INTO ADMISSION VALUES (334,134,'21/8/2019','AP',115,'L','22/12/2019');
INSERT INTO ADMISSION VALUES (335,128,'12/8/2020','AP',115,'A',NULL);
INSERT INTO ADMISSION VALUES (336,125,'22/7/2020','HB',115,'A',NULL);
INSERT INTO ADMISSION VALUES (337,120,'21/8/2020','AP',116,'L',NULL);
INSERT INTO ADMISSION VALUES (338,130,'22/7/2020','AP',125,'N',NULL);
INSERT INTO ADMISSION VALUES (340,131,'22/8/2020','AP',125,'N',NULL);
INSERT INTO ADMISSION VALUES (341,122,'22/8/2020','AP',125,'N',NULL);
INSERT INTO ADMISSION VALUES (490,139,'11/8/2020','AP',125,'F',NULL);
INSERT INTO ADMISSION VALUES (491,140,'7/9/2020','AP',138,'F',NULL);
REM***********************
REM OPERATION TABLE
REM***********************
INSERT INTO OPERATION VALUES (317,'HB',205,'4/2/2011',110,111);
INSERT INTO OPERATION VALUES (355,'LA',276,'25/8/2010',103,111);
INSERT INTO OPERATION VALUES (363,'AP',276,'3/9/2010',103,111);
INSERT INTO OPERATION VALUES (360,'AP',277,'22/9/2010',103,111);
INSERT INTO OPERATION VALUES (361,'AP',274,'2/9/2010',103,111);
INSERT INTO OPERATION VALUES (362,'HT',275,'3/9/2010',110,112);
INSERT INTO OPERATION VALUES (364,'AP',279,'3/9/2010',103,111);
INSERT INTO OPERATION VALUES (365,'AP',280,'6/9/2010',103,111);
INSERT INTO OPERATION VALUES (366,'HB',281,'6/9/2014',103,111);
INSERT INTO OPERATION VALUES (367,'HY',284,'6/12/2015',103,111);
INSERT INTO OPERATION VALUES (368,'LO',285,'6/5/2016',118,112);
INSERT INTO OPERATION VALUES (369,'AP',286,'6/5/2016',118,112);
INSERT INTO OPERATION VALUES (370,'TS',287,'7/5/2016',118,112);
INSERT INTO OPERATION VALUES (371,'LA',288,'26/5/2016',110,111);
INSERT INTO OPERATION VALUES (372,'LO',289,'16/4/2016',110,111);
INSERT INTO OPERATION VALUES (373,'TS',290,'6/5/2016',110,111);
INSERT INTO OPERATION VALUES (381,'TS',298,'26/2/2016',110,111);
INSERT INTO OPERATION VALUES (382,'AP',299,'26/3/2018',110,111);
INSERT INTO OPERATION VALUES (383,'TS',300,'26/5/2018',110,111);
INSERT INTO OPERATION VALUES (384,'TS',309,'16/4/2018',110,111);
INSERT INTO OPERATION VALUES (385,'LO',309,'26/3/2018',110,111);
INSERT INTO OPERATION VALUES (386,'AP',310,'18/5/2018',110,111);
INSERT INTO OPERATION VALUES (387,'AP',311,'4/5/2018',110,111);
INSERT INTO OPERATION VALUES (388,'LO',311,'11/5/2018',110,111);
INSERT INTO OPERATION VALUES (389,'AP',315,'13/6/2019',118,111);
INSERT INTO OPERATION VALUES (390,'HB',316,'23/7/2019',118,111);
INSERT INTO OPERATION VALUES (391,'LO',312,'22/6/2019',118,111);
INSERT INTO OPERATION VALUES (392,'HT',341,'28/8/2019',118,112);
INSERT INTO OPERATION VALUES (393,'HT',340,'27/8/2019',118,112);
INSERT INTO OPERATION VALUES (394,'LO',491,'26/4/2020',103,112);
REM***********************
REM OBSERVATION TABLE
REM***********************
INSERT INTO OBSERVATION VALUES (205,'2/2/2011',1500,'Temp',38,114);
INSERT INTO OBSERVATION VALUES (274,'4/9/2010',0601,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (275,'1/9/2010',1400,'Pulse',64,115);
INSERT INTO OBSERVATION VALUES (275,'1/9/2010',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (275,'3/9/2010',1800,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (275,'3/9/2010',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (275,'4/9/2010',0610,'Pulse',82,116);
INSERT INTO OBSERVATION VALUES (275,'4/9/2010',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (277,'24/9/2010',0600,'Temp',39,115);
INSERT INTO OBSERVATION VALUES (284,'5/12/2015',0600,'Temp',39,115);
INSERT INTO OBSERVATION VALUES (284,'5/12/2015',0600,'Pulse',89,115);
INSERT INTO OBSERVATION VALUES (284,'6/12/2015',0600,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (284,'6/12/2015',0600,'Pulse',87,115);
INSERT INTO OBSERVATION VALUES (285,'7/5/2016',0600,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (285,'8/5/2016',0600,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (285,'8/5/2016',0600,'Pulse',82,115);
INSERT INTO OBSERVATION VALUES (285,'9/5/2016',0600,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (286,'7/5/2016',0600,'Temp',39,125);
INSERT INTO OBSERVATION VALUES (286,'8/5/2016',0600,'Temp',33,125);
INSERT INTO OBSERVATION VALUES (286,'8/5/2016',0600,'Pulse',86,125);
INSERT INTO OBSERVATION VALUES (286,'9/5/2016',0600,'Temp',36,125);
INSERT INTO OBSERVATION VALUES (287,'7/5/2016',0600,'Temp',35,115);
INSERT INTO OBSERVATION VALUES (287,'8/5/2016',0600,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (287,'8/5/2016',0600,'Pulse',82,125);
INSERT INTO OBSERVATION VALUES (287,'9/5/2016',0600,'Temp',37,125);
INSERT INTO OBSERVATION VALUES (289,'19/5/2016',0600,'Temp',39,115);
INSERT INTO OBSERVATION VALUES (289,'20/5/2016',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (289,'22/5/2016',0800,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (289,'26/5/2016',0700,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (289,'29/5/2016',0700,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',1400,'Pulse',74,115);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',1800,'Temp',30,116);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (300,'24/4/2018',0610,'Pulse',82,116);
INSERT INTO OBSERVATION VALUES (300,'24/4/2018',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (303,'13/4/2018',1400,'Pulse',64,125);
INSERT INTO OBSERVATION VALUES (303,'13/4/2018',1400,'Temp',38,125);
INSERT INTO OBSERVATION VALUES (303,'13/4/2018',1800,'Temp',30,125);
INSERT INTO OBSERVATION VALUES (303,'23/4/2018',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (303,'23/4/2018',0610,'Pulse',82,116);
INSERT INTO OBSERVATION VALUES (303,'23/4/2018',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (311,'3/5/2018',1400,'Pulse',54,115);
INSERT INTO OBSERVATION VALUES (311,'4/5/2018',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (311,'3/5/2018',1800,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (311,'4/5/2018',2200,'Temp',41,116);
INSERT INTO OBSERVATION VALUES (311,'4/5/2018',1800,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (311,'5/5/2018',0610,'Pulse',72,116);
INSERT INTO OBSERVATION VALUES (311,'5/5/2018',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (310,'13/5/2018',1400,'Pulse',84,125);
INSERT INTO OBSERVATION VALUES (310,'13/5/2018',1400,'Temp',28,125);
INSERT INTO OBSERVATION VALUES (310,'14/5/2018',1800,'Temp',60,125);
INSERT INTO OBSERVATION VALUES (310,'15/5/2018',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (310,'16/5/2018',0610,'Pulse',92,116);
INSERT INTO OBSERVATION VALUES (310,'16/5/2018',0610,'Temp',35,116);
INSERT INTO OBSERVATION VALUES (341,'22/8/2019',0610,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (340,'25/8/2019',0610,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (340,'25/8/2019',0610,'Pulse',75,116);
INSERT INTO OBSERVATION VALUES (340,'26/8/2019',0610,'Temp',37,116);
INSERT INTO OBSERVATION VALUES (340,'26/8/2019',0610,'Pulse',70,116);
INSERT INTO OBSERVATION VALUES (340,'27/8/2019',0610,'Pulse',68,116);
INSERT INTO OBSERVATION VALUES (340,'27/8/2019',0610,'Temp',37,116);
INSERT INTO OBSERVATION VALUES (340,'29/8/2019',0610,'Pulse',73,116);
INSERT INTO OBSERVATION VALUES (490,'17/4/2020',0600,'Temp',38,125);
INSERT INTO OBSERVATION VALUES (490,'18/4/2020',0600,'Temp',37,125);
INSERT INTO OBSERVATION VALUES (490,'19/4/2020',0600,'Temp',38,125);
select town, c.count(2021- to_char(birth_date,'YYYY'))100/count() as "Population"
from person, person c
group by town
Using Apex Oracle here, I need to set a condition for c.count where it only takes into account >60, but whenever I put the condition in, errors pop up, would greatly appreciate any help possible.
I'm assuming you don't really need a cross join of 'person' with 'person'. So I'm dropping that logic from the sources.
I don't have and Oracle instance to work with, but I believe the code below, which uses a case statement to achieve your conditional need, should work for you:
select town,
c.count(
case when ap.age > 60 then ap.age end
) 100 / count() as "Population"
from person
cross apply (select age = 2021 - to_char(birth_date,'YYYY')) ap
group by town

Finding max value from count without using max

I have
Voters(name,vote, score,time)
Where name is the primary key and vote the ID of the person whom the voters voted for.
1 person can vote more than once.
I have to find the name of the person who has voted the most number of times.
{Maximum Count(vote)} without using max command.
If you can use count(), then try ordering your results by it. In MySQL, your query could look like
SELECT name, COUNT(vote) AS total_votes
FROM Voters
GROUP BY name
ORDER BY total_votes DESC
LIMIT 1
This would return the person who has voted the most together with his vote count.
You can use concept from relational algebra. limit 1 does not give good result when more than one user have the same number of votes.
On PostgreSQL:
with t as (select name, count(*) c from voters group by name)
SELECT t.* from t
except
SELECT t.* from t JOIN t t2 ON t2.c > t.c
Example on SQL Fiddle
And sample data to prove this works:
create table voters (name int,vote int);
insert into voters values (1,1);
insert into voters values (1,1);
insert into voters values (1,1);
insert into voters values (1,1);
insert into voters values (1,1);
insert into voters values (1,1);
insert into voters values (1,1);
insert into voters values (1,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (2,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (3,1);
insert into voters values (4,1);
insert into voters values (4,1);
insert into voters values (4,1);
insert into voters values (4,1);
insert into voters values (4,1);
insert into voters values (5,1);
insert into voters values (5,1);
insert into voters values (5,1);
insert into voters values (5,1);
My solution is based on answer to this question: How can I find MAX with relational algebra?
There is also good explanation of this concept in another answer here Aggregate Relational Algebra (Maximum)
Try Following
select count(*) from table where vote=(select max(count(*)) from table) group by vote
Hope this helps you.

Sql Server query with date filter

I have a table like this;
ID int,
OrderedDate DateTime
I want to select only records of followed month.
For example result set:
ID OrderedDate
110 January
110 February
200 January
200 February
How can I write this query?
I think you want list of months that ID has orders in but with the months sorted by the month number instead of the name?
create table test21210
(
id int,
OrderedDate datetime
)
go
insert test21210 (id, OrderedDate) values (110, '1/1/2010')
insert test21210 (id, OrderedDate) values (110, '1/5/2010')
insert test21210 (id, OrderedDate) values (110, '1/10/2010')
insert test21210 (id, OrderedDate) values (110, '2/2/2010')
insert test21210 (id, OrderedDate) values (110, '2/4/2010')
insert test21210 (id, OrderedDate) values (110, '2/6/2010')
insert test21210 (id, OrderedDate) values (200, '1/3/2010')
insert test21210 (id, OrderedDate) values (200, '1/5/2010')
insert test21210 (id, OrderedDate) values (200, '1/7/2010')
insert test21210 (id, OrderedDate) values (200, '1/9/2010')
insert test21210 (id, OrderedDate) values (200, '2/3/2010')
insert test21210 (id, OrderedDate) values (200, '2/5/2010')
insert test21210 (id, OrderedDate) values (200, '2/7/2010')
insert test21210 (id, OrderedDate) values (200, '2/9/2010')
go
with idmonth (id, MonthNumber) as
(
select id, MONTH(ordereddate) as 'MonthNumber'
from test21210
group by id, MONTH(ordereddate)
)
select id, DATENAME(MONTH, STR(MonthNumber)+'/1/2000')
from idmonth
order by id, MonthNumber
The question seems a bit unclear. But the example makes it look like you are wanting to sort by ID then by month name. If so, then I think this will do it. I don't have SQL Server to test it, so I'm sure it has syntax or other errors.
SELECT ID, DATENAME(month, OrderedDate) AS OrderedDate from table
ORDER BY 1, MONTH( OrderedDate )