I have got a report by my webhost and he told me that there is a key or something in the database that creates many many thousands of queries. I dont know how to understand this report. Maybe someone can help me? I think it has to do with wordfence plugin somehow.
Queries reported are:
SELECT name, val FROM wp_wfConfig WHERE autoload = 'yes'
SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (2,12,13,14,19,91,133,626,3775,7034,7755,9559,10298,11588,13049,13133,13142,13146,15298,15300,15365,15761,17185,18530,20989,21046,23394,25343,25357,29136) ORDER BY meta_id ASC
SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (2,12,13,14,19,91,133,626,3775,7034,7755,9559,10298,11588,13049,13133,13142,13146,15298,15300,15365,15761,17185,18530,20989,21046,23394,25343,25357,29136) ORDER BY meta_id ASC
SELECT name, val FROM wp_wfConfig WHERE autoload = 'yes'
I uninstalled wordfence and have deleted all the tables with wordfence assistant to make sure, all settings and tables are deleted. But the term "autoload" is still cryptical for me.
Related
Assuming the following stucture
table contract: contract_id, status_id, vendor_id
table status: active (status_id = 1), inactive (status_id = 2), deleted (status_id=3)
table vendor: vendor_id, vendor_name
How can I list only vendors with their contracts, all of them either inactive or deleted?
so if there is at least one contract for specific vendor which is active, this vendor and all of his contracts should be omitted
EDIT:
sample schema
http://sqlfiddle.com/#!18/01f4c/5
I was thinking something like this might fit the bill. Qry1 is the set of all contracts with the relevant vendor and status columns. Qry2 returns the set of all vendors with no active contracts. The final query returns everything from Qry1 where the vendor is not in Qry2.
With Qry1 As (
Select Vendor.VendorId,
Vendor.VendorName,
Contract.ContractId,
Contract.ContractName,
Status.StatusName
From Vendor
Inner Join Contract
On Vendor.VendorId = Contract.VendorId
Inner Join Status
On Contract.StatusId = Status.StatusId
),
Qry2 As (
Select Distinct VendorId
From Qry1
Where StatusName = 'Active'
)
Select Qry1.*
From Qry1
Where Not Exists (
Select Qry2.VendorId
From Qry2
Where Qry2.VendorId = Qry1.VendorId
)
First, I'd like to point out that in your question, you state "...inactive or deleted", which would mean two statuses(2,3), yet in your last comment, you specify where status = 1, which is active. This is very confusing. However, based on your last comment, which I will treat as the most up to date requirements, I believe the below query will get what you want.
EDIT: I believe I just figured out what you meant. There's some ambiguity in the way you wrote your last comment. Adjusted the query.
SELECT *
FROM vendors v
LEFT JOIN contract c ON v.vendor_id = c.vendor_id
WHERE c.STATUS in (2,3)
AND c.vendor_id IS NULL
(Submitting on behalf of a Snowflake User)
We have wrong duplicate id loaded in the table and we need to correct it. The rules to update the id is whenever there is a time difference of more than 30 min, the id should be new/unique. I have written the query to filter that out, however update is not happening
The below query is there to find the ids to be updated. For testing I have used a particular id.
select id,
BEFORE_TIME,
TIMESTAMP,
datediff(minute,BEFORE_TIME,TIMESTAMP) time_diff,
row_number() over (PARTITION BY id ORDER BY TIMESTAMP) rowno,
concat(id,to_varchar(rowno)) newid from
(SELECT id,
TIMESTAMP,
LAG(TIMESTAMP_EST) OVER (PARTITION BY visit_id ORDER BY TIMESTAMP) as BEFORE_TIME
FROM table_name t
where id = 'XX1X2375'
order by TIMESTAMP_EST)
where BEFORE_TIME is not NULL and time_diff > 30
order by time_diff desc
;
And i could see the 12 record with same id and time difference more than 30. However when I am trying to update. the query is succesfull but nothing is getting update.
update table_name t
set t.id = c.newid
from
(select id ,
BEFORE_TIME,
TIMESTAMP,
datediff(minute,BEFORE_TIME,TIMESTAMP) time_diff,
row_number() over (PARTITION BY id ORDER BY TIMESTAMP) rowno,
concat(id,to_varchar(rowno)) newid from
(SELECT id,
TIMESTAMP,
LAG(TIMESTAMP) OVER (PARTITION BY visit_id ORDER BY TIMESTAMP) as BEFORE_TIME
FROM table_name t
where id = 'XX1X2375'
order by TIMESTAMP_EST)
where BEFORE_TIME is not NULL and time_diff > 30
order by time_diff desc) c
where t.id = c.id
and t.timestamp = c.BEFORE_TIME
;
please note:
I even created a temp table t1 from the above subquery.
And i can see the records in table t1.
when doing select with join with main table i can even see in record in main table.
But again when I am trying to update using new t1. its just showing zero record updated.
I even tried merge but same issue.
MERGE INTO snowplow_data_subset_temp t
USING t1
ON (trim(t.visit_id) = trim(t1.visit_id) and trim(t1.BEFORE_DATE) = trim(t.TIMESTAMP_EST))
WHEN MATCHED THEN UPDATE SET visit_number = newid;
Any recommendations, ideas, or work-arounds? Thanks!
This looks like they may be running into two things:
The table that you created t1, was it a transient or cloned table? Check out the
Get_DDL('t1', 'schemaname');
to check if there are any constraints on the temp table in the session you work on this next. Or you can query the table constraints view
"Alternatively, retrieve a list of all table constraints by schema (or across all schemas in a database) by querying the TABLE_CONSTRAINTS View view in the Information Schema." from: https://docs.snowflake.net/manuals/user-guide/table-considerations.html#referential-integrity-constraints
Since the sub query is working just fine - the merge and update statements are clues for what to look for, this is what I found in the documentation for more general info:
*Limitations on Sub queries:
https://docs.snowflake.net/manuals/user-guide/querying-subqueries.html#limitations
You can also check to see if there are any errors for the update query by altering the session: https://docs.snowflake.net/manuals/sql-reference/sql/update.html#usage-notes
ALTER SESSION SET ERROR_ON_NONDETERMINISTIC_UPDATE=TRUE;
Here is an example of how to use an update with a Temp table:
https://snowflakecommunity.force.com/s/question/0D50Z00008P7BznSAF/can-you-use-a-cte-or-temp-table-with-an-update-statement-to-update-a-table
I am looking forward to seeing how they ended up solving the issue.
I am trying to create a query that provides me with the total number of Agents (AgentID) for each OfficeID. If someone could please guide me in the right direction, also if there are resources that give you a bunch of examples of different types of queries that would be useful for the future!
My issue right now is the syntax. I'm not sure where things need to go in order to get the desired output above.
Here's what I have as of now:
Tables OFFICE and AGENT:
CREATE TABLE OFFICE
(
OfficeID NVARCHAR(5) UNIQUE,
OfficeAddress NVARCHAR(18) NOT NULL,
PRIMARY KEY(OfficeID)
)
GO
CREATE TABLE AGENT
(
AgentID NVARCHAR(8) UNIQUE,
OfficeID NVARCHAR(5) NOT NULL,
AgentType NVARCHAR(9) NOT NULL,
AgentFName NVARCHAR(10) NOT NULL,
PRIMARY KEY (AgentId),
FOREIGN KEY (OfficeID) REFERENCES OFFICE
ON DELETE CASCADE
ON UPDATE CASCADE
)
GO
Query:
SELECT
OFFICE.OfficeID
FROM
OFFICE,
(SELECT COUNT(AgentID)
FROM AGENT, OFFICE
WHERE OFFICE.OfficeID = AGENT.OfficeID
GROUP BY AGENT.OfficeID)
ORDER BY
OFFICE.OfficeID
I'd do this with a JOIN and GROUP BY, no nesting required or wanted:
SELECT o.OfficeID, COUNT(a.AgentID) NumberOfAgents
FROM Office o
LEFT JOIN Agents a ON a.OfficeID = o.OfficeID
GROUP BY o.OfficeID
Something like this (your desired output appears to be missing):
SELECT O.OfficeID
, (
SELECT COUNT(*)
FROM AGENT A
WHERE A.OfficeID = O.OfficeID
)
FROM OFFICE O
ORDER BY O.OfficeID
Note the use of the table alias which is a recommended practice to keep your queries concise.
You need to be specific with what you want as per what I think no complex query is required in your case. For example you can get your desired output from the below query
select officeid, count(1) as NoofAgents
from Agents
group by officeid
SQL can give you your desired way in a lot way and you can choose them based on the optimized solution.
I've made database design for a small CRM system. It comprises of Companies and Meetings (amongst others).
Companies has the fields:
ID (primary, auto_inc)
Name (text)
Meetings has the fields:
ID (primary, auto_inc)
CompanyId (link to Companies.ID)
WhenTime (datetime, to store when the meeting was)
Notes (text about the meeting)
What I want to accomplish is a query that gives me a list of all Companies (all fields in the table), AND the WhenTime and Notes of the latest meeting with that company (latest is max(WhenTime), and if there is none, a NULL will do fine).
I think I can solve this with cursors, but I'm afraid of speed.
I've tried several Group By formulations, but I fear I lack the finesse required.
My last attempt was this:
select Companies.ID, Companies.name, mts.whentime, mts.notes
from Companies
left outer join (
select top(1) *
from Meetings
order by [whentime] desc
) mts
on Companies.ID = mts.companyID
order by Companies.name asc
but this code only takes one tuple from Meetings, not one per company in the join, so it's no good.
Any ideas?
Try:
select Companies.ID, Companies.name, mts.whentime, mts.notes
from Companies
cross apply
(
select top(1) *
from Meetings
where Companies.ID = Meetings.companyID
order by [whentime] desc
) mts
order by Companies.name asc;
I would start by creating a view of the latest meetings as I find creating views makes complex queries easier to read and maintain and can introduce an element of reusability (if done right).
CREATE VIEW [dbo].[LatestCompanyNotes]
AS
SELECT [CompanyId], [WhenTime], [Notes]
FROM [Meetings] AS M1
INNER JOIN
(
SELECT [CompanyId], MAX([Id]) AS [MaxId]
FROM [Meetings]
GROUP BY [CompanyId]
) AS M2 ON M2.[CompanyId] = M1.[CompanyId] AND M2.[MaxId] = M1.[Id]
Now you should be able to join to this view in your query as you've previously done.
SELECT Companies.[ID], Companies.[Name], mts.[WhenTime], mts.[Notes]
FROM [Companies]
LEFT OUTER JOIN [dbo].[LatestCompanyNotes] AS mts ON mts.[CompanyId] = Companies.[ID]
ORDER BY Companies.[Name] ASC
Please note that I've not tested the code (I don't even have SQL Server installed) and it may require a few small changes to work.
You don't need a cross-apply here, just a correlated sub-query to find the most recent meeting date:
SELECT Companies.ID, Companies.name, mts.whentime, mts.notes
FROM Companies
LEFT OUTER JOIN Meetings mts
ON Companies.ID = mts.companyID
AND mts.WhenTime =
(SELECT MAX(WhenTime) FROM Meetings mtshist WHERE mtshist.companyID = mts.companyID)
ORDER BY Companies.name
Note that this will retrieve all companies, including those which have never had a meeting:
1 Alexander and co. 2010-01-04 some more notes
2 Barnard Partnership 2010-01-03 NULL
3 Collingwood Ltd. 2010-01-07 recent meeting
4 Dimitri and sons NULL NULL
I'm new with SQL Reporting Services 2008 and cannot find out how to do something that should be simple.
What I have is a single select parameter that is populated with a list of salesman from a query. This works great. And I have the dataset query responding to this parameter working well. So my reports for a single salesman are working fine.
My problem is that I would also like to have an option that is ALL Salesman so that the manager can print out a complete list.
Any suggestions on how to do this.
I usually UNION ALL a custom value to the top of my query with a special value that would indicate to my later query that it should not filter.
I usually use NULL, 0, or '%' for this.
Something like:
SELECT 0 [UserId], '(All Users)' [Username]
UNION ALL
SELECT
[UserId],
[Username]
FROM
dbo.[Users]
And then, in the later query:
SELECT * FROM Data WHERE (UserID = #UserID OR #UserID = 0)
Your sales person query probably looks like this:
SELECT SalesPersonName FROM SalesPerson ORDER BY SalesPersonName ASC
You should change it to this:
SELECT 1 as SortOrder, SalesPersonName FROM SalesPerson
UNION SELECT 0 as SortOrder, 'All' ORDER BY SortOrder, SalesPersonName
Now your query will return:
All
Adam
Barry
Steve
...
Now when you pull your data for the report you can do:
WHERE (SalesPersonName = #SalesPersonName OR #SalesPersonName='All')
And make sure you set the default to 'All' if that is what your manager wants.