ASP.NET SQL elimininate duplicate Ids - c#

I have two database tables for documenting a wound healing progression. Those are joined over the wound_id-Column like this:
So for one wound, I can create many progresses to show the healing of it. This is working fine.
Here is the code for the tables:
Table wound_details:
CREATE TABLE [dbo].[epadoc_mod_wound_details] (
[wound_id] INT IDENTITY (1, 1) NOT NULL,
[wound_type] VARCHAR (500) NULL,
[wound_description] VARCHAR (500) NULL,
[decuGrade] INT NULL,
[wound_comments] VARCHAR (500) NULL,
[wound_timeReal] DATETIME NULL,
[wound_timeGiven] DATETIME NULL,
[casenumber] INT NULL,
[username] VARCHAR (50) NULL,
[infectionstate] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([wound_id] ASC)
);
Table wound_progress:
CREATE TABLE [dbo].[epadoc_mod_wound_progress] (
[progress_id] INT IDENTITY (1, 1) NOT NULL,
[wound_length] INT NULL,
[wound_width] INT NULL,
[wound_depth] INT NULL,
[wound_surrounding] VARCHAR (500) NULL,
[wound_consistence] VARCHAR (500) NULL,
[wound_state] VARCHAR (200) NULL,
[wound_painscale] INT NULL,
[wound_itch] INT NULL,
[wound_id] INT NULL,
PRIMARY KEY CLUSTERED ([progress_id] ASC),
CONSTRAINT [FK_epadoc_mod_wound_progress_fk] FOREIGN KEY ([wound_id]) REFERENCES [dbo].[epadoc_mod_wound_details] ([wound_id]) ON DELETE CASCADE
);
Then I wrote a SELECT-Query to show all wounds for specific case number which are documented for the patient:
SELECT DISTINCT
dbo.epadoc_mod_wound_details.wound_id, dbo.epadoc_mod_wound_details.casenumber, dbo.epadoc_mod_wound_details.wound_type, dbo.epadoc_mod_wound_progress.progress_id, dbo.epadoc_mod_wound_details.wound_comments, dbo.epadoc_mod_wound_details.wound_timeReal, dbo.epadoc_mod_wound_details.username
FROM dbo.epadoc_mod_wound_details LEFT JOIN
dbo.epadoc_mod_wound_progress
ON dbo.epadoc_mod_wound_details.wound_id = dbo.epadoc_mod_wound_progress.wound_id
WHERE dbo.epadoc_mod_wound_details.casenumber = #casenr;
This is working fine though, but the problem is that ALL wound progresses are shown in the GridView, here is an example so you can see what I mean:
What I want to do is just show the latest progress of one wound, so for the above example just show the last entry with progressID 65:
33 65 1111111 Dekubitus
34 .. ....... .........
The SELECT DISTINCT approach didn't work and I also tried with MAX(progressID) but I always seem to get errors. I think I have to do something with ORDER BY or a second SELECT-Query before the JOIN.
Thanks for any advice!

You should use GROUP BY combined with MAX in your query.
SELECT
dbo.epadoc_mod_wound_details.wound_id,
dbo.epadoc_mod_wound_details.casenumber,
dbo.epadoc_mod_wound_details.wound_type,
MAX(dbo.epadoc_mod_wound_progress.progress_id) AS progress_id,
dbo.epadoc_mod_wound_details.wound_comments,
dbo.epadoc_mod_wound_details.wound_timeReal,
dbo.epadoc_mod_wound_details.username
FROM
dbo.epadoc_mod_wound_details
LEFT JOIN
dbo.epadoc_mod_wound_progress ON
dbo.epadoc_mod_wound_details.wound_id = dbo.epadoc_mod_wound_progress.wound_id
GROUP BY
dbo.epadoc_mod_wound_details.wound_id,
dbo.epadoc_mod_wound_details.casenumber,
dbo.epadoc_mod_wound_details.wound_type,
dbo.epadoc_mod_wound_details.wound_comments,
dbo.epadoc_mod_wound_details.wound_timeReal,
dbo.epadoc_mod_wound_details.username;

Since you only want the progress_id, The easies way to do it is using a correlated subquery:
SELECT wound_id,
casenumber,
wound_type,
(
SELECT TOP 1 progress_id
FROM dbo.epadoc_mod_wound_progress AS WP
WHERE WP.wound_id = WD.wound_id
ORDER BY progress_id
) As progress_id,
wound_comments,
wound_timeReal,
username
FROM dbo.epadoc_mod_wound_details As WD
WHERE casenumber = #casenr;

I understand you need each record of "epadoc_mod_wound_details" with the latest record of "epadoc_mod_wound_progress".
You can try this:
select wound.wound_id, wound.casenumber, wound.wound_type,
wound.wound_comments, wound.wound_timeReal, wound.username, MAX(progress_id)
from epadoc_mod_wound_details wound
left join epadoc_mod_wound_progress progress on wound.wound_id = progress.wound_id
where wound.casenumber = ''
group by wound.wound_id, wound.casenumber, wound.wound_type,
wound.wound_comments, wound.wound_timeReal, wound.username

Related

Create a filtered view of an observablecollection and display it in listbox

I have a ObservableCollection that contains data from a database using Linq. I need to only have specific filtered items of that collection to be shown in my listbox.
I have created a ICollectionView which i initialized as the default view from that ObservableCollection. Then i am trying to filter that view by using linq and there lies the problem. My 'filter' returns an IEnumerable and i dont know how to properly cast that to my view so i can show the view in my listbox.
The ObservableCollection:
private ObservableMitarbeiterAufgabe mitarbeiterAufgabeData;
The view:
ICollectionView view = CollectionViewSource.GetDefaultView(mitarbeiterAufgabeData);
My filter that isnt working because it returns an IEnumerable:
var selected = (Mitarbeiter)lb_Ma_Au_Zuweisung.SelectedItem;
view.Filter = from ma in selected.MitarbeiterAufgabe select ma.Aufgaben;
How can i make my filter accept an IEnumerable?
Edit: added the complete datastructure.
CREATE TABLE [dbo].[Mitarbeiter] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Vorname] VARCHAR (50) NOT NULL,
[Nachname] VARCHAR (50) NOT NULL,
[Belastung] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Aufgaben] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Beschreibung] NVARCHAR (50) NOT NULL,
[Belastung] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[MitarbeiterAufgabe] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[MitarbeiterID] INT NOT NULL,
[AufgabenID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_MitarbeiterID] FOREIGN KEY ([MitarbeiterID]) REFERENCES [dbo].[Mitarbeiter] ([Id]),
CONSTRAINT [FK_AufgabenID] FOREIGN KEY ([AufgabenID]) REFERENCES [dbo].[Aufgaben] ([Id])
);
var selected = (Mitarbeiter)lb_Mitarbeiter_Zuweisung.SelectedItem;
stores an object from the table 'Mitarbeiter'. I need the filter to give me every entry from 'MitarbeiterAufgabe' where MitarbeiterID matches the id of the selected item.
The filter is supposed to be a function that checks whether an item is to be included or not
you are actually already doing all the filtering yourself by coming up with the list of all correct items.
your filter could look something like this:
view.Filter = ma => selected.MitarbeiterAufgabe.Contains(ma.Aufgaben);
without knowing the exact data structures, it's not possible to give a precise answer here..

My "data default (getdate())", it doesn't work

I'm trying to insert in my SQL Server table the current date.
My table includes 3 files:
CREATE TABLE [dbo].[MyTable]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Date] DATE
CONSTRAINT [DF_MyTable_Date] DEFAULT (getdate()) NOT NULL,
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED ([Id] ASC)
)
When a new user wants to register in the system, he has only to insert his name.
In my table, the Id is generated automatically and the date too, but the date shows 01/01/0001 instead of the current day.
Where is the mistake?
if you create a datetime variable in C# like
var today_date = new DateTime();
and do a Console.WriteLine(today_date); u can see it print 0001-01-01
So this is default value of null..
Use DBNull.Value to insert a SQL NULL from C# and check the result
(Your_Date_Column) Make it Null / Not Null and give default value GetDate() but still it will not work. You have to create a trigger like this,
CREATE TRIGGER [dbo].[Trigger_Date] ON [dbo].[TableName] FOR INSERT AS BEGIN
Declare #Id int
set #Id = (select Id from inserted)
Update [dbo].[TableName]
Set Your_Date_Column = GetDate()
Where Id = #Id
END
Functions and triggers are not required. Just set a column with type Date or DateTime to NOT NULL DEFAULT CURRENT_TIMESTAMP.
Updated example code from the question:
CREATE TABLE [dbo].[MyTable]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Date] DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED ([Id] ASC)
)

How to use sql defined functions as fields?

I am creating tables in Sql Management Studio 2012 using SQL. How do I make fields or columns with names that are already defined in Sql Server e.g User_ID, User_Name. I want to use them as fields in my tables.
Table definition from Duplicate Post:
create table Ticket(
Ticket_Id varchar(10) not null,
TicketType_Id varchar(3) not null,
Ticket_PurchaseDate DateTime null,
LottoDraw_Id int null,
User_Id int null,
Ticket_IsWinner bit null
Primary Key(Ticket_Id,TicketType_Id)
)
Warp the column name like in brackets [ ] ... such as
create table Ticket(
Ticket_Id varchar(10) not null,
TicketType_Id varchar(3) not null,
Ticket_PurchaseDate DateTime null,
LottoDraw_Id int null,
[User_Id] int null,
Ticket_IsWinner bit null
Primary Key(Ticket_Id,TicketType_Id)
)

How to get column definition from query result

I need a way to get column definition from query result. Right now I'm using SQL Server 2012.
Here is the example scenario, I have two tables which are Event and Attendant whose definitions are below :
CREATE TABLE [dbo].[Event] (
[Id] INT NOT NULL,
[Name] NVARCHAR (50) NULL,
[Description] NVARCHAR (50) NULL,
[StartDate] DATETIME NULL,
[EndDate] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Attendant] (
[Id] INT NOT NULL,
[EventId] INT NOT NULL,
[Name] NVARCHAR (50) NULL,
[Company] NVARCHAR (50) NULL
PRIMARY KEY CLUSTERED ([Id] ASC)
);
And then I have query such as :
SELECT Event.Name as EventName, Attendant.Name as GuestName
FROM Event
INNER JOIN Attendant ON Event.Id = Attendant.EventId
How Can I get the column definition for above example query result? My objective is to generate poco class to represent each record of any query result using c#.
you can use sp_columns sproc to retrieve information about the column definition of a specified table ... like this:
exec sp_columns Attendant
Use sp_columns, it returns column information for the specified objects(tables).
Refer this link for details.
select * from information_schema.columns where table_name = '<tablename>'

Database Hotel Reservation

So i have a bit of a problem, on my ASP.Net website i want it to show all the rooms which are currently available and not in use. When i say not in use i mean at present there is no one reserved to that room, so the systemdate is not between any values of the checkin and checkoutdate.
Part of my schema:
CREATE TABLE "Rooms"(
RoomNo int NOT NULL,
RoomType nvarchar(20) NULL,
PricePerNight money NULL,
MaximumOccupancy int NULL,
NoOfBeds int NULL,
NoOfBathrooms int NULL,
Entertainment bit NULL,
RoomService bit NULL,
Gym bit NULL,
CONSTRAINT PK_Rooms PRIMARY KEY(RoomNo)
)
CREATE TABLE "Reservation"(
ReservationID int IDENTITY (1,1) NOT NULL,
CustomerID int NOT NULL,
RoomNo int NOT NULL,
CheckInDate date NOT NULL,
CheckOutDate date NOT NULL,
NoOfDays int NOT NULL,
CONSTRAINT PK_Reservation PRIMARY KEY(ReservationID),
CONSTRAINT FK_Reservation_Customers_CustID FOREIGN KEY(CustomerID)
REFERENCES dbo.Customers(CustomerID),
CONSTRAINT FK_Reservation_Rooms_RoomNo FOREIGN KEY(RoomNo)
REFERENCES dbo.Rooms(RoomNo)
)
So heres my idea, im assuming im going to need an sql query which goes something like this:
Select All FROM Rooms Where &CheckInDate != "Select CheckIndate From
Reservation" AND &CheckOutDate != "Select CheckOutDate From
Reservation".
But this has some flaws in it.
Can someone please tell me how i can do this, how i can make my Sue-do-code (lets call it) more viable, as i'm not sure if something like that will work, and if necessarily suggest improvements i could make to my schema.
TLDR; i need a query where someone enters a checkindate and checkoutdate and it returns all the rooms which are available.
Try this..
SELECT *
FROM Rooms
WHERE NOT EXISTS
(SELECT *
FROM Room r, Reservation rs
WHERE r.RoomNo = rs.RoomNo AND GETDATE() BETWEEN rs.CheckInDate AND rs.CheckOutDate)
This is what i wanted.
SELECT dbo.Rooms.RoomNo
FROM dbo.Rooms JOIN dbo.Reservation
ON (dbo.Rooms.RoomNo = dbo.Reservation.RoomNo)
WHERE '2012-01-01' NOT BETWEEN dbo.Reservation.CheckInDate AND dbo.Reservation.CheckOutDate
AND '2012-01-05' NOT BETWEEN dbo.Reservation.CheckInDate AND dbo.Reservation.CheckOutDate;
Took time but i got it in the end. the where statement initial date can be replaced with &Date.

Categories