adding a field somehow effects a views results - sql

Have a very odd situation. For various reasons we have a bunch of tables where this was done:
TableA was renamed to TableASource and a view was created called TableA
TableB was renamed to TableBSource and a view was created called TableB
This all works fine and has done for a fair while. Yesterday I added a new field to TableASource, TableBSource (and the others). This was a field called 'createDate' a smallDateTime, no nulls with a default value of getDate(). You would think this should have zero impact on anything. Today first thing users are saying they can see these dates inplace of the intended data. E.G Lest says we had a page extracting 10 fields out of the view called TableA, instead of showing these 10 fields its now showing 9 correctly and randomly showing the value in the new createDate field that was added to TableASource (If it matters it placing it in the second field on the page)
I quickly dropped all these new createDate fields and the issue went away. How can this possibly happen? How can adding a field to TableASource effect the results of the TableA view?
Using SQLServer2008 r2
in response the the extra questions below. Here is the view called 'bayTrainCourses' which has been described above as TableA :
SELECT btc.course_id, btc.course_name, btc.course_status, btc.course_expiry, btc.course_startdate, btc.course_enddate, btc.course_type, btc.site_id, btc.channelid, btc.exam_type, btc.enroll_type, btc.last_updated, btc.compid, btc.TOC, btc.rdone, btc.autoqualify, btc.courseIntroId, btc.coursePurposeId, btc.courseBackgroundId,btc.courseObjectivesId, intro. [content] AS course_intro, purpose.[content] AS course_purpose, background.[content] AS course_background,
objectives.[content] AS course_objectives
FROM dbo.bayTrainCoursesSource AS btc
LEFT OUTER JOIN dbo.RTE AS intro ON btc.courseIntroId = intro.pk
LEFT OUTER JOIN dbo.RTE AS purpose ON btc.coursePurposeId = purpose.pk
LEFT OUTER JOIN dbo.RTE AS background ON btc.courseBackgroundId = background.pk
LEFT OUTER JOIN dbo.RTE AS objectives ON btc.courseObjectivesId = objectives.pk
The select is:
select * from baytraincourses....
EDITING - due to some trial an error. It turns out that the trial was actually the advice from JohnS below. Recreate the view.... I renamed the view to baytraincourses_broken. Then right click > script view as > create to > then I changed 'baytraincourses_broken' to 'baytraincourses' to recreate another version with the original name. Now the page works.
I am not a big fan of views. Its very rare I create them to be honest, I am working with code that I did not originally write. Do I really have to re-create views every time I add a new column? Surely not, how can I deal with this?

After adding the new field to TableASource you need to recreate the view. (i.e. DROP VIEW TableA ... then CREATE VIEW TableA ...).

Related

What do I need to change to get a count of deleted, locked posts from SEDE?

I'm trying to query for some stats on how many posts have been deleted as spam/abusive. I've got what I think should work, but the numbers it throws out don't make sense.
Since posts deleted this way are characterized by being both deleted and locked, I'm querying for those attributes by looking at the PostHistory table.
My initial query looks like this:
SELECT
COUNT(DISTINCT ph0.PostId)
FROM
PostHistory ph0
INNER JOIN
PostHistory ph1
ON
ph0.PostId = ph1.PostId AND
ph1.PostHistoryTypeId = 12
WHERE
ph0.PostHistoryTypeId = 14
That one throws up a count of 397, which doesn't make sense. There are at least 6485 posts that have been identified as spam on Stack Overflow. So, to check that query, I'm using a debugging query that outputs the post bodies:
SELECT
Body
FROM
PostsWithDeleted
WHERE
Id IN
(SELECT
DISTINCT ph0.PostId
FROM
PostHistory ph0
INNER JOIN
PostHistory ph1
ON
ph0.PostId = ph1.PostId AND
ph1.PostHistoryTypeId = 12
WHERE
ph0.PostHistoryTypeId = 14)
The bodies that outputs just plain aren't spam - certainly not of the kind I'm used to seeing. A sample:
All I can say is that you need to subclass UIView and make it a delegate of UIGestureRecognizerDelegate and UICollectionViewDelegate, then in your UIView subclass, do the following, I can't give out anymore information on this because the code, although owned by myself, is proprietary to the point of probably enraging quite a few organizations that I've used this for, so here's the secret...
I'm looking for an application or a social wall plugin to be added to a project. After looking at Wordpress and finally sifting through all the plugins (maybe all), I have come to a conclusion the plugins are not giving me enough customization options. For example, customizing the registration form. I need to add javascript for a combo box in order to display different options dependI added a movieclip here, and a number. To get an effect, like a star with a number...
The message is "starCount is not a child of a caller". I don't know...
So, what am I doing wrong that means I'm not selecting deleted, locked posts, and what do I need to do to fix it?
Although your query output is exactly the same as mine I believe my attempt gives a clear view of what you're trying to achieve.
If you find this not giving you the desired output, there must be some more logic into finding these posts that you mention.
Below query returns a number of posts that in their history have been marked as both Locked and Deleted at least once.
SELECT COUNT(*)
FROM (
SELECT
ph.PostId
FROM
PostHistory ph
INNER JOIN PostHistoryTypes pht ON
ph.PostHistoryTypeId = pht.id
WHERE
pht.Name IN ('Post Locked', 'Post Deleted')
GROUP BY ph.PostId
HAVING COUNT(DISTINCT ph.PostHistoryTypeId) >= 2
) foo
You've included below message and it seems alright with your query.
Since posts deleted this way are characterized by being both deleted and locked, I'm querying for those attributes by looking at the PostHistory table.

Error in SQL SELECT with INNER JOIN

So, basically, I have two tables called "dadoscatalogo" and "palavras_chave", with a common field, "patrimonio" which is the primary key of "dadoscatalogo".
I'm using a servlet to connect to the database with these tables, and passing a query to search for entries based on some search criteria that's defined by the user.
Now, since the user can search for entries based on information present in both tables, I need to do an INNER JOIN, and then use WHERE to search for that info. I'm also using LIKE, because the user may pass just part of the information, and not all of it.
So, to test it all out, I tried passing it a few parameters to work with, and see how it went. After some debugging, I found out that there was some mistake in the query. But I can't seem to be able to point out exactly what it is.
Here's the test query:
SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo
INNER JOIN palavras_chave
ON dadoscatalogo.patrimonio=palavras_chave.patrimonio
WHERE dadoscatalogo.patrimonio LIKE '%'
AND dadoscatalogo.titulo LIKE '%tons%'
OR palavras_chave.palchave LIKE '%programming%';
So, basically, what I'm trying to do with this query is, get all the primary keys from "dadoscatalogo" that are linked to a record with a "titulo" containing "tons", or a "palchave" containing "programming".
PS. Sorry for the names not being in English, hopefully it won't be too much of a distraction.
EDIT: Right now, the tables don't have much:
This is the dadoscatalogo table:
http://gyazo.com/fdc848da7496cea4ea2bcb6fbe81cb25
And this is the palavras_chave table:
http://gyazo.com/6bb82f844caebe819f380e515b1f504e
When they join, I'm expecting it to have 4 records, and it would get the one with patrimonio=2 in dadoscatalogo (which has "tons" in titulo), and the one with palchave=programming (which would have patrimonio=1)
As per my understanding run below query:
SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo
INNER JOIN palavras_chave
ON dadoscatalogo.patrimonio=palavras_chave.patrimonio
WHERE dadoscatalogo.titulo LIKE '%tons%'
OR palavras_chave.palchave LIKE '%programming%';

SQL joins with views

I have a view that is now joining two other views with some extra tables.
It's very slow.
My experience tell me it's because views are not indexed by default. I tried to create an index on each of them, but it's not possible since they have self joins or inner queries.
My question is:
It appears to me that in general the join of views is not recommended. So, in short, there is no way to reuse a code from one view into another? Example: the view A calculates the percentage and the view B calculates something else that uses the percentage from view A plus other information from other tables/views. What would be the best approach? Do you really have to replicate the code from view A to view B so it uses the original table's indexes?
Views (simplified view, to show the issue):
View A (calculates the percentage):
SELECT dbo.tblPopAgeGrp.RevID, dbo.tblPopAgeGrp.VarID, dbo.tblPopAgeGrp.LocID,
dbo.tblPopAgeGrp.TimeID, dbo.tblPopAgeGrp.AgeID, tPAGT.AgeID AS AgeTotal,
100 * dbo.tblPopAgeGrp.PopMale / tPAGT.PopMale AS PopMalePerc,
100 * dbo.tblPopAgeGrp.PopFemale / tPAGT.PopFemale AS PopFemalePerc,
100 * dbo.tblPopAgeGrp.PopTotal / tPAGT.PopTotal AS PopTotalPerc
FROM dbo.tblPopAgeGrp
INNER JOIN dbo.tblPopAgeGrp tPAGT
ON dbo.tblPopAgeGrp.GroupID = tPAGT.GroupID
AND dbo.tblPopAgeGrp.AgeID = 700
View A by itself, since there so many records, takes a long time to execute. However, in view B the records are filtered according to the VersionID.
View B (gets the percentage from view A with additional info from another view):
SELECT vPAGP.VersionID,
vPAGP.LocationID AS LocID,
vPAGP.PopTotalPerc AS pPopTot,
vPAGP.PopMalePerc AS pMale,
vPAGP.PopFemalePerc AS pFemale,
vPAGPSR.PopMaleSexRatio AS SexRatio,
vPAGPSR.PopFemaleSexRatio AS FemRatio
FROM dbo.vwA AS vPAGP
INNER JOIN dbo.vwOther AS vPAGPSR
ON vPAGPSR.GroupID = vPAGP.GroupID
WHERE vPAGP.VersionID=10
Executing View A without filters, takes like 10 minutes. Executing it for VersionID=10 only, it executes in 10 seconds. The view vwOther executes very quickly.
Thanks!
You are not correct when you state "It appears to me that in general the join of views is not recommended."
Views can be combined with other views and will perform well provided that all JOINs are optimizable and have the appropriate index created and any filtering done within the view is optimizable and has appropriate indexes created.
A view based on other views should perform as well as the same query written to factor out the views. If you want further help, please post the definition of all views involved in your problem.

How to remove row that exists in another table?

I have two tables. Main table is "CompleteEmailListJuly11" and the second table is "CurrentCustomersEmailJuly11". I want to delete rows in CompleteEmailListJuly11 table that CurrentCustomersEmailJuly11 has based off email.
I've tried this following Delete example, but it doesn't do anything close to what I'm trying to do. This only shows me the ones that EXIST in the database, it doesn't show me the the list of emails that AREN'T matching.
DELETE * FROM CompleteEmailListJuly11 AS i
WHERE EXISTS (
SELECT 1 FROM CurrentCustomersEmailJuly11
WHERE CurrentCustomersEmailJuly11.email = i.EmailAddress
)
Help is greatly appreciated.
This is the query I think you need:
DELETE FROM CompleteEmailListJuly11
WHERE EmailAddress IN (SELECT email FROM CurrentCustomersEmailJuly11)
Ps: The DELETE query does not delete individual fields, only entire rows, so the * is not necessary, you will also need to "Execute" this query rather than "Previewing" or "Exporting"
If you're building your DELETE query in Access' query designer, notice there are two different modes of operation which seem similar to "go ahead and do this".
Datasheet View (represented by the grid icon labeled "View" on the "Design" section of the ribbon). That view enables you to preview the affected records, but does not actually delete them.
The "Run" icon (represented by a red exclamation point). "Run" will actually execute the query and delete the affected records.
If you already know this, my description may seem insulting. Sorry. However, it seems that folks new to Access can easily overlook the distinction between them.
You can use something like this adapted to delete
SELECT ... // complete
EXCEPT
SELECT ... // current
I am not sure exactly how it maps to delete but take a look at that.
I fond it in a similar question: How do I 'subtract' sql tables?
We can use Correlated Query to resolve the issue like
DELETE FROM COMPLETE C
WHERE EMAIL = (SELECT EMAIL FROM CURR CU WHERE CU.EMAIL=C.EMAIL);

Please help me make this Access 2007 Friendly

I asked a friend to hellp me with an issue I was having with my Access Database since I haven't programmed in years, and here's what he replied with:
Let me toss an example out just to make sure I'm looking at this
right. You start with a record with ID 1. This gets renewed, and the
system generates a new record with ID 2, and brings along the old ID
of 1 in the RenewalOf field, and so on for future renewals. If that
is correct, and each record is only allowed to be referenced once (so
there will only ever be one record with ID of 1 in the RenewalOf
field), then the following should work:
This bit of code didn't work:
UPDATE
tblSold
SET
RenewedToID = RenewalRecord.SoldID
FROM
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
Not sure what is allowed in your SQL queries, but this is basic and
should be fine. You can also add in some criteria to only update
records where the RenewedToID field is blank, or only for one record
if you are processing this just after you add a new record. You can
check to make sure this is going to work by running the following:
But this did:
SELECT
tblSold.SoldID
,RenewalRecord.SoldID
FROM
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
This will list the original ID along with the renewal ID, i.e. the one
that will be put in the original record. Let me know if this works or
if you have any issues with it.
Can you help me make his first code snippet work in Access 2007?
You may need to rearrange the update slightly for Access:
UPDATE
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
SET
tblSold.RenewedToID = RenewalRecord.SoldID
Some other SO answers showing this type of syntax:
SQL Update Statement in MS Access
How to create a correlated update subquery in MS-Access?