SQL learner here, trying to make a join, but it seems to not work.
I have the following 2 tables:
#device_combined_players
#final_results2
The goal is to have a new table replacing the player_store_id from #final_results with the pseudo_name from #device_combined_players.
I have tried with:
SELECT #final_results2.player_store_id,
#device_combined_players.pseudo_name, #final_results2.genre FROM #device_combined_players INNER JOIN #final_results2 ON #final_results2.player_store_id = #device_combined_players.store_id
but I can't make it work, my output is simply an empty table.
Could you guys, please, give me some light? Thank you!
My expected result would be: as #final_results2 (image 2), but replacing player_store_id column by pseudo_name from #device_combined_players table.
EDIT: a screenshot with more details:
https://i.stack.imgur.com/lSyLb.png
And, I am answering myself, since I had 2 different issues here. Both are rookie mistakes, but I will leave them here so it helps somebody else in the future:
Take a look at the data type of your columns. They can cause conflicts if they are not the same as the columns you are referencing in a JOIN.
Check if your data doesn't have spaces. You can use TRIM and LEN to help you out answering this.
I sorted my problem like that.
Related
I hope you're well.
I've been toying with an SQL query recently but there's a column that is supposed to be the result of an arithmetic operation that does not seem to work for me. Here is the code I'm using:
select activecampaign.campaign_step,
count(activecampaign.email_name) as email_opened,
count(contracts.uuid) as contracts,
count(contracts.uuid)/count(activecampaign.email_name)*100 as cr_rate
from il.mktg_activecampaign_email_opens activecampaign
full outer join bl.leads leads
on leads.uuid = activecampaign.uuid
full outer join bl.contracts contracts
on leads.uuid = contracts.uuid
and contracts.conversion_channel = 'crm'
group by activecampaign.campaign_step
The first three select parameters are working just fine but the fourth one is not displaying anything I would expect. Here's a screenshot of the table it's producing:
My expectation here for the second row would be to see 3.86 but nothing of the sort is showing unfortunately.
Would anyone be able to point me in the right direction to solve this :) ?
Thank you very much for your time!
How to fix this error?. I've already tried looking for a double declaration of plant in my sql script, but there seems nothing wrong with my script. All other sql joins I made involving/having a column name ID work just fine anyway. Also when I try to run my original script which is this
select tblCustomerList.CustomerName, tblPackages.Package,tblChangeLevels.ChangeLevels,tblDevices.Devices,tblPlant.Plant
from ATPChangeControlBoard.tblCCB
inner join ATPChangeControlBoard.tblCustomerList
on ATPChangeControlBoard.tblCCB.custAffected=ATPChangeControlBoard.tblCustomerList.ID
inner join ATPChangeControlBoard.tblPackages
on ATPChangeControlBoard.tblCCB.packageAffected=ATPChangeControlBoard.tblPackages.ID
inner join ATPChangeControlBoard.tblChangeLevels
on ATPChangeControlBoard.tblCCB.changeLevel=ATPChangeControlBoard.tblChangeLevels.ID
inner join ATPChangeControlBoard.tblDevices
on ATPChangeControlBoard.tblCCB.devsAffected=ATPChangeControlBoard.tblDevices.ID
inner join ATPChangeControlBoard.tblPlant
on ATPChangeControlBoard.tblCCB.plant=ATPChangeControlBoard.tblPlant.ID;
it works just fine and doesn't show any error on the plant column.
Below is the image screenshot of the actual code I used that shows the error.
Pls. Help. TIA. :)
Just found the answer on another thread.
I change all the plant/[column name] declaration to tblPlant.plant/[tablename].[columnname] to directly differentiate between columns with the same column name.
In my case I change my code in the where clause from
and (#plant is null or plant= #plant)
to
and (#plant is null or tblPlant.plant= #plant)
although the other sql joins didn't require me to add extra code like this, this one did so I did so and just added extra code to the one that requires it.
I found the answer on a comment here, and since I can't upvote or comment yet, I just decided to emphasize here that it is what solved my problem. :)
Ambiguous column name error, how do I fix it?
by #JohnFx
he said that
"The problem is that you haven't specified the table name for the field "Flags" and it probably exists in more than one table in the query. Add the table name in the format "Tablename.flags" to the front of all references to fix the problem."
can someone please help me getting the correct SQL statement, I added a picture of the example and I need some help using the sql statements as a beginner..
I want the result to show exactly the same in a gridview..I attached a picture to explain..
appreciate any help guys!
I hope this helps you. I do not have a reputation to comment on Stack Overflow right now, hence posting as answer. Also, please post the table names, it helps to write query easily.
I have named the first table as ORDER, second as ORDER_SUMMARY and third as MODEL. Let me know if this solves your query.
SELECT O.CUSTOMER_NAME,
O.CUSTOMER_ADDRESS,
O.DATE_OF_PURCHASE,
O.ORDER_BY,
O.ORDER_NUMBER,
O.PALLET_NUMBER,
M.SERIAL_NUMBER,
OS.PIECE_PRICE
FROM ORDER O
JOIN ORDER_SUMMARY OS ON O.ORDER_NUMBER = OS.ORDER_NUMBER
JOIN MODEL M ON M.PALLET_NUMBER = OS.PALLET_NUMBER;
I' not getting an error message but i seem to be only getting 5 results no matter what i choose, so this leaves me to beleive that there is something wrong with my SQL statement. I'm usint node.js tedious to connect to sql 2008 r2 database. Previous statements worked, but that was a simple select statement with the posting date where clause. I know there are spaces in my column names which is not good practice, but there is nothing i can do about that this because thedatabase is utilized by Navision. Normally there are no line breaks, i just did that to make it appear more orderly to you guys.
SELECT
TA.[Amount],
TA.[Posting Date],
TA.[Entry No_],
TA.[Document No_],
TB.[Salesperson Code]
FROM [ShowTex Belgie NV$G_L Entry] as TA
LEFT OUTER JOIN [ShowTex Belgie NV$Sales Invoice Header] as TB
ON TA.[Document No_]=TB.[No_]
WHERE TA.[Posting Date]>'05/01/2015'
AND TA.[Document Type]='2'
AND TA.[Gen_ Posting Type]='2'
Strangely Enough the SQL turns out not to be the issu. It is the conversion from JSON.stringify to JSON.parse. I was certain i had tested this successfully before. But perhaps i was negligent. Anyways My code looks like this. when i Console.log(retVal) the data still looks correct about 100 reccords as expected.
retVal = JSON.stringify({result:dataSet});
fn(retVal);
function fn(retVal){
var obj = JSON.parse(retVal);
for(var i = 0; i<Object.keys(obj.result[0]).length;i++){
console.log(obj.result[i]);
}
}
If i console.log(retVal) inside the fn function i still get my wall of text as expected. but within the FOR statement i only get the first 5 records. Hmm I bet I'm headbutting a rookie mistake at the moment. :/
Totally depends on what you want but here is a cheat sheet with venn diagrams. http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg so that it might be more logically to see what you are doing.
Wow, i found my problem. It's really embarrassing, the SQL was correct, i was counting the number of columns in my json file, this returned 5 each time. Sorry for Wasting people's time. Thank you so much for your feedback. It was helpfull in a sense that i was fixated on getting my sql statement wrong because I'm not good at those so your feedback was helpfull. It never occurred to me that I could still be running into rookie mistakes likes this.
i<Object.keys(obj.result[0]).length
needs to be
i<Object.keys(obj.result).length
Before I ask my question will layout what I'm trying to do.
I have this table such as below
Columns - PID, Choice1, Choice2,......Choice10
Rows - 1,X, O, X, O.........
Ive been searching on the net for quite some time and need a little push in the right direction if what I'm trying to do is possible. While getting the coding will help me with the small project I'm doing, it doesn't really help me learn more about SQl.
Is it possible to do a search on the table and return only the columns that have a value of X where PID = some value??
My gut instinct is saying no and I might have to restructure my database to accomplish what I'm doing. As i said a point in the right direction where I can read up on what I'm trying to do is great, getting the coding for it.. really doesn't help me learn it for future reference.
It does sound like you should restructure your database, but you can use PIVOT and UNPIVOT to transpose and restructure the output table. Columns are normally fixed with a variable number of rows depending on the WHERE clause. Using PIVOT can swap columns for rows, giving you what you need.