I have been using the following export query (CTE) for a few years but a client has requested that we do not give them all of the data from the 'C' line as they do not need some of the data. I have all of the PlaintiffNames that they do not need but I have no idea how to adjust the query to exclude the PlaintiffNames they do not need. And using NOT IN as is shown below has not worked and I am guessing I am missing something!
SELECT 4 AS grpOrd
, null AS posInGrp
, 'C'
, A.CaseNumberKey
, 'C|' + IsNull(J.CType, '') + '|' + IsNull(J.plaintiffName,'') + '|' + IsNull(J.plaintiffAdd1, '') + '|' + IsNull(J.plaintiffCity, '') + '|' + IsNull(J.plaintiffState, '') + '|' + IsNull(J.plaintiffZip, '') + '|' + '|' + IsNull(J.defendantName, '') + '|' + IsNull(J.defendantAdd1, '') + '|' + IsNull(J.defCity, '') + '|' + IsNull(J.defState, '') + '|' + IsNull(J.defZip, '') + '|' + '|' + IsNull(J.Court, '') + '|' + IsNull(J.CaseID, '') + '|' + IsNull(J.JAmt, '') + '|' + IsNull(replace(convert(VarChar(10), JDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(VARCHAR(10), revivedDate, 101), '/', ''), '') AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN Acme.new_judgment_system.dbo.selected_compiled_clean J
ON J.CaseNumber = A.CaseNumberKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd
AND ClientKey = 2
AND (J.plaintiffName NOT IN (SELECT Plaintiff FROM dbo.excluded_Plaintiffs))
I am getting invalid object name dbo.excluded_Plaintiffs when I try to run this. Note that is the very bottom of the CTE which has numerous lines but the issue is solely in this last line.
An example of a common plaintiff that they do not want is 'PHILA TRAFFIC COURT'
Is it possible to adjust the above to check the table (excluded_Plaintiffs) and exclude that data? I have tried to no avail and am hoping someone has the answer.
Well I answered my own question. I missed the database in this line dbo.excluded_Plaintiffs. Should be written newCityCollection.dbo.excluded_Plaintiffs.
Related
I have viewed some of the related question but the solutions on them are bit older. Most of them are using EntityManager. I have written the following native query in JpaRepository and when I get geojson as a String I'm getting the error mentioned in the title.
Here is my query
#Query(value = "SELECT\n"
+ " json_build_object(\n"
+ " 'type', 'FeatureCollection',\n"
+ " 'features', json_agg(\n"
+ " json_build_object(\n"
+ " 'type', 'Feature',\n"
+ " 'geometry', ST_AsGeoJSON(a.check_in_geom)\\:\\:json,\n"
+ " 'properties', json_build_object(\n"
+ " 'username', a.username,\n"
+ " 'users', (\n"
+ " -- Generate json array of \"users\"\n"
+ " SELECT array_to_json(array_agg(u.*)) \n"
+ " FROM users u \n"
+ " WHERE u.username = a.username\n"
+ " GROUP BY u.username\n"
+ " )\n"
+ " )\n"
+ " )\n"
+ " )\n"
+ " ) Json\n"
+ "FROM\n"
+ " attendances a, users u\n"
+ " WHERE a.username = u.username AND u.designation = 'Manager' AND date_trunc('day', a.created_at)\\:\\:DATE = '2022-04-04'"
+ " ;", nativeQuery = true)
String getAttendanceGeoJsonByDesignationAndDate(String designation, String dateStr);
For now I'm passing designation and date as static values.
When I run this query on Postgresql it runs successfully and return the expected geojson. But spring boot does not allow the result to be as String or it throws exception while running the query.
Please let me know how can I fix it. Also if I need to provide more detail, do let me know.
I have code :
CourseName + ' ' + isnull(GradeLevel,'')) as CourseName
The result :
Permit Working High 3
How to make result like this :
Permit Working High (3)
CourseName + ' ' +
CASE
WHEN GradeLevel IS NULL THEN ''
ELSE '(' + GradeLevel + ')'
END
AS CourseName
I have a query/view that is created to be used in a report. When I run in the SMS and in SSRS it run fine. But when I connect the view to the tool that generates our reports it throws the following error. Incorrect syntax near '.8'. When I contact the support for this product they say it has to do with how we calculate the 8thgradyear . I have placed the code below. Any suggestions.
SELECT
dbo.studemo.suniq,
dbo.studemo.ident,
dbo.studemo.lastname,
dbo.studemo.firstname,
dbo.studemo.emailaddr AS stuemail,
dbo.studemo.birthdate,
dbo.track.schoolc,
dbo.school.schname,
dbo.stustat.graden,
dbo.stustat.edate,
dbo.zstustat.descript AS status,
RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname AS advisor,
dbo.track.schyear,
SUM(8) - dbo.stustat.graden + dbo.track.schyear AS [8thgradyear],
sf.Email, LOWER(sf.Username) AS [user],
LOWER(RIGHT(SUM(8) - dbo.stustat.graden + dbo.track.schyear, 2) + LEFT(dbo.studemo.firstname, 1) + REPLACE(REPLACE(REPLACE(dbo.studemo.lastname, '-', ''), ' ', ''), '''', '') + RIGHT(dbo.studemo.ident, 3)) AS newuser,
CONVERT(varchar(8), dbo.studemo.birthdate,1) AS password,
'STUDENTS' + '/' + (CASE WHEN track.schoolc IN ('19', '43', '17', '23') THEN 'Middle' ELSE 'Elementary' END) + '/' + dbo.school.schname AS neworg,
sf.OU, sf.LastLoginTime
FROM dbo.studemo INNER JOIN
dbo.stustat ON dbo.studemo.suniq = dbo.stustat.suniq INNER JOIN
dbo.track ON dbo.stustat.trkuniq = dbo.track.trkuniq INNER JOIN
dbo.zstustat ON dbo.stustat.stustatc = dbo.zstustat.stustatc INNER JOIN
dbo.facdemo ON dbo.stustat.funiq = dbo.facdemo.funiq LEFT OUTER JOIN
dbo.vw_google_OU AS sf ON sf.Firstname = dbo.studemo.firstname AND sf.Lastname = dbo.studemo.lastname INNER JOIN
dbo.school ON dbo.school.schoolc = dbo.track.schoolc
WHERE (dbo.stustat.stustatc IN
(SELECT stustatc
FROM dbo.zstustat AS zstustat_1
WHERE (snstatus IN ('A', 'M', 'P')))) AND (dbo.stustat.xdate IS NULL OR
dbo.stustat.xdate < dbo.stustat.edate) AND (dbo.track.schoolc NOT IN ('P34', 'P24', '802', '801'))
GROUP BY dbo.studemo.suniq, dbo.studemo.ident, dbo.studemo.lastname, dbo.studemo.firstname, dbo.studemo.birthdate, RIGHT(dbo.studemo.ident, 3), dbo.track.schoolc,
dbo.stustat.graden, dbo.zstustat.descript, RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname, dbo.stustat.edate, dbo.studemo.gradyear, dbo.track.schyear,
sf.Email, CONVERT(varchar(8), dbo.studemo.birthdate, 1), sf.Username, dbo.school.schname, sf.OU, dbo.studemo.emailaddr, sf.LastLoginTime
SUM function was introduced in SQL Server 2008 and your tool must be using and older version of SQL Server.
I have a query that looks basically like this, wrapped in a dynamic query to accommodate table names that can change. I got the date functions in the middle converted but it doesn't like the LoadedDateTime and CallPlacedTime at the end. I've tried every conversion and combination of quoting those lines that I can think of. How can I accomplish this?
DECLARE #sql_TotalDialsNewLeads nvarchar(1000) = N'
SELECT COUNT(*)
FROM ' + #tbl_CH + ' ch, ' + #tbl_CL + ' cl, ' + #tbl_DA + ' da
WHERE ch.IDENTITY = cl.IDENTITY
AND cl.CRMID = da.CRMID
AND CallPlacedTime BETWEEN ''' + CONVERT(varchar(30),DATEADD(HOUR,-#TimezoneOffset,#StartDate),126) + '''
AND ''' + CONVERT(varchar(30),DATEADD(HOUR,-#TimezoneOffset,#EndDate),126) + '''
AND Product = ''' + #Product + '''
AND Country = ''' + #Country + '''
AND DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,LoadedDateTime))
= DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,CallPlacedTime))'
EXECUTE(#sql_TotalDialsNewLeads);
Thanks,
Sean
Try to use sp_executesql with parameters instead of making string to execute manually:
...
AND CallPlacedTime BETWEEN DATEADD(HOUR,-#TimezoneOffset,#StartDate)
AND DATEADD(HOUR,-#TimezoneOffset,#EndDate)
...
and execute it with the following statement:
exec sp_executesql #sql_TotalDialsNewLeads,
N'#StartDate datetime, #EndDate datetime',
#StartDate,#EndDate;
Here is a MSDN guide. Also you can do it for other parameters in your SQL statement.
Instead of using the conversion outside use it inside the dynamic query.
something like this. (Not tested)
DECLARE #sql_TotalDialsNewLeads nvarchar(1000) = N'
SELECT COUNT(*)
FROM ' + #tbl_CH + ' ch, ' + #tbl_CL + ' cl, ' + #tbl_DA + ' da
WHERE ch.IDENTITY = cl.IDENTITY
AND cl.CRMID = da.CRMID
AND CallPlacedTime BETWEEN CONVERT(varchar(30),DATEADD(HOUR,'''+ -#TimezoneOffset +''','''+ #StartDate +'''),126)
AND CONVERT(varchar(30),DATEADD(HOUR,'''+ -#TimezoneOffset +''','''+ #EndDate +'''),126)
AND Product = ''' + #Product + '''
AND Country = ''' + #Country + '''
AND DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,LoadedDateTime))
= DATEPART(DayOfYear,DATEADD(HOUR,-#TimezoneOffset,CallPlacedTime))'
EXECUTE(#sql_TotalDialsNewLeads);
Trying to do something like this:
select 'Setup for Car ' + CAST(varchar(50), #CarID) + ' for user ' + CAST(varchar(5), #UserID)
doesn't work, not sure what the syntax would be then for T-SQL. I've hunted around, have not found it yet.
You can user either of these:
select 'Setup for Car ' + Convert(varchar(50), #CarID) + ' for user ' + Convert(varchar(5), #UserID)
OR
select 'Setup for Car ' + CAST(#CarID as varchar(50)) + ' for user ' + CAST(#UserID as varchar(5))
You are using CAST wrong. It should be
CAST(#Variable AS WhatYouAreCastingTo)
Here is a fiddle showing this
Remember, MSDN is your friend :)