|
-
Nov 22nd, 2018, 09:41 AM
#1
Thread Starter
Addicted Member
VB Give integer value to column based on sort
I am building a VB.Net applications using SQL server and cannot figure the following out :
I need to give a sequential integer (starting from 1) to a column in the database table (column called Prioriteit) based on a sort that is as follows :
First column kultivar (i.e. string Ascending), then GroepperingsGroep (i.e. string Ascending), Then LaasteOntledingsdatum (i.e. date oldest to newest) and lastly Laastesuiker (i.e. real largest to smallest). According to this sort integer values should be given to Prioriteit for each GroepperingsGroep. I am attaching a picture to explain the end result needed
.
Regards
-
Nov 22nd, 2018, 06:05 PM
#2
Re: VB Give integer value to column based on sort
Execute a query against the database using an appropriate ORDER BY clause. Use a data adapter to populate a DataTable. Use a For loop to iterate over the Rows of that DataTable and set that Prioriteit column based on the loop counter. When done, use the same data adapter to save the changes. Note that the only columns you need to retrieve are the primary key and the one you want to modify.
-
Nov 25th, 2018, 11:23 PM
#3
Thread Starter
Addicted Member
Re: VB Give integer value to column based on sort
Thank you jmcilhinney. Came up with the following :
SQL :
Code:
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY kultivar,GroepperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit,
*
FROM myTable
)
UPDATE CTE
SET CTE.Prioriteit = CTE.NewPrioriteit
VB.Net :
vb.net Code:
Dim konneksie As New SqlConnection Dim opdragprioriteit As New SqlCommand konneksie.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\SKEDULERING\Skedulering6\Skedulering6\SkeduleringsDatabasis6.mdf;Integrated Security=True" konneksie.Open() opdragprioriteit.Connection = konneksie opdragprioriteit.CommandText = "WITH CTE AS " & " (Select Case ROW_NUMBER() OVER (PARTITION BY kultivar,GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit, * " & " From skedulering2)" & " Update CTE" & " SET CTE.Prioriteit = CTE.NewPrioriteit " opdragprioriteit.ExecuteNonQuery() konneksie.Close()
Working in SQL, however I get a error message (Incorrect syntax near 'NewPrioriteit'). What syntax am I missing?
Regards and thank you.
-
Nov 25th, 2018, 11:25 PM
#4
Thread Starter
Addicted Member
Re: VB Give integer value to column based on sort
Thank you jmcilhinney. Came up with the following :
SQL :
Code:
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY kultivar,GroepperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit,
*
FROM skedulering2
)
UPDATE CTE
SET CTE.Prioriteit = CTE.NewPrioriteit
VB.Net :
vb.net Code:
Dim konneksie As New SqlConnection
Dim opdragprioriteit As New SqlCommand
konneksie.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\SKEDULERING\Skedulering6\Skedulering6\SkeduleringsDatabasis6.mdf;Integrated Security=True"
konneksie.Open()
opdragprioriteit.Connection = konneksie
opdragprioriteit.CommandText = ";WITH CTE AS " &
" (Select Case ROW_NUMBER() OVER (PARTITION BY kultivar,GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit, * " &
" From skedulering2)" &
" Update CTE" &
" SET CTE.Prioriteit = CTE.NewPrioriteit "
opdragprioriteit.ExecuteNonQuery()
konneksie.Close()
Working in SQL, however I get a error message in VB.Net (Incorrect syntax near 'NewPrioriteit'). What syntax am I missing?
Regards and thank you.
Last edited by GideonE; Nov 25th, 2018 at 11:39 PM.
-
Nov 26th, 2018, 08:15 AM
#5
Re: VB Give integer value to column based on sort
What you're doing wrong is that you can't create a CTE and update... because you can't update a CTE... I'm not even sure why you created the CTE in the first place...
Code:
Select Case ROW_NUMBER() OVER (PARTITION BY kultivar,GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit, * " &
" From skedulering2
And as I paste that, I see that you used a CASE statement... ????? why? No...
Code:
Select
kultivar,
LaasteOntledingsdatum,
Laastesuiker,
GroepperingsGroep,
ROW_NUMBER() OVER (PARTITION BY kultivar, GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) Prioriteit
From skedulering2
There... nothing complicated about it...
-tg
-
Nov 27th, 2018, 07:42 AM
#6
Thread Starter
Addicted Member
Re: VB Give integer value to column based on sort
Thank you technome. Your code works well to select, but how do then do I update skedulering2?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|