I have this code to convert over to CR. I am having the most issue with converting #ReportType over to a formula and parameter to CR. Any suggestions of what to do? I am stumped and no support at work.
select i.IncidentTypeDescription,
isnull(sum(datediff(second, i.IncidentStartedDateTime, i.IncidentDate)), 0) as avgrectoinit,
isnull(sum(datediff(second, i.IncidentDate, i.FirstUnitDispatchedTime)), 0) as avginittodisp,
isnull(abs(sum(datediff(second, i.FirstUnitDispatchedTime, i.FirstUnitArrivedTime))), 0) as avgdisptoarrive,
isnull(sum(datediff(second, i.FirstUnitArrivedTime, i.RouteClosedTime)), 0) as avgarrivetoclose,
count(*) as reccount
FROM MV_Incident i
WHERE i.IncidentNumber LIKE N'bvpd%'
AND i.ReportNumber LIKE N'bvpd%'
and i.IncidentStartedDateTime >= #StartDate
and i.IncidentStartedDateTime < dateadd(day, 1, #EndDate)
and (
(#ReportType = 'All') and (i.CallSource = i.CallSource or i.CallSource is null) or
(#ReportType = 'Officer' and (i.CallSource in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated'))) or
(#ReportType = 'NonOfficer' and (i.CallSource not in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated') or i.CallSource is NULL))
)
group by i.IncidentTypeDescription
order by i.IncidentTypeDescription
You can use the query as it is in crystal reports using the command option while creating a database connection.
Second option would be, USe the below query and create parameters and write your conditions in record selection formula which will add where clause to crystal reports.
select i.IncidentTypeDescription, i.IncidentNumber ,i.ReportNumber ,i.CallSource ,
isnull(sum(datediff(second, i.IncidentStartedDateTime, i.IncidentDate)), 0) as avgrectoinit,
isnull(sum(datediff(second, i.IncidentDate, i.FirstUnitDispatchedTime)), 0) as avginittodisp,
isnull(abs(sum(datediff(second, i.FirstUnitDispatchedTime, i.FirstUnitArrivedTime))), 0) as avgdisptoarrive,
isnull(sum(datediff(second, i.FirstUnitArrivedTime, i.RouteClosedTime)), 0) as avgarrivetoclose,
count(*) as reccount
FROM MV_Incident i
group by i.IncidentTypeDescription,i.IncidentNumber ,i.ReportNumber ,i.CallSource
order by i.IncidentTypeDescription,i.IncidentNumber ,i.ReportNumber ,i.CallSource
Now to crystal report design and create two parameters Start date and End Date and write your where condition as it is in record selection formula as:
i.IncidentNumber LIKE N'bvpd%'
AND i.ReportNumber LIKE N'bvpd%'
and i.IncidentStartedDateTime >= #StartDate
and i.IncidentStartedDateTime < dateadd(day, 1, #EndDate)
and (
(#ReportType = 'All') and (i.CallSource = i.CallSource or i.CallSource is null) or
(#ReportType = 'Officer' and (i.CallSource in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated'))) or
(#ReportType = 'NonOfficer' and (i.CallSource not in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated') or i.CallSource is NULL))
)
Don't forget to create reporttype formula.
Let me know incase any issue
Related
I am unsure how to rewrite this. I understand the issue or at least I think I do. I am unable to use instances of t before it is defined by the follow "AS" statement. How can I resolve this so that so that it works the same way without using the instance of t before definition?
The error that I am getting thrown from the converter is "unable to convert .t"
Converted code
INSERT INTO t$trades_long (portfolio_name, fund, cusip, td_num, desc_instmt, trd_price, trd_trade_dt, t.trd_settle_dt, trd_counterparty, trd_td_par, tran_type, sec_type, trd_type, t.trd_trader)
SELECT
p.portfolio_name, t.fund, t.cusip, t.td_num, a.desc_instmt, t.trd_price, t.trd_trade_dt, t.trd_settle_dt, t.trd_counterparty, t.trd_td_par, t.tran_type, a.sec_type, 'long' AS trd_type, trd_trader
FROM pfi_pilot.br_transaction AS t, pfi_pilot.br_portfolio_group AS p, pfi_pilot.br_asset AS a
WHERE t.fund = p.fund AND p.portfolio_group = 'ALL_FUNDS' AND t.cusip = a.cusip AND t.tran_type IN ('BUY', 'SELL', 'ISSUE', 'ALLOC') AND t.trd_status <> 'C' AND a.sm_sec_group NOT IN ('CASH', 'FUTURE', 'FX', 'FUND', 'SWAP', 'OPTION') AND a.sec_type != 'MBS_TBA' AND t.trd_counterparty NOT IN ('IFUND', 'GHOST', 'SPO', 'SPOBO', 'CONV', 'ASSGN') AND t.trd_trade_dt >= par_from_dt AND t.trd_trade_dt <= par_to_dt AND t.cusip NOT IN (SELECT DISTINCT
cusip
FROM pfi_pilot.br_asset
WHERE (COALESCE(risk_country, country) = 'US' AND sm_sec_type IN ('GOVT', 'TBILL'))) AND t.trd_td_par > 0;
Original code
insert into #trades_long(portfolio_name, fund, cusip, td_num, desc_instmt, trd_price, trd_trade_dt, t.trd_settle_dt,
trd_counterparty, trd_td_par, tran_type , sec_type, trd_type, t.trd_trader)
select p.portfolio_name, t.fund, t.cusip, t.td_num, a.desc_instmt, t.trd_price, t.trd_trade_dt, t.trd_settle_dt,
t.trd_counterparty, t.trd_td_par, t.tran_type, a.sec_type,'long' as trd_type, trd_trader
from br_transaction t, br_portfolio_group p, br_asset a
where t.fund = p.fund
and p.portfolio_group = 'ALL_FUNDS'
and t.cusip = a.cusip
and t.tran_type in ('BUY','SELL','ISSUE','ALLOC')
and t.trd_status <> 'C'
and a.sm_sec_group not in ('CASH','FUTURE','FX','FUND','SWAP','OPTION')
and a.sec_type != 'MBS_TBA'
and t.trd_counterparty not in ('IFUND','GHOST','SPO','SPOBO','CONV','ASSGN')
and t.trd_trade_dt >= #from_dt
and t.trd_trade_dt <= #to_dt
and t.cusip not in (select distinct cusip from br_asset where (isnull(risk_country,country) = 'US'
and sm_sec_type in ('GOVT','TBILL')))
and t.trd_td_par > 0
How to convert SQL query to TYPO3 query builder.
SELECT * FROM tableName ORDER BY (CASE WHEN DATE(dateColumn) < DATE(GETDATE()) THEN 1 ELSE 0
END) DESC, dateColumn ASC
enter link description here
Same functionality i need in typo3 query builder.
To get the sql query you posted, you can do it like following:
// little helper function to debug querybuilder,
// queries with parameter placeholders
function debugQuery(QueryBuilder $builder)
{
$preparedStatement = $builder->getSQL();
$parameters = $builder->getParameters();
$stringParams = [];
foreach ($parameters as $key => $parameter) {
$stringParams[':' . $key] = $parameter;
}
return strtr($preparedStatement, $stringParams);
}
// get querybuilder
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName('Default')
->createQueryBuilder();
// build query
$queryBuilder
->select('*')
->from('table')
->getConcreteQueryBuilder()->orderBy(
'(CASE WHEN DATE('
.$queryBuilder->quoteIdentifier('dateColumn')
.') < DATE(GETDATE()) THEN 1 ELSE 0 END)',
'DESC'
)
;
$queryBuilder->addOrderBy('dateColumn', 'ASC');
// build query
$sql = debugQuery($queryBuilder);
The generates following sql query:
SELECT
FROM `table`
ORDER BY
(CASE WHEN DATE(`dateColumn`) < DATE(GETDATE()) THEN 1 ELSE 0 END) DESC,
`dateColumn` ASC
Some note beside:
To my knowlage GETDATE() is not a valid mysql method, more MSSQL. Eventually you want CURRENT_DATE() instead.
edit #1
Not tested/run .. just created the sql string to match what you provided. So don't blame me if provided sql query is wrong itself.
(SQL SERVER 2005)
I have a table of multiple products that relate to an ItemCode. I can establish the best saving using the query below (I think) but what I need to include are the RRP and SellingPrice fields for the combination that provides the best saving.
Apologies in advance this is probably a common issue but I can't find a solution that fits.
SELECT ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
FROM ItemCodePricingDetail
WHERE ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y')
AND (RRP > 0) AND ([SellingPrice] > 0)
GROUP BY ItemCode
Many Thanks
select * from ItemCodePricingDetail
JOIN
(
SELECT ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
FROM ItemCodePricingDetail
WHERE ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y')
AND (RRP > 0) AND ([SellingPrice] > 0)
GROUP BY ItemCode
) as t1 on ItemCodePricingDetail.ItemCode=t1.ItemCode
and RRP - [SellingPrice]= t1.BestSaving
I wish to write a Query for SAP B1 (t-sql) that will list all Income and Expenses Items by total and month by month.
I have successfully written a Query using PIVOT, but I do not want the column headings to be hardcoded like: Jan-11, Feb-11, Mar-11 ... Dec-11.
Rather I want the column headings to be parametrically generated, so that if I input:
--------------------------------------
Query - Selection Criteria
--------------------------------------
Posting Date greater or equal 01.09.10
Posting Date smaller or equal 31.08.11
[OK] [Cancel]
the Query will generate the following columns:
Sep-10, Oct-10, Nov-10, ..... Aug-11
I guess DYNAMIC PIVOT can do the trick.
So, I modified one SQL obtained from another forum to suit my purpose, but it does not work. The error message I get is Incorrect Syntax near 20100901.
Could anybody help me locate my error?
Note: In SAP B1, '[%1]' is an input variable
Here's my query:
/*Section 1*/
DECLARE #listCol VARCHAR(2000)
DECLARE #query VARCHAR(4000)
-------------------------------------
/*Section 2*/
SELECT #listCol =
STUFF(
( SELECT DISTINCT '],[' + CONVERT(VARCHAR, MONTH(T0.RefDate), 102)
FROM JDT1
FOR XML PATH(''))
, 1, 2, '') + ']'
------------------------------------
/*Section 3*/
SET #query = '
SELECT * FROM
(
SELECT
T0.Account,
T1.GroupMask,
T1.AcctName,
MONTH(T0.RefDate) as [Month],
(T0.Debit - T0.Credit) as [Amount]
FROM dbo.JDT1 T0
JOIN dbo.OACT T1 ON T0.Account = T1.AcctCode
WHERE
T1.GroupMask IN (4,5,6,7) AND
T0.[Refdate] >= '[%1]' AND
T0.[Refdate] <= '[%2]'
) S
PIVOT
(
Sum(Amount)
FOR [Month] IN ('+#listCol+')
) AS pvt
'
--------------------------------------------
/*Section 4*/
EXECUTE (#query)
I don't know SAP, but a couple of things spring to mind:
It looks like you want #listCol to contain a collection of numbers within square brackets, for example [07],[08],[09].... However, your code appears not to put a [ at the start of this string.
Try replacing the lines
T0.[Refdate] >= '[%1]' AND
T0.[Refdate] <= '[%2]'
with
T0.[Refdate] >= ''[%1]'' AND
T0.[Refdate] <= ''[%2]''
(I also added a space before the AND in the first of these two lines while I was editing your question.)
I'm trying to convert this to something sqlalchemy expression
language compatible, I don't know if it's possible out of box and
are hoping someone more experienced can help me along. The backend
is PostgreSQL and if I can't make it as an expression I'll create
a string instead::
SELECT
DISTINCT date_trunc('month', x.x) as date,
COALESCE(b.res1, 0) AS res1,
COALESCE(b.res2, 0) AS res2
FROM
generate_series(
date_trunc('year', now() - interval '1 years'),
date_trunc('year', now() + interval '1 years'),
interval '1 months'
) AS x
LEFT OUTER JOIN(
SELECT
date_trunc('month', access_datetime) AS when,
count(NULLIF(resource_id != 1, TRUE)) AS res1,
count(NULLIF(resource_id != 2, TRUE)) AS res2
FROM tracking_entries
GROUP BY
date_trunc('month', access_datetime)
) AS b
ON (date_trunc('month', x.x) = b.when)
First of all I got a class TrackingEntry mapped to tracking_entries,
the select statement within the outer joined can be converted to something
like (pseudocode)::
from sqlalchemy.sql import func, select
from datetime import datetime, timedelta
stmt = select([
func.date_trunc('month', TrackingEntry.resource_id).label('when'),
func.count(func.nullif(TrackingEntry.resource_id != 1, True)).label('res1'),
func.count(func.nullif(TrackingEntry.resource_id != 2, True)).label('res2')
],
group_by=[func.date_trunc('month', TrackingEntry.access_datetime), ])
Considering the outer select statement I have no idea how to build it, my guess is
something like::
outer = select([
func.distinct(func.date_trunc('month', ?)).label('date'),
func.coalesce(?.res1, 0).label('res1'),
func.coalesce(?.res2, 0).label('res2')
],
from_obj=[
func.generate_series(
datetime.now(),
datetime.now() + timedelta(days=365),
timedelta(days=1)
).label(x)
])
Then I suppose I have to link those statements together without using
foreign keys::
outer.outerjoin(stmt???).??(func.date_trunc('month', ?.?), ?.when)
Anyone got any suggestions or even better a solution?
http://pastie.org/994367
not a full answer but when you want to join select() objects together they have a ".c." attribute, they effectively gain the same interface as the Table itself. so
s1 = select(...)
s2 = select(...)
s3 = select([s1,s2]).select_from(s1.join(s2, s1.c.foo==s2.c.bar))
The same applies for functions
select ([func.foo(s1.c.x)]).select_from(s1.join(s2, ...))