I get the following error:
System.Data.OleDb.OleDbException (0x80040E14): Undefined function 'STR_TO_DATE' in expression.
when I run this query
SELECT
ProductCode,
Description,
SUM(Quantity) as Quantity,
STR_TO_DATE(REPLACE(DateIn, '-', '/'), '%m/%d/%Y') as DateIn
FROM
Product as P,
StockIn as S
WHERE
S.ProductNo = P.ProductNo
AND
STR_TO_DATE(REPLACE(DateIn, '-', '/'), '%m/%d/%Y') BETWEEN '" + StartDate.ToString("yyyy-MM-dd") + "' AND '" + EndDate.ToString("yyyy-MM-dd") + "' GROUP BY P.ProductNo, DateIN ORDER BY DateIn, Description";
Try with Access SQL:
"SELECT
ProductCode,
Description,
SUM(Quantity) as Quantity,
DateValue(DateIn) as DateIn
FROM
Product as P,
StockIn as S
WHERE
S.ProductNo = P.ProductNo
AND
DateValue(DateIn) BETWEEN #" + StartDate.ToString("yyyy-MM-dd") + "# AND #" + EndDate.ToString("yyyy-MM-dd") + "# GROUP BY P.ProductNo, DateValue(DateIn) ORDER BY DateValue(DateIn), Description";
Related
qClient.SQL.Clear;
qClient.SQL.Text := 'Select C.Lastname,C.Firstname,C.Clino,C.Address,' +
'C.City,C.State,C.Zip,C.Phone,C.HerCell,C.HisCell,' +
'C.Texting,C.Since,C.Selected,' +
'SUM( I.Charges - I.Discount ) as Revenue ' +
'from Client C join Invoice I ' +
'on I.Clino = C.Clino ' +
'where I.Date between :S and :E ' +
'and I.Inv = true ' +
'group by C.Clino ' +
'order by Revenue desc ';
qClient.ParamByName('S').AsDate := dStart;
qClient.ParamByName('E').AsDate := dEnd;
qClient.DetailFields := 'Clino';
qClient.Open();
How can I modify the above code so that the query includes only 'Revenue' values over a minimum (like maybe 1000)? It currently includes all clients.
Thanks,
Spyke
You need a HAVING clause:
qClient.SQL.Text := 'Select C.Lastname,C.Firstname,C.Clino,C.Address,' +
'C.City,C.State,C.Zip,C.Phone,C.HerCell,C.HisCell,' +
'C.Texting,C.Since,C.Selected,' +
'SUM( I.Charges - I.Discount ) as Revenue ' +
'from Client C join Invoice I ' +
'on I.Clino = C.Clino ' +
'where I.Date between :S and :E ' +
'and I.Inv = true ' +
'group by C.Clino ' +
'having Revenue > 1000 ' +
'order by Revenue desc ';
The HAVING clause applies conditions and filters the results of the query after the aggregation.
In this case Revenue is an aggregated column and could not be used in a WHERE clause which also filters a query, because the conditions of the WHERE clause are applied before the aggregation.
I am having difficulty formatting a string to be used with SP_executesql. I keep getting the following error:
Msg 102, Level 15, State 1, Line 144
Incorrect syntax near '))'
Line 144 is the last line of this code
All variables were declared above
set #Sql = #Sql + ' select distinct provider_id, ' + #changestring + ', null,'''
+ 'case when ' + #changestring + '= 6 then ds.excldate +
else +
CONVERT(varchar(20),' + GETDATE() + ') end as '+ '''Effective_Date'
+ ''', null,1,' + #username + '''
+ ,NULL,' + CONVERT(varchar(20), GETDATE()) + ''',''' + CONVERT(varchar(20), GETDATE()) + ''', '''+ #username + '''' +
' from (
select distinct provider_id
FROM provider pv WHERE provider_id In (select Provider_id from dbo.##sanctiontermswid )) ds
where ds.provider_id not in (
select provider_id from provider_status ps where ps.provider_id in (
select distinct provider_id
FROM Provider pv WHERE provider_id IN (select Provider_ID from ##sanctiontermswid ) and ps.provider_status_type_id = ' + #changestring + '))'
It looks like you are forgetting to convert the first GETDATE() to varchar.
Was able to finally format my SQL. The problem was in formatting the case statement.
set #Sql = #Sql + 'select distinct DS.provider_id, ' + #changestring + ', null, case when ''' + #changeString + '''!= ''6'' then '''
+ CONVERT(varchar(20), GETDATE()) + ' '' else CONVERT(DATE,DS.exCldate) end as ''Effective_date'''
+ ', null,1,'''+ #username + '''
,NULL,''' + CONVERT(varchar(20), GETDATE()) + ''',''' + CONVERT(varchar(20), GETDATE()) + ''', '''+ #username + '''' +
' from (
select distinct PV.provider_id, T1.EXCLDATE
FROM provider pv
JOIN dbo.##tmpSanctions T1 ON PV.PROVIDER_ID=T1.PROVIDER_id
) ds
where ds.provider_id not in (
select provider_id from provider_status ps where ps.provider_id in (
select distinct provider_id
FROM Provider pv WHERE provider_id IN (select Provider_ID from dbo.##tmpSanctions ) and ps.provider_status_type_id= '+ #changestring + ' and ps.effective_thru is null))'
select saleid, orderno, orderdate,
sum(purchaseprice+purchaseshipping+paypalfee+storefee) as totalcost,
customerpaid as totalrevenue,
(customerpaid - sum(purchaseprice+purchaseshipping+paypalfee+storefee)) as profit,
ROUND((((customerpaid - sum(purchaseprice+purchaseshipping+paypalfee+storefee)) / customerpaid) * 100.00),2) as profitmargin
from tblsales
group by orderno having " . $having . "
order by $sort $order limit $offset,$rows
This query works fine. Is there a way to add running total profit field to this query that performs a running sum of profit already calculated in the query?
Just put it in a subquery and use variables:
select t.*,
(#cumesum := #cumesum + profit) as runningprofit
from (select saleid, orderno, orderdate,
sum(purchaseprice+purchaseshipping+paypalfee+storefee) as totalcost,
customerpaid as totalrevenue,
(customerpaid - sum(purchaseprice+purchaseshipping+paypalfee+storefee)) as profit,
ROUND((((customerpaid - sum(purchaseprice+purchaseshipping+paypalfee+storefee)) / customerpaid) * 100.00),2) as profitmargin
from tblsales
group by orderno
having " . $having . "
) t cross join
(select #cumesum := 0) vars
order by $sort $order
limit $offset, $rows;
I have one problem with this query; I can't seem to get ((total + rec_host) / 2) AS total2 to work. How would I go about this procedure without doing:
((((rank_ur + rank_scs + rank_tsk + rank_csb + rank_vfm + rank_orr) / 6) + rec_host ) / 2)
Here's my Query:
SELECT host_name,
SUM(rank_ur) AS cnt1,
SUM(rank_scs) AS cnt2,
SUM(rank_tsk) AS cnt3,
SUM(rank_csb) AS cnt4,
SUM(rank_vfm) AS cnt5,
SUM(rank_orr) AS cnt6,
SUM(IF(rec_host = 1,1,0)) AS rh1,
SUM(IF(rec_host = 0,1,0)) AS rh2,
((rank_ur + rank_scs + rank_tsk + rank_csb + rank_vfm + rank_orr) / 6) AS total,
((total + rec_host) / 2) AS total2
FROM lhr_reviews
GROUP BY host_name
ORDER BY total
DESC LIMIT 0,10
Use a subquery like so:
SELECT
host_name,
cnt1,
cnt2,
cnt3,
cnt4,
cnt5,
cnt6,
rh1,
rh2,
total,
((total + rec_host) / 2) AS total2
FROM
(
SELECT host_name,
rec_host,
SUM(rank_ur) AS cnt1,
SUM(rank_scs) AS cnt2,
SUM(rank_tsk) AS cnt3,
SUM(rank_csb) AS cnt4,
SUM(rank_vfm) AS cnt5,
SUM(rank_orr) AS cnt6,
SUM(IF(rec_host = 1,1,0)) AS rh1,
SUM(IF(rec_host = 0,1,0)) AS rh2,
((rank_ur + rank_scs + rank_tsk +
rank_csb + rank_vfm + rank_orr
) / 6) AS total
FROM lhr_reviews
GROUP BY host_name, rec_host
) t
ORDER BY total
DESC LIMIT 0,10;
What you could do is this:
select x.*, ((x.total + rec_host) / 2) AS total2
from (
SELECT host_name, rec_host,
SUM(rank_ur) AS cnt1,
SUM(rank_scs) AS cnt2,
SUM(rank_tsk) AS cnt3,
SUM(rank_csb) AS cnt4,
SUM(rank_vfm) AS cnt5,
SUM(rank_orr) AS cnt6,
SUM(IF(rec_host = 1,1,0)) AS rh1,
SUM(IF(rec_host = 0,1,0)) AS rh2,
((rank_ur + rank_scs + rank_tsk + rank_csb + rank_vfm + rank_orr) / 6) AS total
FROM lhr_reviews
GROUP BY host_name
ORDER BY total
DESC LIMIT 0,10
) as x
;
You cannot use the column as an alias when the alias and other column are in the same level of SELECT. So you can use a derived query which lets you basically rename your columns and/or name any computed columns.Check on Rubens Farias and Rob Van Dam answer here
PS: will search for a better article to update the answer :)
SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%'
AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
This query will fetch all the application id that begin with 1...i need to exclude 101....from result...how can i modify the query
Simply add a AND.
SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%'
AND application_id <> 101
AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%' AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
AND application_id <> '101'