How to insert multiple rows in Microsoft Access tables? - sql

I have tried every way I can find with Google to insert multiple rows in Access with queries.
None of the these are working for me:
INSERT INTO MyTable (fld02, fld02, fld03)
SELECT '1','2','3'
UNION SELECT '4','5','6'
UNION SELECT '7','8','9'
INSERT INTO faculty1 (first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise)
VALUES ('a','b','Male','January 15, 1955','4202 E Fowler Ave','Tampa','813-974-2268','50000','Computer Engineering');
VALUES ('c','d','Male','October 22, 1948','4202 E Fowler Ave','Tampa','813-974-1538','80000','Computer Engineering');
VALUES ('e','f','Male','May 15, 1933','4202 E Fowler Ave','Tampa','813-974-4425','120000','Computer Engineering');
VALUES ('g','h','Female','December 3, 1960','4202 E Fowler Ave','Tampa','813-974-1276','60000','Computer Engineering');
VALUES ('i','j','Female','November 17, 1962','4202 E Fowler Ave','Tampa','813-974-2154','62000','Computer Engineering');
INSERT INTO faculty1 (first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise)
VALUES ('a','b','Male','January 15, 1955','4202 E Fowler Ave','Tampa','813-974-2268','50000','Computer Engineering');
INSERT INTO faculty1 (first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise)
VALUES ('c','d','Male','October 22, 1948','4202 E Fowler Ave','Tampa','813-974-1538','80000','Computer Engineering');
INSERT INTO faculty1 (first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise)
VALUES ('e','f','Male','May 15, 1933','4202 E Fowler Ave','Tampa','813-974-4425','120000','Computer Engineering');
INSERT INTO faculty1 (first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise)
VALUES ('g','h','Female','December 3, 1960','4202 E Fowler Ave','Tampa','813-974-1276','60000','Computer Engineering');
INSERT INTO faculty1 (first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise)
VALUES ('i','j','Female','November 17, 1962','4202 E Fowler Ave','Tampa','813-974-2154','62000','Computer Engineering');
I even tried a single entry, and didn't work either.
INSERT INTO faculty1 ( first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise )
VALUES ('a', 'b', 'Male', 'January 15, 1955', '4202 E Fowler Ave', 'Tampa', '813-974-2268', '50000', 'Computer Engineering');
Is there something wrong with my CREATE statement?
CREATE TABLE faculty1
(
facultynumber int PRIMARY KEY,
first_name CHAR(20),
last_name CHAR(20),
gender CHAR(10),
birthdate CHAR(25),
address CHAR(50),
city CHAR(20),
phone CHAR(20),
salary INTEGER,
area_of_expertise CHAR(20)
);
http://www.pixhost.org/show/1360/23620036_access_error.jpg
This surprisingly works. Is Access not able to handle you not giving a primary key? I thought this was optional.
http://www.w3schools.com/sql/sql_insert.asp
INSERT INTO faculty1 ( facultynumber, first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise )
VALUES ('5', 'a', 'b', 'Male', 'January 15, 1955', '4202 E Fowler Ave', 'Tampa', '813-974-2268', '50000', 'Computer Engineering');

Salary is defined as an integer but you are trying to insert it with quotes. Also you have to either provide a faculty number or make it identity / counter
INSERT INTO faculty1 (facultynumber, first_name, last_name, gender, birthdate, address, city, phone, salary, area_of_expertise )
VALUES (1, 'a', 'b', 'Male', 'January 15, 1955', '4202 E Fowler Ave', 'Tampa', '813-974-2268', 50000, 'Computer Engineering');

If you want the primary key to be automatically generated, you need to define the column as an Autonumber type.
CREATE TABLE faculty1
(
facultynumber PRIMARY KEY AUTOINCREMENT,
...
More Detail in this thread: https://stackoverflow.com/a/1072938/2712185

Related

Oracle SQL Developer data format

I have created a table 'USER' in Oracle SQL Developer.
CREATE TABLE USER(
USER_ID INTEGER NOT NULL,
FIRST_NAME VARCHAR2(50) NOT NULL,
LAST_NAME VARCHAR2(50) NOT NULL,
ID_CODE INTEGER NOT NULL,
DATE_OF_BIRTH TIMESTAMP NOT NULL,
TRAINING_CLUB VARCHAR2(50) NOT NULL,
HAVE_LICENCE NUMBER(1) NOT NULL,
AGE_GROUP VARCHAR2(3) NULL,
SPECIALITY VARCHAR2(100) NOT NULL,
COMMENT VARCHAR2(200) NULL,
CONSTRAINT USER_PK PRIMARY KEY (USER_ID)
);
Now I'd like to insert data in it, but it will only accept date which is 2000 or more and when it's less than 2000, for example '1998-13-07', it shows not a valid month.
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (1, 'Mari', 'Mets', 4990713134, '1999-13-07', 'Erki Noole Athletics club', 1, 'U23', 'Sprint, hurdle race' );
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (2, 'Meelis', 'Valgepea', 39704230213, '1997-23-04', 'Runningpartner', 1, 'M', 'Middle- ja long distance running');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (3, 'Karina', 'Justinov', 6020330872, '2002-30-03', 'SK Fortis', 1,'U20','Long and high jump');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (4, 'Lewis', 'Kordon', 38711120678, '1987-12-11', 'Niidupargi Athletics club', 1, 'M', 'Shot put');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (5, 'Getter', 'Tihhikov', 49802280417, '1998-28-02', 'Trainingpartner', 1, 'W','Long distance running');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (6, 'Andres', 'Allikvee', 50603212165, '2006-21-03', 'SK Lindon', 1, 'U16', 'Pole vault');
It shows that row 3 and 6 will be added and 1, 2, 4, 5 have not valid month.
create table users(
user_id integer not null,
date_of_birth timestamp not null
);
insert into users values (1, '1999-13-07');
-- ORA-01843: not a valid month
The problem is with the date literal. We are giving a literal string for a timestamp column, so how this is interpreted depends on database settings.
We can either explictly turn the strings to timestamps, providing the proper format specification:
into users values (1, to_timestamp('1999-13-07', 'YYYY-DD-MM'));
-- 1 rows affected
Or we can change the default timestamp format for our current session, so strings are correctly implicitly converted:
alter session set nls_timestamp_format = 'YYYY-DD-MM';
insert into users values (2, '1999-13-07');
-- 1 rows affected
Demo on DB Fiddle

SQL - ORA-00911 - Invalid Character while trying to insert records

Oracle Live SQL.
I am trying to insert records for a table. The table goes through just fine but in each line of records, I am apparently entering some invalid characters. Of course, this is an extremely vague error. I don't know what's wrong.
I've tried changing the date values, tried removing values individually, but no matter what I get the same error.
CREATE TABLE Employee_Information (
employee varchar2(25) NOT NULL,
Address varchar2(50) NOT NULL,
Phone_number CHAR(10) NOT NULL,
Hire_date date,
Position varchar2(20),
Sales_Numbers varchar2(4),
Salary varchar2(6),
PRIMARY KEY(employee)
);
INSERT INTO Employee_Information (employee, address, phone_number,
hire_date, position, sales_numbers, salary)
VALUES (‘John Smith’, ‘123 1st street’, 5554622919, '2017-11-25', ‘assistant manager’, null, 55000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Bob Goldman’, ‘321 2nd street’, 5553392454, '2018-12-13', ‘cashier’, 345, 38000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Harry Wilson’, ‘222 3rd street’, 5553457777, '2018-01-10', ‘cashier’, 401, 42000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Sarah Adams’, ‘333 4th street’, 5555677654, '2019-03-15', ‘cashier’, 316, 36000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Alison Johnson’, ‘777 5th street’, 5559091111, '2017-11-01', ‘manager’, null, 60000);
ORA-00911 for every line of insertion.
try this: (date column you need to convert it to date and use ' chars instead of ‘ and ’)
INSERT INTO Employee_Information (employee, address, phone_number,
hire_date, position, sales_numbers, salary)
VALUES ('John Smith', '123 1st street', 5554622919, to_date('2017-11-25','yyyy-mm-dd') , 'assistant manager', null, 55000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Bob Goldman', '321 2nd street', 5553392454, to_date('2018-12-13','yyyy-mm-dd'), 'cashier', 345, 38000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Harry Wilson', '222 3rd street', 5553457777, to_date('2018-01-10','yyyy-mm-dd'), 'cashier', 401, 42000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Sarah Adams', '333 4th street', 5555677654, to_date('2019-03-15','yyyy-mm-dd'), 'cashier', 316, 36000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Alison Johnson', '777 5th street', 5559091111, to_date('2017-11-01','yyyy-mm-dd'), 'manager', null, 60000);

INSERT ALL error ORA-02291 integrity constraint but PK is referenced

I'm creating a basic sales and inventory DB for a class.
I've successfully created the tables necessary with where I want my PK and FK's to be.
The issue I'm running into now is when I try to perform a multiple insert into my employees table.
Here's what has been created and inserted so far:
CREATE TABLE location (
zipcode int CONSTRAINT zipcode_pk PRIMARY KEY,
city varchar2 (50)
)
;
CREATE TABLE employees (
employeeid int CONSTRAINT employeeid_pk PRIMARY KEY,
firstname varchar2 (50),
lastname varchar2 (50),
street varchar2 (50),
state varchar2 (2),
zipcode int REFERENCES location (zipcode),
datehired date,
phonenum int,
salaryhr number
)
;
CREATE TABLE customer (
customerponum int CONSTRAINT customerponum_pk PRIMARY KEY,
firstname varchar2 (50),
lastname varchar2 (50),
street varchar2 (50),
zipcode int REFERENCES location (zipcode)
)
;
CREATE TABLE products (
productid int CONSTRAINT productid_pk PRIMARY KEY,
productname varchar2 (50),
price number (*,2),
costpercent int
)
;
CREATE TABLE inventory (
productid int REFERENCES products (productid),
unitonhand int,
CONSTRAINT productid_pkt PRIMARY KEY (productid)
)
;
CREATE TABLE sales (
ordernum int CONSTRAINT ordernum_pk PRIMARY KEY,
customerponum int REFERENCES customer (customerponum),
productid int REFERENCES products (productid),
employeeid int REFERENCES employees (employeeid),
saledate date,
unitssold int
)
;
INSERT ALL
INTO location (zipcode, city) VALUES (77095, 'Houston')
INTO location (zipcode, city) VALUES (77451, 'Dallas')
INTO location (zipcode, city) VALUES (77114, 'Austin')
INTO location (zipcode, city) VALUES (77369, 'Lubbock')
INTO location (zipcode, city) VALUES (75451, 'El Paso')
SELECT * FROM dual
;
INSERT ALL
INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (101, 'Josh', 'Smith', '100 Baker St',77095)
INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (102, 'John', 'Doe', '12 Yankee Ave',77451)
INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (103, 'Brandon', 'Markle', '1 Longhorn Blvd',77114)
INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (104, 'Mary', 'Eglin', '223 Aggie St',77369)
INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (105, 'Sue', 'Fields', '91 Patriot',75451)
SELECT * FROM dual
;
'''
---Oracle liveSQL this is the statement that I receive the error
---
INSERT ALL
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (1, 'Jason', 'Wayne', '103 Brown St', 'TX', 77453, '14-may-13', 2814441304, 13)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (2, 'Jacob', 'Dutch', '14 Yawn Rd', 'TX', 77096, '12-july-11', 8325472222, 10)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (3, 'Susan', 'Anthony', '1 Patronas Ln', 'TX', 77231, '08-jan-17', 2819993547, 9)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (4, 'David', 'Lane', '888 Madrid Blvd', 'TX', 78113, '27-dec-18', 8321119876, 8)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (5, 'Anthony', 'Barnard', '21 Adiom Cl', 'TX', 79448, '13-nov-17', 2814558008, 10)
SELECT * FROM dual
;
When I run the statement INSERT ALL INTO employees I get this error:
"ORA-02291: integrity constraint (SQL_NCBYEZZVAYRPDIJSWAZMSKRHK.SYS_C0016383126) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721"
The zip code values in your insert into the employees table don't match the ones you created in the location table, so the error is valid and expected.
Either change the employees zip codes to match the locations you already have, or more likely - since you already use those for customers - add new locations for the zip codes you are trying to use:
INSERT ALL
INTO location (zipcode, city) VALUES (77453, 'Lane City')
INTO location (zipcode, city) VALUES (77096, 'Houston')
INTO location (zipcode, city) VALUES (77231, 'Houston')
INTO location (zipcode, city) VALUES (78113, 'Falls City')
INTO location (zipcode, city) VALUES (79448, 'Richland')
SELECT * FROM dual
;
5 rows inserted.
INSERT ALL
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (1, 'Jason', 'Wayne', '103 Brown St', 'TX', 77453, '14-may-13', 2814441304, 13)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (2, 'Jacob', 'Dutch', '14 Yawn Rd', 'TX', 77096, '12-july-11', 8325472222, 10)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (3, 'Susan', 'Anthony', '1 Patronas Ln', 'TX', 77231, '08-jan-17', 2819993547, 9)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (4, 'David', 'Lane', '888 Madrid Blvd', 'TX', 78113, '27-dec-18', 8321119876, 8)
INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (5, 'Anthony', 'Barnard', '21 Adiom Cl', 'TX', 79448, '13-nov-17', 2814558008, 10)
SELECT * FROM dual
;
5 rows inserted.
db<>fiddle

Selecting data across mulitple tables in Oracle SQL

I am new to SQL and I am trying to lists all organizations by name in ascending order with the maximum salary across all employees who belong to the organization. I have the Schema Below. Can anyone offer any help? I am also trying to lists all employees by name and if the employee is a manager of at least one organization, provide a row in the results with the name
of each org he/she manages else null.
SELECT Org.name, MAX(Employee.salary) AS "Highest salary"
From Org, Employee
GROUP BY Org.name;
The above code gets me a list of all the Org Names with the global max salary, but I am looking for the max of each Org. I think I may need to use some joins but I am not very familiar.
CREATE TABLE Employee
(
employeeId numeric(9) not null,
name varchar2(100) unique,
salary numeric(9) not null,
CONSTRAINT employeeId_pk PRIMARY KEY (employeeId)
);
CREATE TABLE Org
(
orgId numeric(9) not null,
name varchar2(100) not null unique,
managerId numeric(9) not null,
CONSTRAINT orgId_pk PRIMARY KEY (orgId),
CONSTRAINT managerId_fk FOREIGN KEY (managerId)
REFERENCES Employee(employeeId)
);
CREATE TABLE EmployeeOrg
(
employeeId numeric(9) not null,
orgId numeric(9) not null,
CONSTRAINT employeeId_orgId_pk PRIMARY KEY (employeeId, orgId),
CONSTRAINT employeeId_fk FOREIGN KEY (employeeId)
REFERENCES Employee(employeeId),
CONSTRAINT orgId_fk FOREIGN KEY (orgId)
REFERENCES Org(orgId)
);
INSERT ALL
INTO Employee (employeeId, name, salary) VALUES (123, 'Jim', 123)
INTO Employee (employeeId, name, salary) VALUES (456, 'Bill', 1456)
INTO Employee (employeeId, name, salary) VALUES (789, 'Frank', 456)
INTO Employee (employeeId, name, salary) VALUES (987, 'Sara', 45668)
INTO Employee (employeeId, name, salary) VALUES (654, 'Liz', 4456)
INTO Employee (employeeId, name, salary) VALUES (321, 'Morgan', 4556)
SELECT * FROM dual;
INSERT ALL
INTO Org (orgId, name, managerId) VALUES (1, 'Sales', 123)
INTO Org (orgId, name, managerId) VALUES (2, 'HR', 789)
INTO Org (orgId, name, managerId) VALUES (3, 'E Suite', 987)
INTO Org (orgId, name, managerId) VALUES (4, 'Marketing', 654)
SELECT * FROM dual;
INSERT ALL
INTO EmployeeOrg (employeeId, orgId) VALUES (123, 1)
INTO EmployeeOrg (employeeId, orgId) VALUES (789, 2)
INTO EmployeeOrg (employeeId, orgId) VALUES (987, 3)
INTO EmployeeOrg (employeeId, orgId) VALUES (654, 4)
INTO EmployeeOrg (employeeId, orgId) VALUES (123, 4)
INTO EmployeeOrg (employeeId, orgId) VALUES (456, 1)
INTO EmployeeOrg (employeeId, orgId) VALUES (321, 2)
INTO EmployeeOrg (employeeId, orgId) VALUES (789, 4)
INTO EmployeeOrg (employeeId, orgId) VALUES (456, 2)
SELECT * FROM dual;
EDIT (from OP's comment)
The below code gets me a list of all the Org Names with the global max salary, but I am looking for the max of each Org. I think I may need to use some joins but I am not very familiar.
SELECT Org.name, MAX(Employee.salary) AS "Highest salary"
From Org, Employee
GROUP BY Org.name
select o.orgId, min(o.name) as orgName, max(e.salary) as maxSalary
from Org o
inner join EmployeeOrg eo on eo.orgId = o.orgId
inner join Employee e on e.employeeId = eo.employeeId
group by o.orgId
order by orgName;
select e.name as empName, o.name as orgName
from Employee e left outer join Org o on o.managerId = e.employeeId
order by empName, orgName;

How to write a paged, sorted query that also returns group buckets

Trying to write a query that returns paged/sorted results that also has grouping in buckets something like this:
0-9999, 10,000-99,999, 100,000-999,999, 1,000,000-49,999,999, 50,000,000+
Each should have a startIndex on where to jump to when they click on it
Below is the sample query to load this data. The user would see the groups at the bottom of the page, but they depend on what column they are sorting off of. Clicking on one of these groups would then jump the user to that page and index. In my case, there are many more columns, but I think the grouping comes down to either a string, number, or datetime. For the string, the UI wants each letter of the alphabet to be a group, but only those letters that have data. For everything else, there should be around 5 or 6 buckets.
Does anyone know how to do this?
What is this even called? Couldn't find anything on the web, but I'm not sure what to even look for.
declare #startIndex int
declare #pageSize int
declare #Names table
(
FirstName varchar(100),
LastName varchar(100),
Age int,
NetWorth money
)
select #startIndex = 1
select #pageSize = 10
insert into #Names (FirstName, LastName, Age, NetWorth) values ('John', 'Smith', 24, 304050)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('John', 'Williams', 31, 430901)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Adam', 'Wilson', 29, 121000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Jen', 'Phillips', 75, 1450000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Tommy', 'John', 33, 99000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Alex', 'Smith', 48, 12800000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Dianne', 'Lane', 60, 94000000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Charles', 'Barkley', 46, 21500000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Will', 'Allen', 21, 4000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Michael', 'Jordan', 50, 94500000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Jenny', 'Block', 43, 509000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Oprah', 'Winfrey', 61, 55000000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Charles', 'Smith',50, 3400000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('LeBron', 'James', 29, 74678000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Chris', 'Paul', 30, 19400000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Kobe', 'Bryant', 38, 124600000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Antonio', 'Gates', 36, 17040190)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Tony', 'Romo', 35, 28403400)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Nancy', 'Cammo', 44, 167880)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Bill', 'Brown', 45, 98000)
insert into #Names (FirstName, LastName, Age, NetWorth) values ('Paul', 'Jones', 26, 987900);
WITH SearchResults AS
(
SELECT
'RowNum' = ROW_NUMBER() over(ORDER BY NetWorth asc)
,Age
,NetWorth
,FirstName
,LastName
FROM #Names
group by Age, NetWorth, FirstName, LastName
),
counts as (
select 'TotalRows' = count(*) from #Names
),
maxvalue as (
select 'Val' = max(NetWorth) from #Names
)
SELECT sr.RowNum
,sr.FirstName
,sr.LastName
,sr.Age
,sr.NetWorth
,'TotalRows' = c.TotalRows
,'MaxValue' = mv.Val
FROM SearchResults sr
cross apply counts c
cross apply maxvalue mv
WHERE sr.RowNum BETWEEN #startIndex AND #pageSize - #startIndex + 1
/*
select NetWorth,
count(NetWorth)
from #Names
group by NetWorth
order by NetWorth
*/
Any help would be appreciated.
Here's one attempt, I don't have sql-server (which I assume you are using) at hand so you may have to adjust the query slighly:
select * from (
select ROW_NUMBER() over(ORDER BY NetWorth asc) as rn
, Age, NetWorth, FirstName, LastName
, count(*) over () as totalrows
, max(networth) over () as max_value
from names
) as t
where rn between 1 and 10;