I have a query as:
SELECT MAX(SubmissionLog.ID), AccountTypes.Description,
AccountContacts.FirstName, AccountContacts.LastName, Accounts.Name,
Accounts.StreetAddress, Accounts.MailAddress,SubmissionLog.EffectiveDate,
SubmissionLog.ExpirationDate, StatusCodes.Description
FROM
AccountContacts RIGHT JOIN (StatusReasons
RIGHT JOIN ([JILL_MG utah sub query]
RIGHT JOIN (StatusCodes
RIGHT JOIN (Carriers
RIGHT JOIN (AgencyLocations
RIGHT JOIN (Brokers
RIGHT JOIN (Agencies
RIGHT JOIN (SubmissionLog
LEFT JOIN (AccountTypes
RIGHT JOIN (BusinessTypes
RIGHT JOIN Accounts ON BusinessTypes.ID = Accounts.BusinessTypeID)
ON AccountTypes.ID = Accounts.AccountTypeID)
ON SubmissionLog.AccountID = Accounts.ID)
ON Agencies.ID = SubmissionLog.AgencyID)
ON Brokers.ID = SubmissionLog.BrokerID)
ON (AgencyLocations.LocationID = Brokers.AgencyLocationID) AND
(AgencyLocations.AgencyID = Brokers.AgencyID))
ON Carriers.ID = SubmissionLog.WinningCarrierID)
ON StatusCodes.ID = SubmissionLog.StatusID)
ON [JILL_MG utah sub query].SubLogID = SubmissionLog.ID)
ON StatusReasons.StatusReasonID = SubmissionLog.StatusReasonID)
ON AccountContacts.AccountID = Accounts.ID
WHERE ( (AccountTypes.Description) Like "prospect" Or (AccountTypes.Description) Like "prev*")
AND ( (SubmissionLog.EffectiveDate)>#12/31/2010#)
AND ((StatusCodes.Description) Like "not*")
AND ((Accounts.dbType_id) In (15)) )
GROUP BY AccountTypes.Description, AccountContacts.FirstName,
AccountContacts.LastName, Accounts.Name, Accounts.StreetAddress,
Accounts.MailAddress,SubmissionLog.EffectiveDate,
SubmissionLog.ExpirationDate, StatusCodes.Description
ORDER BY Accounts.Name
It gives submission details based on Account Names. But we can have Account names repeated as we can have many submissions for an Account with different Effective Dates. But here i need to modify thr query so that I can get Account details with he most recent Effective date(having greatest value of SubmissionLog.Id) irrespective of unique combinations of Account Name with other columns. I jus want Account name with the recent effective date so that there is only one row per Account Name. I have used MAX(SubmissionLog.Id) to filter but due to unique combinations with other columns the Account Name is getting repeated as it has more than one combination with different values with other columns. Any workaround on this guys..??
What you need is a subquery that takes the data from these three tables:
SELECT MAX(SubmissionLog.ID) MaxId, Accounts.Name
FROM
SubmissionLog
LEFT JOIN (AccountTypes
RIGHT JOIN (BusinessTypes
RIGHT JOIN Accounts ON BusinessTypes.ID = Accounts.BusinessTypeID)
ON AccountTypes.ID = Accounts.AccountTypeID)
ON SubmissionLog.AccountID = Accounts.ID
GROUP BY Accounts.Name
Then you can join your main query to this sub-query by Accounts.Name and SubmissionLog.ID to the MaxID and that way you will get the additional columns restricted by the max submission id
Related
Currently i have two count functions, 1 showing the count of all record including NULLS and the other to show only those with a value. I would like to be able to count the number of records of the pcf.ID depending on which ca.ServiceID it is. So for example, i only want to count the pcf.IDs - 7,14,15,25 for ca.ServiceID - 71. Not sure if this is something i could using cases or where to start with it. Is this possible within a single Query? I will need to put this information into a Matrix in SSRS aswell showing all of the services, not sure if that makes any difference to how to approach. Any help would be massively appreciated :)
Currently the first count, counts all records and the second count, counts all those with a value. I would like it to count values based on the Service ID like below.
If serviceID is 71 only count rows with pcf.ID of 7,14,15,25 etc. and return the count value. And do this for all of the different ServiceIDs.
SELECT
COUNT(*)AS [CountAll]
,(COUNT(pcfd.LookupId) + (COUNT(pcfd.TextValue))) As [Answered]
,pcf.ID AS [Field_ID]
,ca.CourseID
,pcf.Label
,pcfd.TextValue
,pcfd.LookupId
,cs.ContractID
,Contract.Name AS [Contract Name]
,Service.Name AS [Service Name]
,ca.ServiceID
,p.ID
FROM
pcfd
INNER JOIN pcf
ON pcfd.pcfID = pcf.ID
INNER JOIN p
ON pcfd.pID = p.ID
INNER JOIN ca
ON p.ID = ca.pID
INNER JOIN Service
ON ca.ServiceID = Service.ID
INNER JOIN cs
ON Service.ID = cs.ServiceID
INNER JOIN Contract
ON cs.ContractID = Contract.ID
WHERE pcf.ID IN (7,14,15,25,12,8,24,23,22)
GROUP BY ca.pID
,ca.CourseID
,pcf.Label
,pcfd.TextValue
,pcfd.LookupId
,p.ID
,ca.ServiceID
,cs.ContractID
,cs.ServiceID
,Service.ID
,Contract.Name
,Service.Name
,pcf.ID
You can add to the select statement
,SUM(CASE WHEN pcf.IDs IN ( 7,14,15,25) AND ca.ServiceID = 71 THEN 1 ELSE 0 END) AS CountField
The "Last" function in the query below (line 4 & 5)that I'm using is not exactly what I'm after. The last function finds the last record in that table.
What i need find is the most recent record in the table according to a date field.
SELECT
tblinmate.statusid,
tblinmate.activedate,
Last(tblclassificationhistory.classificationid) AS LastOfclassificationID,
Last(tblsquadhistory.squadid) AS LastOfsquadID,
tblperson.firstname,
tblperson.middlename,
tblperson.lastname,
tblinmate.prisonnumber,
tblinmate.droppeddate,
tblinmate.personid,
tblinmate.inmateid
FROM tblsquad
INNER JOIN (tblperson
INNER JOIN ((tblinmate
INNER JOIN (tblclassification
INNER JOIN tblclassificationhistory
ON tblclassification.classificationid =
tblclassificationhistory.classificationid)
ON tblinmate.inmateid =
tblclassificationhistory.inmateid)
INNER JOIN tblsquadhistory
ON tblinmate.inmateid =
tblsquadhistory.inmateid)
ON tblperson.personid = tblinmate.personid)
ON tblsquad.squadid = tblsquadhistory.squadid
GROUP BY tblinmate.statusid,
tblinmate.activedate,
tblperson.firstname,
tblperson.middlename,
tblperson.lastname,
tblinmate.prisonnumber,
tblinmate.droppeddate,
tblinmate.personid,
tblinmate.inmateid;
This query below does just that, finds the most recent record in a table according to a date field.
my problem is i dont know how to integrate this Query into the above to replace the "Last" function
SELECT a.inmateID,
a.classificationID,
b.max_date
FROM (
SELECT tblClassificationHistory.inmateID,
tblClassificationHistory.classificationID,
tblClassificationHistory.reclassificationDate
FROM tblinmate
INNER JOIN tblClassificationHistory
ON tblinmate.inmateID = tblClassificationHistory.inmateID
) a
INNER JOIN (
SELECT tblClassificationHistory.inmateID,
MAX(tblClassificationHistory.reclassificationDate) as max_date
FROM tblinmate
INNER JOIN tblClassificationHistory
ON tblinmate.inmateID = tblClassificationHistory.inmateID
GROUP BY tblClassificationHistory.inmateID
) b
ON a.inmateID = b.inmateID
AND a.reclassificationDate = b.max_date
ORDER BY a.inmateID;
I got a tip from another forum to combine queries like this
SELECT qryMainTemp.*, qrySquad.*, qryClassification.*
FROM (qryMainTemp INNER JOIN qrySquad ON qryMainTemp.inmateID = qrySquad.inmateID) INNER JOIN qryClassification ON qryMainTemp.inmateID = qryClassification.inmateID;
and it worked :) i separated the first query into the two queries it was made of and then combined the three like shown above.
Sadly this made another problem arise the query is now not up-datable..working on a solution for this
The 2 queries below are exactly the same except for the "Not In" condition.
The following query yields a correct sorting of the following names:
Query:
Select (Bp.Last_Name||', '||Bp.First_Name||' '||Substr(Bp.Middle_Name,1,1)) As Username
From Scrty.User_Account Ua
Inner Join Buspty.Business_Person Bp On Ua.Business_Person_Uuid = Bp.Business_Party_Uuid And Ua.Active_Flag = 'Y' And Bp.Last_Name Not In (**'a2s','Abdallah','Abnoosi','Abrahamson','Abrams'**) And Ua.Type = 'APPLICATION'
Inner Join Scrty.User_Account_Aaa Uaa On Ua.Uuid = Uaa.User_Account_Uuid
Left Outer Join Buspty.Business_Organization Bo On Bo.Business_Party_Uuid = Bp.Primary_Department_Uuid
Left Outer Join Buspty.Business_Org_Type_Domain Botd On Bo.Business_Org_Type_Uuid = Botd.Uuid And Botd.Code =2 Left Outer Join Buspty.Network N On Bp.Primary_Network_Uuid = N.Uuid
Order By Upper(Username)
Result:
TAYLOR, BRANDON
Taylor, Brandon
However, when I exclude one more names in the "Not In" condition, the query results in the lowercase brandon taylor coming first.
query:
Select (Bp.Last_Name||', '||Bp.First_Name||' '||Substr(Bp.Middle_Name,1,1)) As Username
From Scrty.User_Account Ua
Inner Join Buspty.Business_Person Bp On Ua.Business_Person_Uuid = Bp.Business_Party_Uuid And Ua.Active_Flag = 'Y' And Bp.Last_Name Not In (**'a2s','Abdallah','Abnoosi','Abrahamson','Abrams','Abruzzo'**) And Ua.Type = 'APPLICATION'
Inner Join Scrty.User_Account_Aaa Uaa On Ua.Uuid = Uaa.User_Account_Uuid
Left Outer Join Buspty.Business_Organization Bo On Bo.Business_Party_Uuid = Bp.Primary_Department_Uuid
Left Outer Join Buspty.Business_Org_Type_Domain Botd On Bo.Business_Org_Type_Uuid = Botd.Uuid And Botd.Code =2 Left Outer Join Buspty.Network N On Bp.Primary_Network_Uuid = N.Uuid
Order By Upper(Username)
Result:
Taylor, Brandon
TAYLOR, BRANDON
You are ordering by identical values and thus the database is free to choose any order it wants. It's essentially as if you had no order by at all (this is actually a nice example on how a database changes the sort order based on conditions in the query - another proof that you should never rely on any implicit order in a query result)
To get a consistent sort order you need to include some unique column into the order by clause:
Order By Upper(Username), Ua.Uuid -- or whatever makes the combination unique
that will guarantee the same sort order if two usernames (uppercased) are the same. You could also include a timestamp value or something else.
I am trying to get a record by adding values of one column based on key value. Here is the query I have:
SELECT
PM_ProductPayment.ProjectId, SalesDetail.SalesPerson,
PM_ProductCost.ProductCost, dbo.PM_ProductPayment.ProductPayment,
dbo.PM_ProductPayment.PaymentDate
FROM
dbo.PM_ProductPayment
INNER JOIN
dbo.PM_Product ON dbo.PM_ProductPayment.ProjectId = dbo.PM_Product.ProductId
INNER JOIN
dbo.PM_ProductCost ON dbo.PM_ProductPayment.ProductId = dbo.PM_ProductCost.ProductId
INNER JOIN
dbo.SalesDetail ON dbo.PM_Product.SalesPersonId = dbo.SalesDetail.ID
The result I am getting is:
Now, here I want to get single row by adding "Payment" for each product and last payment date.
Please optimize my query or suggest any other better way to do that..
Thanks,
SELECT pay.ProjectId,
sum(pay.ProductPayment),
max(pay.PaymentDate)
FROM dbo.PM_ProductPayment pay
INNER JOIN dbo.PM_Product pro ON pay.ProjectId = pro.ProductId
INNER JOIN dbo.PM_ProductCost cos ON pay.ProductId =cos.ProductId
INNER JOIN dbo.SalesDetail sal ON pro.SalesPersonId = sal.ID
GROUP BY pay.ProjectId
I have the following query:
SELECT min(salesorder.SOM_SalesOrderID) AS salesorder,
Item.IMA_ItemID,
Item.IMA_ItemName,
Customer.CUS_CorpName,
WK.WKO_WorkOrderID,
min(WK.WKO_OrigRequiredDate),
WK.WKO_WorkOrderTypeCode,
min(WK.WKO_RequiredDate),
max(WK.WKO_LastWorkDate),
min(wk.WKO_RequiredQty),
wk.WKO_MatlIssueDate,
min(SalesOrderDelivery.SOD_RequiredQty),
Item.IMA_ItemTypeCode,
Item.IMA_OnHandQty,
min(SalesOrderDelivery.SOD_PromiseDate),
min(WO.woo_operationseqID) AS seqid
FROM SalesOrder
INNER JOIN SalesOrderLine ON SalesOrder.SOM_RecordID = SalesOrderLine.SOI_SOM_RecordID
INNER JOIN SalesOrderDelivery ON SalesOrderLine.SOI_RecordID = SalesOrderDelivery.SOD_SOI_RecordID,
WO.
INNER JOIN Item ON SalesOrderLine.SOI_IMA_RecordID = Item.IMA_RecordID
INNER JOIN WKO wk ON Item.IMA_ItemID = WK.WKO_ItemID
INNER JOIN Customer ON SalesOrder.SOM_CUS_RecordID = Customer.CUS_RecordID
INNER JOIN woo WO ON WO.WOO_WorkOrderID = WK.WKO_WorkOrderID
WHERE wk.WKO_StatusCode = 'Released'
AND WO.WOO_StatusCode IS NULL
AND SalesOrderDelivery.SOD_ShipComplete = 'false'
GROUP BY WK.WKO_WorkOrderID,
Item.IMA_ItemID,
Item.IMA_ItemName,
Customer.CUS_CorpName,
WK.WKO_WorkOrderTypeCode,
wk.WKO_MatlIssueDate,
Item.IMA_ItemTypeCode,
Item.IMA_OnHandQty
I need 1 record returned for each wk.wko_workorderid. There is a field that is not included that I'm not sure how to get. I need to retrieve the woo.woo_workcenterid that corresponds to min(WO.woo_operationseqID)as seqid. I cannot include it in the general query since there are multiple workcenterids in the table and I only want the specific one that is part of the min operation sequence record.
Any help would be appreciated.