Selecting Data from multiple tables using IN and Subquery - sql-server

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)

Related

Make a summary report in 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;

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

How can I aggregate data based on sub query results

I have a sql query like this:
SELECT (SUM(DURATIONSECS) / 3600.00) AS LaborHrs,
(SELECT LABORLEV2 t2 FROM LABOR WHERE LABORID = t1.LABORID) As Dept
FROM Table1 t1
WHERE DTM BETWEEN 'datetime1' AND 'datetime2' AND
(SELECT LABORLEV1 FROM LABOR WHERE LABORID = t1.LABORID) = '100'
AND PAYCODEID='1'
Group BY LABORID
This gives me the correct data like this:
LaborHrs Dept
90.5000000 office
1033.2500000 retail
522.2500000 retail
217.5000000 misc
1145.5000000 misc
40.0000000 retail
However I need the results to be aggregated if the dept name is the same for instance I need the sum of all labor hours of "retail" and "misc" to be in the same column. I have tried grouping by Dept, but it did not recognize that column name.
I rewrote your query based on what I understand of your question, but I'm pretty suspicious of this part:
DTM BETWEEN 'datetime1' AND 'datetime2'
I assume that it is a datetime variable or number that you would substitute in.
If that is the case, I guess here might be the answer.
SELECT (SUM(DURATIONSECS) / 3600.00) AS LaborHrs,
, l1.LABORLEV2 AS Dept
FROM Table1 t1
JOIN LABOR l1 ON l1 ON l1.LABORID=t1.LABORID
WHERE DTM BETWEEN 'datetime1' AND 'datetime2' AND l1.LABORLEV1='100'
AND t1.PAYCODEID='1'
Group BY t1.LABORID, l1.LABORLEV2
If you want to Group some column, the column can't be a subselect, so you will have to rewrite the query using JOINS.
By what I understant of your query, you can join the tables Tabl1 and LABOR and move your conditions to a single WHERE statement, something like this:
SELECT
l.LABORLEV2 AS Dept,
(SUM(t1.DURATIONSECS) / 3600.00) AS LaborHrs
FROM Table1 t
INNER JOIN LABOR l ON t.LABORID=l.LABORID
WHERE
l.LABORLEV1='100' AND
t1.PAYCODEID='1' AND
t1.DTM BETWEEN 'datetime1' AND 'datetime2'
GROUP BY l.LABORLEV2

Resources