SQL Server Stored procedure to return single record for same ID using group by [closed]

ippsafx7  于 2023-05-21  发布在  其他
关注(0)|答案(2)|浏览(142)

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 yesterday.
Improve this question

I have the following database table data:

I must write SQL Server stored procedure to fetch the following output data:

Here the column Data has the JSON data. We need to fetch only those data with the same BatchId more than once. We need one entry for the same BatchId with the Earliest Timestamp .

I created the following temp table in the procedure with the query:

SELECT 
    JSON_VALUE(a.Data, '$.BatchId') BatchID, 
    COUNT(JSON_VALUE(a.Data, '$.BatchId')) AS BatchIDCount, 
    a.Name
INTO
    #BULKDOCUMENT
FROM 
    Table1 a
GROUP BY 
    JSON_VALUE(a.Data, '$.BatchId'), a.Name
HAVING 
    COUNT(JSON_VALUE(a.Data, '$.BatchId')) > 1

Which returns the following output:

Can you please help me with how I can return the required data from the SQL Server stored procedure?

iklwldmw

iklwldmw1#

Create a stored procedure, something like-

CREATE PROCEDURE GetBulkData
AS
BEGIN
  SELECT JSON_VALUE(a.Data, '$.BatchId') BatchID,
    COUNT(JSON_VALUE(a.Data, '$.BatchId')) AS BatchIDCount,
    a.Name
  FROM Table1 a
  GROUP BY JSON_VALUE(a.Data, '$.BatchId'), a.Name
  HAVING COUNT(JSON_VALUE(a.Data, '$.BatchId')) > 1;
END;

Execute this sp using - EXEC GetBulkData;

t2a7ltrp

t2a7ltrp2#

You didn't specify what exactly you needed help with. I'm assuming you're struggling with how to do the aggregation where the grouped row only has values from the earliest row in the group.

You need to use a window function and sort the rows by batch ID.

with sorted_batches as (
  select
    t.RecordID,
    t.Timestamp,
    t.Data,
    t.Category,
    JSON_VALUE(t.Data, '$.Name') as Name,
    JSON_VALUE(t.Data, '$.BatchId') as BatchID,
    row_number() over(partition by JSON_VALUE(t.Data, '$.BatchId'), order by t.Timestamp asc) as position_in_batch,
    count(*) over (partition by JSON_VALUE(t.Data, '$.BatchId')) AS record_count_in_batch
  from table1 t
)
select
  bt.Timestamp,
  json_object('RecordId': bt.RecordID, 'Name': bt.Name, 'Count', bt.record_count_in_batch) as Data,
  bt.Category,
from sorted_batches bt
where bt.position_in_batch = 1;

I used the json_object function to stitch the fields together into a JSON document. I used the count and row_number window functions to do the aggregation.

相关问题