SQL combining multiple results to one table with Sums - sql

I have the following stored procedure working. It sums up records in a given year (2013) on a specific TIN. The TINS have a Taxpayer name identified with the TIN along with the results.
The columns render out as TIN, TaxpayerName, Total.
This is an accounting based application.
What I would like to do is add a 4th column that will show the sum of another year, so these can be compared to later. The year will always be the preceeding year (2012 in this example).
There may or may not be any entires for the previous year, so I have to give it a 0 or null for the previous year. I am struggling to figure out how to combine them together.
Somehow I am going to have to match each TIN and get the sum of the previous year if it exists.
The portion of this query will look something like this: Trans.Main_AbstractNumber =136 and SUBSTRING(Trans.Main_TaxPeriod,1,4) = '2012', Using SUM(Main_AbstractAmount) as PreviousTotal or something like that.
Does anyone have any idea how I should approach this?
Thank you
Select Trans.Main_TIN, C.TaxpayerName, SUM(Main_AbstractAmount) as Total
from qetl.RECORDS Trans join qetl.TAXPAYERS C on Trans.Main_TIN=C.TIN
where Trans.Main_AbstractNumber =136 and SUBSTRING(Trans.Main_TaxPeriod,1,4) = '2013'
GROUP BY Trans.Main_TIN,C.TaxpayerName

You want to use conditional summation:
Select Trans.Main_TIN, C.TaxpayerName,
SUM(case when SUBSTRING(Trans.Main_TaxPeriod,1,4) = '2013' then Main_AbstractAmount end) as Total2013,
SUM(case when SUBSTRING(Trans.Main_TaxPeriod,1,4) = '2012' then Main_AbstractAmount end) as Total2012
from qetl.RECORDS Trans join
qetl.TAXPAYERS C
on Trans.Main_TIN=C.TIN
where Trans.Main_AbstractNumber = 136
GROUP BY Trans.Main_TIN, C.TaxpayerName ;

Related

SQL multiple constrained counts in query

I am trying to get with 1 query multiple count results where each one is a subset of the previous one.
So my table would be called Recipe and has these columns:
recipe_num(Primary_key) decimal,
recipe_added date,
is_featured bit,
liked decimal
And what I want is to make a query that will return the amount of likes grouped by day for any particular month with
total recipes as total_recipes,
total recipes that were featured as featured_recipes,
total number of recipes that were featured and had more than 100 likes liked_recipes
So as you can see each they are all counts with each being a subset of the previous one.
Ideally I don't want to run separate select count's where that query the whole table but rather get from the previous one.
I am not very good at using count with Where, Having, etc... and not exactly sure how to do it, so far I have the following which I managed via digging around here.
select
recipe_added,
count(*) total_recipes,
count(case is_featured when 1 then 1 else null end) total_featured_recipes
from
RECIPES
group by
recipe_added
I am not exactly sure why I have to use case inside the count but I wasn't able to get it to work using WHERE, would like to know if this is possible as well.
Thanks
With a CASE expression inside COUNT() you are doing conditional aggregation and this is exactly what you need for this requirement:
select recipe_added,
count(*) total_recipes,
count(case when is_featured = 1 then 1 end) total_featured_recipes,
count(case when is_featured = 1 and liked > 100 then 1 end) liked_recipes
from Recipes
group by recipe_added
There is no need for ELSE null because the default behavior of a CASE expression is to return null when no other branch returns a value.
If you want results for a specific month, say October 2020, you can add a WHERE clause before the GROUP BY:
where format(recipe_added, 'yyyyMM') = '202010'
This will work for SQL Server.
If you are using a different database then you can use a similar approach.

SQL months_between grouping issue

First post; go easy on me.
Relatively new to SQL (anything beyond simple queries really), but attempting to learn more complex functions in an effort to take advantage of superior server resources. My issue:
I would like to use a SUM function to aggregate cash flows across a very large variety of sources. I would like to see these cash flows along a monthly time period. Because the cash flows start at different times, I would like to season them so that they are all aligned. My current code:
select
months_between(A.reporting_date, B.start_date) as season,
sum(case when A.current_balance is null then B.original_balance
else A.current_balance end) as cashflow
from dataset1 A, dataset2 B
group by season
order by season
Now, executing the code like this generates an error message that states that A.reporting_date and B.start_date must be GROUPED or part of an AGGREGATE function.
The problem is, if I add them to the GROUP BY statement, while it generates output without error, I get cash flow sums that are essentially Cartesian crosses with all the grouped variables.
So long story short, is there any way for me to get cash flow sums grouped by only the season? If so, any ideas how to do it?
Thank you.
Most databases don't allow using column aliases defined previously, in where, group by and order by clauses.
For your query you should use months_between(A.reporting_date, B.start_date) instead of the alias season in group by and order by.
Also your query will return a cross product, as a join condition isn't specified.
select
months_between(A.reporting_date, B.start_date) as season,
sum(case when A.current_balance is null then B.original_balance
else A.current_balance end) as cashflow
from dataset1 A
JOIN dataset2 B ON --add a join condition
group by months_between(A.reporting_date, B.start_date)
order by months_between(A.reporting_date, B.start_date)

Oracle SQL Newbie needs help finding similar records

Please forgive my naivety, I’m an Oracle SQL newbie using Toad. I have a table with sales records, call it Sales. It has records of customers (by CustID) the date of a sale (SaleDate) and the item sold (by ItemID). It’s an Mview actually of other tables with final sales status in it.
I am trying to construct a query to return CustID, SaleDate, and ItemID if there is a sale on the same day for that customer for both ItemID=A and ItemID=B if between SaleDate 7/1/2013 and 7/31/2013. If this condition exists I want both records returned with the CustID, SaleDate and ItemID. I assume the two records would be on separate rows.
I’ve been researching IN, EXISTS, and sub queries but have yet to strike upon the right approach. The table has about 7 million records on it so I need something fairly efficient. Can someone point me in the right direction to achieve this? I’m learning, but I need to learn faster :)
GOT IT WOKING!
2/24/2014: Hey, I got it working and it returns the results on thesame row. One caveat to this. In my orginal example I was looking for dates when both 5P311 and 6R641 existed. In actuality I wanted all the days where 5P311 and any of the values from the RES group exists - of which 6R641 is a member. The code below achieves the results as I need them:
SELECT ItemA.CLM_SSN,
ItemA.CLM_SERV_STRT Service_Date,
ItemA.CLM_COST_CTR_NBR,
ItemA.CLM_RECV_AMT,
ItemB.CLM_COST_CTR_NBR RES_Cost_Center,
ItemB.CLM_RECV_AMT,
GroupCode,
Service
FROM DDIS.PTS_MV_CLM_STAT ItemA,
DDIS.PTS_MV_CLM_STAT ItemB,
DDIS.CST_SERV
WHERE TRUNC(ItemA.CLM_SERV_STRT) between to_date ('01-07-2013','dd-mm-yyyy') and to_date('31- 07-2013','dd-mm-yyyy')
and TRUNC(ItemA.CLM_SERV_STRT) = TRUNC(ItemB.CLM_SERV_STRT)
and TRIM(ItemA.CLM_COST_CTR_NBR) = '5P311'
and ITEMB.FK_SERV = CST_SERV.PKSERVICE
and CST_SERV.GroupCode = 'RES'
and Itema.CLM_SSN = ItemB.CLM_SSN
and ItemA.CLM_RECV_AMT <> 0
and ItemB.CLM_RECV_AMT <> 0
ORDER BY ItemA.CLM_SSN, ItemA.CLM_SERV_STRT
Try this, replace 'A' and 'B' values of course
SELECT CustID, SaleDate, ItemID
FROM Mview AS mv
WHERE EXISTS(SELECT 1 FROM Mview AS itemA WHERE itemA.ItemID = 'A'
AND TRUNC(itemA.SaleDate) = TRUN(mv.SaleDate) )
AND EXISTS(SELECT 1 FROM Mview AS itemB WHERE itemB.ItemID = 'B'
AND TRUNC(itemB.SaleDate) = TRUNC(mv.SaleDate) )
AND mv.SaleDate BETWEEN TO_DATE ('2003/01/07', 'yyyy/mm/dd')
AND TO_DATE ('2003/01/31', 'yyyy/mm/dd');
The exists combined ensures you that there is a sell that day that had those 2 items, the TRUNC in the date is to get rid of the hours and minutes of the date.
The between lets you seek the current range of dates, you have to convert it to date, since you are passing a string.
Edit:
ItemA is a alias for the table Mview inside the exists oracle: can you assign an alias to the from clause? sql understand alias without the AS, but you can put it if it makes it easier for you to read.
In the full example you posted, you are not using any alias for DDIS.PTS_MV_CLM_STAT, so, the database motor doesnt distict wich table you are refering and that's why you dont get the values you want.

Join two queries together with default values of 0 if empty in Access

I have seen some close answers and I have been trying to adapt them to Access 2013, but I can't seem to get it to work. I have two queries:
First query returns
original_staff_data
Month
Year
staff_uid
staff_abbrev
employee_name
staff_salary
It pulls this from tables staff, and salary_by_month and employee_name and number_of_days_at_spec_building (this records where they check in when they work)
transaction_data_by_staff.total
Month
Year
staff_uid
total_revenue
totat_profit
this also pulls information from staff, but sums up over multiple dates in a transaction table creating a cumulative value for each staff_uid so I can't combine the two queries directly.
My problem is I want to create a query that brings results from both. However, not all staff members in Q1 will be in Q2 every day/week/month (vacations, etc) and since I want to ultimately create a final results:
Final_Result
Month
Year
staff_uid
staff_abbrev
employee_name
staff_salary
total_revenue
total_profit
The SQL:
SELECT
original_staff_data.*
, transaction_data_by_staff.total_rev
, transaction_data_by_staff.total_profit
FROM transaction_data_by_staff
RIGHT JOIN original_staff_data
ON (
transaction_data_by_staff.year = original_staff_data.year
AND transaction_data_by_staff.month = original_staff_data.month
) WHERE transaction_data_by_staff.[staff_uid] = [original_staff_data].[staff_uid];
I would like it if there is no revenue or profit that month from that employee, it makes those values 0. I have tried join (specifically RIGHT join with Q1 as the RIGHT join) and it doesn't seem to work, I still only get the subset. There are originally in the original_staff_data query 750 entries so therefore there should be in the final query 750 entries, I am only getting 252, which is the total in transaction_data_by_staff. Any clue on how the ACCESS 2013 SQL should look?
Thanks
Jon
Move the link by stuff_uid to the ON clause, like this:
SELECT original_staff_data.*, transaction_data_by_staff.total_rev, transaction_data_by_staff.total_profit
FROM transaction_data_by_staff RIGHT JOIN original_staff_data ON (transaction_data_by_staff.year = original_staff_data.year) AND (transaction_data_by_staff.month = original_staff_data.month)
AND (((transaction_data_by_staff.[staff_uid])=[original_staff_data].[staff_uid]));

SQL Output Question

Edited
I am running into an error and I know what is happening but I can't see what is causing it. Below is the sql code I am using. Basically I am getting the general results I want, however I am not accurately giving the query the correct 'where' clause.
If this is of any assistance. The count is coming out as this:
Total Tier
1 High
2 Low
There are 4 records in the Enrollment table. 3 are active, and 1 is not. Only 2 of the records should be displayed. 1 for High, and 1 for low. The second Low record that is in the total was flagged as 'inactive' on 12/30/2010 and reflagged again on 1/12/2011 so it should not be in the results. I changed the initial '<=' to '=' and the results stayed the same.
I need to exclude any record from Enrollments_Status_Change that where the "active_status" was changed to 0 before the date.
SELECT COUNT(dbo.Enrollments.Customer_ID) AS Total,
dbo.Phone_Tier.Tier
FROM dbo.Phone_Tier as p
JOIN dbo.Enrollments as eON p.Phone_Model = e.Phone_Model
WHERE (e.Customer_ID NOT IN
(Select Customer_ID
From dbo.Enrollment_Status_Change as Status
Where (Change_Date >'12/31/2010')))
GROUP BY dbo.Phone_Tier.Tier
Thanks for any assistance and I apologize for any confusion. This is my first time here and i'm trying to correct my etiquette on the fly.
If you don't want any of the fields from that table dbo.Enrollment_Status_Change, and you don't seem to use it in any way — why even include it in the JOINs? Just leave it out.
Plus: start using table aliases. This is very hard to read if you use the full table name in each JOIN condition and WHERE clause.
Your code should be:
SELECT
COUNT(e.Customer_ID) AS Total, p.Tier
FROM
dbo.Phone_Tier p
INNER JOIN
dbo.Enrollments e ON p.Phone_Model = e.Phone_Model
WHERE
e.Active_Status = 1
AND EXISTS (SELECT DISTINCT Customer_ID
FROM dbo.Enrollment_Status_Change AS Status
WHERE (Change_Date <= '12/31/2010'))
GROUP BY
p.Tier
Also: most likely, your EXISTS check is wrong — since you didn't post your table structures, I can only guess — but my guess would be:
AND EXISTS (SELECT * FROM dbo.Enrollment_Status_Change
WHERE Change_Date <= '12/31/2010' AND CustomerID = e.CustomerID)
Check for existence of any entries in dbo.Enrollment_Status_Change for the customer defined by e.CustomerID, with a Change_Date before that cut-off date. Right?
Assuming you want to:
exclude all customers whose latest enrollment_status_change record was since the start of 2011
but
include all customers whose latest enrollment_status_change record was earlier than the end of 2010 (why else would you have put that EXISTS clause in?)
Then this should do it:
SELECT COUNT(e.Customer_ID) AS Total,
p.Tier
FROM dbo.Phone_Tier p
JOIN dbo.Enrollments e ON p.Phone_Model = e.Phone_Model
WHERE dbo.Enrollments.Active_Status = 1
AND e.Customer_ID NOT IN (
SELECT Customer_ID
FROM dbo.Enrollment_Status_Change status
WHERE (Change_Date >= '2011-01-01')
)
GROUP BY p.Tier
Basically, the problem with your code is that joining a one-to-many table will always increase the row count. If you wanted to exclude all the records that had a matching row in the other table this would be fine -- you could just use a LEFT JOIN and then set a WHERE clause like Customer_ID IS NULL.
But because you want to exclude a subset of the enrollment_status_change table, you must use a subquery.
Your intention is not clear from the example given, but if you wanted to exclude anyone who's enrollment_status_change as before 2011, but include those who's status change was since 2011, you'd just swap the date comparator for <.
Is this any help?