Find any hard coded values in SQL Server - select

I need to identify any objects in a database containing "scenarioID" that has hard coded value.
I am looking to identify following cases:
scenarioId = XX (two digit value)
scenarioId=XX (two digit value)
scenarioId= XX (two digit value)
Below query I wrote seems to be pulling objects containing "scenarioid", but is giving me more than above cases.
SELECT DISTINCT a.[name]
FROM sysobjects a
INNER JOIN syscomments b on a.id = b.id
WHERE b.[text] LIKE '%scenarioId = __[^0-9]%'

I realized my mistake. I was using [^0-9] instead of using [0-9].
SELECT DISTINCT a.[name]
FROM sysobjects a
INNER JOIN syscomments b on a.id = b.id
WHERE b.[text] LIKE '%scenarioId = [0-9][0-9]%'
or b.[text] like '%scenarioId=[0-9][0-9]%'
or b.[text] like '%scenarioId =[0-9][0-9]%'
or b.[text] like '%scenarioId = [0-9][0-9]%'
or b.[text] like '%scenarioId = [0-9][0-9]%'

Related

Selecting single rows that have max value for each unique name in different column

I need to find a unique set of province, district (accordingly 'wojewodztwo', 'powiat')
for max value in column called 'liczba'
So basically i need to find district with the biggest value for each unique province
My data base looks like this
And I used this command
select
wojewodztwo,
powiat,
count(typmsc) as liczba
from
wojewodztwa
inner join powiaty
on wojewodztwa.klwoj = powiaty.klwoj
inner join gminy
on powiaty.klpow = gminy.klpow
inner join miejscowosci
on gminy.klgm = miejscowosci.klgm
inner join typymsc
on miejscowosci.kltm = typymsc.kltm
where
typmsc = 'miasto'
group by
wojewodztwo,
powiat
To obtain table that looks like this
I tried to obtain the result by using this query above and making it a temporary (temp) table so i can work on it that way but i couldn't come up with a solution
I want my result to look something like this
The result should have 16 rows of each unique province name (thats how many provinces my country have)
I managed to do it
I used this query
select distinct on(wojewodztwo) wojewodztwo, powiat, liczba from(
select wojewodztwo, powiat, count(typmsc) as liczba from wojewodztwa
inner join powiaty on wojewodztwa.klwoj = powiaty.klwoj
inner join gminy on powiaty.klpow = gminy.klpow
inner join miejscowosci on gminy.klgm = miejscowosci.klgm
inner join typymsc on miejscowosci.kltm = typymsc.kltm
where typmsc = 'miasto'
group by wojewodztwo,powiat
) as temp
order by wojewodztwo,liczba desc
And here is the output i got

NOT IN does works with array of string

I was writing a PostgreSQL function. In that function, I had 2 cases to be checked.
Check if a value exists in the array given
Check if a value does not exist in an array
below are the queries I'm trying
For the first case
SELECT * FROM TableA A
INNER JOIN TableB B on A.inuri = B.resource_uri
INNER JOIN TableI I on I.resource_id = B.resource_id
WHERE B.resource_type like '%%' AND
outuri='./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)' AND (("isDeleted"='true')) OR A.inuri = ANY
(
'{./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6),
./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6)/d(f0576f89-1e0e-4eda-b498-0976f3e19c5c),
./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6)/d(baf1d782-77f8-4372-9601-47a486f0700a)}'
)
and for the second case
SELECT * FROM TableA A
INNER JOIN TableB B on A.inuri = B.resource_uri
INNER JOIN TableI I on I.resource_id = B.resource_id
WHERE B.resource_type like '%%' AND
outuri='./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)' AND (("isDeleted"='false')) AND A.inuri NOT IN
(
'{./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6),
./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6)/d(f0576f89-1e0e-4eda-b498-0976f3e19c5c),
./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6)/d(baf1d782-77f8-4372-9601-47a486f0700a)}'
)
The first case works without any issue. But for the second case, it won't work when multiple values are passed.
Eg: if I give
A.inuri NOT IN
(
'{./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6)/d(f0576f89-1e0e-4eda-b498-0976f3e19c5c)}'
)
I will get all except ./a/b(d91dae45-9e10-43c5-bf08-f52ec80732d3)/c(ee0cc326-fbaf-4d04-a9a6-31d515dea1f6)/d(f0576f89-1e0e-4eda-b498-0976f3e19c5c). But if I give 3 of them it is not validating anything. I will get all 3 items listed in the NOT IN Clause
How can I fix this?
You need to use
A.inuri <> ALL (.... your array here ...)

Why using COUNT with SELF JOIN gives different result value

Can somebody explain me why if I use SELF JOIN and COUNT it gives me different result than just using COUNT command?
Same table with ControlNo column. The value in a column is NOT Unique.
This query gives me total counts 15586.
select (Select COUNT(ControlNo)
from tblQuotes Q1
where Q1.ControlNo = a.ControlNo
) QuotedTotal
FROM tblQuotes a
inner join lstlines l on a.LineGUID = l.LineGUID
where l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
But then, if I run this query it gives me total counts of 15095.
select COUNT(ControlNo) as QuotedTotal
from tblQuotes a
inner join lstlines l on a.LineGUID = l.LineGUID
where l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
What exactly changing the total amount and why?
And why would I use the first scenario?
And is any way to modify the first query to get the sum of 15586 without breaking down by each row?
Thank you
It seems to be because field ControlNo is not unique and there are some records sharing that value, although not all of them join against the lstlines table with that condition. So basically your last query does:
SELECT COUNT(a.ControlNo)
FROM lstlines l
INNER JOIN tblQuotes a ON a.LineGUID = l.LineGUID
WHERE l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
While the first one basically does:
SELECT COUNT(b.ControlNo)
FROM lstlines l
INNER JOIN tblQuotes a ON a.LineGUID = l.LineGUID
INNER JOIN tblQuotes b ON a.ControlNo = b.ControlNo
WHERE l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
As you can see, in this second query you are not only counting the rows that match your lstlines table, but also all the rows in tblQuotes which have the same ControlNo as those who match against lstlines.

TSQL efficiency - INNER JOIN replaced by EXISTS

Can the following be rewritten to be more efficient?
I would use EXISTS if I didn't need fields from country but I do need those fields, and am not sure how to write this to make it more efficient.
SELECT distinct
p.ProvinceID,
p.Abbv as RegionCode,
p.name as RegionName,
cn.Code as CountryCode,
cn.Name as CountryName
FROM dbo.provinces AS p
INNER JOIN dbo.Countries AS cn ON p.CountryID = cn.CountryID
INNER JOIN dbo.Cities c on c.ProvinceID = p.ProvinceID
INNER JOIN dbo.Listings AS l ON l.CityID = c.CityID
WHERE l.IsActive = 1 AND l.IsApproved = 1
There are two things to note:
You're joining to dbo.Listings which results in many records, so you need to use DISTINCT (usually an expensive operator)
For any tables with columns not in the select you can move into an EXISTS (but the query planner effectively does this for you anyway)
So try this:
SELECT
p.ProvinceID,
p.Abbv as RegionCode,
p.name as RegionName,
cn.Code as CountryCode,
cn.Name as CountryName
FROM dbo.provinces AS p
INNER JOIN
dbo.Countries AS cn
ON p.CountryID = cn.CountryID
WHERE EXISTS (SELECT 1 FROM
dbo.Listings l
INNER JOIN dbo.Cities c
on l.CityID = c.CityID
WHERE c.ProvinceID = p.ProvinceID
AND l.IsActive = 1 AND l.IsApproved = 1
)
Check the query plans before and after - the query planner might be smart enough to do this anyway, but you have removed your distinct
The following will often perform even better by providing the optimizer more useful information:
SELECT
p.ProvinceID,
p.Abbv as RegionCode,
p.name as RegionName,
cn.Code as CountryCode,
cn.Name as CountryName
FROM dbo.provinces AS p
INNER JOIN
dbo.Countries AS cn
ON p.CountryID = cn.CountryID
INNER JOIN (
SELECT
p.ProvinceID
FROM
dbo.Listings l
INNER JOIN dbo.Cities c
on l.CityID = c.CityID
WHERE l.IsActive = 1 AND l.IsApproved = 1
GROUP BY
p.ProvinceID
) list
on list.ProvinceID = p.ProvinceID

TSQL Msg 1013 "Use correlation names to distinguish them."

I looked trough many suggestions and can't figure how to solve this one for the last two hours.
SET DATEFORMAT DMY
DECLARE #Source DATETIME = '01/01/2001'
DECLARE #Destenaition DATETIME = '01/01/2020'
SELECT ST.[Group],
ST.Shop,
SUM(ST.Purchased) AS Total,
CHG.Charged
FROM (SELECT Personals.Groups.[Name] AS 'Group',
Cards.vPurchases.PersonalID,
Personals.Registry.[Name],
SUM(Cards.vPurchases.Ammont) AS Purchased,
Cards.vPurchases.ShopName AS Shop
FROM Cards.vPurchases
INNER JOIN Personals.Registry
ON Personals.Registry.Id = Cards.vPurchases.PersonalID
INNER JOIN Personals.Groups
ON Personals.Registry.[Group] = Personals.Groups.Id
INNER JOIN Personals.Groups
ON Personals.Groups.Id = CHG.GroupID
WHERE Cards.vPurchases.[TimeStamp] >= #Source
AND Cards.vPurchases.[TimeStamp] <= #Destenaition
GROUP BY Cards.vPurchases.PersonalID,
Personals.Registry.[Name],
Personals.Groups.[Name],
Cards.vPurchases.ShopName) ST,
(SELECT PG.Id AS GroupID,
SUM(Cards.vCharges.Amount) AS Charged
FROM Cards.vCharges
INNER JOIN Personals.Registry
ON Personals.Registry.Id = Cards.vCharges.PersonalID
INNER JOIN Personals.Groups AS PG
ON Personals.Registry.[Group] = PG.Id
WHERE Cards.vCharges.[TimeStamp] >= #Source
AND Cards.vCharges.[TimeStamp] <= #Destenaition
GROUP BY Personals.Groups.[Name]) AS CHG
GROUP BY ST.Shop,
ST.[Group]
And then I get this error:
Msg 1013, Level 16, State 1, Line 6 The objects "Personals.Groups" and
"Personals.Groups" in the FROM clause have the same exposed names. Use
correlation names to distinguish them.
Thanks.
You are using the table Personals.Groups two times in the first sub query.
If you really mean to have the table Personals.Groups you need to give them an alias that you then use instead of the table names in the rest of the query.
INNER JOIN Personals.Groups as PG1
and
INNER JOIN Personals.Groups as PG2
If you only need one you can combine the on clauses to use just one instead.
INNER JOIN Personals.Groups
ON Personals.Registry.[Group] = Personals.Groups.Id and
Personals.Groups.Id = CHG.GroupID