Grouping and sum in sql - sql

I am trying to extract the following columns from a sql table called Vouchers:
Date,
VoucherID,
ValueIssued,
ValueRedeemed,
Valueexpired
That bit is straight forwards and returns the below data:
However I would like to show them
by day,
by voucherID
and then sum up the values for each individual voucherID to produce a view similar to the below:
Can anyone point me in the correct direction in regards to what sql code will do this?
I am using SSMS 2014

It's pretty straight forward
SELECT PosDate, VoucherId, SUM(ValueIssued), SUM(ValueRedeemed)
FROM Vouchers
GROUP BY PosDate, VoucherId
Note that column DateExpired isn't correct in this context. It should be either grouped by or removed entirely (as I did)

Related

Group records from a set by a common value

I have a problem with groping a set of records by a common value. A common value isn't one single value thought therefore I'm not sure how to approach it
This is a set / table:
This is what I'd need to achieve:
Is it too tricky or it could be achieved (SQL 2012). If it could please point me into a direction coz I'm hit a bit wall :D Thanks!
Use disctinct form when you get group data from specific column
SELECT DISTINCT SSC FROM Table_name;

SQL to identify duplicate columns from table having hundreds of column

I've 250+ columns in customer table. As per my process, there should be only one row per customer however I've found few customers who are having more than one entry in the table
After running distinct on entire table for that customer it still returns two rows for me. I suspect one of column may be suffixed with space / junk from source tables resulting two rows of same information.
select distinct * from ( select * from customer_table where custoemr = '123' ) a;
Above query returns two rows. If you see with naked eye to results there is not difference in any of column.
I can identify which column is causing duplicates if I run query every time for each column with distinct but thinking that would be very manual task for 250+ columns.
This sounds like very dumb question but kind of stuck here. Please suggest if you have any better way to identify this, thank you.
Solving this one-time issue with sql is too much effort. Simply copy-paste to excel, transpose data into columns and use some simple function like "if a==b then 1 else 0".

SQL to Spotfire query filtering issue with multiple tables

I am trying to calculate hours flowing in and out of a cost center. When the cost center lends out an employee for an hour it's +1 and when they borrow an employee for an hour it's -1.
Right now I'm using a query that says
select
columns
from dbo.table
where EmployeeCostCenter <> ProjectCostCenter
So when ProjectCostCenter = ID_CostCenter it returns +HoursQuantity.
Then I update ID_CostCenter = EmployeeCostCenter then where ID_CostCenter = EmployeeCostCenter to take -HoursQuantity.
That works fine. The problem is when I import it to Spotfire I can't filter on the main table even after I added the table relations. Can anyone explain why?
I can upload the actual code if needed, but I use 4 queries and a couple of them are quite lengthy. The main table, a temp table to calculate incoming hours, and a temp table to calculate outgoing hours are the only ones involved in this problem I think.
(moved to answer to avoid lengthy discussion)
Essentially, data relations are used to populate filtering / marking between different data-sets. Just like in RDBMS, the relation is what Spotfire uses as the link between dataset. Essentially it's the same as the column or columns you join on. Thus, any column that you wish to filter in TableA and have the result set limited in TableB (or visa versa) must be a relation.
Column matches aren't related columns, but are associated for aggregations, category axis, etc within each visualization. So if TableA has "amount" and TableB has "amount debit" and you wanted to use both of these in an expression, say Sum([TableA].[amount],[TableB].[amount debit]), they would need to be matched in order to not produce erroneous results.
Lastly, once you set up your relations, you should check your filter panel to set up how you want the filtering to work. You can have the rows included, excluded, or ignored all together. Here is a link explaining that.

How is it possible to see a column name from a different table within a subquery from different table?

I was practicing a subqueries in sql and all of a sudden i jumped into an unsual query which i never thought of could happen.
The question of my query is....
Write a query to display the average rate of Australian dollar,where the currency rate date is July 1 2005??
And the query was...
USE AdventureWorks2012
SELECT AverageRate FROM Sales.CurrencyRate
WHERE ToCurrencyCode='AUD' AND CurrencyRateDate IN
(SELECT CurrencyRateDate FROM Sales.Currency
WHERE CurrencyRateDate='2005-07-01')
So,my question is how is it possible to get the column name "CurrencyRateDate" in the sub query when it is actually from the table "CurrencyRate"??
I know my query is not in the correct format as it should be.
I'm extremely sorry if my title doesn't make sense.If you guys can give any better please change it..
Thanks
AND CurrencyRateDate IN
(SELECT CurrencyRateDate FROM Sales.Currency
WHERE CurrencyRateDate='2005-07-01')
All the CurrencyRateDate references here point to the column from the outer query.
So for each row in the outer query, you are getting a list consisting of only that row's CurrencyRateDate, repeated once for every row in the Sales.Currency table (if the CurrencyRateDate of that row is 2005-07-01, otherwise the list is empty).
Then you check whether the outer CurrencyRateDate value is in that list. Which it is, if and only if it's equal to 2005-07-01 (assuming there is at least one row in Sales.Currency).
So your query is equivalent to:
SELECT * FROM Sales.CurrencyRate
WHERE ToCurrencyCode='AUD' AND CurrencyRateDate='2005-07-01'

How to get a column with sum

I have written SQL query which brings data from multiple tables and displays the same in this format..
I want the Sum of Total for every person and display in a new column, like the below.
Can you point some examples doing same kind of stuff please..
If your RDBMS supports windowed aggregates you can add
,SUM(Total) OVER (PARTITION BY Name/*Or PersonId if not unique*/) AS All_Total
to your SELECT list
look for "sum" and "group by". But note, that grouping by name will invalidate the startDate and endDate.
edit: ah, mssql - nvm ;)