This question already has answers here:
How to return a incremental group number per group in SQL
(3 answers)
Closed 4 years ago.
I am busy creating a VB.Net Windows Application. I am using a microsoft sql server database with a table called skedulering. I am trying to update a column (i.e Groep) with unique values based on another column (i.e. Kode). This value takes the first three characters of the Kode value and adds a integer to it. I think what I mean is best explained with the following :
All the red bordered rows have the same Groep value. Can anyone please help me to create the sql statement?
Regards
use dense_rank() to generate a running no
UPDATE t
SET Groep = left(Kode, 3) + convert(varchar(10), rn)
FROM
(
SELECT Kode, Groep, rn = dense_rank() over (partition by left(Kode, 3) order by Kode)
FROM yourtable
) t
Related
This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
MSSQL - GROUP_CONCAT
(1 answer)
I want to do group_concat in SQL Server
(1 answer)
Closed 2 years ago.
I have a bit of an issue. I have a dataset with around 6000 records stored in a MSSQL 2016 database, where I need to concatenate each column, sorted on row basis and grouped by an ID.
It is no problem to sort the data within the column itself, but the position of the concatenated values is the key in the system that needs the data. So, if I merge four rows with the same ID, each row value must be in the same position throughout the whole resultset row.
To make it even more messy, I must be able to apply some logic to determine which row should be in position one.
If I use the the sort functionality in GROUP_CONCAT_DS (I'm using a semicolon delimiter), it sorts on the column values, which make the index position of each merge inconsistent related to the rows that holds the data.
Example data:
id col1 col2
--------------------
1 foo bar
1 lorem ipsum
2 hi cowboy
Expected output:
id, concat_col1, concat_col2
----------------------------
1 foo;lorem bar;ipsum
2 hi cowboy
Absolutely unacceptable output:
id concat_col1 concat_col2
----------------------------
1 bar;foo lorem;ipsum
2 hi cowboy
In pseudo-SQL, this is a simplified example of what I want to achieve:
SELECT
[id]
,dbo.GROUP_CONCAT_D([col1], ';') AS [concat_col1]
,dbo.GROUP_CONCAT_D([col2], ';') AS [concat_col2]
FROM [hrm].[vEmploymentsPositions]
GROUP BY [id]
ORDER BY [id] asc, [col1] desc, [col2] asc
... But for loical reason the sort doesn't work that way.
How can I approach this issue and make a robust solution that fit our needs?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am inserting production orders into a SQL Server database with a stored procedure called via Microsoft PowerApps. Currently, the build date column is just using today's date as the build date. Attached below is a sample of records:
As you can see, all 10 of these orders have today's date as the set build date. However, I'd like to dynamically set this build date based upon the necessary output for the day. This will most likely be passed a variable to the stored procedure. For example - if the build output was 5 per day, rows 1-5 would have a build date of '02/17/2020' while the next 5 would have have a build date of '02/18/2020'.
This is simple enough since the number of orders is divisible by the build output. However, let's say the build output is 3 instead of 5. The first three orders would be '02/17/2020', the next three would be '02/18/2020', and the next three would be '02/19/2020'. This would leave one order to be set to '02/20/2020'. Then if I added more orders, it would stack upon each date filling each up to 3. Is there a way to do this via a stored procedure?
You might need to do some adjustments but based on my understanding, this will do the trick ([Date] is the date column in your sample table above):
CREATE PROC yourProcName #pNUM AS INT = 1 --PARAMETER
AS
(
DECLARE #pDate DATE;
SET #pDate = COALESCE((SELECT TOP 1 CONVERT(DATE, MAX([Date])), GETDATE()) FROM yourDestTable); --SETTING THE DATE AS MAX DATE ALREADY EXISTS IN THE TABLE. IF NOTHING IS THERE, WE'LL USE TODAY
DECLARE #pLead INT = COALESCE((SELECT TOP 1 COUNT(*) FROM yourDestTable WHERE [Date] = #pDate), 0);--SEE HOW MANY RECORDS IN THE DATABASE EXIST WITH THE DATE WE DEFINED
WITH cte
AS
(
SELECT *,
FLOOR((
ROW_NUMBER() OVER(ORDER BY #pDate) -
0.001)/#pNUM) AS RN -- SEPARATING RECORDS USING ROW_NUMBER DIVIDED BY DEFINED NUMBER OF PRODS PER DAY. USING FLOOR AND MINUS A SMALL AMOUNT TO ROUND IT RIGHT
FROM yourSourceTable
)
INSERT INTO yourDestTable
SELECT
C.*,
DATEADD(DAY, LEAD(C.RN, #pLead), #pDate) AS [Date] --PUSHING THE NUMBER BASED ON ROWS ALREADY EXISTED WITH THE MAX_DATE
INTO yourDestTable
FROM cte AS C
)
You might want to use MOD in CTE based on your usecase. But the idea should work. Then call it like this EXEC yourProcName <number of transactionsday>
This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 5 years ago.
I have a table that currently looks like what is below. I'd like to only have one row per date and then have each EditType value be a separate column with the count so I can easily view the different types of edits based on the day. Can this be done using a pivot?
Or... would it be better to back up a step before I've grouped the rows in order to get the counts, like what is shown below?
The desired output would be something like this:
SELECT
Gdb
, Tbl
, EditDate
, [I]
, [U]
, [D]
FROM #TABLE
PIVOT (
COUNT(EditType)
FOR EditType IN ([U], [I], [D])
) AS PIVOTED
This question already has answers here:
Understanding PIVOT function in T-SQL
(7 answers)
Closed 5 years ago.
I need help in SQL. I have a table with this structure.
:
What I need is a query to return the data like this.
:
Thank you very much in advance!
Kind regards,
bgergoe
This has been asked and answered dozens of times but it is super simple to write out the code. Conditional aggregation is pretty straight forward and the code is far less obtuse than PIVOT. Here is how you would go about this.
SELECT ID
, MAX(CASE WHEN Field = 'name' THEN Value END) AS name
, MAX(CASE WHEN Field = 'phone number' THEN Value END) AS PhoneNumber
FROM YourTable
GROUP BY ID
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to Get Last Created Entry's ID From Sql Database With Asp.Net
How to get last inserted id?
I am facing difficulty to get the last inserted field in
selct scope identity();
SELECT *
FROM Person
ORDER BY PersonID DESC
LIMIT 1