EX/
CLIENTS INCOME DETAILS ARE AS FOLLOWS,
- NET INCOME FROM WOOD SUPPLYING BUSINESS- RS 60,000/-
- INCOME FROM HIRING THE VEHICLES- RS 20,000/-
- TOTAL INCOME- RS 80,000/-
I only need to separate Total income only. final output should be 80000
how can i get this number using sql in sql server 2016?
Please try this.
SELECT CustomerName, (NetIncome + VehicleIncome) AS 'TotalIncome'
FROM Client_Income
GROUP BY CustomerName
ORDER BY CustomerName
Assuming that you have a table named ClientIncome with three rows named Customer, NetIncome, and VehicleIncome, the following query should do what you want:
SELECT Customer, NetIncome + VehicleIncome
FROM ClientIncome
It'll return a table with two rows, the first being the Customer and the second being the sum of the two incomes.
EDIT: Breaking number out of string
If you're using SQL Server 2016, and the column with the information string is called 'info', you can try this (I don't have access to a sql server so I can't test it myself):
SELECT Cast( Substring(info,
Charindex('RS', info, 3) + 3
Len(info) - Charindex('RS', info, 3) - 2)
As Int )
FROM ClientInfo
DISCLAIMER
Forgive any weirdness or wrong information as I did all of the research for this from my phone, so it may not be as thorough as I would like it to be. Take this answer with a pinch of salt.
SELECT REPLACE(REPLACE('80,000/-','/-',''),',','')
Related
I have a query I'm working on in Microsoft SQL Server Management Studio and I'm not sure how to accomplish something.
Here's the current query:
SELECT DISTICNT
PRONOTES.CPK,
REPLACE(PRONOTES.SUBJECT, ',','') AS SUBJECT,
PRONOTES.CREATOR,
PRONOTES.DATE_CREATED
FROM
PRONOTES
WHERE
DATE_CREATED BETWEEN '2020-01-01' AND '2020-01-31'
My issue is that the software creates a SUBJECT that includes a prescription number when an order is discontinued. So I get results in the SUBJECT column that look like this:
Discontinued RX #2341241341
Discontinued RX #23455859900
All other possible SUBJECTS are locked because users have to select them from a dropdown, it's just this instance that causes that unique value. I'm trying to measure productivity of different users by how many notes they create and what types of notes they create.
I'd like the results to just show "Discontinued RX" instead of including the number, so that when this gets shipped off to excel and a pivot table is created there won't be a million lines because of the uniqueness of that prescription number.
I can't do it with a simple:
LEFT(REPLACE(PRONOTES.SUBJECT, ',', ''), 15)
because then I'll lose too much data from other subjects, so I was wondering how to do this with a case when or if there's some other better way. I thought maybe modification so that only subjects that start with the words "Discontinued Rx" get chopped off.
Right now it generates this:
But I'd like this:
You can use a CASE expression on SUBJECT so that when it starts with Discontinued RX that is all you show:
SELECT DISTINCT
PRONOTES.CPK,
CASE WHEN LEFT(PRONOTES.SUBJECT, 15) = 'Discontinued RX' THEN 'Discontinued RX'
ELSE REPLACE(PRONOTES.SUBJECT, ',','')
END AS SUBJECT,
PRONOTES.CREATOR,
PRONOTES.DATE_CREATED
FROM PRONOTES
WHERE DATE_CREATED BETWEEN '2020-01-01' AND '2020-01-31'
The following is a simplistic pattern that you can then adjust into a CASE statement in your SELECT statement as required.
DECLARE #test varchar(100) = 'Discontinued RX #2341241341 Discontinued RX #23455859900'
SELECT CASE WHEN PATINDEX('Discontinued RX #%', #test) > 0 THEN 'Replace' ELSE 'Keep' END
This an MS Access related question.
I get the Collateral divided 50 times because I have 50 rows in my ExchangeRates table... however the SELECT statement is supposed to only extract the value associated to CurrencyCode="EUR". How can I change the below statement to have the division being applied once only?
SELECT tbl_A.Security, tbl_A.Typ, Sum(([Collateral]/(SELECT tblExchangeRates.RateToUSD
FROM tblExchangeRates
WHERE (((tblExchangeRates.CurrencyCode)="EUR"))))) AS CollateralUSD
FROM tbl_A, tblExchangeRates
GROUP BY tbl_A.Security, tbl_A.Typ
HAVING (((tbl_A.Typ)="PR"));
It looks like this is what I was willing to get, just an Alias. SQL gurus, you are welcome to review.
SELECT tbl_A.Security, Sum(([Collateral]/[RateToUSD])) AS CollateralUSD
FROM tbl_A, (SELECT RateToUSD
FROM tblExchangeRates
WHERE CurrencyCode = 'EUR') AS MyAliasQ
GROUP BY tbl_A.Security
HAVING (((tbl_A.Typ)="PR"));
I dont know if its the right way to multiply that way, I want to multiply the avg_galoon that is sold weekly by the price of it which is 20
SELECT avg_galoon,
avg_galoon * 20 + NVL(total income , 0)
FROM customer;
total income isn't a column name from the customer table.
If you disagree and can see that column name (via methods mentioned in the comments already) then you will need to enclose that column name in the appropriate syntax for the database engine you are using i.e. [total income] for Microsofts SQL Server, `total income` for MySQL etc.
In future avoid the use of spaces in column names.
I have a question in my VB.NET POS Development and could't find a solution myself. I'm currently using VS2010 and MS Access as my database.
I have two database table as shown below:
SalesReceipt(#Receipt_ID, Sales_Date, Receipt_Number, Customer_ID, Sales_BDiscount, Sales_ADiscount, Sales_Payment)
Customer(#Customer_ID, Customer_Name, Customer_Contact, Customer_Debt)
NOTE : BDiscount = Before Discount / ADiscount = After Discount
In my system, one customer can have many SalesReceipt. Now my problem is how can I update the correct Customer_Debt for customer? My logic is update the respective customer's Customer_Debt by looping every row in SalesReceipt and calculate the debt by doing something like :
totalDebt = totalDebt + (Sales_Payment - Sales_ADiscount)
But I not sure how can I make sure it only loop and calculate the result for the selected customer only. How can I achieve this correctly in .NET?
If you want to calculate totalDebt per customer you can use query
SELECT Customer_ID, sum(Sales_Payment - Sales_ADiscount) as totalDebt FROM SalesReceipt
GROUP BY Customer_ID
Result contains totalDebts aggregated by Customer_ID and can be used to update Customer (all the looping and calculating is done by a database engine).
The query can also update be more complex to do even update for you.
Couldn't you just write a query in your Access db that performs your calculation (Sales_Payment - Sales_ADiscount) on your SalesReceipt table grouped by CustomerID?
I'm trying to sort some data by sales person initials, and the sales rep field is 3 chars long, and is Firstname, Lastname and Account type. So, Bob Smith would be BS* and I just need to sort by the first two characters.
How can I pull all data for a certain rep, where the first two characters of the field equals BS?
In some databases you can actually do
select * from SalesRep order by substring(SalesRepID, 1, 2)
Othere require you to
select *, Substring(SalesRepID, 1, 2) as foo from SalesRep order by foo
And in still others, you can't do it at all (but will have to sort your output in program code after you get it from the database).
Addition: If you actually want just the data for one sales rep, do as the others suggest. Otherwise, either you want to sort by the thing or maybe group by the thing.
What about this
SELECT * FROM SalesTable WHERE SalesRepField LIKE 'BS_'
I hope that you never end up with two sales reps who happen to have the same initials.
Also, sorting and filtering are two completely different things. You talk about sorting in the question title and first paragraph, but your question is about filtering. Since you can just ORDER BY on the field and it will use the first two characters anyway, I'll give you an answer for the filtering part.
You don't mention your RDBMS, but this will work in any product:
SELECT
my_columns
FROM
My_Table
WHERE
sales_rep LIKE 'BS%'
If you're using a variable/parameter then:
SELECT
my_columns
FROM
My_Table
WHERE
sales_rep LIKE #my_param + '%'
You can also use:
LEFT(sales_rep, 2) = 'BS'
I would stay away from:
SUBSTRING(sales_rep, 1, 2) = 'BS'
Depending on your SQL engine, it might not be smart enough to realize that it can use an index on the last one.
You haven't said what DBMS you are using. The following would work in Oracle, and something like them in most other DBMSs
1) where sales_rep like 'BS%'
2) where substr(sales_rep,1,2) = 'BS'
SELECT * FROM SalesRep
WHERE SUBSTRING(SalesRepID, 1, 2) = 'BS'
You didn't say what database you were using, this works in MS SQL Server.