Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Count each item

Author  Topic 

Oliver Harris
Starting Member

6 Posts

Posted - 2003-12-16 : 17:06:41
I've been asked to create a new numbering system for an existing table. They want the records numbered by month, year and count of records in that month and year. So, for instance if there are 8 records created in Dec-03, the numbers for those items would be 12-03-001 - 12-03-008. I can generate the month and year portions, but how do I generate the count?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-16 : 18:01:24
If your data already was corrected with this new format, then this would work for future INSERTs:

INSERT INTO Table1 (Column1)
SELECT CONVERT(VARCHAR(2), DATEPART(m, GETDATE())) + '-' + SUBSTRING(CONVERT(VARCHAR(4), DATEPART(yy, GETDATE())), 3, 2) + '-' + CONVERT(VARCHAR(3), COUNT(Column1) + 1)
FROM Table1
GROUP BY SUBSTRING(Column1, 1, 5)

As far as fixing your data for this new format, I can only figure it out with a loop. Hopefully someone else can figure out a set-based method for you. Or is the data already "fixed"?

Tara
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-12-16 : 18:03:08
I would ask: what is the significance of the numbering? Why are you storing this in the table? what are you using it for? What is the primary key of this table, and what kind of data is stored in it?

- Jeff
Go to Top of Page

Oliver Harris
Starting Member

6 Posts

Posted - 2003-12-16 : 18:27:19
quote:
Originally posted by tduggan

If your data already was corrected with this new format, then this would work for future INSERTs:

INSERT INTO Table1 (Column1)
SELECT CONVERT(VARCHAR(2), DATEPART(m, GETDATE())) + '-' + SUBSTRING(CONVERT(VARCHAR(4), DATEPART(yy, GETDATE())), 3, 2) + '-' + CONVERT(VARCHAR(3), COUNT(Column1) + 1)
FROM Table1
GROUP BY SUBSTRING(Column1, 1, 5)

As far as fixing your data for this new format, I can only figure it out with a loop. Hopefully someone else can figure out a set-based method for you. Or is the data already "fixed"?

Tara



Tara,
Thanks for the reply. The data hasn't been fixed yet, that was what I was trying to do. I will keep your insert statement in mind though. Thanks.

quote:
Originally posted by jsmith8858

I would ask: what is the significance of the numbering? Why are you storing this in the table? what are you using it for? What is the primary key of this table, and what kind of data is stored in it?

- Jeff


The numbering system is what the user asked for. Personnally I would have done it differently, but they think it will be easier to track using that number rather than using the automatically generated ID number (the primary key). The application is a web application similar to this forum software. A question is asked and other users provide answers. The person who made the request then decides when the question has been answered and closes the question. The table I'm working on is basically a table of threads.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-16 : 19:27:11
Here is the looping solution to "fix" the data:



DECLARE @num INT
DECLARE @WhichMonth INT
DECLARE @WhichYear INT
DECLARE @WhichOne VARCHAR(50)

SET @num = 1

WHILE (SELECT COUNT(*) FROM Table1 WHERE ColumnA NOT LIKE '%-%') > 0
BEGIN

SELECT @WhichOne = ColumnA
FROM Table1
WHERE ColumnA NOT LIKE '%-%' AND ColumnB = (SELECT MIN(ColumnB) FROM Table1 WHERE ColumnA NOT LIKE '%-%')

SELECT @WhichMonth = DATEPART(m, ColumnB), @WhichYear = DATEPART(yy, ColumnB)
FROM Table1
WHERE ColumnA = @WhichOne

UPDATE Table1
SET ColumnA = CASE
WHEN DATEPART(m, ColumnB) < 10 THEN '0' + CONVERT(VARCHAR(2), DATEPART(m, ColumnB))
ELSE CONVERT(VARCHAR(2), DATEPART(m, ColumnB))
END
+ '-' + SUBSTRING(CONVERT(VARCHAR(4), DATEPART(yy, ColumnB)), 3, 2) + '-' +
CASE
WHEN @num < 10 THEN '00'+ CONVERT(VARCHAR(3), @num)
WHEN @num < 100 THEN '0'+ CONVERT(VARCHAR(3), @num)
ELSE CONVERT(VARCHAR(3), @num)
END
WHERE ColumnA = @WhichOne

IF @WhichMonth = (SELECT DATEPART(m, MIN(ColumnB)) FROM Table1 WHERE ColumnA NOT LIKE '%-%')
AND @WhichYear = (SELECT DATEPART(yy, MIN(ColumnB)) FROM Table1 WHERE ColumnA NOT LIKE '%-%')
SET @num = @num + 1
ELSE
SET @num = 1

END




If you've got thousands of rows in your table, this is going to be resource intensive and slow due to the looping.

Tara
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-12-17 : 01:35:16
A little set-based processing, anyone?


update a
set orderid = cast(datepart(mm, a.orderdate) as varchar) + '-' +
substring(cast(datepart(yy, a.orderdate) as varchar),3,2) + '-' +
cast(((select count(*) from #orders b
where
datepart (yy, b.orderdate) = datepart(yy, a.orderdate)
and datepart (m, b.orderdate) = datepart(m, a.orderdate)
and b.orderdate < a.orderdate) + 1) as varchar)
from #orders a


Owais

We make a living out of what we get, but we make a life out of what we give.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-17 : 12:15:33
For Owais' solution, which btw is much better because it is set-based, you'll need to add another case statement to get the format that you need since it does not give 000 format for the last three characters. If you grab my second case statement and tweak it a bit for his solution, you'll be able to do this all in one statement without the looping.

I knew it could be done the set-based way, I just don't know how to come up with those solutions yet.

Tara
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-12-17 : 13:11:19
The second case could also be replaced with this to give you a formatted three digit number:

RIGHT('000' + CONVERT(varchar, @num), 3)


Owais

We make a living out of what we get, but we make a life out of what we give.
Go to Top of Page

Oliver Harris
Starting Member

6 Posts

Posted - 2003-12-18 : 19:11:28
This is way over my head. Not sure how to translate that into my particular situation. I did manage to come up with a hack of my own to get it done though. Thanks for the help.
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2003-12-19 : 08:29:48
I see you're up+running, but as a learning exercise......it's definitely worth the investment of your time to work out what's going on here.....it'll bring you 4 leaps forward in your understanding of the capabilities of SQL, rather than inching forward otherwise....


Try running the code given....if necessary break it down into smaller parts so that you can understand each bit.


You'll be pleasantly surprised at the outcome.
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-12-19 : 10:29:44
Well said, Andrew...any time you spend on understanding SQL will not be wasted. If you can provide the table structure and some sample data in the form of DDL and DML statements, we'd be willing to show exactly how the above queries would fit into the context of your problem.


Owais

We make a living out of what we get, but we make a life out of what we give.
Go to Top of Page

Oliver Harris
Starting Member

6 Posts

Posted - 2003-12-19 : 10:45:32
I agree it would definately be worth it. If you guys don't mind helping a newbie I'd love to learn. I'm afraid you already lost me on some of the terminology though. What are DDL and DML statements?
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2003-12-19 : 11:00:20
DDL...data definition langguage....statements to create databases, tables, stored procedures, etc...
DML...data manipulation language....regular SQL statements...select, insert, delete, etc...
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-19 : 11:03:25
quote:
Originally posted by Oliver Harris

I agree it would definately be worth it. If you guys don't mind helping a newbie I'd love to learn. I'm afraid you already lost me on some of the terminology though. What are DDL and DML statements?



DDL: Data Definition Language

CREATE TABLE myTable99 (Col1 int, Col2char(1), ect)

DML: Data Manipilation Language

INSERT INTO myTable99(Col1, col2, ect
SELECT 1,'a',ect

DELETE FROM myTable99

UPDATE myTable99 SET Col1=

SELECT...


Sample Data:

Use the INSERT DML above...

but like

INSERT INTO myTable99(Col1, col2, ect
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect UNION ALL
SELECT 1,'a',ect

To give us a lot of data to work with..

And then there DCL...which I forget... Data Control Language?



Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-19 : 11:06:36
AHA...

what a scrub I yam...

http://www.computerbits.com/archive/1996/0600/sql2.html



Brett

8-)
Go to Top of Page

Oliver Harris
Starting Member

6 Posts

Posted - 2003-12-19 : 11:28:45
Wow, thanks for all the responses. I haven't ever built a table with a SQL statement, I usually just build it in the enterprise manager.

This statement would give you the pertinent fields

CREATE TABLE tblRFIs (
RFIID INTEGER NOT NULL,
RFI_Add_Date DATETIME NOT NULL,
ControlNumber CHARACTER (9) );

There's more fields than that, but these are the ones that I'm concerned with. I don't know how t set the Identity or Identity Seed property, but those are set to Yes and 1 respectively for RFIID. Does this make sense?
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2003-12-19 : 11:39:25
The above makes sense alright.....but now that you're beginning to get more than your toes wet....you should start to migrate away from EM...and get to learn and know SQL from the raw commands.


The windows interface of EM is a reasonable shortcut for avoiding the raw commands of SQL....but it's no substitute for knowing what goes on under the hood. It'll be good for you in the long rung.
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-12-20 : 03:26:41
Oliver, right click on the table in Enterprise Manager, and choose "All tasks -> Generate SQL Script" to generate the DDL for the table. It's a good way to learn how an existing table translates into DDL and how you can create tables using Query Analyzer. Unfortunately, there is no way to generate DML (for the sample data) statements from EM, but there are a few third party tools that could do the trick.

Owais

We make a living out of what we get, but we make a life out of what we give.
Go to Top of Page

Oliver Harris
Starting Member

6 Posts

Posted - 2003-12-22 : 11:33:58
quote:
Originally posted by mohdowais

Oliver, right click on the table in Enterprise Manager, and choose "All tasks -> Generate SQL Script" to generate the DDL for the table. It's a good way to learn how an existing table translates into DDL and how you can create tables using Query Analyzer. Unfortunately, there is no way to generate DML (for the sample data) statements from EM, but there are a few third party tools that could do the trick.

Owais

We make a living out of what we get, but we make a life out of what we give.



Owais,
I wondered if there wasn't something like that. That answers my question about how to set the identity property. Here is what it produced.

CREATE TABLE [dbo].[tblRFIs] (
[RFID] [int] IDENTITY (1, 1) NOT NULL ,
[RFI_Add_Date] [datetime] NULL ,
[RFI_Resp_Date] [datetime] NULL ,
[RFI_Req_Date] [datetime] NULL ,
[RFI_Close_Date] [datetime] NULL ,
[RFI_Classification] [int] NULL ,
[RFI_Precedence] [int] NULL ,
[RSID] [int] NULL ,
[SBID] [int] NULL ,
[RFI_Suggested_Resp] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[RFI_Title] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[PID_Init] [int] NULL ,
[PID_Resp] [int] NULL ,
[RFI_Web_Flag] [bit] NULL ,
[RFI_Document] [nvarchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[RFI_Alt_POC] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[RFI_Alt_Phone] [nvarchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[RFI_Alt_Email] [nvarchar] (75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ControlNumber] [nchar] (9) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Go to Top of Page
   

- Advertisement -