I'm having some trouble figuring this out.
"Choose all people and their orders, it's important that you show their name, id - the products name and the amount. If a person has made 2 orders with the same item, you want to only show 1 order, but add together the amounts from both orders."
This is what I have so far, but I can't wrap my head around how to get the multiple orders only show up as 1 with the amount being added together:
SELECT p.navn, p.id, v.varenavn, o.antal
FROM Vare as v
INNER JOIN Ordre as o
ON v.vareid = o.v_id
INNER JOIN Person as p
ON o.p_id = p.id
These are the tables:
Vare:
create table Vare(
vareid int identity(1,1) primary key,
nummer int,
varenavn nvarchar(100),
højde int,
vægt int,
pris money,
dato nvarchar(20),
datotil nvarchar(20),
)
Ordre:
create table Ordre(
ordreid int identity(1,1) primary key,
dato date,
tid int,
antal int,
totalpris money,
betalingsfrist date,
rabatgruppe int,
v_id int foreign key references Vare(vareid) not null,
p_id int,
)
Person:
create table Person(
id int identity(1,1) primary key,
navn nvarchar(30),
vejnavn nvarchar(50),
postnr varchar(10),
bynavn nvarchar(50),
tlf int,
kategori int,
)
You can try.
sum all antal and group by orders.
SELECT p.navn, p.id, v.varenavn, SUM(o.antal)
FROM Vare as v
INNER JOIN Ordre as o
ON v.vareid = o.v_id
INNER JOIN Person as p
ON o.p_id = p.id
GROUP BY
p.navn, p.id, v.varenavn
Related
I'm trying to do this query : find the champion with the highest number of selections in match for each champion found display their name as well as the number of times they ha\ve been selected in match .
Here are the tables:
CREATE TABLE champions
(
id_champion INT PRIMARY KEY NOT NULL,
name VARCHAR(20),
title VARCHAR(20),
attack INT,
defense INT,
magic INT,
difficulty INT
);
CREATE TABLE players
(
id_player INT PRIMARY KEY NOT NULL,
name VARCHAR(20),
country VARCHAR(20)
);
CREATE TABLE matchs
(
id_match INT,
player INT,
champion INT,
number INT,
gold INT,
team INT,
position VARCHAR(10),
PRIMARY KEY (id_match, player),
FOREIGN KEY(id_match) REFERENCES matchsmeta(id_match) ON DELETE SET NULL,
FOREIGN KEY(player) REFERENCES players(id_player) ON DELETE SET NULL,
FOREIGN KEY(champion) REFERENCES champions(id_champion) ON DELETE SET NULL
);
CREATE TABLE matchsmeta
(
id_match INT PRIMARY KEY NOT NULL,
time INT,
victory INT,
date DATE,
game_mode VARCHAR(10)
);
First aggregate in the table matchs to get the number of times each champion was picked and use MAX() window function to get the max of these counts:
SELECT champion,
COUNT(*) counter,
MAX(COUNT(*)) OVER () max_counter
FROM matchs
GROUP BY champion
Then join the above query to the table champions:
SELECT c.name, m.counter
FROM champions c
INNER JOIN (
SELECT champion,
COUNT(*) counter,
MAX(COUNT(*)) OVER () max_counter
FROM matchs
GROUP BY champion
) m ON m.champion = c.id_champion
WHERE m.counter = m.max_counter;
You have to make the count of times a champion appears in matchs table.
Then you can join the champions table to get the name.
SELECT
m.champion,
c.name,
SUM(1) as num_times_picked
FROM matchs m
LEFT JOIN champions c on (m.champion = c.id_champion)
GROUP BY m.champion
ORDER BY 3 DESC
I'm trying to find out how can I extract the rows with the highest value on Suma column from a table which I did with this script:
SELECT specializare,
Count(codprocedura) AS NrProceduri,
Sum(pret) AS Suma
FROM (SELECT p.pret AS Pret,
fp.specializaredoctor AS Specializare,
fp.codprocedura AS CodProcedura
FROM proceduri p
INNER JOIN fisepacienti fp
ON fp.codpacienti = p.codproc)intermediar
GROUP BY intermediar.specializare
The DB tables can be created with this:
create table Pacienti(
CodP int not null primary key,
Nume varchar(50),
Prenume varchar(50),
Descriere varchar(50),
Varsta int,
DataNasterii varchar(50)
);
create table Proceduri(
CodProc int not null primary key,
Nume varchar(50),
Descriere varchar(50),
Pret int
);
create table FisePacienti(
PRIMARY KEY(CodPacienti, CodProcedura),
CodPacienti int FOREIGN KEY REFERENCES Pacienti(CodP),
CodProcedura int FOREIGN KEY REFERENCES Proceduri(CodProc),
Data varchar(50),
NumeDoctor varchar(50),
PrenumeDoctor varchar(50),
SpecializareDoctor varchar(50),
VechimeDoctor varchar(50)
);
You can use TOP (1) WITH TIES:
SELECT TOP (1) WITH TIES fp.specializare,
Count(fp.codprocedura) AS NrProceduri,
Sum(p.pret) AS Suma
FROM proceduri p JOIN
fisepacienti fp
ON fp.codpacienti = p.codproc
GROUP BY fp.specializare
ORDER BY SumA DESC;
Note that a subquery is not needed for the aggregation.
CREATE TABLE Subject_Marks
(
Marks_ID int primary key,
Maths_Score int,
Science_Score int,
Social_Score int,
English_Score int,
SUPW_Score int,
Student_ID int not null
);
INSERT into Subject_Marks VALUES
(1,1001, 50, 99,98,45,57);
NOT ABLE TO JOIN THIS TABLE WITH OTHER TABLE SHOWN BELOW.
CREATE TABLE Student_Data
(
Student_ID int IDENTITY(1001,1) PRIMARY KEY,
Student_Name VARCHAR(20) NOT NULL,
Student_Address VARCHAR(100),
Student_Phone bigint,
Student_Email VARCHAR(30),
college_id int NOT NULL
);
Since Student_ID is the key providing the relationship, you need to use this in a join. So, you need to construct a join like below for SQL Server:
select sd.* , sm.*
from Student_Data sd
inner join Subject_Marks sm on sm.Student_ID = sd.Student_ID
I have SQL Server tables defined like this:
create table users
(
user_id int NOT NULL IDENTITY(1,1),
name varchar(200),
username varchar(200),
password varchar(50),
encrypted_password varchar(100),
email varchar(30),
phone_no int,
address varchar(200),
PRIMARY KEY(user_id)
)
create table electricity
(
table_name VARCHAR(50),
electricity_bill_id int NOT NULL IDENTITY(1,1),
billing_month_year varchar(50),
units_consumed int,
user_id int,
account_number varchar(100),
amount varchar(100),
due_date varchar(100),
PRIMARY KEY(electricity_bill_id),
FOREIGN KEY(user_id) REFERENCES users(user_id)
);
insert into electricity (table_name, billing_month_year, units_consumed, user_id, account_number, amount, due_date)
values ('Electricity','2015-05-22',13,1,'acc01',2500,'2015-06-22');
create table water
(
table_name VARCHAR(10),
water_bill_id int NOT NULL IDENTITY(1,1),
billing_month_year_date varchar(50),
units_consumed int,
user_id int,
account_number varchar(100),
amount varchar(100),
due_date varchar(100),
PRIMARY KEY(water_bill_id),
FOREIGN KEY(user_id) REFERENCES users(user_id)
)
insert into water(table_name, billing_month_year_date, units_consumed, user_id, account_number, amount, due_date)
values ('Water','2015-04-22',17,1,'acc01',2500,'2015-05-14');
create table telephone
(
table_name VARCHAR(10),
telecom_bill_id int NOT NULL IDENTITY(1,1),
bill_Period varchar(100),
user_id int,
account_number varchar(100),
amount varchar(100),
issued_date varchar(50),
due_date varchar(50),
PRIMARY KEY(telecom_bill_id),
FOREIGN KEY(user_id) REFERENCES users(user_id)
)
insert into telephone(table_name, bill_Period, user_id, account_number, amount, issued_date, due_date)
values ('Telephone', '2015-04-22', 1, 'acc01', 2500, '2015-05-14', '2015-05-18');
The problem is that I want to join electricity, water and telephone tables based on the user id that I need to pass in to the query.
And also I want to pick the maximum due_date from the three tables. But, I've some issues with it.
Please can somebody help me.
One method is to use outer apply:
select u.*, e.*, w.*, t.*
from users u outer apply
(select top 1 e.*
from electricity e
where e.user_id = u.user_id
order by e.due_date desc
) e outer apply
(select top 1 w.*
from water w
where w.user_id = u.user_id
order by w.due_date desc
) w outer appy
(select top 1 t.*
from telephone t
where t.user_id = u.user_id
order by t.due_date desc
) t
where u.user_id = #user_id;
With indexes on (user_id, due_date) on all three tables, the query should have very good performance as well.
If I understand your question well, you want to do something like this:
SELECT MAX(e.due_date) AS ElectricityDueDate, MAX(w.due_date) AS WaterDueDate, MAX(t.due_date) AS TelephoneDueDate
FROM user u, electricity e, water w, telephone t
WHERE u.user_id=#user_id AND
e.user_id=u.user_id AND
w.user_id=u.user_id AND
t.user_id=u.user_id AND
GROUP BY u.user_id
However, to do so you should change the due_date type to DATE, in this way
you can compute the max value.
I have 3 tables
CREATE TABLE Hotell
(
hotelNum int primary key,
hotelName varchar(50),
city varchar(50)
)
CREATE TABLE Roomm
(
roomNum int primary key,
hotelNum int ,
type varchar(50),
price int
)
CREATE TABLE Bookingg
(
hotelNum int,
guestNum int primary key,
dateFrom int,
dateTo int,
roomNum int
)
I need to list the guests who stay at hotels costing more than 250 per night, and I also want to include the hotel name and city in my screen
I hope this could help you
Select
A.guestNum,B.hotelName,C.price
From
Bookingg as [A] Inner Join Hotell as [B] On A.hotelNum = B.hotelNum
Inner join Roomm as [C] B.hotelNum = C.hotelNum
Where
C.price > 250