Solved

T-SQL Stored Procedure Count by recurring ID

Posted on 2014-12-18
5
141 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:Simon
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:Simon
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:
Simon earned 500 total points
ID: 40508849
Hi, please see my proposed solution on your re-formulated version of this question...
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

832 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