question

Ghazaly avatar image
Ghazaly asked

SQL database and case statement

I am waiting for a interview on monday they said their will be a test also.

So i am guessing for some questions.

1) What precaustions should you take when restoring an database which is already in the server.

2)

                    
Customer no    pay type  amount                     
     1               pay     100                    
     2               pay     150                    
     1               cre      20                    
     3               pay    200                    
     3               cre      50                    

I need to get the total for each customer but in the pay type 'pay' add and if the pay type is 'cre' it has to deduct from the total. How do you get a group total for each customer.

any body knows about question of SQL server please pass it with the answers. Altough I have experience in ORACLE and INFORMIX , I have limited experince in SQL Server I need some help on this.

Thanking you

group-byinterview-questions
10 |1200

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

Kristen avatar image
Kristen answered

1a) All users excluded during the restore (so they know what is going on)

1b) Consider what data has been lost and how that will be dealt with. For example, if your database is taking orders in real time order numbers will have been given to customers, those orders will now not be delivered. If you just Restore those same order numbers will be reassigned to new customers; when the existing customers phone up to complain, and quote the order number they were given, confusion will be total!

Bumping up all IDENTITY fields by " a reasonable" amount so that no previously assigned IDENTITY numbers will be reallocated might be prudent.

2)

            
SELECT [Customer no],            
       [Total] = SUM(CASE WHEN [pay type] = 'cre'             
                          THEN 0 - [amount]             
                          ELSE [amount]             
                          END            
                     )            
FROM MyTable            
WHERE ...            
GROUP BY [Customer no]            
ORDER BY [Customer no]            
10 |1200

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

Ghazaly avatar image
Ghazaly answered

hi thanks for that answers, i need to know one more question what\ is the difference between stored procedures and functions

10 |1200

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

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.