Please, help me with query:
SELECT
N.site_name,
Count(N.site_name),
SUM (IF((SC.type_conn = 'GET') && (SC.type_conn = 'CONNECT'),N.size_site,0)) as Traffic_IN,
SUM (IF(SC.type_conn = 'POST',N.size_site,0)) as Traffic_OUT
From
news N,
status_conn SC,
users U
where
N.id_conn = SC.id_conn and
N.id_user = U.id_user and
U.name_user = 'max' and
N.date_conn = '2015-08-04'
Group by
N.site_name
Order by
SUM (IF((SC.type_conn = 'GET') && (SC.type_conn = 'CONNECT'),N.size_site,0))
I don't know witch is the problem; in any case you could try this:
Select N.site_name,
Count(N.site_name),
SUM (IF((SC.type_conn = 'GET') && (SC.type_conn = 'CONNECT'),N.size_site,0)) as Traffic_IN,
SUM (IF(SC.type_conn = 'POST',N.size_site,0)) as Traffic_OUT
From news N, status_conn SC, users U
where N.id_conn = SC.id_conn
and N.id_user = U.id_user
and U.name_user = 'max'
and N.date_conn = '2015-08-04'
Group by N.site_name
Order by Traffic_IN
otherwise
select ...
...
Order by 3
You may very well have a parenthesis problem at the end of your query. Try
[...] ORDER BY SUM (IF((SC.type_conn = 'GET') && (SC.type_conn = 'CONNECT')),N.size_site,0)
Related
SELECT ST.Id,
ST.Label,
ST.Asset,
CASE
WHEN ST.RFID = '' THEN 'NULL'
ELSE ST.RFID
END AS RFID,
CASE
WHEN ST.Type = 'O' THEN 'Odometer'
ELSE 'Engine Hours'
END AS AccumulatorType,
ST.AppId,
ST.Timestamp,
ST.Accumulator,
CASE
WHEN ST.Flag = 0 THEN 'Disabled'
WHEN ST.Flag = 1 THEN 'Active'
WHEN ST.Flag = 3 THEN 'Rented'
WHEN ST.Flag = 4 THEN 'Bypass'
WHEN ST.Flag = 5 THEN 'Tanker'
END AS TYPE,
(SELECT COUNT(*)
FROM sync
WHERE RowId = ST.Id
AND DefinitionId = 1
AND Status = 1) AS Updated,
(SELECT COUNT(*)
FROM sync
WHERE RowId = ST.Id
AND DefinitionId = 1
AND Status = 0) AS Remaining
FROM SecondaryTags AS ST
WHERE AppId = #AppId
Why do you want to rewrite the logic? Because you have filtering in the outer query, the subquery is likely to be the most performant method for the calculation.
You can speed the subquery by being sure that you have the write indexes. In this case, you want an index on sync(RowId, DefinitionId, StatusId):
create index idx_sync_rowid_definitionid_statusid
on sync(RowId, DefinitionId, StatusId)
You can rewrite the query as:
select . . .,
s.updated, s.remaining
from SecondaryTags st join
(select rowid, sum(status = 1) as updated, sum(status = 0) as remaining
from sync s
where s.definitionId = 1
group by s.rowid
) s
on s.rowid = st.id
where st.AppId = #AppId;
From a performance perspective, though, I think the index is a better idea.
select st.*
, s.updated
, s.remaining
from SecondaryTags st
join (select rowid
, sum(status = 1) as updated
, sum(status = 0) as remaining
from sync s
where s.definitionId = 1
group
by s.rowid
) s
on s.rowid = st.id
where st.AppId = 2;
When I try this query:
SELECT *
FROM sds_posts
WHERE topic_id = '2439'
AND author = ANY (SELECT mid
FROM sds_actions
WHERE whoami = '710' AND type = 'block')
AND status = '1'
AND deleted = '0'
ORDER BY
id ASC
LIMIT 50
it is working correctly.
But I need this one:
SELECT *
FROM sds_posts
WHERE topic_id = '2439'
AND author <> ANY (SELECT mid
FROM sds_actions
WHERE whoami = '710' AND type = 'block')
AND status = '1'
AND deleted = '0'
ORDER BY
id ASC
LIMIT 50
This time query have to select opposite of first query, but it is just select all author. I tried != and also NOT IN, but result is same.
So why? Why does <> not work as expected?
I would think that changing
and author = any...
to
and NOT author = any...
would work... But if that does not, then I would try doing as a left-join and looking for null. Since the author is the "mid" from the sds_actions, I would write it as...
SELECT
sp.*
FROM
sds_posts sp
LEFT JOIN sds_actions sa
on sp.author = sa.mid
AND sa.whoami = '710'
AND sa.type = 'block'
WHERE
sp.topic_id = '2439'
AND sp.status = '1'
AND sp.deleted = '0'
AND sa.mid IS NULL
ORDER by
sp.id ASC
LIMIT 50
You can try change author = any(...) to author IN (...)
and change author <> any(...) to author NOT IN (...)
i just want to use LinqToSql classes query. here i just want to convert this sql query to appropriate linq query.
this is my sql query:
SELECT j.[JobID], p.[PreparedEmailID],
p.[Name] AS 'PreparedEmailName',
j.[CreatedOn], j.[CompletedOn],
j.[SubscriberCount], j.[EmailsSent],
(SELECT TOP 1 [Message] FROM
[LoggedMessages] WHERE [JobID] =
j.[JobID] ORDER BY [LoggedMessageID] DESC)
AS 'LoggedMessage' FROM [Jobs] AS j
INNER JOIN [PreparedEmails] AS p
ON p.[PreparedEmailID] =
j.[PreparedEmailID]
and my generated linq query is like:
var query = from j in db.Jobs
join p in db.PreparedEmails on j.PreparedEmailID equals p.PreparedEmailID
join l in db.LoggedMessages on j.JobID equals l.JobID into ej
from l in ej.DefaultIfEmpty() orderby l.LoggedMessageID descending
orderby l.LoggedMessageID descending
orderby j.CreatedOn descending
select new
{
JobID = j.JobID,
PreparedEmailID = p.PreparedEmailID,
PreparedEmailName = p.Name,
CreatedOn = j.CreatedOn,
CompletedOn = j.CompletedOn,
SubscriberCount = j.SubscriberCount,
EmailsSent = j.EmailsSent,
LoggedMsg = l.Message
};
I prepared some linQ query for you (but i didn't test it in VS because i have no access to it now, so please be careful because it can contain some errors):
var list = from Jobs
join PreparedEmails on Jobs.PreparedEmailID == PreparedEmails.PreparedEmailID
join LoggedMessages on LoggedMessages.JobID == Jobs.JobID
select
{
JobID = Jobs.JobID,
PreparedEmailID = PreparedEmails.PreparedEmailID,
PreparedEmailName = PreparedEmails.Name,
CreatedOn= Jobs.CreatedOn,
CompletedOn = Jobs.CompletedOn,
SubscriberCount = Jobs.SubscriberCount,
EmailsSent = Jobs.EmailsSent,
LoggedMessage = LoggedMessages.Message
} orderby descending LoggedMessages.LoggedMessageID;
It should help a little bit ...
this is solution:
var query = from j in db.Jobs
join p in db.PreparedEmails on j.PreparedEmailID equals p.PreparedEmailID
orderby j.CreatedOn descending
select new
{
JobID = j.JobID,
PreparedEmailID = p.PreparedEmailID,
PreparedEmailName = p.Name,
CreatedOn = j.CreatedOn,
CompletedOn = j.CompletedOn,
SubscriberCount = j.SubscriberCount,
EmailsSent = j.EmailsSent,
LoggedMsg = (from l in db.LoggedMessages
where j.JobID == l.JobID
orderby l.LoggedMessageID descending
select l.Message).FirstOrDefault()
};
I have checked former posts and none has solved my problem yet, any help would be appreciated
MYSQL query to 3 tables (Users, Match, Interview)
Want to return the users name and interview # (if there is an interview number) for the specified Match.
But I do not want to limit the results to only users who have an interview. I tried JOIN, but cannot get it to work for 3 tables.
Here is what I have so far in PHP:
$query = "SELECT CONCAT(u.first_name,' ',u.last_name,'----',COALESCE(i.v_code,'')) as name, u.id as id
FROM #__users as u
JOIN #__bl_match as m ON ( (u.team_id = m.team1_id) OR
(u.team_id = m.team2_id) OR
(u.team_id = m.team3_id AND m.team3_id != 0) OR
(u.team_id = m.team4_id AND m.team4_id != 0) OR
(u.team_id = m.team5_id AND m.team5_id != 0) OR
(u.team_id = m.team6_id AND m.team6_id != 0) OR
(u.team_id = m.team7_id AND m.team7_id != 0) OR
(u.team_id = m.team8_id AND m.team8_id != 0) OR
(u.team_id = m.team9_id AND m.team9_id != 0) OR
(u.team_id = m.team10_id AND m.team10_id != 0))
AND m.id = ".$t_id."AND m.id != 0
JOIN #__bl_interview as i ON i.u_id = u.id";
$db->setQuery($query);
$parti12 = $db->loadObjectList();
If you want Users with no Interview, you'll need to use a LEFT JOIN to the interview table, but that means the interview number for that user will be NULL.
Since you're concatenating the number with the user name in your SELECT list, this results in the user's name being returned as NULL when they've got no interviews (since concatenating a string with a NULL value - or performing pretty much any other operation on a NULL value - produces a NULL result).
To avoid this, you must handle the case when i.v_code is NULL. The typical way to do that in MySQL is with the COALESCE function:
SELECT CONCAT(u.first_name, ' ', u.last_name, '----', COALESCE(i.v_code,'')) as name,
u.id as id
FROM #__users as u
JOIN #__bl_match as m ON ( (u.team_id = m.team1_id) OR
(u.team_id = m.team2_id) OR
(u.team_id = m.team3_id AND m.team3_id != 0) OR
(u.team_id = m.team4_id AND m.team4_id != 0) OR
(u.team_id = m.team5_id AND m.team5_id != 0) OR
(u.team_id = m.team6_id AND m.team6_id != 0) OR
(u.team_id = m.team7_id AND m.team7_id != 0) OR
(u.team_id = m.team8_id AND m.team8_id != 0) OR
(u.team_id = m.team9_id AND m.team9_id != 0) OR
(u.team_id = m.team10_id AND m.team10_id != 0))
AND m.id = ".$t_id."AND m.id != 0
LEFT JOIN #__bl_interview as i ON i.u_id = u.id"
Now, given the formatting you're applying to the name, you may be left with an extraneous ---- after the name with no subsequent interview code. If you'd like to avoid that, we can use IF instead of COALESCE to get a little smarter with our output, like so:
SELECT CONCAT(u.first_name, ' ', u.last_name, IF(i.v_code, '---- ' + i.v_code, '')) as name
Note what that's doing: if i.v_code is not NULL, the first expression in the IF evaluates to true, so the second expression is returned. Otherwise, the third expression (the empty string) is returned - so when i.v_code is NULL, you only output the user's name.
Morning i would like to include a group by in this linq statement, Some records i have being brought back have multiple entries so i need to group them by the productAsin. so i dont have duplicates in my table.
var query = from a in dc.aProducts
join t in dc.tProducts on a.sku equals t.sku
join lp in dc.LowestPrices on a.asin equals lp.productAsin
orderby t.title
select new GetLowestPrices
{
productAsin = lp.productAsin,
sku = t.sku,
title = t.title,
tweprice = Convert.ToString(t.twePrice),
lowprice = Convert.ToString(lp.price),
amzprice = Convert.ToString(lp.tweAmzPrice),
lastupdated = Convert.ToDateTime(lp.priceDate)
};
return query.ToList();
many thanks in advance.
Use GroupBy Extension method:
var query = from a in dc.aProducts
join t in dc.tProducts on a.sku equals t.sku
join lp in dc.LowestPrices on a.asin equals lp.productAsin
orderby t.title
select new GetLowestPrices
{
productAsin = lp.productAsin,
sku = t.sku,
title = t.title,
tweprice = Convert.ToString(t.twePrice),
lowprice = Convert.ToString(lp.price),
amzprice = Convert.ToString(lp.tweAmzPrice),
lastupdated = Convert.ToDateTime(lp.priceDate)
};
var groupedData = query.GroupBy(d => d.productAsin);
return groupedData;
After that you can access that group values and put some aggregate function on them as below:
var groupedData = query.GroupBy(d => d.productAsin).Select( data => new {
productAsin = data.Key,
LowPriceSum = data.Sum(s => Convert.ToInt32(s.price))
}).ToList();
Ref:
group clause (C# Reference)
How to Use LINQ GroupBy