/ / Ako získať číselný rozsah medzi riadkovou hodnotou SQL - sql

Ako získať numerického rozpätia medzi sebou hodnota SQL - sql

Mám tabuľku, ktorá zobrazuje stupne a percentá.

Teraz chcem spustiť dotaz na tabuľke, ktorá načíta Grade medzi tieto percentá.
Napríklad ak študent dostane 72%, chcem ukázať známku ako C.
Ako získať Grade z tabuľky?

Pozrite si tento obrázok tabuľky:

tu zadajte popis obrázku

odpovede:

2 pre odpoveď č. 1
Drop Table Grades
Drop Table Students

Create Table Students (Name Varchar(200), Percentage Numeric(5,2))
Insert Students Values ("John", 0.00)
Insert Students Values ("Jane", 38.00)
Insert Students Values ("Joe", 45.00)
Insert Students Values ("Greg", 50.00)
Insert Students Values ("Buck", 55.00)
Insert Students Values ("Harold", 60.00)
Insert Students Values ("Jack", 65.00)
Insert Students Values ("Bill", 68.00)
Insert Students Values ("Gerald", 75.00)
Insert Students Values ("Steve", 79.00)
Insert Students Values ("Walter", 85.00)
Insert Students Values ("Mike", 92.00)
Insert Students Values ("Mary", 100.00)
Insert Students Values ("Mark", 101.00)
Select * From Students

Create Table Grades (Grade Char(2), Percentage Numeric(5,2))
Go
Insert Grades Values ("A*", 101.00)
Insert Grades Values ("A", 85.00)
Insert Grades Values ("B", 75.00)
Insert Grades Values ("C", 65.00)
Insert Grades Values ("D", 55.00)
Insert Grades Values ("E", 45.00)
Insert Grades Values ("F", 0.00)

Select S.*, G.Grade
From
(
Select  *, IsNull(Lead(Percentage) Over (Order By Percentage), (Select Max(Percentage)+.01 From Grades)) NextPercentage
From Grades ) G
Join Students S On S.Percentage >= G.Percentage And S.Percentage < G.NextPercentage

0 pre odpoveď č. 2

ORDER BY Percentage DESC s <= percentuálny podiel vo WHERE a TOP 1 Grade očakávaný výsledok

CREATE TABLE #GradeMaster (Grade VARCHAR(2), Percentage DECIMAL(5,2))

INSERT INTO #GradeMaster
SELECT "A*", 101 UNION
SELECT "A", 85 UNION
SELECT "B", 75 UNION
SELECT "C", 65 UNION
SELECT "D", 55 UNION
SELECT "E", 45 UNION
SELECT "F", 0

SELECT TOP 1 Grade
FROM #GradeMaster
WHERE Percentage <= 72
ORDER BY Percentage DESC

DROP TABLE #GradeMaster

0 pre odpoveď č. 3
  select grade from table1 where precentage in (
select max(percentage) from table1 where 72 > percentage);

Môžete nahradiť 72 za akékoľvek skóre sa vám páči. Tam môže byť spôsob, ako to urobiť bez 2 výbery, ale to by malo fungovať.


0 pre odpoveď č. 4

Môžete použiť poradie podľa limitu 1

select grade from my_table
where  percentage <= 72
order by percentage desc
limit 1;

0 pre odpoveď č. 5

Za predpokladu, že by mohol byť aj študentský stôl atabuľka priradenia ... Myslel by som, že vyhľadávací dopyt bude vyzerať takto. Nižšie vám všetci študenti bez ohľadu na to, či majú nejaké klasifikované úlohy. Prípadne by ste sa mohli pripojiť k študentskému stolu priamo, ak máte celkovú známku už agregovanú.

SELECT
S.*,
A.*,
G.grade
FROM
Student S
LEFT OUTER JOIN Assignment A ON S.Student_id = A.Student_id
LEFT OUTER JOIN Grade G ON A.Percentage >= G.Percentage AND A.Percentage < G.Percentage