Make a summary report in SQL Server - sql-server

I'm working on this database that I want it to create a summary sum(Amount) report on the all the tables that will be specified. I tried this but it didn't get what I expected.
SELECT SUM(Amount) AS Expenditure
FROM Expenditure
WHERE Amount IS NOT NULL
UNION ALL
SELECT SUM(Amount)
FROM auxiliary
WHERE Category IS NOT NULL AND Amount >0;
This only shows
and this is want to achieve:
How do I make that possible?

SELECT 'Expenditure' AS TableName, SUM(Amount) AS Amount
FROM Expenditure
WHERE Amount IS NOT NULL
UNION ALL
SELECT 'Auxiliary' AS TableName, SUM(Amount) AS Amount
FROM auxiliary
WHERE Category IS NOT NULL AND Amount >0;

Related

How to join columns with same name from Different tables

I'm trying to select different tables that have the same Column e.g(Auxiliary.Categorgy, BulkContribution.Category, Expenditure.Category) name and their sum(Amount) to get a result like this.
Is there any code for that?
I think you're looking for something like this
with tables_cte(Categorgy, Amount) as (
select Categorgy, Amount from Auxiliary
union all
select Categorgy, Amount from BulkContribution
union all
select Categorgy, Amount from Expenditure)
select Categorgy, sum(Amount) Amount
from tables_cte
group by Categorgy
order by 1;

using Row_number() and Partition, then ordering desc, and selecting top result in DB2

i have a db2 linked server i'm running a query on through SQL Server.
select *
from openquery (DO,'
select distinct HOUSE_NUM, NAME, DOB, AGE, row_number()
over(partition by DOB) rownum
from schema.INFO
where HOUSE_NUM = ''332''
group by HOUSE_NUM, NAME, DOB, AGE
order by NAME, rownum desc
limit 1
with ur');
the table has historical records, so there is a row for each age of the individual. i want to select the highest numbered row for each partition because that will give me their current age, however when i put limit 1 to select the top result i only get 1 row which ignores all the other people. the problem is there are multiple people living in a house and i need all of their ages, not just one of them. how do i select the top result of each partition in db2?
Before applying limit
After applying limit, i need the other names too
A Db2 query would look like this - rownumber cannot be referred to in the same query part this is why I used a CTE
with temp as (
select distinct HOUSE_NUM, NAME, DOB, AGE
, row_number() over(partition by HOUSE_NUM, NAME, DOB order by age desc) as rownum
from schema.INFO
where HOUSE_NUM = '332'
)
select *
from temp
where rownum = 1
Hope this helps - due to the limited information about the data it is only a best guess

SQL Server: Apply different multipliers by date

I have a table that records sales and I want a report on how much tax was paid on each item. The basic rule is all sales before April 1st had a .52% tax, all sales after that date will have a 0.5% tax.
I'm trying to get this using the following query
select ItemCode,
sum(Quantity) QuantitySold,
WhsCode as Store,
case when (docdate < '20170401') then sum(Quantity * .0052) else sum(Quantity * .005) end as Tax
from SalesTable
group by whscode,
itemcode
However I get a
DocDate' is invalid in the select list because it is not contained in
either an aggregate function or the GROUP BY clause.
error if I run it as is. Adding DocDate to the group by clause creates a mess of duplicates (On one example I went from 185 rows to 1508 rows).
Does SQL simply not allow you to use WHEN statements whose conditional is not included in the Group clause or should I be using something else?
Error is because you are using docdate directly without adding it to the group by clause.
I think this is what you want:
select ItemCode,
sum(Quantity) QuantitySold,
WhsCode as Store,
sum(price * case when docdate < '20170401' then 0.0052 else 0.005 end) as Tax
from SalesTable
group by whscode,
itemcode

Get latest row and sum of a column in it togather

In my table I have three columns amount,date,memberID. Now I want to get Latest amount inserted in to the table and the sum of whole amount inserted so far.
My Query was like this
SELECT amount , SUM(amount) as TotalAmount FROM [Transactions]
WHERE memberid = 1629 Order By Date DESC
But this throws an error like this
Msg 8120, Level 16, State 1, Line 1
Column 'Transactions.amount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone point out what I am doing wrong here?
SELECT amount AS LatestAmount,
(SELECT SUM(amount) FROM [Transactions]) AS TotalAmount
FROM [Transactions]
WHERE date = (SELECT MAX(date) FROM [Transactions])
Note that in the event of a tie for more than one latest amount, the above query would produce one record for each tying transaction. If you want only one result, and you are using SQL Server 2008 or later, you can use TOP(1) to limit to a single result:
SELECT TOP(1) amount AS LatestAmount,
(SELECT SUM(amount) FROM [Transactions]) AS TotalAmount
FROM [Transactions]
WHERE date = (SELECT MAX(date) FROM [Transactions])
Just Add Group by clause
SELECT amount , SUM(amount) as TotalAmount FROM [Transactions]
GROUP BY amount WHERE memberid = 1629 Order By Date DESC

Selecting Data from multiple tables using IN and Subquery

I have three tables in SQL Server:
Pay
Allowances
Deductions
All of them have following columns: empID, Amount.
I am writing a query to select amounts from all three tables using IN. Following is my query
Select sum(P.Amount), sum(A.Amount), sum(D.Amount)
from Pay P, Allowances A, Deductions D
where P.empID=A.empID=D.empID IN (Select EmpId from Employees)
Basically I want to get Pay, Allowance and Deductions of each employee one by one. But I cannot get the query correct.
It looks like you need an UNION ALL query:
SELECT empID, 'Pay' AS fromtable, Amount
FROM Pay
WHERE empID IN (1200,1201)
UNION ALL
SELECT empID, 'Allowances' AS fromtable, Amount
FROM Allowances
WHERE empID IN (1200,1201)
UNION ALL
SELECT empID, 'Deductions' AS fromtable, Amount
FROM Deductions
WHERE empID IN (1200,1201)

Resources