Query to select last order (entry) of every product belonging to user that's not returned - sql

I am stuck with rather confusing query.
Assume I have a ProductLending table that tracks what product each user has borrowed, when it was renewed, was it returned or not etc.. Given a user, I want to be able to select, all unique products that are still with the user.
table example:
userid DateRenewed ProductId isReturned
````````````````````````````````````````````````
1 2011-07-21 15 0
1 2011-08-20 16 0
1 2011-09-21 15 1
2 2011-09-21 17 0
1 2011-09-21 15 0
This is a mock up so sorry if it's not accurate.
Now, given userId = 1, I want to select just unique productId that are NOT returned, but are with the user. So this should give me 15, 16 as result, as even though 15 was returned, it was re-borrowed. If we delete the last row, then the result would just be 16, since user has only 16 with him.
I tried ordering by dateRenewed and selecting top 1 but it did totally something else.. how do I construct a query for this please?

If product is not returned by user, then the sum of bought products must be larger than sum of returned products
SELECT userid,ProductId FROM <table>
GROUP BY userid,ProductId HAVING SUM(CASE CAST(isReturned AS INT) WHEN 0 THEN 1 ELSE 0 END)-SUM(CAST(isReturned AS INT))>0

Try this:
;WITH qry AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY userID, ProductID ORDER BY DateRenewed DESC) rn
FROM YourTable
)
SELECT *
FROM qry
WHERE rn = 1 AND isReturned = 0;

select distinct ProductId
from TABLE_NAME t1
where UserId= #UserId
and IsReturned = 0
and not exists
(
select *
from TABLE_NAME t2
where t2.UserId = t1.UserId
and t2.ProductId = t1.ProductId
and t2.IsReturned = 1
and t2.DateRenewed > t1.DateRenewed
)

Related

Last record per transaction

I am trying to select the last record per sales order.
My query is simple in SQL Server management.
SELECT *
FROM DOCSTATUS
The problem is that this database has tens of thousands of records, as it tracks all SO steps.
ID SO SL Status Reason Attach Name Name Systemdate
22 951581 3 Processed Customer NULL NULL BW 2016-12-05 13:33:27.857
23 951581 3 Submitted Customer NULL NULL BW 2016-17-05 13:33:27.997
24 947318 1 Hold Customer NULL NULL bw 2016-12-05 13:54:27.173
25 947318 1 Invoices Submit Customer NULL NULL bw 2016-13-05 13:54:27.300
26 947318 1 Ship Customer NULL NULL bw 2016-14-05 13:54:27.440
I would to see the most recent record per the SO
ID SO SL Status Reason Attach Name Name Systemdate
23 951581 4 Submitted Customer NULL NULL BW 2016-17-05 13:33:27.997
26 947318 1 Ship Customer NULL NULL bw 2016-14-05 13:54:27.440
Well I'm not sure how that table has two Name columns, but one easy way to do this is with ROW_NUMBER():
;WITH cte AS
(
SELECT *,
rn = ROW_NUMBER() OVER (PARTITION BY SO ORDER BY Systemdate DESC)
FROM dbo.DOCSTATUS
)
SELECT ID, SO, SL, Status, Reason, ..., Systemdate
FROM cte WHERE rn = 1;
Also please always reference the schema, even if today everything is under dbo.
I think you can keep it this simple:
SELECT *
FROM DOCSTATUS
WHERE ID IN (SELECT MAX(ID)
FROM DOCSTATUS
GROUP BY SO)
You want only the maximum ID from each SO.
An efficient method with the right index is a correlated subquery:
select t.*
from t
where t.systemdate = (select max(t2.systemdate) from t t2 where t2.so = t.so);
The index is on (so, systemdate).

SQL: status flips between 1 and 2; select all statuses, which are 2 since the last time it was 1

I have write only DB log of changes I keep track of (or statuses) and values fluctuate between 1 and 2. In the below table; ID is identity column, STATUS is either 1 or 2 and USER is a user id.
If the latest status (i.e. max ID) for a given user is 1 then my query should return nothing (1 = good). So running the query against the data above would be just that.
Here comes my question: I want to query for all statuses of 2 since the last time it was 1. Here is sample data:
In this case my query should return 2 and 3 (ID) because these have statuses of 2 since the last time it was 1.
This next query should return nothing because the latest status for this user was switched to 1:
And finally this next one should return 5 (because the latest status is 2 since the last time it was 1):
There is no date field in this table, you can only work with MAX(ID) ... GROUP BY ID, USER
How can I do this? I'm using MS SQL 2016.
You can use windowed aggregates to do this
WITH T
AS (SELECT ID,
STATUS,
[USER],
MAX(CASE WHEN Status = 1 THEN ID END) OVER ( PARTITION BY [USER]) AS MaxS1
FROM YourTable)
SELECT *
FROM T
WHERE Status = 2
AND (ID > MaxS1 OR MaxS1 IS NULL)
Remove the OR MaxS1 IS NULL if you don't want the rows returned for users that have 2 and have never had 1 as a status
You can filter with not exists:
select t.*
from mytable t
where
t.status = 2
and not exists (
select 1 from mtyable t1 where t1.user = t.user and t1.id > t.id and t1.status = 1
)
This phrases as: all records with status 2 that have no following record (ie a record with the same user and a greatest id) with status = 1. If there are no records with status = 1 for a given user, all its records will be returned.
This can also be expressed with a left join antipattern:
select t.*
from mytable t
left join mytable t1 on t1.user = t.user and t1.id > t.id and t1.status = 1
where t1.id is null and t.status = 2

Subquerying To Obtain Specific Values From Table

I need help with querying and will do my best to explain my issue. I have the following table below of 11 rows which is created from importing values from SharePoint list.
ID SHPT_ID STATUS_DESC REC_UPDT_DTTM REC_CRTE_DTTM EXPIR_DT
1 270 Active 1-18-2019 1-19-2019 1-24-2019
2 270 In Progress 1-23-2019 1-24-2019 2-3-2019
3 270 Completed 2-2-2019 2-3-2019 2-19-2019
4 270 Completed 2-18-2019 2-19-2019 2-28-2019
5 270 In Progress 2-27-2019 2-28-2019 3-2-2019
6 270 Completed 3-1-2019 3-2-2019 3-6-2019
7 270 Completed 3-5-2019 3-6-2019 12-31-9999
8 295 Active 12-20-2018 12-21-2018 12-26-2018
9 295 Completed 12-25-2018 12-26-2018 12-31-9999
10 345 Active 6-7-2017 6-8-2017 6-14-2017
11 345 Completed 6-13-2017 6-14-2017 6-22-2017
12 345 Completed 6-21-2017 6-22-2017 12-31-9999
The last record associated to a particular SharePoint ID brings in the EXPIR_DT (Expire Date) of '12/31/9999'. Everytime a value in the SharePoint ID record is updated, a new row is created.
From this table, I am trying to pull back 3 rows in particular (rows where ID = #6, 9, and 11.)
These are the records having the minimum REC_UPDT_DTTM when STATUS_DESC equals 'Completed' for the last time. For the rows where SharePoint ID = 270, there is an instance when the record was 'Completed' but was reversed to 'In Process' and then later was put back in to 'Completed.' For this record, it should not take the row where ID =3, it should take the row where ID = 6.
Is there anyone who can help me with this code as I am stuck with how to proceed to get the rows that I want? I know I have to use subquerying and functions but I am really stuck at the moment.
Please let me know if need more info.
This query works for the dataset above. However, if a SHPT_ID could have two Completed records on the same day, this will return two rows for that SHPT_ID:
SELECT m.*
FROM MyTable m
INNER JOIN (
SELECT Min(Rec_UPDT_DTTM) MinUpdt,
Shpt_ID
FROM MyTable m1
WHERE Status_Desc = 'Completed'
AND NOT EXISTS (
SELECT *
FROM MyTable m2
WHERE m2.Shpt_ID = m1.Shpt_ID
AND m2.REC_UPDT_DTTM > m1.REC_UPDT_DTTM
AND m2.Status_Desc <> 'Completed'
)
) filter
ON filter.MinUpdt = m.REC_UPDT_DTTM
AND filter.Shpt_ID = m.Shpt_ID
To handle the case with duplicates on the same day, the code would look like this:
SELECT MyTable.*
FROM MyTable
INNER JOIN (
SELECT Shpt_ID,
MIN(ID) as ID
FROM MyTable m
INNER JOIN (
SELECT Min(Rec_UPDT_DTTM) MinUpdt,
Shpt_ID
FROM MyTable
WHERE Status_Desc = 'Completed'
AND NOT EXISTS (
SELECT *
FROM MyTable m2
WHERE m2.Shpt_ID = m1.Shpt_ID
AND m2.REC_UPDT_DTTM > m1.REC_UPDT_DTTM
AND m2.Status_Desc <> 'Completed'
)
) filter
ON filter.MinUpdt = MyTable.REC_UPDT_DTTM
AND filter.Shpt_ID = MyTable.Shpt_ID
) as IDs
ON MyTable.ID = IDs.ID
Not sure if I understood your problem, but since the date format is mm-dd-yyyy, 2-2-2019 (#3) is less than 3-1-2019 (#6).
Use MIN and GROUP BY. Something like:
SELECT [ID], [SHPT_ID], [STATUS_DESC], MIN(REC_UPDT_DTTM), [REC_CRTE_DTTM], [EXPIR_DT] FROM [myTable] WHERE [STATUS_DESC] = 'Completed' GROUP BY [SHPT_ID].

SELECT records based on criteria

I'm looking to find a way to select an ID (PersonID) based on criteria. Should this personID not meet the criteria, I need to select another one. To make it visual I have two tables:
1.
ID Company LatestOrder
50593 TEST 1
10955 TEST 2
2.
ID Criteria_A Criteria_B
50593 1 0
10955 0 0
I need to find a way to SELECT ID from ``table1 withlatestOrder = 1 Check whether this person meets criteria from table 2 (value 1 for any criteria A or B), then if Yes, select ID from table1 with latestOrder = 2.
And check the criteria as long as table1.lastorder > 0.
Does it makes sense? I've been bogged with CASE's and IIF's, CTA's but I just cannot seem to find a way to work. I'd love someone to shed some light, in which function direction should I go.
Big thanks for any help guys! This is the 3rd version :(
HERE is where I got so far:
WITH LastOrdered AS
(
SELECT
OrderBillToID AS PersonID
, OrderDate
, OrderBillToCompanyID AS CompanyID
, OrderBillToCompany
, ROW_NUMBER() OVER(PARTITION BY ODE.OrderBilltoCompanyID ORDER BY ODE.OrderDate DESC) AS LatestOrder
FROM dbo.OrderDetails ODE
INNER JOIN dbo.Persons PER ON ODE.OrderBillToID = PER.ID AND PER.Segment = 'B' AND ODE.OrderBillToCompanyID IS NOT NULL
)
Second try
SELECT
PER.ID
, PER.Company
, LatestOrder
, CASE WHEN PER.ExcludeAll = 0 AND (PER.Age > 17 OR PER.Age = 0) AND PER.DateOfDeath = '1900-01-01' AND PER.TLC = 'NA' AND PER.BulkAskExclude = 0 AND PER.EmailExclude = 0 THEN LastOrder
....here is when I realised that this case won't work.....
FROM dbo.Persons PER
INNER JOIN LastOrdered LO ON LO.PersonID = PER.ID
EXPECTED output would be:
ID Company
10955 TEST
......
Basically, you want the last person who ordered but not does not satisfy any of the criteria in Table2:
SELECT TOP 1
id, Company
FROM Table1
WHERE
id NOT IN(
SELECT id
FROM Table2
WHERE
Criteria_A = 1
OR Criteria_B = 1
)
ORDER BY LatestOrder DESC

How do I fix this SQL query returning improper values?

I am writing an SQL query which will return a list of auctions a certain user is losing, like on eBay.
This is my table:
bid_id bid_belongs_to_auction bid_from_user bid_price
6 7 1 15.00
8 7 2 19.00
13 7 1 25.00
The problematic area is this (taken from my full query, placed at the end of the question):
AND EXISTS (
SELECT 1
FROM bids x
WHERE x.bid_belongs_to_auction = bids.bid_belongs_to_auction
AND x.bid_price > bids.bid_price
AND x.bid_from_user <> bids.bid_from_user
)
The problem is that the query returns all the auctions on which there are higher bids, but ignoring the user's even higher bids.
So, an example when the above query works:
bid_id bid_belongs_to_auction bid_from_user bid_price
6 7 1 15.00
7 7 2 18.00
In this case, user 1 is returned as losing the auction, because there is another bid higher than the users bid.
But, here is when the query doesn't work:
bid_id bid_belongs_to_auction bid_from_user bid_price
6 7 1 15.00
8 7 2 19.00
13 7 1 25.00
In this case, user 1 is incorrectly returned as losing the auction, because there is another bid higher than one of his previous bids, but the user has already placed a higher bid over that.
If it's important, here's my full query, but I think it won't be necessary to solve the aforementioned problem, but I'm posting it here anyway:
$query = "
SELECT
`bid_belongs_to_auction`,
`auction_unixtime_expiration`,
`auction_belongs_to_hotel`,
`auction_seo_title`,
`auction_title`,
`auction_description_1`
FROM (
SELECT
`bid_belongs_to_auction`,
`bid_from_user`,
MAX(`bid_price`) AS `bid_price`,
`auctions`.`auction_enabled`,
`auctions`.`auction_unixtime_expiration`,
`auctions`.`auction_belongs_to_hotel`,
`auctions`.`auction_seo_title`,
`auctions`.`auction_title`,
`auctions`.`auction_description_1`
FROM `bids`
LEFT JOIN `auctions` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
WHERE `auction_enabled`='1' AND `auction_unixtime_expiration` > '$time' AND `bid_from_user`='$userId'
AND EXISTS (
SELECT 1
FROM bids x
WHERE x.bid_belongs_to_auction = bids.bid_belongs_to_auction
AND x.bid_price > bids.bid_price
AND x.bid_from_user <> bids.bid_from_user
)
GROUP BY `bid_belongs_to_auction`
) AS X
WHERE `bid_from_user`='$userId'
";
Here's a different approach:
$query = "
SELECT
`max_bids`.`bid_belongs_to_auction`,
`auctions`.`auction_unixtime_expiration`,
`auctions`.`auction_belongs_to_hotel`,
`auctions`.`auction_seo_title`,
`auctions`.`auction_title`,
`auctions`.`auction_description_1`
FROM `auctions`
INNER JOIN (
SELECT
`bid_belongs_to_auction`,
MAX(`bid_price`) AS `auction_max_bid`,
MAX(CASE `bid_from_user` WHEN '$userId' THEN `bid_price` END) AS `user_max_bid`
FROM `bids`
GROUP BY `bid_belongs_to_auction`
) AS `max_bids` ON `auctions`.`auction_id` = `max_bids`.`bid_belongs_to_auction`
WHERE `auctions`.`auction_enabled`='1'
AND `auctions`.`auction_unixtime_expiration` > '$time'
AND `max_bids`.`user_max_bid` IS NOT NULL
AND `max_bids`.`user_max_bid` <> `max_bids`.`auction_max_bid`
";
Basically, when you are retrieving the max bids for all the auctions, you are also retrieving the specific user's max bids along. Next step is to join the obtained list to the auctions table and apply an additional filter on the user's max bid being not equal to the auction's max bid.
Note: the `max_bids`.`user_max_bid` IS NOT NULL condition might be unnecessary. It would definitely be so in SQL Server, because the non-nullness would be implied by the `max_bids`.`user_max_bid` <> `max_bids`.`auction_max_bid` condition. I'm not sure if it's the same in MySQL.
Untested, but this is how I would approach it. Ought to perform OK if there's an index on userid and also one on auctionid.
select OurUserInfo.auctionid, OurUserInfo.userid,
OurUserInfo.ourusersmaxbid, Winningbids.TopPrice
from
(
select A.auctionid, A.userid, max(A.price) as OurUsersMaxBid
from auctions A where userid = ?
group by A.auctionid, A.userid
) as OurUserInfo
inner join
(
-- get the current winning bids for all auctions in which our user is bidding
select RelevantAuctions.auctionid, max(auctions.price) as TopPrice
from auctions inner join
(
select distinct auctionid from auctions where userid = ? -- get our user's auctions
) as RelevantAuctions
on auctions.auctionid = RelevantAuctions.auctionid
group by RelevantAuctions.auctionid
) as WinninBids
on OurUserInfo.auctionid = winningbids.auctionid
where WinninBids.TopPrice > OurUserInfo.ourusersmaxbid
Instead of
SELECT 1
FROM bids x
WHERE x.bid_belongs_to_auction = bids.bid_belongs_to_auction
AND x.bid_price > bids.bid_price
AND x.bid_from_user <> bids.bid_from_user
try this:
SELECT 1
FROM (SELECT BID_ID,
BID_BELONGS_TO_AUCTION,
BID_FROM_USER,
BID_PRICE
FROM (SELECT BID_ID,
BID_BELONGS_TO_AUCTION,
BID_FROM_USER,
BID_PRICE,
RANK ()
OVER (
PARTITION BY BID_BELONGS_TO_AUCTION, BID_FROM_USER
ORDER BY BID_PRICE DESC)
MY_RANK
FROM BIDS)
WHERE MY_RANK = 1) x
WHERE x.bid_belongs_to_auction = bids.bid_belongs_to_auction
AND x.bid_price > bids.bid_price
AND x.bid_from_user <> bids.bid_from_user;