Search This Blog

Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

September 19, 2014

Fixed SQL Query: Adding Group By When Using ROW_NUMBER ( )

If we have ROW_NUMBER ( )  in SQL query, using SELECT DISTINCT shows the duplicate record.

SQL query

SELECT DISTINCT
COL1 AS ID,
ROW_NUMBER( ) OVER( ORDER BY COL1) AS RowNum,
COL2 AS FirstName
FROM table
INNER JOIN table ON xx=xx
INNER JOIN table ON xx=xx
INNER JOIN table ON xx=xx
INNER JOIN table ON xx=xx
WHERE ...

Result:


  
How can I remove the duplicate record when using ROW_NUMBER( ) in SQL query? The answer is to use Group By.

Modify SQL query: adding Group By
SELECT
COL1 AS ID,
ROW_NUMBER( ) OVER( ORDER BY COL1) AS RowNum,
COL2 AS FirstName
FROM table
INNER JOIN table ON xx=xx
INNER JOIN table ON xx=xx
INNER JOIN table ON xx=xx
INNER JOIN table ON xx=xx
WHERE ...
GROUP BY COL1, COL2

Result:

September 13, 2013

SQL Query: Transpose Columns to Rows via UNPIVOT

In this sprint, we have new features and I need to validate the data accuracy in our Silverlight Application and some related tables in SQL Server 2008 R2.

A table has 300+ columns

B table has 200+ columns.

The new features of Silverlight application are still in Development and automation is not possible at this moment.

If I would like to validate data accuracy in many columns in the tables, after running SQL query via SSMS, I need to use the scrollbar at the bottom to switch to the right to take a look at the values gradually. That’s not efficient.

One efficient way is to use “copy with headers” to copy the headers & values and past the data into the Excel Spreadsheet. After that, I can compare the values between Silverlight UI and the spreadsheet quickly after using transpose function in Excel. If I can transpose columns to rows in SQL query directly, that’s great too.

The following one is the simple SQL query for me to transpose columns to rows easily via UNPIVOT.


See Also

A basic SQL query to validate the distance via two Latitude / Longitude Points

March 25, 2013

A basic SQL query to validate the distance via two Latitude / Longitude Points


In this Sprint, we add a new feature called Relocation. Developers complete this feature in C# and I need to validate if the distance between stores is correct when doing relocation. No one told me how to validate the distance. As a SDET, it is important to know how to test in my own way.

After doing some research, the following Excel formula works well if you have 2 Latitude and Longitude points.

ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-Lat2)) *COS(RADIANS(Long1-Long2))) *6371

However, this is not what I want. What I want to do is:

Enter one Lat/ Long store information and the nearby stores within 16 Kilometers (about 10 Miles) appear in the database. I can use the result set to compare the data in Silverlight Application.

I can’t write a Store Procedure and a User-Defined function in SQL Server because after we push to Production, those can’t be implemented in Production and I still need to validate this scenario for my smoke test. Therefore, writing a basic SQL query is a good way.  Here is my solution:

·         We have the Longitude and Latitude Columns in the table for each store.
·         The Excel Formula is still working in SQL Server 2008 R2.
·         I need to manually enter Lat1 and Long1 as my baseline to validate the distance between stores. That being said, I don’t have to enter Lat2 and Long2 information.

After my several attempts, the following SQL Query is helpful to find the nearby stores after I enter the values of Lat1 and Long1. 



Next time if I want to use another store (Lat/Long) as a baseline to search the nearby stores within 16 Kilometers, it is easy for me to get the list of data. 

September 2, 2007

Choosing a cursor strategy

This is my key note of choosing a cursor strategy when I studied Microsoft 70-442 exam. Hope it can be very helpful for you


.



。

August 19, 2007

Do we really need a foreign key in the table?

If you worked on database design before and now I ask you a question:
“Do we really need a foreign key in the table?” , you will definitely say: Of course!
Foreign keys are important in database design. We need to follow normalization(First normal form, Second normal form and third normal from).

If you get a chance to ask Database Administrators in large companies, they might tell you: we don’t want foreign keys.

Is this the big conflict between SAs and DBAs?

Just imagine one situation:

If ABC Company has a famous website that sells many products to customers, their network traffic is very heavy. When customers place orders and want to receive their orders as soon as possible, how can ABC Company handle it?

ABC must have many database servers, but the data on the main server needs to be replicated to other servers quickly. Only 3-5 minutes, the data must be updated automatically and they can ship the products quickly. If the network traffic is so heavy and many tables have many foreign keys, the replication will be very slow and might cause failure.

Just imagine another situation:

Many companies have the daily build or weekly milestone build for developers and testers to test if their products are reliable. If there are so many tables including so many foreign keys in the database, how can DBAs setup the new build as soon as possible? If they can’t set it up immediately, many people will say: they block my task. No wonder that many DBAs say: Foreign keys are the burden of replication.


In the real world, you can have many chances to see if my opinion is right. Do we really need a foreign key in the table? Obviously, it depends on the situation.

I will say:

No replication, need foreign keys.
Need replication, no foreign keys.