I'm working on a customer level, and I have a column that contains the 3 types of products I'm selling, what I want is to know every customer what products they already purchased?
but what if that customer has purchased more than one item?
I want to create a column that tell me what they exactly bought 'Coffee','mug' or 'chocolate'..
how can I represent that in the output? again, all these info are stored in one column called 'product'
Thank you
For Snowflake, you can use LISTAGG or ARRAY_AGG depending on whether you want to store the data as a string or as an array. In Snowflake, I would recommend that you store this data as an array, as it is easier to deal with when querying later.
https://docs.snowflake.com/en/sql-reference/functions/array_agg.html
https://docs.snowflake.com/en/sql-reference/functions/listagg.html
You want to use GROUP_CONCAT (mySQL) or STRING_AGG (tSQL)
So you want to do something like this for mysql:
SELECT a.[CustomerID], GROUP_CONCAT(a.[Product]) as Products
FROM [tblSells] a
GROUP BY a.[CustomerID];
Or something like this for tsql:
SELECT a.[CustomerID], STRING_AGG(a.[Product],'.') as Products
FROM [tblSells] a
GROUP BY a.[CustomerID];
Related
I have a table called products. I have the values like XYZ-PR,PR-XYZ,XPR-YZ,XYPR-Z, XYZ.
Assume I have PR is common in that column for most of the values. Now I need to pick the data based on PR
irrespective of the position of PR.
Lets say in this example now I want to retrieve these records. XYZ-PR,PR-XYZ,XPR-YZ,XYPR-Z
Please let me know what is the function we have to use it here in Snowflakae,
You would use LIKE:
where col like '%PR%
I have an SQL table with product names in one column and a product category in another column. So each product belongs to a specific category. I am trying to figure out an sql command that will return distinct values from the product name column but will also display the product category each product belongs to. Any ideas?
You can try like this,
SELECT Distinct([ProductName]),[ProductCategeory] FROM [DB].[dbo].[tblProduct]
I'm having a table with an id and a name.
I'm getting a list of id's and i need their names.
In my knowledge i have two options.
Create a forloop in my code which executes:
SELECT name from table where id=x
where x is always a number.
or I'm write a single query like this:
SELECT name from table where id=1 OR id=2 OR id=3
The list of id's and names is enormous so i think you wouldn't want that.
The problem of id's is the id is not always a number but a random generated id containting numbers and characters. So talking about ranges is not a solution.
I'm asking this in a performance point of view.
What's a nice solution for this problem?
SQLite has limits on the size of a query, so if there is no known upper limit on the number of IDs, you cannot use a single query.
When you are reading multiple rows (note: IN (1, 2, 3) is easier than many ORs), you don't know to which ID a name belongs unless you also SELECT that, or sort the results by the ID.
There should be no noticeable difference in performance; SQLite is an embedded database without client/server communication overhead, and the query does not need to be parsed again if you use a prepared statement.
A "nice" solution is using the INoperator:
SELECT name from table where id in (1,2,3)
Also, the IN operator is syntactic sugar built for exactly this purpose..
SELECT name from table where id IN (1,2,3,4,5,6.....)
Hoping that you are getting the list of ID's on which you have to perform a query for names as input temp table #InputIDTable,
SELECT name from table WHERE ID IN (SELECT id from #InputIDTable)
Pulling data from a cmdb into another repository. Problem is the cmdb data has misspelled/duplicate records (e.g., some assets have a Department Name as Marketing, or Markting, or Marketing& -- when they are all just in Marketing). Want to run a select query that displays all incorrectly named department records as the single, correct name. Any help on how to approach this?
You can use CASE in to display "marketing" for its wrong entries. But query can be complicated depending on variations.
Better + easier way is a global search and replace in column. Following article describes it:
http://www.codecandle.com/articles/sql/update/483-sql-update-with-global-search-and-replace.html
Cleaning duplicate rows, following article may help:
http://www.codecandle.com/articles/sql/windowing/503-deleting-duplicate-rows-using-windowing.html
I'm sure this is passed but http://openrefine.org/ would probably help you clean the messy data.
you can use the SELECT DISTINCT statement is used to return only distinct (different) values.
you should use distinct keyword before coloumn names in select statement.
e.g: select distinct name (Coloumn name)
from table name;
I have a customer table, which has a field "categories". It's a string holding IDs divided by commas like 1,11,14,21.
Let's say I want to query all customers which have categoryID 1 - what's the proper way to query it?
The problem is that ID 1 could be at the beginning, middle or the end of the string, or even be the only ID so I'd have to cover all cases like:
WHERE categories LIKE '1'
OR categories LIKE '1%,'
OR categories LIKE '%,1,%'
OR categories like '%,1'
Is there a more elegant (and probably much faster) way to do this?
You should instead have a CustomerCategories table which has CustomerID, CategoryID columns, and then have one entry per customers category. This is far easier to query - and is very extremely highly the norm for relational databases.
Storing arrays of IDs in strings is non-relational and as you've found is a nightmare to query.
You should strangle the person who designed the DB to work like that. Never can I think of an instance where you should have delimited data in a field.
On that note, I think this will be a suitable workaround for you.
WHERE ','+[categories]+',' LIKE '%,1,%'