So say I have a mysql statement like this:
SELECT username, #n := #n + 1 ranking, `1st places`, `2nd places`, `3rd places`, `top5`, `top3
FROM
(
SELECT username,
SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) `1st places`,
SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) `2nd places`,
SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) `3rd places`,
SUM(CASE WHEN rating < 6 THEN 5 ELSE 0 END) `top5`,
SUM(CASE WHEN rating < 4 THEN 5 ELSE 0 END) `top3`
FROM Table1
GROUP BY username
ORDER BY `1st places` DESC
) q, (SELECT #n := 0) n
How can I add up the columns that I created called, '1st places' + '2nd places' + '3rd places' in this sql statement and create another column called "total finishes"? Seems easy, but I cannot seem to figure it out.
You can just add them together and give an alias to that extra column:-
SELECT username, #n := #n + 1 ranking, `1st places`, `2nd places`, `3rd places`, `top5`, `top3, `1st places` + `2nd places` + `3rd places` AS `total finishes`
FROM
(
SELECT username,
SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) `1st places`,
SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) `2nd places`,
SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) `3rd places`,
SUM(CASE WHEN rating < 6 THEN 5 ELSE 0 END) `top5`,
SUM(CASE WHEN rating < 4 THEN 5 ELSE 0 END) `top3`
FROM Table1
GROUP BY username
ORDER BY `1st places` DESC
) q, (SELECT #n := 0) n
I'm pretty sure that you can't re-use a field with a calculated value in the same SELECT query, however wrapping into an additional sub-query may be an option:
SELECT username, #n := #n + 1 ranking, `1st places`, `2nd places`, `3rd places`, `top5`, `top3
FROM
(
SELECT
*,
`1st places`+`2nd places`+`3rd places` `total finishes`
FROM (
SELECT username,
SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) `1st places`,
SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) `2nd places`,
SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) `3rd places`,
SUM(CASE WHEN rating < 6 THEN 5 ELSE 0 END) `top5`,
SUM(CASE WHEN rating < 4 THEN 5 ELSE 0 END) `top3`
FROM Table1
GROUP BY username
ORDER BY `1st places` DESC
)
)
q, (SELECT #n := 0) n
Haven't tried, still...
Related
This MySQL query gives me this error 'Unknown column 'winnings' in 'field list'
SELECT
o.user_id,
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no,
sum(case when o.result = 1 then 1 else 0 end) as winnings,
sum(case when o.result = 2 then 1 else 0 end) as loses,
sum(case when winnings = 10 then 0.5 else 0 end) as counter
FROM `odds_tahminler` o
I know that winnings is the value of the sum() aggregation function, But is there any way to check the winnings value within the query?
You can't use an aggregated column inside the select. However you can use a subquery to obtain the counter value after all the aggregated columns have been computed.
How is the counter value calculated? I assumed that the counter should be (winnings - 10) / 2 if there's at least 10 winnings and 0 otherwise. In that case you can obtain it with this query
SELECT O.*,
GREATEST( (O.winnings - 10) / 2, 0) as counter
FROM
(
SELECT u.username,
o.user_id,
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no,
sum(case when o.result = 1 then 1 else 0 end) as winnings,
sum(case when o.result = 2 then 1 else 0 end) as loses
FROM `odds_tahminler` o
) as O
you can try:
SELECT
o.user_id,
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no,
sum(case when o.result = 1 then 1 else 0 end) as winnings,
sum(case when o.result = 2 then 1 else 0 end) as loses,
sum(case when winnings = 10 then 0.5 else 0 end) as counter
FROM `odds_tahminler` o
GROUP BY o.user_id
HAVING counter>2
I have a query,which give me wrong result.Here is my query.
SELECT sum(open1) open1, sum(closed1) closed,sum(pending1) NotSpecified,
status,type,sub_status,created_date,bystatus,lst_type
FROM (
SELECT agent_1_id,status,type,sub_status,created_date,'Open' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
count(*) as open1, 0 closed1, 0 pending1
FROM crm_mydeals
where status = 1 AND agent_1_id>0 and is_active=1
GROUP BY status
UNION ALL
SELECT agent_1_id,status,type,sub_status,created_date,'Closed' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
0 open1, count(*) as closed1, 0 pending1
FROM crm_mydeals
where status = 2 AND agent_1_id>0 and is_active=1
GROUP BY status
UNION ALL
SELECT agent_1_id,status,type,sub_status,created_date,'NotSpecified' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
0 open1, 0 closed1, count(*) as pending1
FROM crm_mydeals
where status = 3 AND agent_1_id>0 and is_active=1
GROUP BY status
) s
WHERE DATE(created_date) BETWEEN '2013-11-22' AND '2014-2-22'
GROUP BY status
If you simplify the query, it might help to find the correct answer?
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0) AS open1,
SUM(CASE WHEN status = 2 THEN 1 ELSE 0) AS closed,
SUM(CASE WHEN Status = 3 THEN 1 ELSE 0) AS NotSpecified,
status,
type,
sub_status,
created_date,
(CASE WHEN status = 1 THEN 'Open'
WHEN status = 2 THEN 'Closed'
WHEN status = 3 THEN 'NotSpecified') AS bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type
FROM crm_mydeals
WHERE status IN (1,2,3)
AND agent_1_id > 0
AND is_active=1
AND DATE(created_date) BETWEEN '2013-11-22' AND '2014-2-22'
GROUP BY status, type, sub_status, created_date
I have 3 tables; contracts, dealers, and users.
users have many dealers and dealers have many contracts but the contracts are not directly associated with the users.
I am trying to build a report that gets me a monthly count of completed contracts grouped by user for the last 12 months.
So far I have built a multiple subquery, which is very slow: SQL Fiddle
SELECT *,
( SELECT count(*) FROM contracts
WHERE
dealer_id IN
( SELECT id FROM dealers WHERE user_id = User.id )
AND status = 'Paid'
AND completion_date BETWEEN
'2012-08-01 00:00:00' AND '2012-08-31 23:59:59'
) AS Aug_2012,
( SELECT count(*) FROM contracts
WHERE
dealer_id IN
( SELECT id FROM dealers WHERE user_id = User.id )
AND status = 'Paid'
AND completion_date BETWEEN
'2012-09-01 00:00:00' AND '2012-09-30 23:59:59'
) AS Sep_2012
FROM users AS User
WHERE
id IN( SELECT user_id FROM dealers WHERE active = 1 AND user_id IS NOT NULL GROUP BY user_id )
AND id != 1
ORDER BY User.name ASC
Instead of the subquery which selects each month I'd like to use something like this:
COUNT(*) as last_12_months,
SUM(case when MONTH(completion_date) = 8 then 1 else 0 end) as Aug_2012,
SUM(case when MONTH(completion_date) = 9 then 1 else 0 end) as Sep_2012,
etc.
Since I'd be returning multiple columns I would have to restructure it, but I'm not sure how. If I use an INNER JOIN what clause do I join on?
Here's the final query based on Mikhail's answer below:
SELECT
User.*,
SUM(case when MONTH(completion_date) = 8 then 1 else 0 end) AS Aug_2012,
SUM(case when MONTH(completion_date) = 9 then 1 else 0 end) AS Sep_2012,
SUM(case when MONTH(completion_date) = 10 then 1 else 0 end) AS Oct_2012,
SUM(case when MONTH(completion_date) = 11 then 1 else 0 end) AS Nov_2012,
SUM(case when MONTH(completion_date) = 12 then 1 else 0 end) AS Dec_2012,
SUM(case when MONTH(completion_date) = 1 then 1 else 0 end) AS Jan_2013,
SUM(case when MONTH(completion_date) = 2 then 1 else 0 end) AS Feb_2013,
SUM(case when MONTH(completion_date) = 3 then 1 else 0 end) AS Mar_2013,
SUM(case when MONTH(completion_date) = 4 then 1 else 0 end) AS Apr_2013,
SUM(case when MONTH(completion_date) = 5 then 1 else 0 end) AS May_2013,
SUM(case when MONTH(completion_date) = 6 then 1 else 0 end) AS Jun_2013,
SUM(case when MONTH(completion_date) = 7 then 1 else 0 end) AS Jul_2013,
SUM(case when completion_date BETWEEN '2012-08-01 00:00:00' AND '2013-07-31 23:59:59' then 1 else 0 end) as last_12_months
FROM users AS User
LEFT OUTER JOIN
(
SELECT id, user_id FROM dealers
WHERE active = 1 AND user_id IS NOT NULL
) AS Dealer ON User.id = Dealer.user_id
LEFT OUTER JOIN
(
SELECT completion_date, status, dealer_id FROM contracts
WHERE completion_date BETWEEN '2012-08-01 00:00:00' AND '2013-07-31 23:59:59' AND status = 'Paid' AND cancelled = 0
) AS Contract on Dealer.id = Contract.dealer_id
WHERE
User.id IN
(
SELECT user_id FROM dealers
WHERE active = 1 AND user_id IS NOT NULL
GROUP BY user_id
)
GROUP BY
User.id order by User.name asc
This is about 4 times faster.
Try this:
select
User.id, User.name,
sum(case when MONTH(completion_date) = 8 and Year(completion_date)=2012 then 1 else 0 end) as Aug_2012,
sum(case when MONTH(completion_date) = 9 and Year(completion_date)=2012 then 1 else 0 end) as Sep_2012,
sum(case when MONTH(completion_date) = 10 and Year(completion_date)=2012 then 1 else 0 end) as Oct_2012,
sum(case when MONTH(completion_date) = 11 and Year(completion_date)=2012 then 1 else 0 end) as Nov_2012,
sum(case when MONTH(completion_date) = 12 and Year(completion_date)=2012 then 1 else 0 end) as Dec_2012,
sum(case when MONTH(completion_date) = 1 and Year(completion_date)=2013 then 1 else 0 end) as Jan_2012,
sum(case when MONTH(completion_date) = 2 and Year(completion_date)=2013 then 1 else 0 end) as Feb_2012,
sum(case when MONTH(completion_date) = 3 and Year(completion_date)=2013 then 1 else 0 end) as Mar_2012,
sum(case when MONTH(completion_date) = 4 and Year(completion_date)=2013 then 1 else 0 end) as Apr_2012,
sum(case when MONTH(completion_date) = 5 and Year(completion_date)=2013 then 1 else 0 end) as May_2012,
sum(case when MONTH(completion_date) = 6 and Year(completion_date)=2013 then 1 else 0 end) as Jun_2012,
sum(case when MONTH(completion_date) = 7 and Year(completion_date)=2013 then 1 else 0 end) as Jul_2012
from users AS User
left outer join dealers on
User.id=dealers.user_id
left outer join contracts on
dealers.id=contracts.dealer_id
group by
User.id,
contracts.status
having
contracts.status='Paid'
order by
User.name asc;
I have a SP and UDF that brings me Totals. But I know there is a COUNT CASE WHEN query possible to get totals in a single go. Can anyone help me in this regard? My current query is just a redundant pause.
ALTER PROCEDURE [dbo].[GetMailBasketsForLocums]
AS
BEGIN
SET NOCOUNT ON;
SET FMTONLY OFF;
SET DATEFORMAT DMY;
SELECT DISTINCT Locum.OID,
Locum.FirstName + ' ' + Locum.LastName AS Name,
dbo.GetMailBasketTotalsForLocums(MailBasket.LocumId, 1) AS BookingsConfirmed,
dbo.GetMailBasketTotalsForLocums(MailBasket.LocumId, 3) AS BookingsCancelled,
dbo.GetMailBasketTotalsForLocums(MailBasket.LocumId, 5) AS BookingsSwitched,
(dbo.GetMailBasketTotalsForLocums(MailBasket.LocumId, 1) +
dbo.GetMailBasketTotalsForLocums(MailBasket.LocumId, 3) +
dbo.GetMailBasketTotalsForLocums(MailBasket.LocumId, 5)) AS Total
FROM MailBasket INNER JOIN
Locum
ON Locum.OID = MailBasket.LocumID
WHERE MailBasket.IsSent = 0
AND MailBasket.MailTypeID IN (1, 3, 5);
END
And
ALTER FUNCTION [dbo].[GetMailBasketTotalsForLocums]
(
#LocumID BIGINT,
#MailTypeID INT
)
RETURNS int
AS
BEGIN
DECLARE #Result int
SELECT #Result = COUNT(MailBasket.OID)
FROM MailBasket
WHERE MailBasket.MailTypeID = #MailTypeID
AND MailBasket.LocumID = #LocumID
AND MailBasket.IsSent = 0;
RETURN #Result
END
Regards.
SELECT
DISTINCT Locum.OID,
Locum.FirstName + ' ' + Locum.LastName AS Name,
COUNT(CASE WHEN MailBasket.MailTypeID = 1 THEN 1 ELSE NULL END) AS BookingsConfirmed,
COUNT(CASE WHEN MailBasket.MailTypeID = 3 THEN 1 ELSE NULL END) AS BookingsCancelled,
COUNT(CASE WHEN MailBasket.MailTypeID = 5 THEN 1 ELSE NULL END) AS BookingsSwitched,
COUNT(*) AS Total
FROM
MailBasket INNER JOIN
Locum
ON Locum.OID = MailBasket.LocumID
WHERE MailBasket.IsSent = 0
AND MailBasket.MailTypeID IN (1, 3, 5);
COUNT ignores NULLs to the conditional count works
You can also use SUM
SUM(CASE WHEN MailBasket.MailTypeID = 5 THEN 1 ELSE 0 END)
Try this:
SELECT DISTINCT Locum.OID, Locum.FirstName + ' ' + Locum.LastName AS Name,
COUNT(CASE WHEN MailBasket.MailTypeID = 1 THEN MailBasket.MailTypeID END) as BookingsConfirmed,
COUNT(CASE WHEN MailBasket.MailTypeID = 3 THEN MailBasket.MailTypeID END) as BookingsCancelled,
COUNT(CASE WHEN MailBasket.MailTypeID = 5 THEN MailBasket.MailTypeID END) as BookingsSwitched,
COUNT(*) as TOTAL
FROM MailBasket INNER JOIN
Locum
ON Locum.OID = MailBasket.LocumID
WHERE MailBasket.IsSent = 0
AND MailBasket.MailTypeID IN (1, 3, 5);
You can use a case inside a sum and group on the Locum records:
select
Locum.OID,
Locum.FirstName + ' ' + Locum.LastName AS Name,
sum(case when MailBasket.MailTypeID = 1 then 1 else 0 end) as BookingsConfirmed,
sum(case when MailBasket.MailTypeID = 3 then 1 else 0 end) as BookingsCancelled,
sum(case when MailBasket.MailTypeId = 5 then 1 else 0 end) as BookingsSwitched,
count(*) as Total
from
MailBasket
inner join Locum on Locum.OID = MailBasket.LocumID
where
MailBasket.IsSent = 0
and MailBasket.MailTypeID IN (1, 3, 5)
group by
Locum.OID, Locum.FirstName, Locum.LastName
I am using SUM ( CASE WHEN ) to count number of Yes and No, it work fine.
I am havin problem counting number of matching mobile number from two tables. It dont seem to be counting correctly.
There is a MobileNO field in dairy table and mobile field in the sales table
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
(SELECT SUM(CASE WHEN D.MobileNo = S.mobile THEN 1 ELSE 0 END) from sales as S) as Sales,
COUNT(*) as TOTAL FROM dairy as D
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S ON D.MobileNo = S.mobile
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC