I have a table with 3 columns.
Column A contain numbers.
Column B contain numbers.
Column C is empty.
Is there a way where I can get the sum of column A and column B into column C like in excel =sum(a1+b1)
You can do math in your SELECT statements.
SELECT price, tax, price+tax AS total
FROM orders
This will give you three columns: price and tax are direct from the row, and total is calculated on the fly when the SELECT is executed.
You could update a third column like this:
UPDATE orders
SET total=price+tax
and that would update the total column in every row, but that is unnecessary and is bad practice. You don't need to store the values when they can be calculated on the fly.
Related
I have a SQL Query i need to do, it basically looks for 2 values in a column called item_code.
The values are "FWDBAL" and "INVLATECHARGE", i need it to calculate the sum of item_price of 2 columns with the values above.
Select sum(item_price) overdueBal from invoice_items where invoiceid='9' AND item_code='CADB-FWDBAL' AND item_code='CADB-INVLATECHARGE';
That is returning NULL from the Workbench, and i have no idea how to accomplish this. Is there a way that you can look for 2 values in one column and get the sum of another column from the 2 values i need?
No single record can match both of those item_codes at the same time. Think logically, it is day OR it is night. It can't be day AND night. So you need to say one or the other in parens or u can just use an IN which is a little nicer.
Select sum(item_price) overdueBal
from invoice_items
where invoiceid='9'
AND item_code IN ( 'CADB-FWDBAL', 'CADB-INVLATECHARGE' )
I have sales data that contains Cust_ID, Invoice_Date, Invoice, Item_Number (and plenty other fields). This table has multiple records per Customer's Invoice, because each record holds different Item_Number info. Let's call this table sales_main.
I have a second table that contains only three of the fields mentioned above, namely Cust_ID, Invoice_Date, and Invoice. This table only contains one row per customer. Let's call this table sales_filter.
What I want to do is essentially use the second table as a filter on the first, so as to keep only rows from table sales_main (and all its columns) where the Cust_ID, Invoice_Date and Invoice columns in sales_filter and sales_main are an exact match.
So if sales_filter has a row that contains:
"1024", "24/02/2016", 533
and sales_main has four rows that match all three criteria, all four rows are returned in the result set. Same for any other rows in sales_main that match all three columns in any row from sales_filter exactly.
But I just cannot figure out what code to use to do this!
You need to join those two tables based on those 3 columns then it will return all the records with matched condition
select * from sales_main sm join sales_filter sf on (sm.Cust_ID=sf.Cust_ID and sm.Invoice_Date=sf.Invoice_Date and sm.Invoice=sf.Invoice)
You can select the fields whichever you want in select
I've got the following column chart:
Product names as Category groups with values for budget and revenue on each column. Now I want to create a final column (Total) which is the sum of each column. ie. one column with the total value of the budget and one with the total value of the revenue.
Can this be done directly in the graph without having to do the calculations in the dataset? It's very easy to add a total to a table but seems to be hard to add it to a chart.
No, you cannot just add a total column like you can add a total to a table and you are correct that the best approach is to modify your dataset to query.
Either perform a UNION to append a total row or utilize GROUPING SETS (if you want to get fancy) and you should get what you need.
Example UNION:
SELECT product, bedget, revenue
FROM myTable
UNION ALL
SELECT 'total', SUM(budget) as budget, SUM(revenue) as revenue
FROM myTable
I'm trying to create a new column with the daily orders (the count of OrderNumber for each day). Since I have data coming from multiple sources, I'm using SSIS. My final table should look like this:
Date | Product Number | Quantity Sold | Number of Orders (for that date)
I've tried using Aggregate, but it's not working because of the other columns. I was thinking about creating a parallel source (the same staging table), on which I would use Aggregate to find the number of daily orders, and then find a way to bring it back to the final table, but there must be an easier way?
Aggregate transform takes and outputs only columns you select. So, for your case, select Date, Product, Quantity and some column for Order Count - we will return to this later. Specify Group by for the first two columns, Sum for the third, and Count for the forth. At output you will receive four columns with desired result.
Source column for Count should represent orders and does not include columns used in the first three functions. If you need to use one of these three columns, create a copy of it with Derived Column transfer. I would not recommend using (*) (all columns) for Count, since it will count rows with Null values as well.
If I have a database of first names and annual incomes for two years as enumerated columns (I know this is poor database design but I don't have the liberty to change it at the moment). For example:
Name_2000, Name_2010, Income_2000, Income_2010
How can I construct a query to return all the unique names in both name columns in the first result column. The second result column should be the sum of all incomes for that name for 2000. The third should be the sum of all incomes for that name for 2010.
Obviously, in this example a person (record) may change names between years or not have a name in either year too.
Can I do this in a single query? The ability to filter by a certain subset of names would also be handy..
I would suggest using a sub query that unions 2 results together, one for each set of names and incomes, but each returning a dummy 0 for the income for the other year.
Something like this:-
SELECT aname, SUM(aincome2000), SUM(aincome2010)
FROM
(
SELECT Name_2000 AS aname, Income_2000 AS aincome2000, 0 AS aincome2010
FROM sometable
UNION ALL
SELECT Name_2010 AS aname, 0 AS aincome2000, Income_2010 AS aincome2010
FROM sometable
) sub1
GROUP BY aname