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
 General SQL Server Forums
 New to SQL Server Programming
 Update num ID in table by catagory ?!

Author  Topic 

mberggren
Starting Member

15 Posts

Posted - 2009-10-21 : 18:00:57
Hi,

Have a little question. I have a temporary table to store some temporary values that I´m gonna use later on to update other tables.

TABLE test

number INT
sub_cat char(16)
main_cat char(16)


Values in the table now:

1, sandals, shoes
2, flipflops, shoes
3, sneakers, shoes
4, no print, t-shirts
5, with print, t-shirts
6, cartoon, t-shirts


Something what I´m looking for:

100, shoes
101, sandals
102, flipflops
103, sneakers
200, t-shirts
201, no print
202, with print
203, cartoon


Is it possible to do this ? Update the 'number' column nr depending of the values in column 'sub_cat' and 'main_cat' .. and then do a UNION on the 'sub_cat' and 'main_cat' column.

I guess you first have to do UNION on the 'sub_group' column and then use alter table and delete the 'main_cat' column. Then use row_number with a count and do an update for on 'number' for every 'main_cat' maybe. Insecure how to to it and if I´m thinking right :)

As you can see all the 'main_cat' are staring by 100, 200 and so on.

//
//

Regards


X002548
Not Just a Number

15586 Posts

Posted - 2009-10-21 : 19:06:01
quote:
Originally posted by mberggren

Is it possible to do this ? Update the 'number' column nr




Paging Nr. Rivett, Mr. Nigel Rivett

I would ask why you want to do this

If it's a presentation issue you would do it there.

But you probably need to assign a product # to each product

I might even have a product table for main_cat




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

mberggren
Starting Member

15 Posts

Posted - 2009-10-22 : 08:38:54
Brett:

Do you mena something like this ? :)

Exampel below.

My table

1, san_product 1, sandals, shoes
2, san_product 2, sandals, shoes
3, san_product 3, sandals, shoes
4, flip_product 1, flipflops, shoes
5, flip_product 2, flipflops, shoes
6, flip_product 3, flipflops, shoes
7, snea_product 1, sneakers, shoes
8, snea_product 2, sneakers, shoes
9, snea_product 2, sneakers, shoes
10, nop_product 1, no print, t-shirts
11, nop_product 2, no print, t-shirts
12, nop_product 3, no print, t-shirts
13, wit_product 1, with print, t-shirts
14, wit_product 2, with print, t-shirts
15, wit_product 3, with print, t-shirts
16, car_product 1, cartoon, t-shirts
17, car_product 2, cartoon, t-shirts
18, car_product 3, cartoon, t-shirts



wanted result in table

100, shoes
110, sandals
111, san_product 1
112, san_product 2
113, san_product 3
120, flipflops
121, flip_product 1
122, flip_product 2
123, flip_product 3
130, sneakers
131, snea_product 1
132, snea_product 2
133, snea_product 3
200, t-shirts
210, no print
211, nop_product 1
212, nop_product 2
213, nop_product 3
220, with print
221, wit_product 1
222, wit_product 2
223, wit_product 3
230, cartoon
231, car_product 1
232, car_product 2
233, car_product 3
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-10-22 : 10:01:45
How about:



SET NOCOUNT ON
GO

CREATE TABLE #MerchandiceType (
MerchTypeCd int
, MerchType char(16)
, MerchTypeGroup int
, MerchTypeDesc varchar(256)
)

CREATE TABLE #Merchandice (
Merch char(16)
, MerchTypeCd char(16)
, MerchGroup int
, MerchDesc varchar(256)
)
GO

INSERT INTO #MerchandiceType (
MerchTypeCd
, MerchType
, MerchTypeGroup
, MerchTypeDesc)
SELECT 1, 'Shoes', 10, 'We Sell Shoes' UNION ALL
SELECT 2, 'T-Shirst', 20, 'We Sell Tee''s that Don''t Shrink'
GO
INSERT INTO #Merchandice (
Merch
, MerchTypeCd
, MerchGroup
, MerchDesc)
SELECT 'Sandals', 1, 1, 'This is not the resort' UNION ALL
SELECT 'FlipFlops', 1, 2, 'This is Georg Bush Sr.' UNION ALL
SELECT 'Sneakers', 1, 3, 'This is Slick Willy' UNION ALL
SELECT 'No Print', 2, 1, 'This is more a different type than a thing' UNION ALL
SELECT 'With Print', 2, 2, 'This is more a different type than a thing 2' UNION ALL
SELECT 'Cartoon', 2, 3, 'GW Bush'
GO


SELECT LEFT(CONVERT(varchar(3),MerchTypeGroup) + '0',3) AS Category
, MerchType AS Description
FROM #MerchandiceType
UNION
SELECT LEFT(CONVERT(varchar(3),MerchTypeGroup)
+ CONVERT(varchar(1),MerchGroup),3) AS Category
, ' ' + Merch AS Description
FROM #Merchandice m
JOIN #MerchandiceType t
ON m.MerchTypeCd = t.MerchTypeCd
ORDER BY 1,2


DROP TABLE #MerchandiceType, #Merchandice
GO

SET NOCOUNT OFF
GO






Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

mberggren
Starting Member

15 Posts

Posted - 2009-10-23 : 04:11:37
Thx a lot! :)

See if I get time this weekend to try it out. Give you an reply as soon possible!

Regards
Magnus
Go to Top of Page

mberggren
Starting Member

15 Posts

Posted - 2009-11-18 : 07:13:48
Brett:

That worked great! :)

Have a follow up question. I have a table called table1, in this table there are o lot of different columns (seq ,item, main, sub, price, .. etc) all this imported from a excel sheet.
From this columns I create different tables for storing information


CREATE TABLE custom.main (
Seq int identity
main CHAR (50)
)

CREATE TABLE custom.sub (
Seq int identity
sub CHAR (50)
)

INSERT INTO custom.main (main)
SELECT DISTINCT table1.main
FROM custom.table1
WHERE table1.main IS NOT NULL

INSERT INTO custom.sub (sub)
SELECT DISTINCT table1.sub
FROM custom.table1
WHERE table1.sub IS NOT NULL

Result of SQL
Custom.main
1, main1
2, main2
3, main3

Custom.sub
1, sub1
2, sub2
3, sub3
**

So the question is. If I have two tables like in this case. How do I pivot these tables and create a similar result as the previous post ?

Add a column called row number in the custom.sub ? This field is gonna generate this below:
1000, main1
1001, sub1
1002, sub3
1.., etc….
2000, main2
2001, sub1
2002, sub3
2.., etc….

Regards
Go to Top of Page
   

- Advertisement -