Pivot Table select all the data in my column & Extra Sum column - sql

i got a question here.
I'm trying to select all the date inside my database (1-31), instead of using 1,[2],[3]... manually way is there any easier way to select them all?
I tried using some dumb way like:
SUM(TOTAL) FOR DATE IN (*)
SUM(TOTAL) FOR DATE IN ([*])
Here is my query:
SELECT * FROM
(
SELECT BRANCH.NAME,SALES.TOTAL AS TOTAL,TIME.DATE
FROM SALES
INNER JOIN BRANCH
ON SALES.BRANCH_ID=BRANCH.BRANCH_ID
INNER JOIN TIME
ON SALES.TIME_ID=TIME.TIME_ID
WHERE TIME.MONTH='APR'
)AS TABLE1
PIVOT (
SUM(TOTAL) FOR DATE IN ([1],[2],[3],[5],[6],[7],[8],[9],[10])
) PIVOTTABLE
Also is there possible to create an extra grand total column by SQL like excel do ?
SAMPLE:

Related

How to select all columns with sum function

Assuming there is a table with 100 columns, how can I select all columns with a sum without having to type out all the columns?
For example something like this:
select *, sum(price) as sales
from table
group by *
order by date
try this
select table.* , t.sales from table
inner join (select id, sum(price) as sales from table group by id ) t
on table.id=t.id
order by date
But in general it is not recommended to use an stare in a select statement,
for example dont use * in table-valued function

SQL Server: Error when trying to use SUM in SELECT Statement

I have this sql query where I want to retrieve the sum of the amount of sales in August 2005. Whenever I run this, I receive the error "Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause". How can I fix this so it displays all columns and display the sum in my repsales.amount column?
SELECT *, SUM(repsales.amount) AS amount
FROM repsales
WHERE repsales.saledate BETWEEN '2005-08-01' AND '2005-08-31'
You can use window functions:
SELECT rs.*, SUM(rs.amount) OVER () AS total_amount
FROM repsales rs
WHERE rs.saledate BETWEEN '2005-08-01' AND '2005-08-31';
My answer is using SUM as an analytic/window function. This means that each row will be assigned a value, as contrasted to using SUM with GROUP BY, where an aggregation of rows takes place.
Using SUM(amount) OVER () defines the window to be the entire table. That is, the sum will be the total sum of the amount.
Another alternative solution is to state the column names one by one in the select and groupby statements. I.E. the example below groups by the data based department and category:
select rs.department,rs.category,sum(rs.amount) as SumAmount
from repsales rs
where rs.saledate BETWEEN '2005-08-01' AND '2005-08-31';
group by rs.department, rs.category
If you need all columns, you can save the resultset of the query above in a temp table (select ... into #temp from ...) and use it like:
select rs.*,t.SumSmount
from repsales rs
inner join #temp t on t.department=rs.department and t.category=rs.category
insert the sum of amount into a temp table with the unique value columns dept,category etc ...
Select rs.salespersonID, rs.OtherUniqueColumes, Sum(rs.amount) AS Amout
INTO #tempSales FROM repsales
WHERE repsales.saledate BETWEEN '2005-08-01' AND '2005-08-31'
Group By rs.salespersonID, rs.OtherUniqueColumes
join the temp table with the original table to get all other columns
SELECT rs.*, t.Amount
FROM repSales rs INNER JOIN #tempSales t on rs.salespersonID = t.salespersonID AND rs.OtherUniqueColumes = t.OtherUniqueColumes
DROP TABLE #tempSales

Creating a total row to display the total difference of column items in ms sql

I am trying to create a total row that takes items from a column and displays the difference in the total field as opposed to the sum. Is this even possible in SQL?
SELECT P.Part_Number, P.Units_On_Hand
FROM Part AS P
WHERE P.Part_Number IN ('AX12', 'AZ52')
This is the query I currently have to display the items I need the processing to be done on. How can I amend it to incorporate a total column to show the difference?
;with cte as
(
select part_number, SUM(units_on_hand) total
from yourtable
where part_number IN ('AX12', 'AZ52')
group by part_number
)
select t1.part_number, t2.part_number, abs(t1.total-t2.total)
from cte t1
inner join cte t2
on t1.part_number<t2.part_number

SQL Percentage Count Query By Date

I am able to calculate the percentage count on a particular date in a Microsoft Access 2007 SQL query using:
SELECT Date, Val, (Count(Val) / (SELECT Count(*) From Table HAVING Date=#7/31/2012#) as PercentVal
FROM Table
GROUP BY Date, Val
HAVING Date=#7/31/2012#
However, I would like to make this same calculation over every date using the count totals . For instance, the query:
SELECT Date, Val, Count(*) AS CountVal
FROM Table
GROUP BY Date, Val
finds the counts in every period. I would like to add an additional column with the percent counts. However, I can't seem to figure out how to calculate count percentage in every period without using the above block of text and setting up queries for each individual period.
You can subquery it like this:
SELECT A.ADate, A.Val, COUNT(A.Val) / B.DateCount
FROM Table1 AS A
INNER JOIN (
SELECT C.ADate, COUNT(*) AS DateCount
FROM Table1 C
GROUP BY C.ADate
) AS B ON A.ADate = B.ADate
GROUP BY A.ADate, A.Val, B.DateCount

combine SELECTS in ONE VIEW DISPLAY

I need to know of a way to combine multiple SELECT statements in one VIEW? I tried the UNION ALL, but it fails since I am using unique columns to aggregate the GRAND TOTAL.
I am a student this is part of a group project.
I have one table with 4 columns: account, description, short_description, and balance. The COA (chart of accounts) is an excel spreadsheet that is imported.
CREATE VIEW [account_balance_sums]
AS
SELECT SUM(balance) AS total,
SUBSTRING (Account,0,2) AS account_group
FROM COA
GROUP BY account_group
GO
SELECT * FROM [account_balance_sums]
SELECT SUM(total) AS Grand_total
FROM [account_balance_sums]
Assuming that you are trying to create a view that gives account group and total balance with a single extra row for the total across all accounts then this view should help:
CREATE VIEW [account_balance_sums] AS
SELECT SUM(balance) AS total, SUBSTRING (Account,0,2) AS account_group
FROM COA
GROUP BY account_group
UNION ALL
SELECT SUM(balance), 'Grand Total'
FROM account_group
By the way, the sub-string of the first characters of the account name suggests that you have more than one piece of data in a single column. This indicates a data that is not properly normalised, which you should probably address if you want top marks. See wikipedia on normal form
In a UNION'd statement, there must be:
The same number of columns in each SELECT statement
The data types must match at each position in the SELECT statement
Use:
SELECT *
FROM [account_balance_sums]
UNION ALL
SELECT SUM(total),
NULL AS account_group
FROM [account_balance_sums]
UNION ALL should work. basic structure like this
select a,b,c,d
from t1
union all
select a,b,c,e
from t2
so long as d and e are the same data type.
to do the sum, then you wrap this with the aggregation layer - using this structure as an inline view (among other methods)
something like:
select sum( d )
from (
select a,b,c,d
from t1
union all
select a,b,c,e
from t2
)