SQL condition sum from two joined tables - sql

I have two tables as below:
Invoice
InvId | Amount | Name
-----------------------
1 | 50 | John
2 | 30 | Mike
3 | 20 | John
Detail
MetalType| Weight | InvId
-------------------------
Gold | 2 | 2
Silver | 4 | 3
Silver | 3 | 3
Gold | 5 | 1
I would like to have the following output, but my query will only provide the total for silver and gold for John. How can I build a query that will also include the total invoice amount for John.
Total Invoice Amount For John = 70
Total Silver Weight = 7
total Gold Weith = 5
SELECT
SUM(IFF(D.MetalType=”Gold”, D.Weight, 0)) AS TotGold,
SUM((IFF(D.MetalType=”Silver”, D.Weight, 0)) AS TotSilver
FROM Invoice I INNER JOIN Detail D ON I.InvId = D.InvId WHERE I.Name = “John”

Try this:
For Sql-Server:
SELECT
SUM(TotalAmount) AS TotalAmount,
SUM(TotGold) AS TotGold,
SUM(TotSilver) AS TotSilver
FROM(
SELECT
SUM (I.Amount) OVER (Partition by D.Invid) AS TotalAmount,
SUM(CASE WHEN D.MetalType='Gold' THEN D.Weight ELSE 0 END) AS TotGold,
SUM(CASE WHEN D.MetalType='Silver' THEN D.Weight ELSE 0 END) AS TotSilver
FROM Invoice I INNER JOIN Detail D ON I.InvId = D.InvId
WHERE I.Name = 'John'
GROUP BY D.InvId, I.Amount) n
Here is an SQL Fiddle - now it kills the duplicate detail and counts it only once.
EDITED for Access:
SELECT
n.Name,
MAX(TotalAmount),
SUM(TotGold) AS TotGold,
SUM(TotSilver) AS TotSilver
FROM(
SELECT
I.Name,
SUM(CASE WHEN D.MetalType='Gold' THEN D.Weight ELSE 0 END) AS TotGold,
SUM(CASE WHEN D.MetalType='Silver' THEN D.Weight ELSE 0 END) AS TotSilver
FROM Invoice I
INNER JOIN Detail D ON I.InvId = D.InvId
GROUP BY I.Name, D.InvId, I.Amount) n
INNER JOIN (
SELECT
I.Name, SUM (I.Amount) AS TotalAmount
FROM Invoice I
GROUP BY I.Name) m ON m.Name = n.Name
GROUP BY n.Name

Try with this:
With tbl3 (Amt,Gold,Silver)
as
(
SELECT
SUM (I.Amount) OVER (Partition by D.Invid) AS TotalAmount,
SUM(CASE WHEN D.MetalType='Gold' THEN D.Weight ELSE 0 END) AS TotGold,
SUM(CASE WHEN D.MetalType='Silver' THEN D.Weight ELSE 0 END) AS TotSilver
FROM Invoice I Right JOIN Detail D ON I.InvId = D.InvId
WHERE I.Name = 'John' Group by D.InvId, I.Amount
)
Select SUM(Amt) as Total_Invoice_Amount_For_John,
SUM(Gold) as Total_Silver_Weight,
SUM(Silver) as Total_Gold_Width from tbl3
SQL Fiddle

I havent tried out the other queries already posted and they might already be suitable for what you want but here's my take on it :-
SELECT X.NAME, X.METALTYPE, X.WEIGHT, Y.TOTAL
FROM
(SELECT NAME, METALTYPE, SUM(Weight) AS WEIGHT
FROM INVOICE i
INNER JOIN DETAIL d ON i.InvId = d.InvId
GROUP BY NAME, METALTYPE) X
INNER JOIN
(SELECT SUM(AMOUNT) AS Total, NAME
FROM INVOICE
GROUP BY NAME)Y
ON X.NAME = Y.NAME
ORDER BY NAME, TOTAL, METALTYPE

select name, sum(Amount) as 'total invoice',sum(Gold) as 'Gold',sum(Silver) as Silver from(
select aa.Name,aa.Amount,
sum(case when bb.MetalType='Gold' then bb.Weight else 0 end) as 'Gold',
sum(case when bb.MetalType='Silver' then bb.Weight else 0 end) as 'Silver'
from a aa left outer join b bb on aa.InvID=bb.InvID group by aa.InvID) as c group by c.name

Related

SQL - SUM within subquery

I have the following code that looks at the SalesVol of different products and groups it by transaction_week
SELECT a.transaction_week,
SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol
FROM table 1 a
LEFT JOIN table 2 b ON b.Date = a.transaction_date
LEFT JOIN table 3 c ON c.sku = a.product
WHERE series in (62,236,501,52)
GROUP BY a.transaction_week
ORDER BY a.transaction_week
| tw | SalesVol |
| 1 | 4768 |
| 2 | 4567 |
| 3 | 4354 |
| 4 | 4678 |
I want to be able to have multiple subqueries where I change the series numbers for example.
SELECT a.transaction_week,
(SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol
FROM table 1 a
LEFT JOIN table 2 b ON b.Date = a.transaction_date
LEFT JOIN table 3 c ON c.sku = a.product
WHERE series in (62,236,501,52)) as personal care
(SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol
FROM table 1 a
LEFT JOIN table 2 b ON b.Date = a.transaction_date
LEFT JOIN table 3 c ON c.sku = a.product
WHERE series in (37,202,203,456)) as white goods
FROM table 1 a
LEFT JOIN table 2 b ON b.Date = a.transaction_date
LEFT JOIN table 3 c ON c.sku = a.product
GROUP BY a.transaction_week
ORDER BY a.transaction_week
I can't get the subqueries at work as it is giving me the overall sum value and not grouping it by transaction_week
Instead of using subqueries, add series to the condition of the CASE statements:
SELECT a.transaction_week,
sum(CASE WHEN series IN (62,236,501,52) AND record_type IN (6,37,13)
THEN quantity ELSE 0 END) as personal_care,
sum(CASE WHEN series IN (37,202,203,456) AND record_type IN (6,37,13)
THEN quantity ELSE 0 END) as white_goods
FROM table 1 a
LEFT JOIN table 2 b ON b.Date = a.transaction_date
LEFT JOIN table 3 c ON c.sku = a.product
GROUP BY a.transaction_week
ORDER BY a.transaction_week;
You just miss the a.transaction_week in you subquery. The JOIN in outer query is unneccessary.
SELECT a.transaction_week,
(
SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol
FROM table 1 a2
LEFT JOIN table 2 b ON b.Date = a2.transaction_date
LEFT JOIN table 3 c ON c.sku = a2.product
WHERE series in (62,236,501,52) AND a2.transaction_week = a.transaction_week
) as personal care,
(
SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol
FROM table 1 a 2
LEFT JOIN table 2 b ON b.Date = a2.transaction_date
LEFT JOIN table 3 c ON c.sku = a2.product
WHERE series in (37,202,203,456) AND a2.transaction_week = a.transaction_week
) as white goods
FROM table 1 a
GROUP BY a.transaction_week
ORDER BY a.transaction_week
Try this it would work fast as well as up to your requirement:
SELECT a.transaction_week ,
whitegoods.SalesVol AS 'White Goods' ,
personalcare.SalesVol1 AS 'Personal Care'
FROM table1 a
LEFT JOIN table2 b ON b.[Date] = a.transaction_date
LEFT JOIN table3 c ON c.sku = a.product
CROSS APPLY ( SELECT SUM(CASE WHEN record_type IN ( 6, 37, 13 )
THEN quantity
ELSE 0
END) AS SalesVol
FROM table1 a2
WHERE b.[Date] = a2.transaction_date
AND c.sku = a2.product
AND series IN ( 37, 202, 203, 456 )
AND a2.transaction_week = a.transaction_week
) whitegoods
CROSS APPLY ( SELECT SUM(CASE WHEN record_type IN ( 6, 37, 13 )
THEN quantity
ELSE 0
END) AS SalesVol1
FROM table1 a2
WHERE b.[Date] = a2.transaction_date
AND c.sku = a2.product
AND series IN ( 62, 236, 501, 52 )
AND a2.transaction_week = a.transaction_week
) personalcare
GROUP BY a.transaction_week
ORDER BY a.transaction_week
You should use the UNION operator. Please refer to the query below:
select a.transaction_week, SalesVol from
(SELECT a.transaction_week as transaction_week,
SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol
FROM table 1 a
LEFT JOIN table 2 b ON b.Date = a.transaction_date
LEFT JOIN table 3 c ON c.sku = a.product
WHERE series in (62,236,501,52)
UNION
SELECT a.transaction_week as transaction_week,
SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol
FROM table 1 a
LEFT JOIN table 2 b ON b.Date = a.transaction_date
LEFT JOIN table 3 c ON c.sku = a.product
WHERE series in (37,202,203,456)
) AS tbl1
GROUP BY tbl1.transaction_week
ORDER BY tbl1.transaction_week

SQL query to compare subsets of rows between each other

There is an SQL query I'm after (in SQL Server). I need to get the count of instances where company Y is more expensive than company X. How would I start to tackle this? I've looked through various examples, but I cannot find anything similiar. I see PARTITION BY could be helpful, but not sure how to start from there - any hint will be very helpful.
ReadingId | Product | Price | Company
----------------------------------------------
1 | A | 3 | X
2 | A | 4 | Y
3 | A | 5 | Z
4 | B | 11 | X
5 | B | 12 | Y
6 | B | 13 | Z
...
One method is conditional aggregation. For each product:
select product,
max(case when company = 'Y' then price end) as Yprice
max(case when company = 'X' then price end) as Xprice
from t
group by product;
For a count, you can then do:
select count(*)
from (select product,
max(case when company = 'Y' then price end) as Yprice
max(case when company = 'X' then price end) as Xprice
from t
group by product;
) p
where Yprice > Xprice;
There are other methods as well. Pivot can be used, as well as a join with aggregation:
select count(*)
from t ty join
t tx
on ty.company = 'Y' and tx.company = 'X' and ty.product = tx.product
where ty.price > tx.price;
I should point out that all these methods sort of assume that X and Y only appear once for each product. That seems reasonable given your data.
Rather straight-forward. Get product prices for companies X and Y. Join them together on product and compare prices.
It assumes that each product is listed once for a company.
WITH
CTE_X
AS
(
SELECT Product, Price
FROM T
WHERE Company = 'X'
)
,CTE_Y
AS
(
SELECT Product, Price
FROM T
WHERE Company = 'Y'
)
SELECT COUNT(*) AS cc
FROM
CTE_X
INNER JOIN CTE_Y ON CTE_Y.Product = CTE_X.Product
WHERE
CTE_Y.Price > CTE_X.Price
;
You can do this with conditional aggregation.
with xandy as (select product,
max(case when company = 'X' then price end) as xprice,
max(case when company = 'Y' then price end) as yprice
from tablename
group by product)
select count(*)
from xandy
where yprice > xprice
This query, tho not efficient, will give you the details of what you need:
Select
CompanyY.*,
CompanyX.*
FROM
(
select * from OrderDetails
where Company = 'Y'
) CompanyY
JOIN
(
select * from OrderDetails
where Company = 'X'
) CompanyX
ON CompanyX.Product = CompanyY.Product
WHERE CompanyY.Price > CompanyX.Price
Try the SQLFiddle Here
You can use:
SELECT COUNT(*)
FROM (
SELECT Product
FROM mytable
GROUP BY Product
HAVING MAX(CASE WHEN Company = 'Y' THEN Price END)
>
MAX(CASE WHEN Company = 'X' THEN Price END) ) AS t
The sub-query returns the list of products where company Y is more expensive than company X. The outer query simply counts the number of these products.
Yet another version using a window function:
SELECT COUNT(*)
FROM (
SELECT Company,
ROW_NUMBER() OVER (PARTITION BY Product
ORDER BY Price DESC) AS rn
FROM mytable
WHERE Company IN ('X', 'Y')) AS t
WHERE t.rn = 1 AND Company = 'Y'
The sub-query filters out any rows not having either 'X' or 'Y' as their Company. The outer query counts the number of rows for which Company = 'Y' has the highest price.

How to merge rows in select query result set

My result of is like this
date acc cr date acc dr
---------------------------------------------------
null null 0 13/3/12 A 1300
null null 0 13/3/12 c 1200
null null 0 13/3/12 D 1100
13/3/12 A 1000 null null 0
18/3/12 E 2000 null null 0
19/3/12 F 3000 null null 0
31/3/12 G 3000 null null 0
this result i got from following query bu joining 2 tables to get cash book
select(case when mli.voucher_type = 1 THEN tav.voucher_date end)pdate,
(case when mli.voucher_type = 1 THEN mli.description end) acc,
(case when tav.voucher_type_id = 1 then sum(tvl.amount) else 0 end) cr,
(case when mli.voucher_type = 2 THEN tav.voucher_dateend) rdate,
(case when mli.voucher_type = 2 THEN mli.description end) acc,
(case when tav.voucher_type_id = 2 then sum(tvl.amount) else 0 end) dr
from t_acc_voucher tav
join t_voucher_ledger tvl on tvl.voucher_id = tav.voucher_id
join m_ledger_index mli on mli.ledger_index_id = tvl.ledger_index_id
group by mli.description, mli.voucher_type, tav.voucher_type_id,tav.voucher_date
I want result like this
date acc cr date acc dr
---------------------------------------------------
13/3/12 A 1000 13/3/12 A 1300
18/3/12 E 2000 13/3/12 c 1200
19/3/12 F 3000 13/3/12 D 1100
31/3/12 G 3000 null null 0
can any body help me.or give some suggestion is it write way to get it or i can try with 2 diffrent query.
thanks in advance
Break your query into two separate queries, one for credit and another for debit and do a full outer join based on descending date, you can order by any column in fact. From the original query, I assumed that VOUCHER_TYPE = 1 is credits and VOUCHER_TYPE = 2 is debit.
Try this (not tested)
with CREDITS as (select TAV.VOUCHER_DATE PDATE,
MLI.DESCRIPTION ACC,
(case when TAV.VOUCHER_TYPE_ID = 1 then SUM(TVL.AMOUNT) else 0 end) CR
from t_acc_voucher tav
join t_voucher_ledger tvl
on tvl.voucher_id = tav.voucher_id
join M_LEDGER_INDEX MLI
on MLI.LEDGER_INDEX_ID = TVL.LEDGER_INDEX_ID
where MLI.VOUCHER_TYPE = 1
group by MLI.DESCRIPTION,
MLI.VOUCHER_TYPE,
TAV.VOUCHER_TYPE_ID,
TAV.VOUCHER_DATE),
debits as (select TAV.VOUCHER_DATE RDATE,
MLI.DESCRIPTION ACC,
(case when TAV.VOUCHER_TYPE_ID = 2 then SUM(TVL.AMOUNT) else 0 end) DR
from T_ACC_VOUCHER TAV
where mli.voucher_type = 2
join t_voucher_ledger tvl
on tvl.voucher_id = tav.voucher_id
join M_LEDGER_INDEX MLI
on MLI.LEDGER_INDEX_ID = TVL.LEDGER_INDEX_ID
group by MLI.DESCRIPTION,
MLI.VOUCHER_TYPE,
TAV.VOUCHER_TYPE_ID,
TAV.VOUCHER_DATE )
select T1.PDATE, T1.ACC, T1.CR, T2.RDATE, T2.ACC, T2.DR
from (select a.*, row_number() over (order by a.PDATE) RN
from credits a) T1
full outer join
select b.*, row_number() over (order by b.RDATE) RN
from debits b) T2
on (t1.rn = t2.rn);
select t1.date1, t1.acc1, t1.cr, t2.date2, t2.acc2, t2.dr
from (the table or query) t1
join (the table or query) t2 on t1.acc1=t2.acc2
where t1.acc1 is not null
we join the same query (or table) twice joinint to itself by acc.

Join multiple tables, select counts from different tables and group by one column in one query

I need to join multiple tables, select counts from different tables and group by one column in one query. This is how I would do this separately:
select c.CommunityName, SUM(case when m.ListKey = c.ListKey then 1 else 0 end) as Posts
from Community c with(NOLOCK)
join messages_ m with(NOLOCK)
on c.ListKey = m.ListKey
group by c.CommunityName
select c.CommunityName, SUM(case when b.CommunityKey = c.CommunityKey then 1 else 0 end) as Blogs
from Community c with(NOLOCK)
join Blog b with(NOLOCK)
on c.CommunityKey = b.CommunityKey
group by c.CommunityName
select c.CommunityName, SUM(case when ce.CommunityKey = c.CommunityKey then 1 else 0 end) as Events
from Community c with(NOLOCK)
join CalendarEvent ce with(NOLOCK)
on c.CommunityKey = ce.CommunityKey
where ce.StartDateTime >= GETDATE()
group by c.CommunityName
or simply
select c.CommunityName, COUNT(*)
from Community c with(NOLOCK)
join messages_ m with(NOLOCK)
on c.ListKey = m.ListKey
group by c.CommunityName
select c.CommunityName, COUNT(*)
from Community c with(NOLOCK)
join Blog b with(NOLOCK)
on c.CommunityKey = b.CommunityKey
group by c.CommunityName
select c.CommunityName, COUNT(*)
from Community c with(NOLOCK)
join CalendarEvent ce with(NOLOCK)
on c.CommunityKey = ce.CommunityKey
where ce.StartDateTime >= GETDATE()
group by c.CommunityName
There are more tables, some that require additional joins... Can someone please help?
If I understand your question correctly, you are looking for community name along with the counts such as posts, blogs, event etc..
As your queries count individually, add dummy columns in the SELECT for the other counts and then in the end UNION them and get the SUM.
SELECT CommunityName , SUM(MessageCount), SUM(BlogCount), SUM(EventCount)
FROM (
SELECT c.CommunityName CommunityName , COUNT(*) MessageCount, 0 BlogCount, 0 EventCount
FROM Community c with(NOLOCK)
JOIN messages_ m with(NOLOCK)
ON c.ListKey = m.ListKey
GROUP BY c.CommunityName
UNION
SELECT c.CommunityName, 0, COUNT(*), 0
FROM Community c with(NOLOCK)
JOIN Blog b with(NOLOCK)
ON c.CommunityKey = b.CommunityKey
GROUP BY c.CommunityName
UNION
SELECT c.CommunityName, 0, 0, COUNT(*)
FROM Community c with(NOLOCK)
JOIN CalendarEvent ce with(NOLOCK)
ON c.CommunityKey = ce.CommunityKey
WHERE ce.StartDateTime >= GETDATE()
GROUP BY c.CommunityName
) CountsTable
GROUP BY CountsTable.CommunityName
CountsTable will look like
| COMMUNITYNAME | MESSAGECOUNT | BLOGCOUNT | EVENTCOUNT |
|---------------|--------------|-----------|------------|
| Name | 10 | 0 | 0 |
| Name | 0 | 20 | 0 |
| Name | 0 | 0 | 30 |
So, you can GROUP BY name and sum up the counts to get your result
| COMMUNITYNAME | MESSAGECOUNT | BLOGCOUNT | EVENTCOUNT |
|---------------|--------------|-----------|------------|
| Name | 10 | 20 | 30 |
Have you thought about using LEFT JOIN to connect your tables? Then you can check for NULLs and sum up the non-NULL values.
SELECT
c.CommunityName,
SUM(case when m.ListKey IS NOT NULL then 1 else 0 end) as Posts,
SUM(case when b.CommunityKey IS NOT NULL then 1 else 0 end) as Blogs,
SUM(case when ce.CommunityKey IS NOT NULL then 1 else 0 end) as Events
FROM
Community c WITH(NOLOCK)
LEFT JOIN
messages_ m WITH(NOLOCK)
ON c.ListKey = m.ListKey
LEFT JOIN
Blog b WITH(NOLOCK)
ON c.CommunityKey = b.CommunityKey
LEFT JOIN
CalendarEvent ce WITH(NOLOCK)
ON c.CommunityKey = ce.CommunityKey
WHERE
ce.StartDateTime >= GETDATE()
GROUP BY
c.CommunityName

SQL Statement To Group Different Date Ranges as New Columns

SQL beginner here. Looking at a table of items in an Oracle DB and wanted to export items by year (each in a separate column), group them by a userid, and then sum a total field.
I can export them individually with date ranges like
WHERE DATE > '01-JAN-13'
AND DATE < '31-DEC-13'
My table 'CUSTOMER_ORDERS' looks like this Here is how my table looks
Customer Name | Customer ID | Date | Sale
_________________________________________
Customer 1 | CUS01 | 05-JAN-13 | 110.00
Customer 2 | CUS02 | 06-JAN-11 | 110.00
Customer 3 | CUS03 | 07-JAN-12 | 70.00
Customer 1 | CUS01 | 05-JAN-12 | 10.00
Customer 2 | CUS02 | 05-JAN-11 | 210.00
Ideally I want to export something like this
Customer Name | Customer ID | 2011 Total | 2012 Total | 2013 Total
_________________________________________
Customer 1 | CUS01 | 0 | 10 | 110
Customer 2 | CUS02 | 320 | 0 | 0
Customer 3 | CUS03 | 0 | 70 | 0
I'm sure this is super simple, I just can't figure out the right way to do it.
You can use an aggregate function with a CASE expression to PIVOT the data from rows into columns:
select
CustomerName,
CustomerID,
sum(case when to_char(dt, 'YYYY') = 2011 then Sale else 0 end) Total_2011,
sum(case when to_char(dt, 'YYYY') = 2012 then Sale else 0 end) Total_2012,
sum(case when to_char(dt, 'YYYY') = 2013 then Sale else 0 end) Total_2013
from CUSTOMER_ORDERS
group by CustomerName, CustomerID;
See SQL Fiddle with Demo.
Depending on your version of Oracle, you might be able to use the PIVOT function if you are using Oracle 11g+:
select *
from
(
select CustomerName, CustomerId,
'Total_'||to_char(dt, 'YYYY') year, sale
from CUSTOMER_ORDERS
)
pivot
(
sum(sale)
for year in ('Total_2011', 'Total_2012', 'Total_2013')
);
See SQL Fiddle with Demo
Use the power of self-joins to subset the data in the way you need. Try something like
select c.ID , c.Name , sum(c2011.Sale) , sum(c2012.Sale) , sum( c2013.Sale )
from ( select distinct c.ID , c.Name from customer_order ) c
left join customer_order c2011 on c2011.id = c.id and year(c.Date) = 2011
left join customer_order c2012 on c2012.id = c.id and year(c.Date) = 2012
left join customer_order c2013 on c2013.id = c.id and year(c.Date) = 2013
group by c.ID , c.Name
order by c.ID , c.Name
To get the desired result. Alternatively...
select c.ID , c.Name ,
sum(case when year(c.Date) = 2011 then c.Sale else 0 end) ,
sum(case when year(c.Date) = 2012 then c.Sale else 0 end) ,
sum(case when year(c.Date) = 2013 then c.Sale else 0 end)
from customer_order c
group by c.ID , c.Name
order by c.ID , c.Name