Unable to perform arithmetic operations in SQL - sql

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!

Related

Join query giving empty result SQL

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.

MDX cross join result

I'm getting a result from my cross join thay I can't wrap my heads around, see my first MDX query that gives me a result as expected. (SQL Server Analysis Services 2012)
Alright, now I want to do the same thing, only I want to target a specific date, let's take "2020-05-05". This is what happens:
I get an empty result, why don't I see the the results from the first query (limited to the rows with 2020-05-05)? I really do not understand this.
Turned out that the SSAS Cube was only partially processed, doing a full process of everything seems to have solved the problem.
Altough, I would still appricaite an explanation on why the second query failed? The first query shows quite clear that the cube, at that state, contained information on 2020-05-05?

Joining 3 Tables SQL statement

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;

does this LEFT OUTER JOIN statement look correct?

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

Access- Where and OR not working

I have the following query written in MS Access
SELECT DISTINCT Table1.ColumnA, Table1.ColumnB,Table1.ColumnC,Table1.ColumnD,Table1.ColumnE
FROM Table2
RIGHT JOIN Table1 ON (Table2.ColumnB = Table1.ColumnF)
WHERE (Table1.ColumnF <>28) OR (Table1.ColumnF<>29)
Tried to with and without Parentheses
When I just have one where statement the 262 records go down to 160 records, as expected
When I have the two conditions connected by the OR, the records go back up to 262, clearly not doing whats expected. Even if just the first condition held, I should not have gone back up to 262 records.
My question is whats wrong with my query, especially as it pertains to the WHERE XXX OR XXX?
Secondly, does the RIGHT JOIN statement have any bearing on the outcome of a subsequent WHERE statement.
Thirdly, if I cannot combined a RIGHT JOIN and a WHERE, what is the optimal way to apply conditions to a query that relies on a RIGHT JOIN?
Appreciate any help!
replace your OR with AND
WHERE (Table1.ColumnF <>28) AND (Table1.ColumnF<>29)
You can use this instead:
WHERE Table1.ColumnF Not In (28,29)
That approach expresses your intention clearly and concisely. Now that you have resolved the issue of OR vs. AND for your WHERE conditions, this suggestion probably doesn't seem very useful. However, keep it in mind for when you have several more such conditions. Not In (28,29,32,40,119) will be easier to write and understand than 4 AND s.