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
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 Need help in these tables, i want SQL statement to give me this :
the players who played in half or more of the games, lets assume that we have 6 different games in 'GameType' table ... So I want to display all players who played 3 or more games.
like :
ID surName number of games played
1 test1 3
2 test2 4
3 test3 3
4 test4 6
this what i done until now:
SELECT dbo.Player.ID, dbo.Player.surName, count(DISTINCT
dbo.PlayerInAGame.gameTypeName) AS games_played, count(DISTINCT
dbo.Game.gameTypeName) AS games_count
FROM dbo.Player INNER JOIN
dbo.PlayerInAGame ON dbo.Player.ID =
dbo.PlayerInAGame.playerID INNER JOIN
dbo.Game ON dbo.PlayerInAGame.gameTypeName = dbo.Game.gameTypeName AND
dbo.PlayerInAGame.gameDateTime = dbo.Game.gameStartDateTime
GROUP BY dbo.Player.ID, dbo.Player.surName, dbo.Game.DealerInGame
here is the tables:
create table Dealer
(
number int identity(1000,1) primary key,
firstName nvarchar(20) not null,
surName nvarchar(20) not null,
birthDate date not null,
startWorkingDate date not null,
ID char(9) check(ID like replicate('[0-9]',9)) not null unique,
check(datediff(year,birthDate,startWorkingDate)>=24)
)
create table GameType
(
name nvarchar(20) primary key,
description nvarchar(20) not null,
minimumPlayers tinyint check (minimumPlayers > 0) not null,
maximumPlayers tinyint check (maximumPlayers > 0) not null,
check(minimumPlayers <= maximumPlayers)
)
create table Game
(
gameTypeName nvarchar(20) references GameType(name) on delete cascade,
gameStartDateTime datetime,
gameEndTime time,
DealerInGame int not null references Dealer(number),
primary key(gameTypeName,gameStartDateTime)
)
create table Player
(
ID char(9) primary key,
firstName nvarchar(20) not null,
surName nvarchar(20) not null,
city nvarchar(20) not null,
birthDate date check(datediff(year,birthDate,getdate())>=18) not null,
preferred nvarchar(20) references GameType(name) on delete set null
)
create table PlayerInAGame
(
playerID char(9) references Player(ID),
gameTypeName nvarchar(20),
gameDateTime datetime,
betAmount int check(betAmount>0) not null,
winLosAmount int,
check((winLosAmount = -betAmount) or (winLosAmount>=betAmount)),
foreign key(gameTypeName,gameDateTime) references
Game(gameTypeName,gameStartDateTime), primary
key(playerID,gameTypeName,gameDateTime) )
create table PlayerDealerRelation
(
dealerNumber int references Dealer(number) on delete cascade,
playerID char(9) references Player(ID),
relationType char(1) check(relationType in ('P','G','B','C','U','N')),
primary key(dealerNumber,playerID)
)
If your query in the question gives you the correct count, then it is very easy to add an extra filter that would leave only those rows where games_played is more than half of games_count.
Just add HAVING:
SELECT
dbo.Player.ID,
dbo.Player.surName,
count(DISTINCT dbo.PlayerInAGame.gameTypeName) AS games_played,
count(DISTINCT dbo.Game.gameTypeName) AS games_count
FROM
dbo.Player
INNER JOIN dbo.PlayerInAGame ON dbo.Player.ID = dbo.PlayerInAGame.playerID
INNER JOIN dbo.Game
ON dbo.PlayerInAGame.gameTypeName = dbo.Game.gameTypeName
AND dbo.PlayerInAGame.gameDateTime = dbo.Game.gameStartDateTime
GROUP BY dbo.Player.ID, dbo.Player.surName, dbo.Game.DealerInGame
HAVING
count(DISTINCT dbo.PlayerInAGame.gameTypeName) >
count(DISTINCT dbo.Game.gameTypeName) / 2
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