Solved

T-SQL Stored Procedure Count by recurring ID

Posted on 2014-12-18
5
137 Views
Last Modified: 2015-01-08
I am developing in MSSQL 2005

I am creating a report off a vendor system. The tables in the system are accessible but don't make much sense outside of the vendor. The vendor themselves develop of a series of stored procedures that ties data together and makes it available to other Stored Procedures. NOTE: For the solution I am limited to a Stored Proc, funcs etc but no vendor data tables.

One of the columns in the stored procedure is ef.grade. Grade is a varchar(12) it comes in the form of sa14, EF55, XA7856 etc. Another column is a GradeOrdinal column that is populated by the
 Row_Number() OVER (Partitian BY ef.Grade Order By Ef.HeatNumber ASC) As GradeOrdinal ]

Open in new window

. This gives each row of a grade a number starting from 1 to .... if the grade changes it starts from 1 to ........ depending on the number of heats produced. My problem exist where we produce a grade, change to a grade then change back to the original grade. When a grade is repeated in the query window the count doesn't return to 1 it just adds to the last value.

The solution I need is how to make the SQL think a repeat grade is a new grade.
Example Current Result set

sa14            1
sa14            2
sa14            3
sa14            4
XA7856       1
XA7856       2
XA7856       3
sa14            5
sa14            6
sa14            7
sa14            8

Example Required Output

sa14            1
sa14            2
sa14            3
sa14            4
XA7856       1
XA7856       2
XA7856       3
sa14            5
sa14            6
sa14            7
sa14            8

I have added a copy of my code and a picture of the output which shows the issue I am having. Raw Dataset is difficult.
Stored-Proc-Select.txt
Example-Error.JPG
0
Comment
Question by:OST-IS
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40506560
Not clear enough: Your "Example Current results set" and "Example Required Output" are identical?
0
 

Author Comment

by:OST-IS
ID: 40506785
Apologies for that in short I need to have the count restart everytime there is a change in Grade regardless of if it has been counted before or not.


Example Current Result set

 sa14            1
 sa14            2
 sa14            3
 sa14            4
 XA7856       1
 XA7856       2
 XA7856       3
 sa14            5
 sa14            6
 sa14            7
 sa14            8

 Example Required Output

 sa14            1
 sa14            2
 sa14            3
 sa14            4
 XA7856       1
 XA7856       2
 XA7856       3
 sa14            1
 sa14            2
 sa14            3
 sa14            4
0
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40506860
I'm not sure of the terminology for your process. It might be helpful to see a text sample of the full result set returned by the last section of your SP.
Select Top 35
			ef.HeatNumber,
			Row_Number() OVER (
					ORDER BY ef.HeatNumber ASC
				) as HeatCount,-- Generates a row number based on eaf.HeatNumber
			Row_Number() OVER (
					PARTITION BY ef.Grade ORDER BY ef.HeatNumber ASC
					) AS Grade_Ordinal,

			Grade,
			ScrapTotal as ChargeWeight,
			TapTime,
			PowerOnTime,
			[Tap-to-Tap],
			Cast(TotalPower as Decimal(6,1))/1000.0 as MWh, --Cast number to decimal
			TurnaroundTime,
			[Tap-to-Tap]-PowerOnTime as PowerOffTime,
			LadleNumber,
			CASE LadleNumber  --Case Statement to join ladle to correct lives
				WHEN 1 THEN Ladle1Lives
				WHEN 2 THEN Ladle2Lives
				WHEN 3 THEN Ladle3Lives
				WHEN 4 THEN Ladle4Lives
				WHEN 5 THEN Ladle5Lives
				WHEN 6 THEN Ladle6Lives
				ELSE NULL
				END AS LadleLives,
			BilletTonnes,
			ISNull(GeneralComments,'')+ ' ' +ISNull(Comment1,'') AS Comments

Open in new window


my best guess based on what I can make out so far would be to change your second ROW_NUMBER() column
from
                  Row_Number() OVER (
                              PARTITION BY ef.Grade ORDER BY ef.HeatNumber ASC
                              ) AS Grade_Ordinal
to
                  Row_Number() OVER (
                              PARTITION BY ef.heatnumber,ef.Grade ORDER BY ef.HeatNumber ASC
                              ) AS Grade_Ordinal
0
 

Author Comment

by:OST-IS
ID: 40508144
Hi Simoneadept,

No this produces the same result.
I think I need to do a check of the grade against the taptime column (DateTime) the heat column will always be a new unique number. Hence it leave me only the DateTime column and Grade column to compare against.
I could do this with a SCALAR function but I cant see how to do this without a table. The SCALAR function wont allow me to use the needed EXEC command to insert data into the tmp_table. Thanks OSTIT
0
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
ID: 40508849
Hi, please see my proposed solution on your re-formulated version of this question...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL 2014 get SPIDs of users 6 27
Access recordset not updateable 8 39
Sql query to Stored Procedure 6 21
Stored Procedure 2 10
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now