adspace


how would you write a sql query to compute a frequency table of a certain attribute involving two joins? What changes would you need to make if you want to order by or group by some attribute? What would you do to account for nulls?

Answer Posted / Satyendra Anuragi

To compute a frequency table with two joins, use SQL's JOIN and GROUP BY statements. Here is an example using the 'customers', 'orders', and 'order_items' tables where the target attribute is 'product_id'nn```sqlnSELECT order_items.product_id,n SUM(order_items.quantity) as total_quantitynFROM customersnJOIN orders ON customers.customer_id = orders.customer_idnJOIN order_items ON orders.order_id = order_items.order_idnGROUP BY order_items.product_id;n```nTo order by a certain attribute, use the ORDER BY statement at the end of your query like so:n```sqln... ORDER BY total_quantity DESC;n```nTo account for NULL values in your data, you can use the COALESCE function to replace NULLs with zeros or any default value.n```sqln... , COALESCE(order_items.quantity, 0) as quantity ...n```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is clustered index

1101


Disadvantages of the indexes?

1235


Can we make the the chages By Using the Sql if u know any function or process please inform me Actuall result: BRK1 Break 1 Part 1 00:01:00:00 60 BRK1 Break 1 Part 2 00:01:00:00 60 BRK2 Break 2 Part 1 00:01:00:00 60 BRK2 Break 2 Part 2 00:01:00:00 60 BRK2 Break 2 Part 3 00:01:00:00 60 BRK3 Break 3 Part 1 00:01:00:00 60 BRK3 Break 3 Part 2 00:01:00:00 60 Desired O/P: BRK1 Break 1 Part 1 00:01:00:00 60 Part 2 00:01:00:00 60 BRK2 Break 2 Part 1 00:01:00:00 60 Part 2 00:01:00:00 60 Part 3 00:01:00:00 60

2303


Equi join and non equi join is possible with sql server?

1141


What are the risks of storing a hibernate-managed object in a cache? How do you overcome the problems?

1213


What are the different SQL Server Versions you have worked on?

1087


Can we shrink data file in sql server?

1140


What is acid mean in sql server?

1260


How can we solve concurrency problems?

1133


What is an indexed view?

1036


Can you index views?

960


What is the primary use of the model database?

1163


explain different types of backups avaialabe in sql server? Given a particular scenario, how would you go about choosing a backup plan? : Sql server database administration

1140


This question asked during interview, 2) At the end of each month, a new table is created for each bank that contains monthly metrics consolidated at the account level. The table naming convention is bankX_YYYYMM where X represents the numeric designation of the bank and YYYYMM indicates the 4 digit year and 2 digit month. The tables contain the following fields: name data type description account text account number registered boolean indicates whether the account is registered num_trans integer number of transactions made during the time period spend numeric(9,2) total spend during the time period a) Write a SQL query that will display the total number of transactions and total spend for "Bank1" during the 4th quarter of 2009. b) Write a SQL query that will display the total number of transactions and total spend at "Bank1" and "Bank2", broken out by registered vs. non-registered accounts, during January 2010 not sure what is correct answer and how to solve?

2577


Explain “row_number()” in sql server with an example?

1089