I have a table called product_info where there are two columns(product, product_id).There are 5 ‘product_id’ and 10 ‘product’. I wrote the following code to list products and their count.Now I need to create an additional column called ‘favorite_product’ if the ‘product’ count is more than 3.When I tried with a couple of WHERE clause options, it filtered out my existing
Tag: count
Count of all occurrences on all rows
I need to complete an overall count of occurrences of one column and all rows. As an example of data: How many times the place has occurred. I’ve done this so far which counts the years but how do I then do a count on that count So how do I now count my places by the year counts? For
SQL Count Customers where Products are the same
I have a problem. I have a SQL-database named customers with the 3 columns Country, CustomerID and Product. With that database, I want to count all the customers which bought the products bike, flask and helmet but grouped by the country as you can see in the following picture. Is there a chance you can help me out with that?
SQL Update Multiple columns with counts
We currently have SQL update statements that works for our needs and updates the tbl_rprt table. Below are some of the update queries, there are several others like them. I was wondering if there was another way to get this done, maybe combining all of them into one SQL statement. #tbl_rprt is a report of all the counts of specific
Divide the column by the sum grouped by another column
I have a table like this and I would like to divide the frequency by the sum of frequency grouped by the source. Therefore, I am looking for a table like Is there any way to do this in a single sql query? Answer You can use SUM() window function. If you want a numerical result: Depending on your database,
Select values for a column only if there are no rows with that value that satisfy a condition
I have a table that stores logs of http fetches, let’s say we have two columns: fetch_url and success. A few example rows: success fetch_url TRUE A FALSE A FALSE B FALSE B FALSE …
Lookups in for single PostgreSQL table suddenly extremely slow after large update
I have a messages table with a few million records in it. My Rails app includes a query on most pages to count the number of unread messages to show the user. This query – and all queries of the messages table – is unchanged and was working fine until yesterday. Yesterday, I created a new messages column and ran
Group by and display count in Excel
I am trying to learn some Excel’s features and I want to know if it is possible to make an SQL-like statement in Excel. For example, I distincted a dataset and the output is: I want to make a count(*) and a group by like: Answer Initial data: Create a pivot table. When you drag columns like this… …your pivot
Retrieving the distinct count across multiple columns
Say I have the following table in MS SQL Server: Make Model Year Honda Accord 1997 Honda Accord 1997 Honda Accord 1997 Honda Civic 2001 Honda Civic 2005 Honda Civic 2005 Toyota Corolla 2010 Nissan Pathfinder 1997 I want to be able to retrieve the count for the unique combination and get the results like the following: Make Model Year
SQL query filter down to only applicable rows
How would I select a query like this? I am a bit confused because this would return nothing since any of the rows do not fulfill any of the “where clauses”.. What I would like is the answer …