Terrible access database issue - sql

I have a terrible database from a client and I need to count the number of results from a query, which is as follows:
SELECT
Offices.OfficeID
, ContractsBooksCommodities.CommodityID
FROM ((((Offices
INNER JOIN tbl_Sales
ON Offices.CompanyID = tbl_Sales.CompanyID)
INNER JOIN ContractBooks
ON tbl_Sales.CompanyID = ContractBooks.CompanyID)
INNER JOIN ContractsBooksAds
ON ContractBooks.ContractNum = ContractsBooksAds.ContractNum)
INNER JOIN ContractsBooksBrands
ON ContractsBooksAds.ContractNum = ContractsBooksBrands.ContractNum)
INNER JOIN ContractsBooksCommodities
ON ContractsBooksBrands.ContractNum = ContractsBooksCommodities.ContractNum;
How can I make this count the number of records returned?

In general,
select count(*)
from (
your-select-query
)
will give you the number of records returned by your query.

COUNT and GROUP BY would be my guess:
SELECT Offices.OfficeID, ContractsBooksCommodities.CommodityID, COUNT(*) AS COUNT
FROM ((((Offices INNER JOIN tbl_Sales ON Offices.CompanyID = tbl_Sales.CompanyID) INNER JOIN ContractBooks ON tbl_Sales.CompanyID = ContractBooks.CompanyID) INNER JOIN ContractsBooksAds ON ContractBooks.ContractNum = ContractsBooksAds.ContractNum) INNER JOIN ContractsBooksBrands ON ContractsBooksAds.ContractNum = ContractsBooksBrands.ContractNum) INNER JOIN ContractsBooksCommodities ON ContractsBooksBrands.ContractNum = ContractsBooksCommodities.ContractNum
GROUP BY Offices.OfficeID, ContractsBooksCommodities.CommodityID
ORDER BY Offices.OfficeID, ContractsBooksCommodities.CommodityID

Related

I need to group (tbl_types.name AS type) in a same row (postgres 14)

I need help with a select in postgres, I need to group X types into a single line, for example: type: multiple, trully, I need help on the type column
SELECT tbl_questions.id AS id,
tbl_questions.question AS question,
tbl_questions.year AS year,
tbl_question_responses.response_id AS response_id,
tbl_responses.response AS response_content,
tbl_responses.response_type AS response,
tbl_subjects.name AS subject,
tbl_categories.name AS category,
tbl_types.name AS type,
tbl_institutions.name AS institution
FROM tbl_questions
INNER JOIN tbl_question_responses ON tbl_questions.id = tbl_question_responses.question_id
INNER JOIN tbl_responses ON tbl_question_responses.response_id = tbl_responses.id
INNER JOIN tbl_question_subjects ON tbl_questions.id = tbl_question_subjects.question_id
INNER JOIN tbl_subjects ON tbl_subjects.id = tbl_question_subjects.subject_id
INNER JOIN tbl_question_categories ON tbl_questions.id = tbl_question_categories.question_id
INNER JOIN tbl_categories ON tbl_categories.id = tbl_question_categories.category_id
INNER JOIN tbl_question_types ON tbl_questions.id = tbl_question_types.question_id
INNER JOIN tbl_types ON tbl_types.id = tbl_question_types.type_id
INNER JOIN tbl_question_institutions ON tbl_question_institutions.question_id = tbl_questions.id
INNER JOIN tbl_institutions ON tbl_institutions.id = tbl_question_institutions.institution_id
WHERE tbl_questions.id = 'c7aa15cb-27e5-4f28-9141-483f7cce8e56'
This is a select result

SQL Nesting Logic Needed

I have a situation where I need to build a query that pulls gives me a list of member that used an item on their account in timeframe one but that did not use on item on their account in timeframe two.
SELECT DISTINCT SS.memid
FROM SS
INNER JOIN SSUSED ON SS.ssid = SSUSED.ssid
INNER JOIN MEMBERS AS MEMBERS_1 ON SS.memid = MEMBERS_1.memid
INNER JOIN PRODUCTS ON SS.productid = PRODUCTS.productid
INNER JOIN PRODUCTCATS ON PRODUCTS.productcatid = PRODUCTCATS.productcatid
INNER JOIN EMPLOYEES ON SSUSED.employeeid = EMPLOYEES.employeeid
AND NOT EXISTS
(
SELECT DISTINCT SS_1.memid
FROM SS AS SS_1
INNER JOIN SSUSED AS SSUSED_1 ON SS_1.ssid = SSUSED_1.ssid
INNER JOIN MEMBERS AS MEMBERS_1 ON SS_1.memid = MEMBERS_1.memid
INNER JOIN PRODUCTS AS PRODUCTS_1 ON SS_1.productid = PRODUCTS_1.productid
INNER JOIN PRODUCTCATS AS PRODUCTCATS_1 ON PRODUCTS_1.productcatid = PRODUCTCATS_1.productcatid
INNER JOIN EMPLOYEES AS EMPLOYEES_1 ON SSUSED_1.employeeid = EMPLOYEES_1.employeeid
WHERE
MEMBERS_1.siteid = #rvSite
AND SSUSED_1.usedate BETWEEN #rvStartWeek2
AND #rvEndWeek2
AND PRODUCTS_1.productcatid IN (27,28,29,58,77,75,30,61,31,32,47,68)
)
WHERE MEMBERS_1.siteid = #rvSite
AND SSUSED.usedate BETWEEN #rvStartWeek1
AND #rvEndWeek1
AND PRODUCTS.productcatid IN (27,28,29,58,77,75,30,61,31,32,47,68)
The issue that I am running into is that when I try the query this way I get the following error:

Take MAX TransactionId from LEFT JOIN SQL

Hi i have one sql query in sql server that looks like
SELECT
DTST.[Id],
FPT.[StartTime],
--FPT.[BusinessTypeId],
FPT.[PaymentMethodId],
FPT.[3DSecureId],
FPT.[ProductId],
DPT.[Id],
FPT.[TransactionStatusId],
FPT.[Amount],
DC.[Code],
FPT.[PlayerId],
DPL.[SourceOrigId],
DPL.[Username],
DPL.[FirstName],
DPL.[LastName],
DPL.[BrandId],
DPL.[VIPLevelId],
DPL.[MarketingChannelId],
DPL.[MarketingSourceId],
FPT.[PaymentReasonTextId],
FPT_Ref.[PaymentReasonTextId],
AD.Username,
FPT.[OriginalTransactionId],
FPT.[TransactionId],
FPT_Ref.[OriginalTransactionId],
FPT.[ProviderTransactionOrigId]
FROM WarehouseMgmt.FactPaymentTrans AS FPT
JOIN
(
SELECT TransactionId,MAX(TransactionStepId) TransactionStepId
FROM [WarehouseMgmt].[FactPaymentTrans]
WHERE FactType = ''SOURCE''
GROUP BY TransactionId
) AS FPT_Last ON FPT.TransactionId = FPT_Last.TransactionId AND FPT.TransactionStepId = FPT_Last.TransactionStepId
JOIN WarehouseMgmt.DimTransactionStepType AS DTST ON FPT.[TransactionStepTypeId] = DTST.[Id]
JOIN WarehouseMgmt.DimCurrency AS DC ON FPT.CurrencyId = DC.Id
JOIN WarehouseMgmt.DimPlayer AS DPL ON FPT.PlayerId = DPL.Id
JOIN WarehouseMgmt.DimProduct AS DP ON DP.Id = FPT.ProductId
JOIN WarehouseMgmt.DimProductType AS DPT ON DPT.Id = DP.ProductTypeId
JOIN [WarehouseMgmt].[DimLoyaltyProgramLevel] DLPL ON DLPL.[Id]=DPL.VipLevelId
JOIN WarehouseMgmt.DimAdmin AS AD ON AD.Id=FPT.CreatedByAdminId
JOIN WarehouseMgmt.DimPaymentTransactionBusinessType AS DPTBT ON DPTBT.Id = FPT.BusinessTypeId
JOIN WarehouseMgmt.DimTransactionStatus TS ON TS.Id= FPT.TransactionStatusId
JOIN WarehouseMgmt.DimPaymentMethod DPM ON DPM.Id = FPT.[PaymentMethodId]
JOIN WarehouseMgmt.DimTimeZone DTZ on FPT.[TimeId] = DTZ.TimeUTCId
LEFT JOIN [WarehouseMgmt].[FactPaymentTrans] FPT_Ref ON FPT_Ref.OriginalTransactionId = FPT.TransactionId AND FPT_Ref.FactType = ''SOURCE''
WHERE (FPT.FactType = ''SOURCE'')
AND DTZ.TimeId BETWEEN #DimStartDate AND #DimEndDate ' + #sqlFilters
With this query i have duplicated transactions, because of LEFT JOIN. How can i improve my LEFT JOIN to take only MAX TransactionId in from LEFT JOIN ?

How to retrieve count of records in SELECT statement

I am trying to retrieve the right count of records to mitigate an issue I am having. The below query returns 327 records from my database:
SELECT DISTINCT COUNT(at.someid) AS CountOfStudentsInTable FROM tblJobSkillAssessment AS at
INNER JOIN tblJobSkills j ON j.jobskillid = at.skillid
LEFT JOIN tblStudentPersonal sp ON sp.someid2 = at.someid
INNER JOIN tblStudentSchool ss ON ss.monsterid = at.someid
INNER JOIN tblSchools s ON s.schoolid = ss.schoolid
INNER JOIN tblSchoolDistricts sd ON sd.schoolid = s.schoolid
INNER JOIN tblDistricts d ON d.districtid = sd.districtid
INNER JOIN tblCountySchools cs ON cs.schoolid = s.schoolid
INNER JOIN tblCounties cty ON cty.countyid = cs.countyid
INNER JOIN tblRegionUserRegionGroups rurg ON rurg.districtid = d.districtid
INNER JOIN tblGroups g ON g.groupid = rurg.groupid
WHERE ss.graduationyear IN (SELECT Items FROM FN_Split(#gradyears, ',')) AND sp.optin = 'Yes' AND g.groupname = #groupname
Where I run into trouble is trying to reconcile that with the below query. One is for showing just a count of all the particular students the other is showing pertinent information for a set of students as needed but the total needs to be the same and it is not. The below query return 333 students - the reason is because the school the student goes to is in two separate counties and it counts that student twice. I can't figure out how to fix this.
SELECT DISTINCT #TableName AS TableName, d.district AS LocationName, cty.county AS County, COUNT(DISTINCT cc.monsterid) AS CountOfStudents, d.IRN AS IRN FROM tblJobSkillAssessment AS cc
INNER JOIN tblJobSkills AS c ON c.jobskillid = cc.skillid
INNER JOIN tblStudentPersonal sp ON sp.monsterid = cc.monsterid
INNER JOIN tblStudentSchool ss ON ss.monsterid = cc.monsterid
INNER JOIN tblSchools s ON s.schoolid = ss.schoolid
INNER JOIN tblSchoolDistricts sd ON sd.schoolid = s.schoolid
INNER JOIN tblDistricts d ON d.districtid = sd.districtid
INNER JOIN tblCountySchools cs ON cs.schoolid = s.schoolid
INNER JOIN tblCounties cty ON cty.countyid = cs.countyid
INNER JOIN tblRegionUserRegionGroups rurg ON rurg.districtid = d.districtid
INNER JOIN tblGroups g ON g.groupid = rurg.groupid
WHERE ss.graduationyear IN (SELECT Items FROM FN_Split(#gradyears, ',')) AND sp.optin = 'Yes' AND g.groupname = #groupname
GROUP BY cty.county, d.IRN, d.district
ORDER BY LocationName ASC
If you just want the count, then perhaps count(distinct) will solve the problem:
select count(distinct at.someid)
I don't see what at.someid refers to, so perhaps:
select count(distinct cc.monsterid)

Extract between row counts

How can I make the following statement extract row 100 to row 200:
SELECT Offices.OfficeID, ContractsBooksCommodities.CommodityID
FROM ((((Offices
INNER JOIN tbl_Sales ON Offices.CompanyID = tbl_Sales.CompanyID)
INNER JOIN ContractBooks ON tbl_Sales.CompanyID = ContractBooks.CompanyID)
INNER JOIN ContractsBooksAds ON ContractBooks.ContractNum = ContractsBooksAds.ContractNum)
INNER JOIN ContractsBooksBrands ON ContractsBooksAds.ContractNum = ContractsBooksBrands.ContractNum)
INNER JOIN ContractsBooksCommodities ON ContractsBooksBrands.ContractNum = ContractsBooksCommodities.ContractNum;
This might be slow:
SELECT Top 101 Offices.OfficeID, ContractsBooksCommodities.CommodityID
FROM ((((Offices
INNER JOIN tbl_Sales ON Offices.CompanyID = tbl_Sales.CompanyID)
INNER JOIN ContractBooks ON tbl_Sales.CompanyID = ContractBooks.CompanyID)
INNER JOIN ContractsBooksAds ON ContractBooks.ContractNum = ContractsBooksAds.ContractNum)
INNER JOIN ContractsBooksBrands ON ContractsBooksAds.ContractNum = ContractsBooksBrands.ContractNum)
INNER JOIN ContractsBooksCommodities ON ContractsBooksBrands.ContractNum = ContractsBooksCommodities.ContractNum
WHERE Offices.OfficeID NOT IN (SELECT Top 99 Offices.OfficeID
FROM ((((Offices
INNER JOIN tbl_Sales ON Offices.CompanyID = tbl_Sales.CompanyID)
INNER JOIN ContractBooks ON tbl_Sales.CompanyID = ContractBooks.CompanyID)
INNER JOIN ContractsBooksAds ON ContractBooks.ContractNum = ContractsBooksAds.ContractNum)
INNER JOIN ContractsBooksBrands ON ContractsBooksAds.ContractNum = ContractsBooksBrands.ContractNum)
INNER JOIN ContractsBooksCommodities ON ContractsBooksBrands.ContractNum = ContractsBooksCommodities.ContractNum
ORDER BY Offices.OfficeID, ContractsBooksCommodities.CommodityID)
ORDER BY Offices.OfficeID, ContractsBooksCommodities.CommodityID
I do not know what fields make your sort unique, but that is what you need because MS Access will return matches otherwise.