question

abhi2702 avatar image
abhi2702 asked

Store procedure

hi i have a table Name var value1 value2 Abc aa 123 10 Bcd bb 32 21 Fgr cc 12 34 Etc ….. I need to (create new/update) following result table. For every aa I should have three entries ie aa, ab and ac. Similarly for every bb I should have 2 entries like bb and ba and cc should have four entries like cc,ca,cb and cd and so on. And these extra entries will remain constant like for aa resulting table will have 3, for bb it will have 2 and for cc it will be 4 etc Rest of the values in table will not change so my resulting table becomes Name var value1 value2 Abc aa 123 10 Abc ab 123 10 Abc ac 123 10 Bcd bb 32 21 Bcd ba 32 21 Fgr cc 12 34 Fgr ca 12 34 Fgr cb 12 34 Fgr cd 12 34 I would be really appreciate if some one could tell me about how should i approach this ... thank you in advance ....
stored-procedurestsql
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
Forgive me, I'm not sure I understand correctly. How do you determine that for every AA there should be 3 entries, BB just 2 entries and CC will have 4? Where does that magic number come from? Is it set in stone somewhere?
1 Like 1 ·
JohnM avatar image JohnM commented ·
Also, what version of SQL Server are you using?
0 Likes 0 ·
abhi2702 avatar image abhi2702 commented ·
@JohnM sorry about that but i am having another table storing these values say actual var aa aa aa ab aa ac bb bb bb ba cc .... i am using SQL server 2005 ...
0 Likes 0 ·
Tim avatar image Tim commented ·
+1 for voting an answer.
0 Likes 0 ·

1 Answer

·
TimothyAWiseman avatar image
TimothyAWiseman answered
You could do a join between the two tables to get the results, then you can put it in another table or create a view as needed. It would look something like: select a.name, b.actual as var, c.value1, c.value2 from tblTheFirst a join tblTheSecond b on a.var = b.actual
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
Beat me to it. ;-)
0 Likes 0 ·
TimothyAWiseman avatar image TimothyAWiseman commented ·
I looked after he answered your questions ;-)
0 Likes 0 ·
abhi2702 avatar image abhi2702 commented ·
@timothy n @johnM it worked .... thank you guys for the help ...
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.