So I'm having two tables:
Table 1:
Item
id : int
title : varchar
comments : varchar
Table 2:
Rating
id : int
isUpvote : bit
date : datetime
item_id : int
One Item can have several ratings. IsUpvote states whether a Rating is a like or dislike.
I'm using NHibernate to ORM to my MySQL-database.
Want I want to make a kind of trending list of items to show which items have had to most likes in the past time (so from dateTime X to now).
The MySQL code looks like this:
select p.id, sum(case when r.isUpvote = b'1' then 1 else 0 END) - sum(case when r.isUpvote=b'0' then 1 else 0 END) as score
from rating as r
right join item as p
on p.id = r.item_id
group by p.id
order by score desc
limit 7;
This results in:
Id - Score
1 - 2
2 - 1
3 - 0
4 - 0
5 - 0
6 - 0
7 - 0
So I want to do this in NHibernate. I tried but HQL and QueryOver but I just can't figure it out. What I tried is:
GetTrendingItems(int nrOfTrendingItems, DateTime fromDate) {
var trendingItems = Session
.CreateQuery(#"select p, sum(case when r.IsUpvote = 1 then 1 else 0 END) - sum(case when r.IsUpvote=0 then 1 else 0 END) as score
from Item as p, Rating as r
where p.Id = r.Point.Id
group by p.Id
order by sum(case when r.IsUpvote = 1 then 1 else 0 END) - sum(case when r.IsUpvote=0 then 1 else 0 END) desc")
.SetMaxResults(nrOfClimbers)
.List();
}
and I tried:
GetTrendingItems(int nrOfTrendingItems, DateTime fromDate) {
DBRating dbRatingAlias = null;
var iets = Session.QueryOver<DBRating>(() => dbRatingAlias)
.Where(r => r.Date > fromDate)
.OrderBy(Projections.Conditional(
Restrictions.Where(() => dbRatingAlias.IsUpvote),
Projections.Constant(1),
Projections.Constant(-1))).Desc
.Right.JoinQueryOver<DBPoints>(r => r.Point)
.Take(nrOfClimbers)
.List();
}
I'm kinda losing hope here. Can someone help me?
Update:
The HQL generates this SQL:
select
dbpoints0_.id as col_0_0_,
sum(case
when dbrating1_.isUpvote=1 then 1
else 0
end)-sum(case
when dbrating1_.isUpvote=0 then 1
else 0
end) as col_1_0_,
dbpoints0_.id as id3_,
dbpoints0_.active as active3_,
dbpoints0_.title as title3_,
dbpoints0_.comments as comments3_,
dbpoints0_.score as score3_
from
points dbpoints0_,
Rating dbrating1_
where
dbpoints0_.id=dbrating1_.points_id
group by
dbpoints0_.id
order by
sum(case
when dbrating1_.isUpvote=1 then 1
else 0
end)-sum(case
when dbrating1_.isUpvote=0 then 1
else 0
end) desc limit ?p0;
?p0 = 10 [Type: Int32 (0)]
and the Fluent generates this SQL:
SELECT
this_.id as id6_0_,
this_.owners_id as owners2_6_0_,
this_.points_id as points3_6_0_,
this_.isUpvote as isUpvote6_0_,
this_.date as date6_0_
FROM
Rating this_
WHERE
this_.date > ?p0
ORDER BY
(case
when this_.isUpvote = ?p1 then ?p2
else ?p3
end) desc limit ?p4;
?p0 = 25-5-2015 22:39:49 [Type: DateTime (0)],
?p1 = True [Type: Boolean (0)],
?p2 = 1 [Type: Int32 (0)],
?p3 = -1 [Type: Int32 (0)],
?p4 = 10 [Type: Int32 (0)]
I made a stored procedure in de MySQL database
DELIMITER //
create procedure getTrending
(IN $getClimbers bool,
IN $fromDate DateTime,
IN $nrOfTrending int)
Begin
if ($getClimbers) then
select p.id, p.active, p.title, p.comments,
sum(case when r.isUpvote = b'1' and r.date > $fromDate then 1 else 0 END) - sum(case when r.isUpvote=b'0' and r.date > $fromDate then 1 else 0 END) as score
from rating as r
right join points as p
on p.id = r.points_id
group by p.id
order by sum(case when r.isUpvote = b'1' and r.date > $fromDate then 1 else 0 END) - sum(case when r.isUpvote=b'0' and r.date > $fromDate then 1 else 0 END) desc
limit $nrOfTrending;
else
select p.id, p.active, p.title, p.comments,
sum(case when r.isUpvote = b'1' and r.date > $fromDate then 1 else 0 END) - sum(case when r.isUpvote=b'0' and r.date > $fromDate then 1 else 0 END) as score
from rating as r
right join points as p
on p.id = r.points_id
group by p.id
order by sum(case when r.isUpvote = b'1' and r.date > $fromDate then 1 else 0 END) - sum(case when r.isUpvote=b'0' and r.date > $fromDate then 1 else 0 END) asc
limit $nrOfTrending;
end if;
end //
DELIMITER ;
and then called it from my C#-code with:
public virtual List<DBPoints> GetClimbers(int nrOfClimbers, DateTime fromDate)
{
OpenSession();
var query = Session.GetNamedQuery("getTrending");
query.SetBoolean("getClimbers", true);
query.SetDateTime("fromDate", fromDate);
query.SetInt32("nrOfTrending", nrOfClimbers);
var pointList = query.List<DBPoints>();
CloseSession();
return pointList.ToList();
}
For Nhibernate there needs to be made a mapping like this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="TechDash.Business"
namespace="TechDash.Business.Models.Database">
<sql-query name="getTrending">
<return class="DBPoints" />
call getTrending(:getClimbers, :fromDate, :nrOfTrending)
Since the names of my columns that are returned are the same as those that are returned when I'm retrieving a Points-object, I don't have to make a new C#-object nor the mapping belonging to that C#-object.
Related
I have to run two queries in my code to get my tenants balance. However, these queries are too slow.
First query, I get all the tenants and it's unit name:
SELECT t.TenantID
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),
REPEAT('0', (10-CHAR_LENGTH(UnitName))),
Right(Replace(UnitName,'-',''),
CHAR_LENGTH(Replace(UnitName,'-',''))-2
) )
It returns 500 rows
Then I get the balances in 4 conditions. This query will be inside of first query loop:
Select
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Where TenantID= FirstQuery.TenantID
Am I doing the queries wrong? It's taking like 1 minute to run.
Do this in a single query with GROUP BY.
Try something like this:
SELECT t.TenantID, TotalDebit, HousingDebit, TotalCredit, HousingCredit
FROM Tenants t
JOIN Units u ON t.UnitID = u.UnitID
LEFT JOIN (
Select
TenantID,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Group By TenantID
) sums ON sums.TenantID = t.TenantID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-2))
The inner query may still run for a while but it will only run once.
Try a compound covering index on TenantTransactions containing these columns: (TenantID, TransactionTypeID, ChargeTypeID, TransactionAmount) to optimize the query with the SUMs in it.
Try a compound index on Tenants with the columns (PropertyID, Prospect) in it.
Here's another way to do it with a subquery. You know, the performance problem might not be database performance, but the back and forth between your database and application server. So that is where a single query will help.
SELECT t.TenantID,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalCredit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingCredit
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-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'm trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below.
BusinessUnit OrderDate Canceled
UnitA 1/15/2013 N
UnitA 10/1/2013 N
UnitB 10/15/2013 N
UnitB 10/22/2013 N
UnitB 10/22/2013 N
Based on the records above, I'd like the result to appear like below
BusinessUnit TodaysOrders ThisMonthsOrders ThisYearsOrders
UnitA 0 1 2
UnitB 2 3 3
My current code is below. It's giving me error (something about DatabaseName.sum does not exist. Check the
Function Name Parsing and Resolution' section... )
Select
SUM (CASE WHEN (OrderDate)=DATE(NOW()) THEN 1 ELSE 0 END) AS TodaysOrders,
SUM (CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) AND MONTH(OrderDate) = MONTH(CURDATE()) THEN 1 ELSE 0 END) AS ThisMonthsOrders,
SUM (CASE WHEN YEAR(main_order_managers.creation_date) = YEAR(CURDATE()) THEN 1 ELSE 0 END) AS ThisYearsOrders
code continues
FROM OrderTable WHERE OrderTable.Canceled. <> 'Y';
Is Sum Case the best use here?
The error is caused by the space between function name and parenthesis
SUM (CASE WHEN ...
^^
Read more Function Name Parsing and Resolution
Try
SELECT BusinessUnit,
SUM(CASE WHEN OrderDate = CURDATE() THEN 1 ELSE 0 END) TodaysOrders,
SUM(CASE WHEN DATE_FORMAT(OrderDate, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') THEN 1 ELSE 0 END) ThisMonthsOrders,
SUM(CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) THEN 1 ELSE 0 END) ThisYearsOrders
FROM OrderTable
WHERE Canceled <> 'Y'
GROUP BY BusinessUnit
Here is SQLFiddle demo
See the SQL query below, it work fine. It calculate the number of Yes, NOT, Other and the number of matching mobile number [Sales field] (D.MobileNo = S.mobile)
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
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 = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC
I want to exclude WHERE for the Sales field - it should not be part from CheckDate. Meaning it should check any record in the dairy table without CheckDate for the Sales field.
How can that be done?
If you really want those results in only one query, this might do the trick.
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL,
SUM(CASE WHEN UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) AS TOTALINCHECKDATE
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
GROUP BY D.Username
ORDER BY TOTAL DESC
Note that "TOTAL" will count all rows (including those who where not within your CheckDate range), TOTALINCHECKDATE return the same value as in your previous query.
Obviously, this can still be optimized.
Assuming username exists also in SALES table
SELECT Username,SUM(Yes) As Yes, SUM(`Not`) As `Not`
, SUM(Other) As Other, SUM(sales) Sales, SUM(total)
FROM (
-- get diary data
SELECT username,mobileNo As mobile,
CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END as Yes,
CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END as `Not`,
CASE WHEN D.type = '' THEN 1 ELSE 0 END as Other,
0 As sales, 1 as total
FROM dairy as D
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
UNION ALL
-- get all sales
SELECT DISTINCT username,mobile, 0 as Yes, 0 as `Not`, 0 As Other, 1 As sales, 0 As total
FROM sales
WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
) AS a
GROUP BY Username
Also try your original query with date addition
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
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 WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 ) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC
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