SUM of 2 Columns using Crystal Report in VB.NET - sql-server

My table name is "accounting"
it has two columns named "Debit" and "Credit"
Is it possible to have the sum of these two columns?
example:
INPUT:
Debit Credit
200 + -100
300 + -300
500 + -400
-------- ----------
1000 -800
I want it to appear like this in my Crystal Report:
Debit Credit TOTAL
200 -100 100
300 -300 0
500 -400 100
-------- ---------- ---------
1000 -800 200
I dont know what FORMULA I should use in Crystal Report

it is good if you this done in sqlserver as add a column with summary like
select
Debit, Credit, Debit+Credit as Total
from yourtable
If you wish in crystal report side, then also it is possible.
Add one variable and write this as below and put near this fields in detail section.
{#yourtablename.DebitField} + {#yourtablename.creditField} //I forgot the syntax, please change aproopriate.
Adding Sum of fields of two tables giving strange result in Crystal Report
http://vbcity.com/forums/t/154231.aspx

Related

SQL Server : find Instance of values that deviate

Scenario: I have bills on various accounts, stored in a SQL Server database, over a series of months.
i.e.
AccountNum BillNum BillMonth BillTotal
---------------------------------------------------
123456 123456-1 201601 $100000.00
123456 123456-2 201602 $100000.00
123456 123456-3 201603 $100000.00
123456 123456-4 201604 $130000.00
456789 456789-1 201601 $250000.00
456789 456789-2 201602 $250000.00
456789 456789-3 201603 $250000.00
456789 456789-4 201604 $200000.00
I am trying to write a query that will show outliers/deviations that differ by > xx% for a given time period. So in the above dataset, row 4 and 8 differ substantially from what I would consider the "normal" amount of $100,000 p/month, potentially indicating an issue.
I need a way to average the bills by account (as each account has a different bill/charges etc) and find where the deviation is more than say 10% for a given month.
I have tried using STDEV but can't quite work it out.
You can try using the following query:
SELECT AccountNum, BillNum, BillMonth, BillTotal,
CASE
WHEN ABS(BillTotal - AVG(BillTotal) OVER (PARTITION BY AccountNum)) >
STDEV(BillTotal) OVER (PARTITION BY AccountNum)
THEN 1
ELSE 0
END AS deviates
FROM mytable
The above query returns a 1 for those records having a BillTotal amount that deviates more than the standard deviation from the average value of the field for the specific AccountNum.

Multiple updates in SQL Server

I have 500 records in Excel and it contains some columns which is same as the columns in SQL DB Table. I have old column value
as well as new column value in excel.
Example
Excel
Name Age OldEmployeeID NewEmployeeID
x 1 100 200
y 2 101 201
z 3 102 202
SqlTable
EmployeeTable
Name Age Department City EmployeeID
x 1 HR x 100
a 4 HR x 103
y 2 Admin x 101
b 5 Finance x 104
c 3 IT x 102
I want to update EmployeeID column in SQL Table as NewEmployeeID which is there in excel.
Can anyone suggest how to write a sql query to update the sql table.
Assuming the column is not an ID Column or having some other constraint you can:
Option 1:
Assuming your table is similar to the screenshot you have a concatenated column with this formula and drag it down and copy it directly into SQL.
=CONCATENATE("UPDATE dbo.EmployeeTable SET EmployeeID = ",D2," WHERE EmployeeID = ",C2,";")
Option 2:
You can import the Excel file into SQL and use a statement like the one below. However, be cautious if any of the old EmployeeIDs overlap the new EmployeeIDs. For Example if Jim has ID 100 and his new one is 500 and Jane's new ID is 100 and if you accidentally run it a second time, the Jane will also get an ID of 500.
UPDATE EmployeeTable SET EmployeeID = Excel.NewEmployeeID
FROM
EmployeeTable
JOIN ExcelTable
ON Excel.OldEmployeeID = EmployeeTable.EmployeeID
;

RANKX not working when data summarized in power pivot table

I am trying to rank records in power pivot table using DAX as below in MSSQL analysis service tabular model.
Example details:
I have a shop sales detail in table.
e.g.
ShopNo date sales
-----------------
1 2014-11-09 120
1 2014-11-09 130
2 2014-11-10 130
2 2014-11-10 135
In pivot table data is analyzed month and year wise.
I want to see result like
ShopNo sales rank
-----------------
2 265 1
3 250 2
Any solution is there to display statewise population automatically.
Thanks
You should be able to achieve the ranking quite easily with PowerPivot using this formula:
RankShop:=RANKX(ALL(SalesTable[ShopNo]), [Sum of sales],,,Dense)
With SalesTable being your shops sales table. If you then create a pivot table - drag ShopNo onto Rows and add new Measure (Excel 2010, in 2013 it's Calculated Field). The resulting table could then look like this:
To find out more about RANK function, I suggest this article.
In order to hide the rank value in Grand Total row, add a simple condition that puts blank values in case of grandtotals:
=IF(HASONEVALUE(SalesTable[ShopNo]), [RankShop], BLANK())
Hope this helps.

Crystal Report do not do the sum on Database "Cache"

I am using Crystal report to do a sum over 3 columns. The table structure looks like:
table #test (Country VARCHAR(10), Name VARCHAR(10), Weight VARCHAR(10), Qty INT)
I wrote a query in the crystal command pane when I do the connection:
SELECT Country, SUM(Qty) As Qty, Name, Weight FROM #test GROUP BY Country, Name, Weight
I should get something like:
CANADA 2 John 200
US 1 John 160
US 2 Mike 180
US 6 Sam 90
However, the crystal report does not sum the field, instead it pulls every single row, and the result looks like I write the query:
SELECT Country, Qty, Name, Weight FROM #test
CANADA 1 John 200
CANADA 1 John 200
US 1 John 160
US 2 Mike 180
US 3 Sam 90
US 3 Sam 90
By the way, the backend database is called "Cache". It might be due to there are some hidden characters, but I cannot see them. I have used replace (char(10)), replace (char(13) and trim to try to clean.
I also try to pull the table column directly without writing the query, but I do not know how to sum three columns (Country, Name and Weight). I only know how to sum one column. By the way, the request do not want the details, only the sum over these three columns;
First group by country.
Create one more group by quantity
Create one more group by name
Place weight in details and take the sum for all 3 groups if you need or only particular group
Supress tje details.

SQL SERVER Combining two columns as one column if a conditions happens

i have a situation and couln't figure out for this time.Here's the question.
I have a table for accounting. Transactions are stored in this table.
Table Accounting
Plus Minus Flag
100 0 1
150 0 1
0 230 2
0 200 2
I want to combine these two columns as one column. It would be something like that.
New_Column
100
150
-230
-200
SELECT Plus - Minus AS New_Column
FROM Accounting
Cool answer from Ed :)
If you need to get the result based on Flag column though, here is the syntax
SELECT CASE Flag
WHEN 1 THEN Plus
WHEN 2 THEN -Minus
ELSE NULL
END AS Total
FROM Accounting

Resources