SQL Nesting Logic Needed - sql

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:

Related

Long query mistake

I've just written a query here at work but I am getting "ORA-00920: invalid relational operator" when running it and I can't seem to find my mistake.
So here goes:
SELECT
D.DEPARTURE_NO,
D.DEPARTURE_DATE,
I.ITEM_GROUP_ID,
L.LUID,
OA.ADDRESS1,
OA.ADDRESS2,
OA.CITY,
OA.COUNTRY_CODE,
OA.NAME,
OA.POSTAL_CODE,
OA.RECEIVER_ID,
OC.COUNTRY_NAME,
OL.ORDER_LINE_NO,
O.INTERNAL_ORDER_ID,
O.RECEIVER_ORDER_ID,
O.STORER_ID,
O.STORER_ORDER_ID,
PL.BIN_LOCATION_ID,
PLL.PICK_STATUS,
PLL.PICKLIST_LINE_NO,
S.SHIPMENT_ID,
SH.NAME,
SH.SHIPPER_ID,
SS.SHIPPER_SERVICES_ID,
ST.NAME,
SA.ADDRESS_TYPE,
SA.ADDRESS1,
SA.ADDRESS2,
SA.ADDRESS3,
SA.CITY,
SA.POSTAL_CODE,
SC.COUNTRY_NAME,
VOPI.PARENT_INTERNAL_ORDER_ID
FROM
ORDERS O
INNER JOIN VY_ORDER_PARENT_ID VOPI ON VOPI.INTERNAL_ORDER_ID = O.INTERNAL_ORDER_ID
INNER JOIN VY_ORDER_PARENT_ID VOPI ON VOPI.INTERNAL_ORDER_ID = OA.INTERNAL_ORDER_ID
INNER JOIN ORDER_ADDRESS OA ON OA.INTERNAL_ORDER_ID = O.INTERNAL_ORDER_ID
INNER JOIN SHIPMENT S ON S.SHIPMENT_ID = O.SHIPMENT_ID
INNER JOIN SHIPPER_SERVICES SS ON SS.SHIPPER_SERVICES_NO = O.SHIPPER_SERVICES_NO
INNER JOIN STORER ST ON ST.STORER_ID = O.STORER_ID
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = O.INTERNAL_ORDER_ID
INNER JOIN SHIPPER SH ON SH.SHIPPER_ID = O.SHIPPER_ID
INNER JOIN ORDER_COUNTRY OC ON OC.COUNTRY_CODE = OA.COUNTRY_CODE
INNER JOIN STORER_COUNTRY SC ON SC.COUNTRY_CODE OA.COUNTRY_CODE
INNER JOIN STORER_ADDRESS SA ON SA.COUNTRY_CODE = SC.STORER_ADDRESS
INNER JOIN STORER ST ON ST.STORER_ID = SA.STORER_ID
INNER JOIN DEPARTURE D ON D.ACTIVE_TRANSPORT_COUNTRY = SC.COUNTRY_CODE
INNER JOIN DEPARTURE D ON D.DEPARTURE_NO = S.DEPARTURE_NO
INNER JOIN ITEM I ON I.COUNTRY_CODE = SC.COUNTRY_CODE
INNER JOIN SHIPPER SH ON SH.SHIPPER_ID = S.SHIPPER_ID
INNER JOIN SHIPPER_SERVICES SS ON SS.SHIPPER_SERVICES_NO = S.SHIPPER_SERVICES_NO
INNER JOIN SHIPPER SH ON SH.SHIPPER_ID = SS.SHIPPER_ID
INNER JOIN IC_TRANSACTION ICT ON ICT.LUID_NO = L.LUID_NO
INNER JOIN PICKLIST_LINE PLL ON PLL.IC_TRANSACTION_NO = ICT.IC_TRANSACTION_NO
INNER JOIN LUID L ON L.STORER_ID = ST.STORER_ID
INNER JOIN ITEM I ON I.STORER_ID = ST.STORER_ID
INNER JOIN ITEM I ON I.ITEM_ID = OL.ITEM_ID AND I.STORER_ID = OL.ITEM_ID
INNER JOIN PICK_LOCATION PL ON PL.ITEM_ID = I.ITEM_ID AND PL.STORER_ID = I.ITEM_ID
INNER JOIN PICKLIST_LINE PLL ON PLL.EXPECTED_BIN_LOCATION_ID = PL.BIN_LOCATION_ID
INNER JOIN PICKLIST_LINE PLL ON PLL.EXPECTED_BUILDING_ID = PL.BUILDING_ID
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = PLL.INTERNAL_ORDER_ID AND OL.ORDER_LINE_NO = PLL.ORDER_LINE_NO
WHERE S.SHIPMENT_ID = '2000518517'
Obviously you can't know the exact column names but I've gone through them multiple times and can't find any spelling errors.
Any other errors
INNER JOIN STORER_COUNTRY SC ON SC.COUNTRY_CODE OA.COUNTRY_CODE
should be
INNER JOIN STORER_COUNTRY SC ON SC.COUNTRY_CODE = OA.COUNTRY_CODE
and it looks like
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = PLL.INTERNAL_ORDER_ID AND OL.ORDER_LINE NO = PLL.ORDER_LINE_NO
should be ( thanks to LONG )
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = PLL.INTERNAL_ORDER_ID AND OL.ORDER_LINE_NO = PLL.ORDER_LINE_NO

getting repeated rows

I am not able to get why i am getting repeated data for below query. Although i have used distinct.
The query is as below- Please help :
Might be some problem with joins
SELECT DISTINCT(UM.USERNAME)'USER_NAME'
,UM.FIRSTNAME + ' ' + UM.LASTNAME AS 'EMPLOYEE NAME'
,US.USER_NAME 'USER_ID'
,US.MS_RIT_REPORTING_GROUP_MST_KEY
,RG.REPORTING_GROUP_NAME
,US.MS_RIT_REGION_MST_KEY
,RM.REGION_NAME
,US.MS_RIT_SUB_REGION_MST_KEY
,SM.SUB_REGION_NAME
,TP.TASK_MST_KEY
,TTM.TASK_TYPE_NAME
,CT.*
,FM.TASK_STATUS
,FM.TASK_START_DATE
,FM.TASK_END_DATE
,SRM.ROLE_NAME AS ROLENAME
FROM USERS_MASTER UM
INNER JOIN MS_RIT_USER_SKILLSET_MAP US ON UM.USERID = US.USER_NAME
INNER JOIN DIM_MS_RIT_REPORTING_GROUP_MST RG ON US.MS_RIT_REPORTING_GROUP_MST_KEY = RG.MS_RIT_REPORTING_GROUP_MST_KEY
AND UPPER(RG.ACTIVE) IN ('YES','1','Y')
INNER JOIN DIM_MS_RIT_REGION_MST RM ON US.MS_RIT_REGION_MST_KEY = RM.MS_RIT_REGION_MST_KEY
AND UPPER(RM.ACTIVE) IN ('YES','1','Y')
INNER JOIN DIM_MS_RIT_SUB_REGION_MST SM ON US.MS_RIT_SUB_REGION_MST_KEY = SM.MS_RIT_SUB_REGION_MST_KEY
AND UPPER(SM.ACTIVE) IN ('YES','1','Y')
INNER JOIN MS_RIT_USER_TASK_MAP TP ON CONVERT(VARCHAR,UM.USERID) = TP.USER_ID
INNER JOIN MS_RIT_CREATE_TASK CT ON CT.TASK_ID = TP.TASK_MST_KEY
INNER JOIN WF_FRM_28_MST FM ON FM.TASK_ID = CT.TASK_ID
INNER JOIN SEC_USER_ROLE SR ON SR.USER_ID = UM.USERNAME
INNER JOIN SEC_ROLE_MST SRM ON SRM.ROLE_CODE = SR.ROLE_CODE
INNER JOIN MS_RIT_TASK_TYPE_MASTER TTM ON CT.TASK_TYPE = TTM.TASK_TYPE_ID
Use UNION to get rid of duplicates
One solution to check if you have duplicates is to do a UNION using the same query, if no rows were removed then they are not duplicates.
(SELECT DISTINCT(UM.USERNAME)'USER_NAME'
,UM.FIRSTNAME + ' ' + UM.LASTNAME AS 'EMPLOYEE NAME'
,US.USER_NAME 'USER_ID'
,US.MS_RIT_REPORTING_GROUP_MST_KEY
,RG.REPORTING_GROUP_NAME
,US.MS_RIT_REGION_MST_KEY
,RM.REGION_NAME
,US.MS_RIT_SUB_REGION_MST_KEY
,SM.SUB_REGION_NAME
,TP.TASK_MST_KEY
,TTM.TASK_TYPE_NAME
,CT.*
,FM.TASK_STATUS
,FM.TASK_START_DATE
,FM.TASK_END_DATE
,SRM.ROLE_NAME AS ROLENAME
FROM USERS_MASTER UM
INNER JOIN MS_RIT_USER_SKILLSET_MAP US ON UM.USERID = US.USER_NAME
INNER JOIN DIM_MS_RIT_REPORTING_GROUP_MST RG ON US.MS_RIT_REPORTING_GROUP_MST_KEY = RG.MS_RIT_REPORTING_GROUP_MST_KEY
AND UPPER(RG.ACTIVE) IN ('YES','1','Y')
INNER JOIN DIM_MS_RIT_REGION_MST RM ON US.MS_RIT_REGION_MST_KEY = RM.MS_RIT_REGION_MST_KEY
AND UPPER(RM.ACTIVE) IN ('YES','1','Y')
INNER JOIN DIM_MS_RIT_SUB_REGION_MST SM ON US.MS_RIT_SUB_REGION_MST_KEY = SM.MS_RIT_SUB_REGION_MST_KEY
AND UPPER(SM.ACTIVE) IN ('YES','1','Y')
INNER JOIN MS_RIT_USER_TASK_MAP TP ON CONVERT(VARCHAR,UM.USERID) = TP.USER_ID
INNER JOIN MS_RIT_CREATE_TASK CT ON CT.TASK_ID = TP.TASK_MST_KEY
INNER JOIN WF_FRM_28_MST FM ON FM.TASK_ID = CT.TASK_ID
INNER JOIN SEC_USER_ROLE SR ON SR.USER_ID = UM.USERNAME
INNER JOIN SEC_ROLE_MST SRM ON SRM.ROLE_CODE = SR.ROLE_CODE
INNER JOIN MS_RIT_TASK_TYPE_MASTER TTM ON CT.TASK_TYPE = TTM.TASK_TYPE_ID)
UNION
(SELECT DISTINCT(UM.USERNAME)'USER_NAME'
,UM.FIRSTNAME + ' ' + UM.LASTNAME AS 'EMPLOYEE NAME'
,US.USER_NAME 'USER_ID'
,US.MS_RIT_REPORTING_GROUP_MST_KEY
,RG.REPORTING_GROUP_NAME
,US.MS_RIT_REGION_MST_KEY
,RM.REGION_NAME
,US.MS_RIT_SUB_REGION_MST_KEY
,SM.SUB_REGION_NAME
,TP.TASK_MST_KEY
,TTM.TASK_TYPE_NAME
,CT.*
,FM.TASK_STATUS
,FM.TASK_START_DATE
,FM.TASK_END_DATE
,SRM.ROLE_NAME AS ROLENAME
FROM USERS_MASTER UM
INNER JOIN MS_RIT_USER_SKILLSET_MAP US ON UM.USERID = US.USER_NAME
INNER JOIN DIM_MS_RIT_REPORTING_GROUP_MST RG ON US.MS_RIT_REPORTING_GROUP_MST_KEY = RG.MS_RIT_REPORTING_GROUP_MST_KEY
AND UPPER(RG.ACTIVE) IN ('YES','1','Y')
INNER JOIN DIM_MS_RIT_REGION_MST RM ON US.MS_RIT_REGION_MST_KEY = RM.MS_RIT_REGION_MST_KEY
AND UPPER(RM.ACTIVE) IN ('YES','1','Y')
INNER JOIN DIM_MS_RIT_SUB_REGION_MST SM ON US.MS_RIT_SUB_REGION_MST_KEY = SM.MS_RIT_SUB_REGION_MST_KEY
AND UPPER(SM.ACTIVE) IN ('YES','1','Y')
INNER JOIN MS_RIT_USER_TASK_MAP TP ON CONVERT(VARCHAR,UM.USERID) = TP.USER_ID
INNER JOIN MS_RIT_CREATE_TASK CT ON CT.TASK_ID = TP.TASK_MST_KEY
INNER JOIN WF_FRM_28_MST FM ON FM.TASK_ID = CT.TASK_ID
INNER JOIN SEC_USER_ROLE SR ON SR.USER_ID = UM.USERNAME
INNER JOIN SEC_ROLE_MST SRM ON SRM.ROLE_CODE = SR.ROLE_CODE
INNER JOIN MS_RIT_TASK_TYPE_MASTER TTM ON CT.TASK_TYPE = TTM.TASK_TYPE_ID)

SQL Statement Help Needed:Query within a Query

I am working on a query where I need to compile a list of of members who have a particular group of products associated with their, from a particular site and then for each member in that list retrieve all of the products on their account.
The first piece of the query is this:
SELECT
MEMBERS.memid
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE
(PRODUCTS.productcatid = 77)
AND (SS.initialdate BETWEEN #rvPurchaseStart AND #rvPurchaseEnd)
From that list, I then need the following query to run for each member in that list:
SELECT
SITES.sitename, MEMBERS.scancode, MEMBERS.lname,
MEMBERS.fname, MEMBERS.mtypeid, MEMBERS.status,
PRODUCTS.description, SS.initialdate,
SS.initialquantity, SS.usedquantity, SS.dateexpire,
EMPLOYEES.lname AS Expr1, EMPLOYEES.fname AS Expr2
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE
(PRODUCTS.productcatid <> '68')
AND (PRODUCTS.departmentid = '5')
AND (MEMBERS.status = 'A')
AND (SS.usedquantity < SS.initialquantity)
AND (PRODUCTS.scancode <> 'PASSCOMP1' OR
PRODUCTS.scancode <> 'PASSCOMP3' OR
PRODUCTS.scancode <> 'PASSCOMP5')
AND (PRODUCTS.inactive = 'False')
I really appreciate the help!!!
Sometnihg like this? Just add the fist query as table to your second query.
SELECT
SITES.sitename, MEMBERS.scancode,
MEMBERS.lname, MEMBERS.fname, MEMBERS.mtypeid,
MEMBERS.status, PRODUCTS.description, SS.initialdate,
SS.initialquantity, SS.usedquantity, SS.dateexpire,
EMPLOYEES.lname AS Expr1, EMPLOYEES.fname AS Expr2
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
INNER JOIN
(SELECT
MEMBERS.memid
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE
(PRODUCTS.productcatid = 77)
AND (SS.initialdate BETWEEN #rvPurchaseStart AND #rvPurchaseEnd)) TMP_TABLE ON MEMBERS.memid = TMP_TABLE.memid
WHERE
(PRODUCTS.productcatid <> '68')
AND (PRODUCTS.departmentid = '5')
AND (MEMBERS.status = 'A')
AND (SS.usedquantity < SS.initialquantity)
AND (PRODUCTS.scancode <> 'PASSCOMP1' OR PRODUCTS.scancode <> 'PASSCOMP3' OR PRODUCTS.scancode <> 'PASSCOMP5')
AND (PRODUCTS.inactive = 'False')

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.

Terrible access database issue

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