multiple result query as csv string in new column - sql

I ve got 3 tables:
User, product user2product
each product has got ID.
Is it possible to write query in which as result I would get 2 columns:
UserID, Products
And in products column I have all products connected to user by user2product table separated with comma.

what SQL-Dialect are we talking about?
For postgresql, there is a pretty comprehensive answer to be found here:
How to concatenate strings of a string field in a PostgreSQL 'group by' query?
For other SQL-Systems the idea should be pretty much the same. You would have to search for the correct aggregate function, everything else can be done using a simple group by directive.
Cheers
Thilo

Its quite possible to do this using in native tsql using the for xml path statement as detailed here
To access this through linq create a stored procedure on the db, drag it into your dbml file and then call it from the data context like a method.
The code for the sp would look something like this
select
u.userid,
substring(( select ',' + cast(p.productid as nvarchar(20))
from
user2product up inner join
product p on up.productid = p.productid
where
up.userid = u.userid
for xml path('')),2,2000000) as Products
from
users u

Related

INNER JOIN Syntax Error

I would like to JOIN 2 databases.
1 database is keyword_data (keyword mapping)
1 database is filled with Google rankings and other metrics
Somehow I cannot JOIN these two databases.
Some context:
DATA SET NAME: visibility
TABLE 1
keyword_data
VALUES
keyword
universe
category
search_volume
cpc
DATA SET NAME: visibility
TABLE 2
results
VALUES
Date
Keyword
Website
Position
In order to receive ranking data by date I wrote the following SQL line.
SELECT Date, Position, Website FROM `visibility.results` Keyword INNER
JOIN `visibility.keyword_data` keyword ON `visibility.results` Keyword
= `visibility.keyword_data` keyword GROUP BY Date;
(besides that, 100 other lines with no success ;-) )
I am using Google BigQuery for this with standard SQL (unchecked Legacy SQL).
How can I JOIN those 2 data tables?
How familiar are you with SQL? I think you're using aliases wrong, something like this should work
SELECT r.Date, r.Position, r.Website
FROM `visibility.results` AS r
INNER JOIN `visibility.keyword_data` AS k
ON r.Keyword = k.keyword
GROUP BY DATE
First of all i have never worked with Google big query but there is a couple of things wrong in my opinion with this query.
To start with you join tables by including the name of the table then you provide the key that the tables are joined by. Also if you don't use aggregate functions (MIN/MAX etc.) in your select statement you must include all values in the group by clause as well. In reference I can provide you a solution that would work if you would of used Microsoft SQL Server if that would be of any help because if you reference here the syntax is quite similar.
SELECT results.Date AS DATE,
,results.Position AS POSITION
,results.Website AS WEBSITE
FROM visibility.dbo.keyword_data AS keyword_data
INNER JOIN visibility.dbo.results AS results
ON results.keyword = keyword_data.keyword
GROUP BY results.Date
,results.Position
,results.Website

Changing data types and using LIKE in LEFT OUTER JOIN

I an using a database that in my opinion was not set up in the most intuitive way. I have two tables I am trying to join. The store table has info about the store and has a store_id field that is stored as a 7 digit number in the varchar format. The transaction table has a transaction_id field that is set up as hstore and stored as "package_id"=>"10000417". I am trying to join the two tables on these fields.
I have attempted to cast the transaction_id field as a varchar, at the same time tried to use a subquery in the join statement to add the "package_id"=>" portion to the store_id field. Yielding no results. So I tried to use LIKE in the JOIN statement to add wildcards. Still having problems. Here is the code I tried when using LIKE:
SELECT t.amount
,t.quantity_sold
,t.unit
,cast(t.transaction_details as varchar) as join_field
,s.product_name
,s.product_category
,s.metrc_id
FROM transactions t
LEFT JOIN store_info s ON t.transaction_details = s.store_id
WHERE t.transaction_details LIKE '%store_id%'
I have tried a number of other variations. I am hoping someone can provide a hint for me on a better direction to go with trying to link these two tables.
Thanks in advance,
EDIT
I am used to using SQL Server as opposed to Postgresql. I have since done more research on the hstore data type and understand it slightly better. I am still having trouble joining the two tables. I managed to create a new column with only the ID number by using the following query.
SELECT *, transaction_details->'package_id' as ID
FROM transactions
I tried to JOIN by then calling that column to no avail, so I tried the following:
SELECT *
FROM transactions t
LEFT JOIN store_info s ON t.transaction_details->'package_id' = s.store_id
I also tried to cast to integer or varchar using the following
SELECT *
FROM transactions t
LEFT JOIN store_info s ON cast(t.transaction_details->'package_id'
as varchar) = s.store_id
Sorry for being such a noob. Thanks for helping.
Your code doesn't work for two reasons.
The store id and transaction details don't match, by your descriptions of the values in these columns.
The like pattern is simply a string that looks like '%store_id%'. The column value is not in the string (see below on how to do this).
You need something like this:
SELECT . . .
FROM transactions t LEFT JOIN
store_info s
ON t.transaction_details LIKE '%' || s.store_id || '%';
However, I doubt that will do want you want. The store id should be at the beginning or end of transaction details, suggesting:
ON t.transaction_details LIKE '%' || s.store_id
or:
ON t.transaction_details LIKE s.store_id || '%'
And, you are correct that this is a very bad data structure. At the very least, the ETL process should be parsing out the store information from the transaction.

Adding multiple rows of data into one - TSQL

I'm currently working on a project of my own that uses an SQL DB to store character information for an RPG game. I'm trying to write a select statement to retrieve the information related to the equipment the user is using, at the moment I have:
SELECT
CE.CharacterID, CE.EquipmentSlotID, CE.ItemID, I.ItemTypeID, I.Name, I.Image, IT.Name, IT.Description, concat(IA.AttributeID, ' ', IA.value )
FROM
CharacterEquipment CE INNER JOIN Item I
ON CE.ItemID = I.ItemID
INNER JOIN ItemType IT
ON I.ItemTypeID=IT.ItemTypeID
INNER JOIN ItemAttribute IA
ON I.ItemID=IA.ItemID
WHERE CE.CharacterID = 1
ORDER BY CE.ItemID ASC;
This returns this:
My problem is that each item (ItemID) can have multiple attributes (AttributeID) and I want all these attributes to appear on a single line but I'm not sure how I'd go about combining the results
EDIT:
Sorry guys, should have said, I've been experimenting with the group concat function but SQL isnt my strong suit so im having bother writing it and figuring out how it works, any chance any one could explain it abit more indepth? Thanks
Possible solution to this would be improving your database structure and try denormalizing it.
2.But if you wish to continue with same structure try using Concatenation using COALESCE function
For your reference :Concatenate many rows to one

How to write an SQL query that searches for a group of words from another table?

I am trying to write a query for Stack Exchange's very own Data Explorer. This query will create a temporary table containing a list of commonly misspelled words and their proper spellings and then search through the posts to find them.
Here is what I have so far (minus the comments):
DECLARE #SpellingMistakes TABLE (wrong VARCHAR(255), right VARCHAR(255))
INSERT INTO #SpellingMistakes (wrong, right)
VALUES ('ubunut', 'ubuntu')
SELECT Id as [Post Link]
FROM Posts
WHERE
...
And that's where I get stuck - in the WHERE clause. I need some way of saying "if Posts.Body contains any of #SpellingMistakes.wrong". However, I'm not sure how to do that.
Note: the data explorer uses Microsoft SQL Server 2008 R2.
I don't know MS SQL, but most SQL implementations have a 'LIKE' equivalent. So in that case, you could join the two tables and use LIKE in the JOIN condition.
SELECT Id as link
FROM Posts P JOIN SpellingMistakes S
ON P.Body LIKE '%'+S.wrong+'%'
EDIT: Assuming Posts is a large table (and SpellingMistakes is not too small either), this will take a lot of resources. One way to tackle this is to split table Posts into smaller subsets and construct multiple statements.
SELECT Id as link
FROM (SELECT * FROM Posts WHERE Id<=10000) P
JOIN
SpellingMistakes S
ON P.Body LIKE '%'+S.wrong+'%'
SELECT Id as link
FROM (SELECT * FROM Posts WHERE Id<=20000 and Id>10000) P
JOIN
SpellingMistakes S
ON P.Body LIKE '%'+S.wrong+'%'
And so on.
SELECT Id as PostLink
FROM Posts p
WHERE
EXISTS
(
SELECT *
FROM #SpellingMistakes
WHERE p.Body LIKE '%' + wrong + '%'
)

Cross-database queries with numbered database name

I'm a bit of a novice when it comes to SQL Server 2005. I have a database containing most of the stored procedures and tables (we'll call it 'GrandDatabase'). Each user has its own separate database named after the user's numbered ID. So I have a database list as follows, for example:
GrandDatabase
100
101
102
...
I need to join tables across the GrandDatabase and a user's database. I've read elsewhere that the following should work, when executed from GrandDatabase:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
100.dbo.UserInfo ON GrandDatabase.User.UserID = 100.dbo.UserInfo.UserID
This gives me a syntax error, complaining about the '.' right after the first reference to the 100 database. I did a little tweaking and discovered that this code works fine when I use non-numbered databases (for instance, replacing the '100' above with 'User100'). Does anybody know how to make this work with numbered database names?
Thanks!
Chris
Try using [100].dbo.UserInfo instead of just the 100.
Try putting the numbers into square brackets and using aliases, e.g.:
SELECT *
FROM GrandDatabase.User
INNER JOIN [100].dbo.UserInfo u
ON GrandDatabase.User.UserID = u.UserID
Try enclosing the database name with brackets:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
[100].dbo.UserInfo ON GrandDatabase.User.UserID = [100].dbo.UserInfo.UserID