Find Unique Value based on criteria - unique

Hello… I am trying to figure out how to look under ModelName column, which could contain duplicate values and see if ReleaseType column for that same model contains “Final Release.” If yes, do nothing, if not Return Unique ModelName (perhaps based on the latest Date). Fields in Green are the one I am trying to show and the one in red should not be visible:
Is this something that could be done in SQL Server 2019?
Here are my tables:
CREATE TABLE [dbo].[Model](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ModelName] [nvarchar](50) NOT NULL,
[FormFactorID] [int] NOT NULL,
[Revision] [nvarchar](4) NOT NULL,
[SVID] [nvarchar](4) NOT NULL,
[SSID] [nvarchar](4) NULL,
[Picture] [varbinary](max) NULL,
[NVME] [nvarchar](4) NULL,
[ReleaseStatusID] [int] NULL,
CONSTRAINT [PK__Model__3214EC27B0574C2B] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[ReleaseType](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ReleaseType] [nvarchar](15) NULL,
CONSTRAINT [PK__ReleaseT__3214EC27CD0730DB] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[ProductRelease](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ReleaseTypeID] [int] NULL,
[ModelID] [int] NULL,
[Date] [date] NULL,
[ECO] [nvarchar](10) NULL,
[Notes] [nvarchar](800) NULL,
CONSTRAINT [PK__ProductR__3214EC27CE911F54] PRIMARY KEY CLUSTERED
ID from ReleaseType table joined with ReleaseTypeID from ProductRelease table
ID from Model table joined with ModelID from ProductRelease table
Here is my Query so far:
SELECT
dbo.Model.ModelName,
dbo.ReleaseType.ReleaseType,
dbo.ProductRelease.ECO,
dbo.ProductRelease.Date
FROM
dbo.Model
INNER JOIN dbo.ProductRelease ON dbo.Model.ID = dbo.ProductRelease.ModelID
INNER JOIN dbo.ReleaseType ON dbo.ProductRelease.ReleaseTypeID = dbo.ReleaseType.ID
ORDER BY
dbo.Model.ModelName,
dbo.ProductRelease.Date

Yes, it doable for instance with windowed fucntions:
WITH cte AS (
SELECT m.ModelName,
rt.ReleaseType,
pr.ECO,
pr.Date,
cnt = COUNT(CASE WHEN rt.ReleaseType='Final Release' THEN 1 END)
OVER(PARTITION BY m.ModelName),
rn = ROW_NUMBER() OVER(PARTITION BY m.ModelName ORDER BY pr.Date DESC)
FROM dbo.Model m
JOIN dbo.ProductRelease pr
ON m.ID = pr.ModelID
JOIN dbo.ReleaseType rt
ON pr.ReleaseTypeID = rt.ID
)
SELECT ModelName, ReleaseType, ECO, Date
FROM cte
WHERE cnt = 0 -- exclude groups with 'Final Release'
AND rn = 1 -- get only newest occurence per ModelName
ORDER BY ModelName;

Related

How to display multiple items but only the highest values in year and month for each?

I have the following two tables:
CREATE DATABASE IF NOT EXISTS springbootdb;
DROP TABLE IF EXISTS occupancy;
DROP TABLE IF EXISTS hotel;
CREATE TABLE hotel
(
id INT NOT NULL PRIMARY KEY auto_increment,
category int NOT NULL,
name TEXT NOT NULL,
owner TEXT NOT NULL,
contact TEXT NOT NULL,
address TEXT NOT NULL,
city TEXT NOT NULL,
zip TEXT NOT NULL,
phone TEXT NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE occupancy
(
id int not null primary key auto_increment,
hotelid int not null,
month int not null,
year int not null,
room_utilization int not null,
bed_utilization int not null,
room_count int not null,
bed_count int not null,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now I want to display every single hotel.id and hotel.name along with occupancy.room_count, occupancy.bed_count, occupancy.room_utilization and occupancy.bed_utilization - but only the very latest entry for each hotel.id, so the ones where occupancy.year and occupancy.month are the highest values each.
I tried a couple of things, such as
SELECT springbootdb.hotel.id, springbootdb.hotel.name, springbootdb.occupancy.bed_count, springbootdb.occupancy.bed_utilization
From springbootdb.hotel
INNER JOIN springbootdb.occupancy
ON hotel.id = occupancy.hotelid
order by springbootdb.occupancy.`year`, springbootdb.occupancy.`month` asc limit 1;
but haven't had any success unfortunately.
Can a good soul tell me how to get there?
Thanks!
This is best solved with window functions, but that requires you upgrade to MySQL 8.0.
Here's the general idea.
SELECT t.id, t.name, t.bed_count, t.bed_utilization
FROM (
SELECT h.id, h.name, o.bed_count, o.bed_utilization, ROW_NUMBER() OVER (PARTITION BY h.id ORDER BY o.`year` DESC, o.`month` DESC) AS rownum
FROM hotel AS h JOIN occupacy AS o ON h.id = o.hotelid
) AS t
WHERE t.rownum = 1;

t-sql 2012 complex select by endyear and most current record

I have the following table that contains can contain 0,1, or many records
for each endyear when a query is excuted by personID. The enrollmentID key
is not used by any queries in my company. That column only exists since
database tables requie an identity key.
What I need to do is to allow is for a parameter called #endYear to be
used and matched against the endYear column in the Enrollment table.
The user will be able to select #endYear = 2018 or #endYear = 2017.
I need to select the most current enrollment record based upon end Date
and endStatus = 202 or 205.
The problem is when #endYear=2017. I can not select records by personID if
records exist where endyear =2018 and (there is not an endStatus = 202
OR 205).
Here is the table:
CREATE TABLE [dbo].[Enrollment](
[enrollmentID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[personID] [int] NOT NULL,
[calendarID] [int] NOT NULL,
[grade] [varchar](4) NULL,
[active] [bit] NOT NULL,
[startDate] [smalldatetime] NOT NULL,
[startStatus] [varchar](4) NULL,
[startComments] [varchar](250) NULL,
[endDate] [smalldatetime] NULL,
[endStatus] [varchar](4) NULL,
[endYear] [smallint] NOT NULL
)
Thus can you show me the t-sql 2012 on how to accomplish my goal?
I'm not exactly sure if this is what you're asking - but to get the latest row (with the highest endDate) for a #personID and #endYear, this is the query to use:
SELECT
TOP 1 *
FROM
[dbo].[Enrollment]
WHERE
endYear=#endYear AND personID=#PersonID AND endStatus IN (202, 205)
ORDER BY
endDate DESC

Counting number of files in a folder in mysql

I have these tables
Create table series (
id_series Int NOT NULL AUTO_INCREMENT,
name_series Varchar(100) NOT NULL,
kcal Int NOT NULL DEFAULT 0,
type Varchar(20) NOT NULL,
id_user Int NOT NULL,
UNIQUE (name_excercise),
Primary Key (id_series)) ENGINE = MyISAM;
Create table excercise (
id_excercise Int NOT NULL AUTO_INCREMENT,
name_excercise Char(100) NOT NULL,
date Date NOT NULL,
start Time NOT NULL,
end Time NOT NULL,
km Double(10,2) NOT NULL,
id_series Int NOT NULL,
UNIQUE (id_excercise),
Primary Key (id_excercise)) ENGINE = MyISAM;
They are basically like folders. Series has a name of a series and inside, there are subsequent excercises. What I need is to return every series user has and the number of the excercises inside. But I am having trouble with it, since I am not passing any ID of a concrete series in which it could count all the excercises. Is it possible to print that with only a user id?
I basically have this:
"SELECT * FROM series WHERE id_user = $id_user"
but need to combine it with this:
"SELECT COUNT(*) FROM excercise WHERE id_series = $id_series"
without the need of id_series.
This is the query:
SELECT S.*, COUNT(E.id_excercise) AS NumOfExc
FROM series AS S
JOIN excercise AS E
ON S.id_series = E.id_series
WHERE S.id_user = '$id_user'
GROUP BY E.id_series
UNION
SELECT *, 0 AS NumOfExc
FROM series
WHERE id_user = '$id_user'
AND id_series NOT IN (
SELECT S.id_series AS id_series
FROM series AS S
JOIN excercise AS E
ON S.id_series = E.id_series
WHERE S.id_user = '$id_user'
GROUP BY E.id_series
)
The above query will show what you need.

Combine data from different tables in a View. What is wrong with my Code?

I want to create a view that will present details of the vehicle and the amount of loans.
I have a problem with the placing in this view [vehicle_brand],[vehicle_type],[Color] that come from another table. I do not know how to combine data from different tables ... I'll be very grateful for your help.
My tables and my unfinished code view:
[dbo].[Loan](
[id_Loan] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[registration_number] FOREIGN KEY [char](10) NULL,
[Loan_time] [datetime] NOT NULL, ...
[dbo].[vehicle](
[registration_number] [char](10) PRIMARY KEY NOT NULL,
[id_vehicle_brand] FOREIGN KEY [int] NULL,
[id_TypuPojazdu] FOREIGN KEY [int] NULL,
[id_vehicle_color] FOREIGN KEY [int] NULL,
[dbo].[Color](
[id_vehicle_color] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Color] [char](20) NOT NULL,
[dbo].[vehicle_type](KEY
[id_vehicle_type] [int] IDENTITY(1,1)PRIMARY KEY NOT NULL,
[vehicle_type] [char](20) NOT NULL
[dbo].[vehicle_brand](
[id_vehicle_brand] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[vehicle_brand] [char](50) NOT NULL
CREATE VIEW [dbo].[v_loan_statistics ]
AS
with t1 ([registration_number],amount_of_loans)
as
(
select [registration_number], count(*) amount_of_loans
from [dbo].[Loan]
group by [registration_number]
)
select top 100 percent t1.[registration_number],***[vehicle_brand],
[vehicle_type],[Color]***,t1.amount_of_loans
from t1
inner join [dbo].[vehicle] p
on t1.[registration_number]=p.[registration_number]
order by t1.[registration_number]
GO
Since [dbo].[Loan].RegistrationNumber is nullable and p.[registration_number] is not, it raises an error.
you can add where RegistrationNumber is not null

Update Column based on SELECT with Parameter coming from UPDATE

I want to do something like this.
UPDATE tbl_states AS ts
SET tbl_states.country_id = (SELECT tbl_countries.country_id
FROM tbl_states ts1
JOIN tbl_countries
ON tbl_countries.country_id_id =
ts1.country_id
WHERE ts1.country_id_id = ts.country_id_id)
I want to update old country_id which came from different database to new country_id based on new primary keys inserted on the new database. To give you an idea here is the schema.
CREATE TABLE [dbo].[tbl_countries](
[country_id] [int] IDENTITY(1,1) NOT NULL,
[country_id_id] [int] NULL,
[country_name] [varchar](50) NULL)
country_id_id is the old country_id referenced on the next table I will show you tbl_states
CREATE TABLE [dbo].[tbl_states](
[state_id] [int] IDENTITY(1,1) NOT NULL,
[state_name] [varchar](50) NULL,
[country_id] [int] NULL,
[state_abbr] [char](3) NOT NULL)
I want to update country_id column of this table tbl_states to primary column of the table above using the following select statement to get the primary key.
SELECT tbl_countries.country_id
FROM tbl_states_old
JOIN tbl_countries
ON tbl_countries.country_id_id = tbl_states_old.country_id
Sorry for the title, I don't know what this is called. Could you help me with this?
UPDATE s
SET country_id = c.country_id
FROM tbl_states s
INNER JOIN tbl_countries c
ON s.country_id = c.country_id_id