Selecting value via substracting another value from a total number - sql

I am doing simple mysql database, and quite knew so sorry if i am bad at formulating this question.
I am asked to make a select statement finding a value in a table, via substracting another value from a total mass.
I have a table called FA:
14:0 200
16:0 400
18:0 600
so if i have a total mass of 800 i want to find 18:0 by substracting 14:0 (200). How can i do this with a select statement?
Hope you guys can help..

Based on what I can gather from your question:
SELECT ID FROM FA WHERE MASS = 800 - (SELECT MASS FROM FA WHERE ID = '14:0')

Related

Join multiple tables in Microsoft SQL Server where there is only one line match from table 1 and multiple lines from table 2 and 3

I am stuck on something, which I have never used in my 10 years of SQL. I thought it would be useful if there was someway of doing this. Firstly I am running SQL Server Express (latest free version) on Windows. To talk to the database I am using SSMS.
There are three tables/queries.
1 table (A) has one data value I want to pull through.
2 tables (B)/(C) have multiple values.
Column common to all tables is CAMPAIGN NAME
Column common to (B)/(C) is PRODUCT NAME
This is an example of the data:
OUTPUT GOAL
I have tried the following:
UNION ALL (but this does not assist when I want to calculate AMOUNT - MARKETING - TOTAL INVESTMENT
I tried PARTITION (but I simple could now get it to work.
If I use joins, it brings through a head count / total investment and marketing cost per product, which when using SUM brings through the incorrect values for head count / total investment and marketing cost vs total amount, quantity.
I tried splitting the costs based on Quantity / Total Quantity or Amount / Total Amount, but the cost associated with the product is not correct or directly relating to the product this way.
Am I trying to do something impossible, or is there a way to do this in SQL?
The following comes pretty close to what you want:
select . . . -- select the columns you want here
from a join
b
on b.campaign_name = a.campaign_name join
c
on c.campaign_name = b.campaign_name and
c.product_name = b.product_name;
This produces a result set with a separate row for each campaign/product.

Using SQL Server query desired table output for the given table

I have a following table in SQL Server 2008 and it needs to be shown in a particular format. I was thinking of what approach should I follow to get desired output. Whether cursor needs to be used or not or multiple while loops to traverse each rows. This table has multiple accounts and dt_id is unique.
Kindly find the table below:
Desired output:
The problem statements is for eg in first table for first non zero dt_debit amount is 100 and I need to check whether this value cancels out in dt_credit column on FIFO method.
So in this case in first loop 100 gets compared to 500. So here it's less than
500 therefore it will go in the 2nd table.
Then it will go to 2nd value that is 400 and it will compare previous remaining balance that is 500 - 100 = 400 so again since its not greater again entry will go in desired output table.
Now for value 50 it will compare with 300 and 250 balance will be left. Next value is 300 therefore for 300 values will go twice as it will come in 250 + it will also take 50 from 80 dt_credit.
Can this logic be done in SQL Server. I am using SQL Server 2008. Any help would be appreciated.

SQL JOIN with CASE statement result

Is there any way of joining the result of a case statement with a reference table without creating a CTE, ect.
Result AFTER CASE statement:
ID Name Bonus Level (this is the result of a CASE statement)
01 John A
02 Jim B
01 John B
03 Jake C
Reference table
A 10%
B 20%
C 30%
I want to then get the % next to each employee, then the max %age using the MAX function and grouping by ID, then link it back again to the reference so that each employee has the single correct (highest) bonus level next to their name. (This is a totally fictitious scenario, but very similar to what I am looking for).
Just need help with joining the result of the CASE statement with the reference table.
Thanks in advance.
In place of a temporary value as the result of the case statement, you could use a select statement from the reference table.
So if your case statement looks like:
case when variable1=value then bonuslevel =A
Then, replacing it like this might help
case when variable1=value then (select percentage from ReferenceTable where variable2InReferenceTable=A)
Don't know if I am overly simplifying, but based on the results of your case result query, why not just join that to the reference table, and do a max grouped by ID/Name. Since the ID and persons name wont change anyhow since they are the same person, you are just getting the max you want. To complete the Bonus level, rejoin just that portion after the max percentage determined for the person.
select
lvl1.ID,
lvl1.Name,
lvl1.FinalBonus,
rt2.BonusLvl
from
( select
PQ.ID,
PQ.Name,
max( rt.PcntBonus ) as FinalBonus
from
(however you
got your
data query ) PQ
JOIN RefTbl rt
on PQ.BonusLvl = rt.BonusLvl
) lvl1
JOIN RefTbl rt2
on lvl1.FinalBonus = rt2.PcntBonus
Since the Bonus levels (A,B,C) do not guarantee corresponding % levels (10,20,30), I did it this way... OTHERWISE, you could have just used max() on both the bonus level and percent. But what if your bonus levels were listed as something like
Limited 10%
Aggressive 20%
Ace 30%
You could see that a max of the level above would have "Limited", but the max % = 30 is associated with an "Ace" sales rep... Get the 30% first, then see what the label that matched that is.

Confused on this assignment, any guidance?

The Problem:
First create a table called amttopay that has three fields: rec_no, idno and amt (make amount a numeric field that can hold 3 decimal places. You are also going to use a copy of the donor table for this assignment. Take in a number that matches an idno on the donor table. Check the yrgoal for that record. If it is larger than 500 then double it to create a new goal and write four records on the amttopay table containing the quarterly payment number (1 through 4), the idno, and the quarterly amount to pay to achieve the new goal. If it is not larger than 500 then add 50% to the goal to make the new goal and process it by writing the four records with the same information.
I've created the table, and I understand I've gotta write PL/SQL code to accomplish this, but what I'm not understanding is how the question is worded.
"If it is larger than 500 then double it to create a new goal and write four records on the amttopay table containing the quarterly payment number (1 through 4), the idno, and the quarterly amount to pay to achieve the new goal."
What does that mean? How would I go about bringing logic into this?
Thanks so much for the help.
Assuming you are trying to actually understand the question, this is how you would do it:
Break your statement into parts:
Check the yrgoal for that record.
If it is larger than 500 then
double it to create a new goal
and write four records on the amttopay table containing the quarterly payment number (1 through 4), the idno, and the quarterly amount to pay to achieve the new goal.
If it is not larger than 500 then
add 50% to the goal to make the new goal
and process it by writing the four records with the same information.
Simplified, this gives the following:
Create new record
if yrgoal>500 then
double yrgoal
Create 4 records with idnoand the quarterly amount
else
yrgoal * 1.5
Create 4 records as before
The rest is up to you, of course …

Need MDX query for total count

Last time I had posted a question which was a bit confused. But today I got the same question from my manager to get the MDX query.
Here is the scenario:
Fact Table
Ticket No / Ticket ID
1 S
2 S
3 S
3 D
4 D
Dimension Table
Ticket ID / [Count]
S 1
D 1
My manager said they are not using dimension table that they are not using just for understanding they have mentioned that. there is no use of considering it here.
So please ignore the Dimension table data.
The Output will be like this if we do group by based on Ticket ID:
Ticket ID / [Count]
S 3
D 2
If we do so we will get the total Count is
5
But I need the total count as 4 based on Ticket No.
Need help here.
Thanks in Advance.
My educated guess is that you're starting with OLAP/MDX. It's worth taking a bit of time reading in the web about MDX, something like MDX Gentle Tutorial.
Without a dimension you can not have a cube. The minimum is one dimension and one measure in your facts.
In your case
Ticket ID -> dimension with two possible values (S,D)
Ticket No -> the measure, as Aaron pointed out. use unique count as aggregation type.
The MDX would looks like :
Select
{[Ticked ID].allmembers} on 0,
{[Measures].members on 1
from [MyCube]
We could find other ways of solving this but they would be certainly slower and more complicated.