I have a query that returns the following data:
I am trying to adapt it to sum the total number of runs at each venue. I have come up with the query below but there are two issues:
it is not summing by venue it is summing all runs;
it takes a very long time to run.
Can anyone see what I'm doing wrong?
Thanks
SELECT ScorecardBatting.matchId, ScorecardBatting.battingTeam, ScorecardBatting.inningsNo, ScorecardBatting.batsmanId, ScorecardBatting.howDismissed, ScorecardBatting.runs, ScorecardBatting.ballsFaced, Matches.venue, (SELECT SUM(runs) FROM ScorecardBatting WHERE Matches.venue=Matches.venue) AS TOTAL
FROM Matches INNER JOIN ScorecardBatting ON Matches.matchId = ScorecardBatting.matchId
GROUP BY ScorecardBatting.matchId, ScorecardBatting.battingTeam, ScorecardBatting.inningsNo, ScorecardBatting.batsmanId, ScorecardBatting.howDismissed, ScorecardBatting.runs, ScorecardBatting.ballsFaced, Matches.venue;
If you want the total number of runs for each venue, then a simple aggregation query does what you want:
SELECT Matches.venue, SUM(runs) AS TOTAL
FROM Matches INNER JOIN
ScorecardBatting
ON Matches.matchId = ScorecardBatting.matchId
GROUP BY Matches.venue;
If you want this in your original query, you could join it in:
select . . ., t.total
from Matches inner join
ScorecardBatting
on Matches.matchId = ScorecardBatting.matchId join
(select Matches.venue, sum(runs) AS TOTAL
from Matches INNER JOIN
ScorecardBatting
on Matches.matchId = ScorecardBatting.matchId
group by Matches.venue
) t
on t.venue = matches.venue;
I don't think you need a group by fro your query.
Related
I have a table here in which I want to write a SELECT query in SQL Server that allows me to get the following:
For each unique combination of SalesPerson x Country, get only the rows with the latest Upload_DateTime
However, I am trying to do a group-by and inner join, but to no avail. My code is something like this:
SELECT t1.[SalesPerson], t1.[Country], MAX(t1.[Upload_DateTime]) as [Upload_DateTime]
FROM [dbo].[CommentTable] AS t1
GROUP BY t1.[SalesPerson], t1.[Country]
INNER JOIN SELECT * FROM [dbo].[CommentTable] as t2 ON t1.[SalesPerson] = t2.[SalesPerson], t1.[Country] = t2.[Country]
It seems like the GROUP BY needs to be done outside of the INNER JOIN? How does that work? I get an error when I run the query and it seems my SQL is not right.
Basically, this subquery will fetch the person, the country and the latest date:
SELECT
SalesPerson, Country, MAX(uplodaed_datetime)
FROM CommentTable
GROUP BY SalesPerson, Country;
This can be used on a lot of ways (for example with JOIN or with an IN clause).
The main query will add the remaing columns to the result.
Since you tried a JOIN, here the JOIN option:
SELECT
c.id, c.SalesPerson, c.Country,
c.Comment, c.uplodaed_datetime
FROM
CommentTable AS c
INNER JOIN
(SELECT
SalesPerson, Country,
MAX(uplodaed_datetime) AS uplodaed_datetime
FROM CommentTable
GROUP BY SalesPerson, Country) AS sub
ON c.SalesPerson = sub.SalesPerson
AND c.Country = sub.Country
AND c.uplodaed_datetime = sub.uplodaed_datetime
ORDER BY c.id;
Try out: db<>fiddle
I am struggling with taking a Count() from one table and dividing it by a correlating number from a different table in Microsoft SQL Server.
Here is a fictional example of what I'm trying to do
Lets say I have a table of orders. One column in there is states.
I have a second table that has a column for states, and second column for each states population.
I'd like to find the order per population for each sate, but I have struggled to get my query right.
Here is what I have so far:
SELECT Orders.State, Count(*)/
(SELECT StatePopulations.Population FROM Orders INNER JOIN StatePopulations
on Orders.State = StatePopulations.State
WHERE Orders.state = StatePopulations.State )
FROM Orders INNER JOIN StatePopulations
ON Orders.state = StatePopulations.State
GROUP BY Orders.state
So far I'm contending with an error that says my sub query is returning multiple results for each state, but I'm newer to SQL and don't know how to overcome it.
If you really want a correlated sub-query, then this should do it...
(You don't need to join both table in either the inner or outer query, the correlation in the inner query's where clause does the 'join'.)
SELECT
Orders.state,
COUNT(*) / (SELECT population FROM StatePopulation WHERE state = Orders.state)
FROM
Orders
GROUP BY
Orders.state
Personally, I'd just join them and use MAX()...
SELECT
Orders.state,
COUNT(*) / MAX(StatePopulation.population)
FROM
Orders
INNER JOIN
StatePopulation
StatePopulation.state = Orders.state
GROUP BY
Orders.state
Or aggregate your orders before you join...
SELECT
Orders.state,
Orders.order_count / StatePopulation.population
FROM
(
SELECT
Orders.state,
COUNT(*) AS order_count
FROM
Orders
GROUP BY
Orders.state
)
Orders
INNER JOIN
StatePopulation
StatePopulation.state = Orders.state
(Please forgive typos and smelling pistakes, I'm doing this on a phone.)
I am adding up the amount of tickets sold for a sporting event, the answer should be under 100 but my answer is in the thousands.
SELECT Stubhub.Active.Opponent,
SUM(Stubhub.Active.Qty) AS AQty, SUM(Stubhub.Sold.Qty) AS SQty
FROM Stubhub.Active INNER JOIN
Stubhub.Sold ON Stubhub.Active.Opponent = Stubhub.Sold.Opponent
GROUP BY Stubhub.Active.Opponent
This is type of problem occurs because you are getting a cartesian product between each table for each opponent. The solution is to pre-aggregate by opponent:
SELECT a.Opponent, a.AQty, s.SQty
FROM (SELECT a.Opponent, SUM(a.Qty) as AQty
FROM Stubhub.Active a
GROUP BY a.Opponent
) a INNER JOIN
(SELECT s.Opponent, SUM(s.QTY) as SQty
FROM Stubhub.Sold s
GROUP BY s.Opponent
) s
ON a.Opponent = s.Opponent;
Notice that in this case, you do not need the aggregation in the outer query.
Here is my SQL command :
SELECT
ts.CHECK_NUMBER,
ts.CUSTOMER_NAME,
ts.COMPANY_NAME,
( SELECT COUNT(*)
FROM TRANSACTION_ORDER too
WHERE too.CHECK_NUMBER = ts.CHECK_NUMBER
) as NB_OF_ORDERS
FROM
TRANSACTION_SUMMARY ts
ORDER BY
ts.BUSINESS_DATE
It is taking so long to render data, we are talking about minimum 3000 transactions, for each one we have to count the orders.
Is there any better solution?
It is taking too long because when you have this sub-query in your select , it is executed for each row returned by the outer query, so if your outer query returns 50,000 rows this inner select query will be executed 50,000 times which is obviously a performance killer,
You should try something like this....
SELECT
ts.CHECK_NUMBER
,ts.CUSTOMER_NAME
,ts.COMPANY_NAME
,ISNULL(O.Total, 0) AS NB_OF_ORDERS
FROM TRANSACTION_SUMMARY ts
LEFT JOIN --<-- use inner join is you only want records with some orders
( SELECT CHECK_NUMBER, COUNT(*) AS Total
FROM TRANSACTION_ORDER
GROUP BY CHECK_NUMBER
) as O
ON ts.CHECK_NUMBER = O.CHECK_NUMBER
ORDER BY ts.BUSINESS_DATE
I have four tables,in which First has one to many relation with rest of three tables named as (Second,Third,Fourth) respectively.I want to sum only Distinct Rows returned by select query.Here is my query, which i try so far.
select count(distinct First.Order_id) as [No.Of Orders],sum( First.Amount) as [Amount] from First
inner join Second on First.Order_id=Second.Order_id
inner join Third on Third.Order_id=Second.Order_id
inner join Fourth on Fourth.Order_id=Third.Order_id
The outcome of this query is :
No.Of Orders Amount
7 69
But this Amount should be 49,because the sum of First column Amount is 49,but due to inner join and one to many relationship,it calculate sum of also duplicate rows.How to avoid this.Kindly guide me
I think the problem is cartesian products in the joins (for a given id). You can solve this using row_number():
select count(t1234.Order_id) as [No.Of Orders], sum(t1234.Amount) as [Amount]
from (select First.*,
row_number() over (partition by First.Order_id order by First.Order_id) as seqnum
from First inner join
Second
on First.Order_id=Second.Order_id inner join
Third
on Third.Order_id=Second.Order_id inner join
Fourth
on Fourth.Order_id=Third.Order_id
) t1234
where seqnum = 1;
By the way, you could also express this using conditions in the where clause, because you appear to be using the joins only for filtering:
select count(First.Order_id) as [No.Of Orders], sum(First.Amount) as [Amount]
from First
where exists (select 1 from second where First.Order_id=Second.Order_id) and
exists (select 1 from third where First.Order_id=third.Order_id) and
exists (select 1 from fourth where First.Order_id=fourth.Order_id);