I want to find only failed user details, but the below query is giving duplicate records. I am not able to find a proper solution.
To find failed job details I'm using the below query:
select * from executionlog e
join catalog c on e.reportid = c.itemid
where c.name like '%reportname%'
and timestart>= '2013-04-15 09:00:00.000'
and status <> 'rsSuccess'
However, the above query is giving duplicate values for a particular report.
How can I get unique details?
Note: we cannot apply distinct or group by because the table contains columns of ntext and image data types
If you want only "failed user details" then don't select the ntext or image columns at all. That way you can do a DISTINCT normally:
SELECT DISTINCT
--Parameters,
--Content,
--Property,
--Parameter,
InstanceName, ReportID, UserName, RequestType, Format, TimeStart, TimeEnd,
TimeDataRetrieval, TimeProcessing, TimeRendering, Source, Status, ByteCount,
[RowCount], ItemID, Path, Name, ParentID, Type, Intermediate, SnapshotDataID,
LinkSourceID, Description, Hidden, CreatedByID, CreationDate, ModifiedByID,
ModifiedDate, MimeType, SnapshotLimit, PolicyID, PolicyRoot, ExecutionFlag,
ExecutionTime
FROM executionlog e
JOIN catalog c ON e.reportid = c.itemid
WHERE c.name LIKE '%reportname%'
AND timestart>= '2013-04-15 09:00:00.000'
AND status <> 'rsSuccess'
You can even trim many more columns. Note that doing SELECT * is a bad practice for many cases anyway.
If you're interested in the corresponding ntext and/or image values you can always join the catalog against the above subquery again.
Related
I have a database with Orders, Order_content and Vendors table. I want to get the vendors in one order.
Vendor(vid, company_name, phone_number)
Order_content(ocid, product_name, vid)
Order(oid, ocid, address, name, phone_number)
I want to get the company_name, address and phone_number from the Vendor table that are related to a specific order.
What I have tried.
SELECT vendor.company_name, vendor.address, vendor.phone_number FROM vendor WHERE vendor.vid = order_content.vid AND order_content.oid = order.oid;
Using JOIN
SELECT vendor.company_name, vendor.address, vendor.phone_number AS vendor_details
FROM vendor
INNER JOIN orders_content ON (vendor.vid = orders_content.vendor_id AND orders_content.oid = orders.oid)
WHERE vendor.id;
The SQL is wrong and not working, some help will be appreciated. I think I am supposed to use a JOIN, I tried but the SQL throws an error.
Thanks.
You are only joining Vendor and Order_content, but not Order, so you are missing some fields as a result:
SELECT vendor.company_name, vendor.address, vendor.phone_number AS vendor_details
FROM vendor
INNER JOIN orders_content ON (vendor.vid = orders_content.vendor_id AND orders_content.oid = orders.oid)
INNER JOIN order ON (orders_content.ocid = orders.ocid);
Note that I removed the where clause, since it did not make any sense.
I am trying to write a query that summarizes vulnerabilities by host name, and includes information about that host. Query is running in Rapid7 InsightVM
The query that returns asset information runs successfully, except when I append that query to return vulnerability information it returns an ambiguous reference error for description. But the ip address, host_name, and asset_id values returns just fine.
I am just trying to combined them together to return that information. I feel like something obvious is missing.
This returns what I want from the asset table including the OS Description (Windows, RHEL, etc):
SELECT da.asset_id, da.host_name, da.ip_address, dos.description
FROM dim_asset da
JOIN dim_operating_system dos ON dos.operating_system_id = da.operating_system_id
JOIN fact_asset fa ON fa.asset_id = da.asset_id
GROUP BY da.asset_id, da.host_name, da.ip_address, dos.description
This returns an ambiguous reference for the description, it works for asset_id, host_name, and ip_address:
WITH remediations AS (
SELECT DISTINCT fr.solution_id AS ultimate_soln_id, summary, fix, estimate, riskscore, dshs.solution_id AS solution_id
FROM fact_remediation(10,'riskscore DESC') fr
JOIN dim_solution ds USING (solution_id)
JOIN dim_solution_highest_supercedence dshs ON (fr.solution_id = dshs.superceding_solution_id AND ds.solution_id = dshs.superceding_solution_id)
),
assets AS (
SELECT da.asset_id, da.host_name, da.ip_address, dos.description
FROM dim_asset da
JOIN dim_operating_system dos ON dos.operating_system_id = da.operating_system_id
JOIN fact_asset fa ON fa.asset_id = da.asset_id
GROUP BY da.asset_id, da.host_name, da.ip_address, dos.description
)
SELECT
csv(DISTINCT dv.title) AS "Vulnerability Title",
host_name AS "Asset Hostname", ip_address AS "Asset IP", description AS "OS",
round(sum(dv.riskscore)) AS "Asset Risk",
summary AS "Solution",
fix as "Fix"
FROM remediations r
JOIN dim_asset_vulnerability_solution dvs USING (solution_id)
JOIN dim_vulnerability dv USING (vulnerability_id)
JOIN assets USING (asset_id)
GROUP BY r.riskscore, host_name, ip_address, asset_id, summary, fix
ORDER BY "Asset Risk" DESC WITH remediations AS (
Most likely, dim_asset_vulnerability_solution or dim_vulnerability also have a description field. Just qualifying the selected fields with their intended source should resolve this problem.
...
a.host_name AS "Asset Hostname", a.ip_address AS "Asset IP", a.description AS "OS"
...
JOIN assets AS a USING (asset_id)
...
GROUP BY r.riskscore, a.host_name, a.ip_address, asset_id, summary, fix
Note: asset_id is not a problem because USING has some extra "magic" that merges the references joined by it.
Comment: Unless there are very specific reasons, GROUP BY should not be used as a substitute for SELECT DISTINCT (referring to the assets CTE in particular)
I want to create a report with the top 20 customers (based on revenue).
I am using the query:
SELECT dbo.CustTable.AccountNum
,dbo.dirpartytable.NAME
,dbo.hcmworker.PERSONNELNUMBER
,dbo.CustInvoiceJour.SALESBALANCE
,dbo.custinvoicejour.QTY
FROM dbo.CustTable
inner JOIN dbo.HCMWORKER ON dbo.HCMWORKER.RECID = dbo.CustTable.KEV_Worker
inner join dbo.custInvoiceJour on CustInvoiceJour.OrderAccount = CustTable.AccountNum
inner join dbo.dirpartytable on dirpartytable.recid = custtable.PARTY
where CustTable.KEV_Worker = '5633561745'
ORDER BY SalesBalanceMst DESC
I can't find the relation for the customer revenue, after all, that is how I want to sort the report. I am sorting on SalesBalanceMST right now while building the report. Also I am getting multiple records when executing this query.
What am i doing wrong?
EDIT: I now realize I am showing each Invoice Journal, how can I display the Total Revenue of the customer?
A similar search from AX 2012:
CustInvoiceJour CustInvoiceJour;
CustTable CustTable;
DirPartyTable DirPartyTable;
select forceLiterals generateonly sum(SalesBalanceMST), sum(Qty) from CustInvoiceJour
where CustInvoiceJour.OrderAccount == '102372200'
&& CustInvoiceJour.InvoiceDate > today()-365
join TableId from CustTable
group AccountNum
where CustTable.AccountNum == CustInvoiceJour.OrderAccount
join TableId from DirPartyTable
group Name
where DirPartyTable.RecId == CustTable.Party;
info(CustInvoiceJour.getSQLStatement());
This shows the following SQL:
SELECT SUM(T1.SALESBALANCEMST),SUM(T1.QTY),T2.ACCOUNTNUM,T3.NAME
FROM CUSTINVOICEJOUR T1
CROSS JOIN CUSTTABLE T2
CROSS JOIN DIRPARTYTABLE T3
WHERE (((T1.PARTITION=5637144576) AND (T1.DATAAREAID=N'xxx'))
AND ((T1.ORDERACCOUNT=N'102372200')
AND (T1.INVOICEDATE>{ts '2015-11-06 00:00:00.000'})))
AND (((T2.PARTITION=5637144576) AND (T2.DATAAREAID=N'xxx'))
AND (T2.ACCOUNTNUM=T1.ORDERACCOUNT))
AND ((T3.PARTITION=5637144576)
AND (T3.RECID=T2.PARTY))
GROUP BY T2.ACCOUNTNUM,T3.NAME
ORDER BY T2.ACCOUNTNUM,T3.NAME
What is different from your query:
no join on HcmWorker, as I do not have your custom field.
Using sum() to aggregate
selecting on InvoiceDate
selection on OrderAccount
selection on DataAreaId, really important for performance, implicit in AX
selection on Partition, really important for performance, implicit in AX
You cannot directly sort on a sum, but may on a nested SQL query.
I do not know exactly what is wrong in your query but perhaps this information can help you.
Check this standard report CustTopCustomersbyYTDSales, It has some good queries to do that.
https://technet.microsoft.com/en-us/library/hh389751.aspx
What is wrong with this query?
SELECT DISTINCT source,
source_description,
url
FROM staging_census
WHERE Trim(Concat(source, source_description, url)) NOT IN (SELECT
Trim(Concat(source,
source_description, url))
FROM dim_source);
The objective is to pull only the records where a combination of Source, Source_Description and URL that does not exist (which is true in my case). However, if it sees a match in only one column it ignores it.
MySQL Newbie... if there is a better way for this query I would appreciate it.
Try this:
SELECT DISTINCT Source, Source_Description, URL
FROM Staging_Census
WHERE CONCAT(TRIM(Source), TRIM(Source_Description0, TRIM(URL))) NOT IN (SELECT CONCAT(TRIM(Source), TRIM(Source_Description0, TRIM(URL))) FROM DIM_Source);
OR
SELECT DISTINCT SC.Source, SC.Source_Description, SC.URL
FROM Staging_Censusc SC
LEFT JOIN DIM_Source DS ON (TRIM(SC.Source), TRIM(AC.Source_Description0, TRIM(SC.URL)) = (TRIM(DS.Source), TRIM(DS.Source_Description0, TRIM(DS.URL))
WHERE DS.DimsourceId IS NULL
I'm stuggling to replicate a SQL query into LINQ.
Can any one help?
SQL:
SELECT tblInvoice.lngID AS InvoiceID,
tblInvoice.dtTimeStamp AS InvoiceDate,
tblInvoice.strReference,
tblInvoice.fltTotalValue,
max(Project.ProjectID) AS ProjectID,
max(Project.ProjectName) AS ProjectName,
max(Project.Location) AS ProjectLocation
FROM tblInvoice INNER JOIN
tblInvoiceLine ON tblInvoice.lngID = tblInvoiceLine.lngInvoiceID
WHERE (tblInvoice.intStatus != 0)
AND (tblInvoice.lngPersonID = #PersonID)
GROUP BY tblInvoice.lngID, tblInvoice.dtTimeStamp, strReference, fltTotalValue
ORDER BY tblInvoice.lngID DESC
LINQ so far:
var invoices = from inv in db.TblInvoices
join invLine in db.TblInvoiceLines on inv.LngID equals invLine.LngInvoiceID
where inv.IntStatus != 0
where inv.LngPersonID == personID
group inv by new {inv.LngID,inv.DtTimeStamp,inv.StrReference,inv.FltTotalValue} into newInv
Part of the problem is that I want to do a
select new Invoice(){
}
and build up my custom Invoice object but, I cant see any of the properties in newInv.
Can any one advise?
I don't have time for a full answer now, but:
To get at properties of the key, use newInv.Key.StrReference etc
To get at aggregates (e.g. max values) use newInv.Max(x => x.ProjectId) etc
Hopefully that'll be enough to get you going. Basically, newInv will be a group of entries, with an associated key (which is what you grouped by).