Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

T-SQL Stored Procedure Count by recurring ID

Posted on 2014-12-18
5
142 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

790 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