How to get a column with sum - sql

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 ;)

Related

Sorting rows in cross query Access SQL

I have a cross query in Access with an SQL:
TRANSFORM Tab1.Income AS Income
SELECT Tab1.Month
FROM Tab1
GROUP BY Tab1.Month
PIVOT Tab1.Group;
Months are string, not a number, and are in alphabetical order. I want to sort them manually.
In the normal query, I used a Switch() function which works perfectly. But in the cross query I've got an alert that ORDER BY and GROUP BY are mutually exclusive.
I would be grateful for any idea, how to sort them in query. However, if it's not possible, maybe they can be sorted in the report because this is more important.
Options:
calculate month number field to use as primary group criteria, include it along with month name as Row Headers and if this is a multi-year dataset perhaps should also include year as a Row Header - hopefully there is a full date field available to extract date parts from
report design can use expression to dictate sort order
Month is a reserved word and advise not to use reserved words as object names.

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;

Grouping and sum in 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)

SQL Select Query - Removing Duplicates/Misspelled Data

Pulling data from a cmdb into another repository. Problem is the cmdb data has misspelled/duplicate records (e.g., some assets have a Department Name as Marketing, or Markting, or Marketing&amp -- when they are all just in Marketing). Want to run a select query that displays all incorrectly named department records as the single, correct name. Any help on how to approach this?
You can use CASE in to display "marketing" for its wrong entries. But query can be complicated depending on variations.
Better + easier way is a global search and replace in column. Following article describes it:
http://www.codecandle.com/articles/sql/update/483-sql-update-with-global-search-and-replace.html
Cleaning duplicate rows, following article may help:
http://www.codecandle.com/articles/sql/windowing/503-deleting-duplicate-rows-using-windowing.html
I'm sure this is passed but http://openrefine.org/ would probably help you clean the messy data.
you can use the SELECT DISTINCT statement is used to return only distinct (different) values.
you should use distinct keyword before coloumn names in select statement.
e.g: select distinct name (Coloumn name)
from table name;

SQL Latest Date by circuit ID

I would like help with this sql. I need to find the latest date for each circuit ID (field name-strip_ec_circuit_id) based on a created date(field name-create_bill_date). I need to only find the latest date, while the other ones can be deleted. Can you help me do this?
This is a basic group by query that should work in any database:
select strip_ec_circuit_id, max(create_bill_date) as lastDate
from t
group by strip_ec_circuit_id
I'm not sure what you mean by delete all the others. Do you actually want to delete the rows from the table that are not the max?