How to sum from 3 columns - sql-server

I'm new to Microsoft SQL Server Management Studio and I am stumped on this one homework question. I am trying to get a final total of 3 columns (Upkeep, Staging and Marketing by Housing Type) as a new column on the right (next to Marketing column). Please refer to this link for the image of what I mean.
Thanks! Hope you guys can help!!!

Perhaps I'm not understanding the question, but it seems you just need to add the three sums together and then format. Example below.
select format( (sum([Upkeep])
+ sum([Staging])
+ sum([Marketing])), 'c') as 'Total'
from Market.dbo.Data
Group by Housing

Related

Excel 2016 Complex Nested IF

I'm working with an Excel sheet with more than 2,000 rows. The image I've attached shows the problem. I've checked the questions on StackOverflow & I don't see anything addressing this situation:
I need a formula to calculate the percentage in column E (E is formatted as a percentage). I can do this by hand logically but it takes upwards of 15 minutes and the report needs to be produced daily. The list comes from SQL Server and I can't get my SQL query to handle this either, nor can I make it work in VBA for Excel.
Logic for Cell E3:
If C2='house' and C3='house' and C4='house', then '100'
If C2='house' and C3='house' and C4='prospect', then 'C3/SUM(C3:C4)'
If C2='house' and C3='prospect' and C4='house', then 'C3/SUM(C2:C3)'
If C2='house' and C3='prospect' and C4='prospect', then 'C3/SUM(C2:C3)'
If C2='prospect' and C3='prospect' and C4='prospect', then '100'
If C2='prospect' and C3='house' and C4='house', then, '100'
If C2='prospect' and C3='house' and C4='prospect', then 'C3/SUM(C3:C4)'
If C2='prospect' and C3='prospect' and C4='house', then '100'
Thank you, in advance, for any assistance!
Obviously there was a typo in your question, you meant to calculate with numbers on column D not C, i.e D3/Sum(D3:D4) etc.
You could try to simply write that logic with nested if's... But I realize that the rule, which looked like a digital logic exercise, is actually much simpler. In summary the equation should detect the flip from "House" to "Prospect", if any.
Try this formula at E3 and copy it down:
E3:
=D3/(D3+ (D2*(C2="House")*(C3="Prospect")) + (D4*(C3="House")*(C4="Prospect")))

SSAS cube and getting data with MDX for SSRS report

I'm new to OLAP cubes. Can you directed in the right direction with small example.
Let's say I have table "transactions" with 3 columns: transaction_id (int), date (datetime), amount (decimal(16,2)).
I want to create a cube and then get data with MDX query for SSRS report.
I want report to show something like:
Ok. I know i can have fact table with amount and date dimention (date->month->year).
Can you explain what to do in order to get this result (including how to write MDX query). Thanks.
Can someone explain why I get amount of full 201504 and 201606 months even if I specified exact range with days?
SELECT
[Measures].[Amount] ON COLUMNS
,[Dim_Date].[Hierarchy].[Month].MEMBERS ON ROWS
FROM
[DM]
WHERE
(
{[Dim_Date].[Date Int].&[20150414] : [Dim_Date].[Date Int].&[20160615]}
)
Something like below, change the query accordingly :)
SELECT
{ [Date].[EnglishMonthName].[EnglishMonthName]} ON COLUMNS,
{ [Date].[DateHierarchy].[Year].&[2015],
[Date].[DateHierarchy].[Year].&[2016] } ON ROWS
FROM [YourCubeName]
WHERE ([Measures].[amount])
So you want someone to show you how to create a multi-dimensional cube from scratch and report on it in a single answer...? Start here and work through the lessons

Access 2007 Combobox displaying wrong results from table

I have four ComboBoxes that are linked to SQL Tables and then they are linked to a log table.
The problem I have is that the combobox is displaying the correct options but only logging the ID value.
So say I have a combobox called Location and it has 3 locations:
Cape Town
Dallas
London
The ComboBox is showing those 3 choices in the drop down but when the choice is logged it will only return the values 1,2 or 3 and not the cities
Here is the row source:
SELECT Location.ID, Location.Location FROM Location ORDER BY Location.ID, Location.Location;
But when I tried swapping the Location and ID order around it then displays the values and then logs the cities.
Also this is only an issue since I linked the tables to SQL when they were local tables it worked fine.
I must be missing some sort of search field in the source query, can anyone help me please
Thanks
Dan
I managed to solve it, I had to take ID out of the equation completely and then add into extra location columns in my row source
It ended up like this:
SELECT Location.Location, Location.Location, Location.Location FROM Location ORDER BY Location.Location;
To be honest, I stumbled across this and it works. So that will do for me!

How to Select a record in Access if it is not a duplicate value

Ok, from the title it seems to be impossible to understand, I'll try to be as clear as possible.
Basically, I have a table, let's call it 'records'. In this table I have some products, of which I store 'id', 'codex' (which is a unique identifier for a certain product in the whole database), 'price' and 'situation'. This last one is a string which tells me wether the product has just entered the store (in that case it is set to 'IN'), or it has already been sold ('OUT' in this case).
The database was not created by us, I HAVE to work with that although it is horribly structured... The guy who originally projected the database decided to register when a product's situation passes from 'IN' to 'OUT' in the following way: instead of UPDATEing the corresponding value in the table, he used to take the row of data with 'IN' as situation, and to DUPLICATE it setting, that time, 'OUT' as situation.
Just to sum up: if a product has not been sold yet, it will have one row of dedicated data; otherwise those rows will be two, identical except for the 'situation' field.
What I need to do is: select a product if (and ONLY if) there is no duplicate for it. Basically, I can (and should) look for a 'codex', and if I my Count(codex) ends up being >1, I do not select the row.
I hope the explanation of the process is clear enough...
I tryed many alternative (no, SELECT DISTINCT is not a solution): des anyone have an idea of how to do that? Because really, none of us three could come up with a good solution!
Here is the schema for the table, I hope it is sufficiently clear, and if not do not hesitate asking for more details.
Just as a reminder: the project is in (sigh...) VB.net, the database is in Microsoft Access (mdb).
I could not find a solution on StackOverFlow, I hope this is not a duplicate question! Thanks in advance for the help.
id codex price situation
1 1 2.50 IN
2 1 2.50 OUT
3 2 3.45 IN
4 3 21.50 IN
5 2 3.45 OUT
6 4 1.50 IN
To check if I understand what your problem is... In your example table you just want to get the lines with ID 4 a 6, right?
If is that what you want, and If you want only the not sold ones try this command
SELECT
*
FROM
records
WHERE
codex
not in
(
SELECT
codex
FROM
records
WHERE
situation ='OUT'
)

SQL Server query to Crystal Reports

I am trying to move reports that currently run in SQL Server to Crystal Reports.
Essentially the statement I want to reproduce is:
SELECT DATEPART(DD,DATE), COUNT(*)
WHERE FOO = 'BAR'
GROUP BY DATEPART(DD,DATE)
Count the occurrence of records that match a criteria, grouped by date.
I have used the Selection Expert to generate a equivalence relation (to evaluate the records) and would like to use the datepart function in a group by statement. I have gotten the GROUP BY selection expert to group by date - but it is the full timestamp (SS:HH:DD:MM) not by specific day i.e. March 1 2010.
I am sure there is a way to achieve what I want but have yet to find a tutorial explaining this scenario.
Any help you could lend would be appreciated
As with any other SDK/Language, there are many ways to do this. Here is the first one that I can think of:
Get the raw data into Crystal. (Sounds like you already did this)
In Crystal, make a new formula, call it "GroupByDate". In the formula editor, enter:
datepart("yyyy-mm-dd",{mytable.mydatefield})
Go into the Group Expert. Group your report by GroupByDate.
Make a new formula, call it "AddMe". In the formula editor, enter:
iif({mytable.foo="bar",1,0)
Drag & drop your AddMe formula into the details section. Right-click on it to Insert->Summary. Set your summary location as the group footer.
Preview your report and you should see the total counts in every group footer. To simplify the appearance of the report, you can also suppress the display of the detail and grouper header sections.
Again, there are many ways to do this. You can also get creative with a Running Total function. The Crystal Formula Editor has very useful help files. Use the Functions pane to select a function, press F1, and you'll get criteria, examples, etc.

Resources