SQL Beginner : Multiple where clause from same table - sql

I'm very new to SQL and am not sure how to accomplish the following.
I have a list of meter IDs that have a years worth of Target and a years worth of Budget data assigned, however these are all held in the same table with an identifier (TargetType) to distinguish between Target(0) & Budget(1)
ID TARGETTYPE VALUE01 VALUE02 ...(etc up to VALUE12)
123 0 1001 1100
123 1 9000 9100
456 0 5000 5100
456 1 8000 8100
Desired result would be also include information from a couple of other tables want to add that in as well
My query so far can bring through one set of data :
PARAMETERS
[Utility] Text;
SELECT
Contacts.Group,
Contacts.Site,
Points.ID,
Points.Utility,
Points.MPAN1,
Target.Value_01 AS [Target JAN],
Target.Value_02 AS [Target FEB],
Target.Value_03 AS [Target MAR],
Target.Value_04 AS [Target APR],
Target.Value_05 AS [Target MAY],
Target.Value_06 AS [Target JUN],
Target.Value_07 AS [Target JUL],
Target.Value_08 AS [Target AUG],
Target.Value_09 AS [Target SEP],
Target.Value_10 AS [Target OCT],
Target.Value_11 AS [Target NOV],
Target.Value_12 AS [Target DEC]
FROM
((Contacts INNER JOIN Points ON Contacts.[Id] = Points.[Contacts_Id])
INNER JOIN Contracts ON Points.[Id] = Contracts.[Point_Id])
INNER JOIN Target ON Points.Id = Target.DataSetId
WHERE
Points.UtilityType =[Utility]
ORDER BY
Contacts.ClientGroup;
Desired Output
(the values would go TargetJan through to TargetDec and then BudgetJan through to BudgetDec but I've not shown for brevity):
Group Site ID Utility MPAN1 TargetJan TargetFeb etc... BudgetJan BudgetFeb etc...
ABC London 123 Gas 123456 1,000 1,100 9,000 9,100
ABC NewYork 456 Gas ABC123 5,000 5,100 8,000 8,100
How can I add in the same fields but depending on the value of Target.TargetType, I'm guessing that it is a Union query, but I have no idea.
Any pointers of where to start would be gratefully received :)
UPDATE #1
Thank you for your help. I think I understand the query, however there is still a bit of odd behaviour I can't figure out.
QUERY USED
SELECT
Points.ID,
SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_01 else 0 end) AS [TGT JAN],
SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_02 else 0 end) AS [TGT FEB],
FROM
((Contacts INNER JOIN Points ON Contacts.[Id] = Points.[Contacts_Id])
INNER JOIN Contracts ON Points.[Id] = Contracts.[Point_Id] )
INNER JOIN Target ON Points.Id = Target.DataSetId
GROUP BY
Points.ID
ORDER BY
Points.ID;
Where my data row only has one Target.Type then the query returns as desired:
RAW DATA
ID TARGETTYPE VALUE_01 VALUE_02
10079 0 7642 5735
RESULT
ID TGTJAN TGTFEB
10079 7642 5735
However ... If I have an ID with 2 or more TargetTypes then the Value output is being multiplied by 6.
RAW DATA
ID TARGETTYPE VALUE_01 VALUE_02
7423 0 58339 57441
7423 1 1663 1637
RESULT
ID TGTJAN TGTFEB
7423 350034 344646
I have experimented and removed the INNER JOIN to the contacts table (although I will need that join) and then it all works as expected ???? So why is the Contacts JOIN causing this issue ?
Please put me out of my misery as I can not work out what is going on !

What about using a select case syntax?
select
...
sum(case when Target.TargetType = 'Target' then Target.Value_01 else 0 end) AS [Target JAN],
sum(case when Target.TargetType = 'Budget' then Target.Value_01 else 0 end) AS [Budget JAN],
...
group by
--all fields in the select list which are not aggregates

For future reference I have solved this problem by rewriting the joins and changing the way the data is held so I don't need to reference the Contracts table, which is redudant and probably contributed to my bizarre x by 6 problem.
SELECT
Points.ID,
SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_01 else 0 end) AS [TGT kWh JAN],
SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_02 else 0 end) AS [TGT kWh FEB],
SUM(CASE WHEN Target.TargetType = '1' THEN Target.Value_01 else 0 end) AS [BUD kWh JAN],
SUM(CASE WHEN Target.TargetType = '1' THEN Target.Value_02 else 0 end) AS [BUD kWh FEB]
FROM (Contacts INNER JOIN Points ON Contacts.Id = Points.Contacts_Id)
INNER JOIN Target ON Points.Id = Target.DataSetId
GROUP BY
Points.ID
ORDER BY
Points.ID;

Related

SQL Year over Year Performance with Criteria

I am trying to return year over year results based on date criteria. There is additional information I would like to include in the query i.e. first date of activity and first date of activity with spot name like '%6%'. The current query I have is multiplying the correct amounts by 6 and I can't figure out how to solve. When I remove the first "where" clause I get the correct amounts. Any help would be appreciated.
Select
V.IGB_License,
DBA,
V.Sci_Games_Name,
convert(date,v2.Activity_date) as First6thMachineDate,
convert(date,V3.Activity_date) as GoLiveDate,
sum(case when (v.Activity_date between '1/23/2019' and DATEADD(YEAR,-2,getdate()-1)) then v.Funds_in else 0 end) as FundsIn2019,
sum(case when (v.Activity_date between '1/23/2020' and DATEADD(YEAR,-1,getdate()-1)) then v.Funds_in else 0 end) as FundsIn2020,
sum(case when (v.Activity_date between '1/23/2021' and getdate()) then v.Funds_in else 0 end) as FundsIn2021,
sum(case when (v.Activity_date between '1/23/2019' and DATEADD(YEAR,-2,getdate()-1)) then v.Net_funds else 0 end) as NetFunds2019,
sum(case when (v.Activity_date between '1/23/2020' and DATEADD(YEAR,-1,getdate()-1)) then v.Net_funds else 0 end) as NetFunds2020,
sum(case when (v.Activity_date between '1/23/2021' and getdate()) then v.Net_funds else 0 end) as NetFunds2021
From VGT_activity V
Left Join Locations on v.IGB_License = Locations.IGB_License
left join VGT_activity V2 on v.IGB_License = v2.IGB_License
Left join VGT_activity V3 on v.IGB_License = v3.IGB_License
Where v2.Activity_date = (
Select Min(V1.Activity_date)
From VGT_activity V1
Where v1.IGB_License = V2.IGB_License
and Spot_name like '%6%'
)
and v3.Activity_date = (
Select Min(V1.Activity_date)
From VGT_activity V1
Where v1.IGB_License = V3.IGB_License
)
group by V.IGB_License, dba, V.Sci_Games_Name, v2.Activity_date, v3.Activity_date
order by 4

Transact SQL - Table with different Record types requiring calculation

I have a table of invoices and Record_Types that I need to reconcile to open invoice report. I have the process down and know what I need to do. Just dont know how to properly structure the query and would prefer to not create 3 tables.
Record Types.
Invoice = 1 Credit = 5 Payment = 7
Invoice_Number, Record_Type, Dollar figure
Outstanding_Balance = Invoice(1) -(Payment(7)-(Credit))
Invoice_number Record_type Gen_Numeric_3
Basically I need to take the record_Type 1 and subtract the total of record type 7's from the below.
Invoice_Num Rec_Type Dollar_Amt
00820437 1 536.7700000000
00820437 7 469.6200000000
00820437 7 67.1500000000
Any advice would be great. messer
You can do this with aggregation and case statements:
SELECT invoice_num,
SUM(CASE WHEN rec_type = 1 THEN dollar_amt ELSE 0 END) - (SUM(CASE WHEN rec_type=7 THEN dollar_amt ELSE 0 END) - SUM(CASE WHEN rec_type=5 THEN dollar_amt ELSE 0 END)) as outstanding_balance
FROM yourtable
GROUP BY invoice_num

Trying to group several rows based on donatedmoney status

I'm really struggling with this. I just can't seem to figure it out. I've got the concept in my head but don't exactly know how to put my plain language understanding of how to solve the problem into the correct Syntax.
Here is the question.
Give me a list of all donors and their addresses categorized by whether they donated art, money, or both.
Here is the set up for the tables.
CareTakers: CareTakerID, CareTakerName
Donations: DonationID, DonorID, DonatedMoney, ArtName, ArtType, ArtAppraisedPrice, ArtLocationBuilding, ArtLocationRoom, CareTakerID
Donors: DonorID, DonorName, DonorAddress
Here is what I have for my code so far.
SELECT
DISTINCT(DonorName), DonorAddress
FROM
Donors JOIN Donations ON Donors.DonorID = Donations.DonorID
GROUP BY
DonatedMoney
HAVING
DonatedMoney = 'Y' OR DonatedMoney = 'N' OR DonatedMoney = 'Y' AND ArtName IS NOT NULL
Any help would be highly appreciated!
Why would you use a having clause? The question specifies no filtering. The following summarizes the donations to get what you need and then joins the results back to the donors table:
select d.*, don.DonationType
from donors d join
(select don.donorid,
(case when sum(case when donatedmoney = 'Y' then 1 else 0 end) > 0 and
sum(case when artname is not null then 1 else 0 end) > 0
then 'Both'
when sum(case when donatedmoney = 'Y' then 1 else 0 end) > 0
then 'Money'
when sum(case when artname is not null then 1 else 0 end)
then 'Art'
else 'Neither'
end) as DonationType
from donations don
group by don.donorid
) don
on d.donorid = don.donorid

SQL - Dividing by Sum by Group

I am just learning SQL and have run into a problem creating a custom report. I am working with school attendance data. I want to create a report that gives membership days and number of days for each absence type.
I have successfully created a report for these separately.
Membership Days (calculated by counting days school was in session between the student's entry date and the current date. Membership days does not exist as a field on its own)
SELECT sum(case when cd.DATE_VALUE >= s.ENTRYDATE and cd.DATE_VALUE <= current_timestamp THEN cd.INSESSION ELSE 0 END), s.LASTFIRST
FROM CALENDAR_DAY cd,STUDENTS s
WHERE cd.SCHOOLID = 405
GROUP BY s.LASTFIRST
Count per absence type
SELECT s.STUDENT_NUMBER, s.LASTFIRST,SUM(CASE WHEN a.ATTENDANCE_CODEID = 2 THEN 1 ELSE 0 END),SUM(CASE WHEN a.ATTENDANCE_CODEID = 4 THEN 1 ELSE 0 END),SUM(CASE WHEN a.ATTENDANCE_CODEID = 3 THEN 1 ELSE 0 END),SUM(CASE WHEN a.ATTENDANCE_CODEID = 51 THEN 1 ELSE 0 END)
FROM ATTENDANCE a
INNER join STUDENTS s
ON a.STUDENTID = s.ID
WHERE a.att_date between '%param1%' and '%param2%'
GROUP BY s.STUDENT_NUMBER, s.LASTFIRST
The problem is that if I try to put these in the same report, the membership days are multiplied by the number of times the student appears in the attendance table due to joining student and attendance. My thought on a solution was to then divide this line
sum(case when cd.DATE_VALUE >= s.ENTRYDATE and cd.DATE_VALUE <= current_timestamp THEN cd.INSESSION ELSE 0 END)
by the number of times the student showed up in the attendance table to counteract the student information existing on every line. I can't figure out how to do that. I don't know much about these types of problems, so hopefully I've just gone off on the wrong track and there is an easy solution. Thanks.
Your problem is a common problem -- trying to summarize along two dimensions at the same time without using a subquery. You want to do this query with two aggregation subqueries. Something like this:
SELECT *
FROM (SELECT sum(case when cd.DATE_VALUE >= s.ENTRYDATE and cd.DATE_VALUE <= current_timestamp
THEN cd.INSESSION
ELSE 0
END), s. STUDENT_NUMBER
FROM CALENDAR_DAY cd CROSS JOIN
STUDENTS s
WHERE cd.SCHOOLID = 405
GROUP BY s.STUDENT_NUMBER
) sc JOIN
(SELECT s.STUDENT_NUMBER, s.LASTFIRST,
SUM(CASE WHEN a.ATTENDANCE_CODEID = 2 THEN 1 ELSE 0 END),
SUM(CASE WHEN a.ATTENDANCE_CODEID = 4 THEN 1 ELSE 0 END),
SUM(CASE WHEN a.ATTENDANCE_CODEID = 3 THEN 1 ELSE 0 END),
SUM(CASE WHEN a.ATTENDANCE_CODEID = 51 THEN 1 ELSE 0 END)
FROM ATTENDANCE a INNER join
STUDENTS s
ON a.STUDENTID = s.ID
WHERE a.att_date between '%param1%' and '%param2%'
GROUP BY s.STUDENT_NUMBER, s.LASTFIRST
) sa
on sc.STUDENT_NUMBER = sa.STUDENT_NUMBER;

SQL Select Query - problem pivoting rows into columns

I have three tables in an SQL 2005 database, that I need to query and display on one row. The tables are:
MasterStock
StockID, Description
1, Plate
2, Bowl
ShopStock
ShopID, StockID, StockLevel
1,1,6
2,1,0
3,1,0
4,1,10
Sales
StockId, ShopId, SoldQuantity, transDate
1, 1, 1, 5/1/2011
1,2,1, 5/1/2011
I need to get them to show one row:
StockID, Description, 1 Sales, 1 Stock, 2 Sales, 2 Stock, 3 Sales,…
I have managed to get what somewhere with the query below:
SELECT MasterStock.StockID, MasterStock.Description,
SUM(CASE WHEN sales.shopid = 1 THEN sales.Soldquantity ELSE 0 END) AS [1 Sold],
MAX(CASE WHEN shopstock.shopid = 1 THEN shopstock.stockLevel ELSE 0 END) AS [1 Stock],
SUM(CASE WHEN sales.shopid = 2 THEN sales.Soldquantity ELSE 0 END) AS [2 Sold],
MAX(CASE WHEN shopstock.shopid = 2 THEN shopstock.stockLevel ELSE 0 END) AS [2 Stock],
SUM(CASE WHEN sales.shopid = 3 THEN sales.Soldquantity ELSE 0 END) AS [3 Sold],
MAX(CASE WHEN shopstock.shopid = 3 THEN shopstock.stockLevel ELSE 0 END) AS [3 Stock],
SUM(CASE WHEN sales.shopid = 4 THEN sales.Soldquantity ELSE 0 END) AS [4 Sold],
MAX(CASE WHEN shopstock.shopid = 4 THEN shopstock.stockLevel ELSE 0 END) AS [4 Stock]
FROM ShopStock INNER JOIN
Sales ON ShopStock.StockID = Sales.StockID AND ShopStock.shopID = Sales.ShopID
INNER JOIN MasterStock ON ShopStock.StockID = MasterStock.StockID
WHERE (sales.transdate > 1/1/2010)
GROUP BY MasterStock.StockID, MasterStock.Description
However, if there are no sales for the product it doesn’t show any stock levels. If I remove the shopID join on shopstock and sales it shows the stock levels, but reports inaccurate sales - multiplies by four (one for each shopstock record?).
I know I’m missing something here, but I’m not getting anywhere! Any help would be greatly received.
Two problems:
1) You need a LEFT OUTER JOIN between ShopStock and Sales, which will ensure that the query returns records from ShopStock even if there are no related entries in Sales. By definition, an INNER JOIN will not return records from either side of the join, if one of the sides is missing records.
2) You need to move your sales.transdate > 1/1/2010 condition to the inner join, rather than the WHERE clause. Conditions in the WHERE clause will be logically applied after any logic in the table joins. So even if you get your joins right, the where clause will filter out stock without sales because sales.transdate will appear null.
Something like this:
FROM ShopStock LEFT OUTER JOIN Sales
ON ShopStock.StockID = Sales.StockID
AND Sales.transdate > 1/1/2010
INNER JOIN // the rest of your joins here
I'm guessing you also want >= on your transdate filter as well, but that's just a hunch.
Good luck!