sql >> Database teknologi >  >> RDS >> Sqlserver

T-SQL Forespørg en matrixtabel for fri position

SQL violin

CREATE TABLE Coordinate ( X int, Y int,Z int, Disabled bit, OccupiedId int) INSERT INTO Coordinate VALUES (1,1,1, 1, NULL) INSERT INTO Coordinate VALUES (1,1,2, 0, NULL) INSERT INTO Coordinate VALUES (1,1,3, 0, NULL) INSERT INTO Coordinate VALUES (1,1,4, 0, NULL) INSERT INTO Coordinate VALUES (1,2,1, 0, NULL) INSERT INTO Coordinate VALUES (1,2,2, 0, NULL) INSERT INTO Coordinate VALUES (1,2,3, 0, 123) INSERT INTO Coordinate VALUES (1,2,4, 0, NULL) INSERT INTO Coordinate VALUES (1,2,5, 1, NULL) SELECT X, Z, MIN(Y) AS FirstFreePosition FROM Coordinate WHERE Disabled = 0 AND OccupiedId IS NULL GROUP BY X, Z OR -- if you need the unavailable combinations too, then something like this: SELECT X, Z, MIN(CASE WHEN Disabled = 1 OR OccupiedId IS NOT NULL THEN 1000 --a big number ELSE Y END) AS FirstFreePosition FROM Coordinate GROUP BY X, Z

  1. Operator findes ikke:heltal =heltal[] i en forespørgsel med ANY

  2. Får NoSuchMethodError:javax.persistence.Table.indexes() under udførelse af JPA-forespørgsel

  3. PHP, MYSQL indlejrede forespørgsler

  4. Kan jeg automatisere indsættelse af adgangskode til PEM-fil til SSL-certifikater i MySQL?