Hey guys I'm debugging an issue where a specific record doesn't have a value and I'm trying to go about finding all records where this issue happens.
In my table there is a record for each Event for the same OrderId and ProductId. There should be a Event called ShipConfirm and I want to find every ProductId or OrderId within my table that is missing that Event.
The table looks like this:
OrderId
ProductId
Event
1
9845
Checkout
1
9845
CheckInventory
1
9845
SupplierAssignment
Any help would be greatly appreciated!
You can use
Select distinct OrderId, ProductId from Table T
where Event != 'ShipConfirm'
and concat(OrderId,ProductId) not in (Select distinct concat(OrderId,ProductId)
from table where OrderId = t.OrderId
and ProductId = T.ProductId and Event = 'ShipConfirm');
Aggregation provides one simple option:
SELECT OrderId, ProductId
FROM yourTable
GROUP BY OrderId, ProductId
HAVING COUNT(CASE WHEN Event = 'ShipConfirm' THEN 1 END) = 0;
A slightly different answer:
with
input as (
select 1 as OrderId, 9845 as ProductId, 'Checkout' as Event union all
select 1, 9845, 'CheckInventory' union all
select 1, 9845, 'SupplierAssignment'
)
select OrderId, ProductId
from input
group by OrderId, ProductId
having countif(Event = 'ShipConfirm') = 0
I am selecting records from a table as -
Query 1:
Declare #tId varchar(50);
Select top(1) #tId = TransId
From Table1
Where FName = 'Vincent' And LName = 'Hall'
And CustId = 1
Order By TimeStamp Desc
Query 2:
If #tId IS NULL
Select top(1) #tId = TransId
From Table1
Where FName = 'Vincent' And LName = 'Hall'
Order By TimeStamp Desc
I want to get the Transaction ID from table based on FName, LName and CustId in Query 1. If Transaction Id is not found then run the Query 2.
Is there any better way to achieve this.
Although not necessarily faster, you can combine the two queries into one:
Select top (1) #tId = TransId
From Table1
Where FName = 'Vincent' And LName = 'Hall'
Order By (case when CustId = 1 then 1 else 2 end),
TimeStamp Desc;
You can speed this query by using an index on table1(FName, LName). If there are just a handful of rows matching each FName/LName combination, then this should be quite fast.
My table (named Inventory) is the following:
InventoryType,LocationID,InventoryLevel,SafetyStock,MaxLevel,ModifiedAt
Erasors,1,14,3,15,11-2014
Erasors,2,4,10,50,10-2014
Erasors,2,5,10,50,11-2014
Pencils,1,10,5,45,11-2014
Pencils,2,23,15,50,11-2014
Pens,1,9,10,50,11-2014
Pens,2,55,10,50,12-2014
There are three primary keys: InventoryType, LocationID, and ModifiedAt.
Given a specified LocationID, I'd like the query to return all for each distinct InventoryType where the date for each tuple returned is the most recent ModifiedAt date among records for that given inventory type with the specified LocationID. Eg, LocationID = 2 would return:
InventoryType,LocationID,InventoryLevel,SafetyStock,MaxLevel,ModifiedAt
Erasors,2,5,10,50,11-2014
Pencils,2,23,15,50,11-2014
Pens,2,55,10,50,12-2014
My attempt thus far is the following:
SELECT InventoryType, LocationID, InventoryLevel,
SafetyStock, MaxLevel, ModifiedAt
FROM Inventory
WHERE LocationID = 2 AND ModifiedAt IN
(SELECT TOP 1 ModifiedAt
FROM Inventory
WHERE LocationID = 2
ORDER BY ModifiedAt DESC);
My query returns:
InventoryType,LocationID,InventoryLevel,SafetyStock,MaxLevel,ModifiedAt
Pens,2,55,10,50,12-2014
Any help would be appreciated.
You need a correlated subquery. Try this instead:
SELECT InventoryType, LocationID, InventoryLevel, SafetyStock, MaxLevel, ModifiedAt
FROM Inventory as i
WHERE LocationID = 2 AND
ModifiedAt IN (SELECT TOP 1 i2.ModifiedAt
FROM Inventory as i2
WHERE i2.LocationID = 2 AND i2.InventoryType = i.InventoryType
ORDER BY i2.ModifiedAt DESC
);
Use SELECT DISTINCT for selecting distinct InventoryType.
SELECT DISTINCT InventoryType, LocationID,
InventoryLevel, SafetyStock, MaxLevel, ModifiedAt
FROM Inventory
WHERE LocationID = 2
AND ModifiedAt IN
(SELECT TOP 1 ModifiedAt FROM Inventory WHERE LocationID = 2 ORDER BY ModifiedAt DESC)
how to do an sql statement like this one that actually works:
select distinct store_id as myid,
(select count(*) from products where store_id = myid and delete_flag = true)
from products
where delete_flag = true
I want a column with every store id and the number of deleted products with this store id.
select store_id,
count(*) as all_product_count,
sum(case when delete_flag = 1 then 1 else 0 end) as deleted_product_count
from products
group by store_id
Hope it helps!
select store_id as myid , count(*) as noofdeleteditems from products where delete_flag = true group by store_id;
this is my table structure,
create table ArticleTbl
(
ArticleID bigint identity(1,1),
ProductID int ,
ArticleName varchar(100),
PubDate datetime,
AuthorName varchar(50),
AuthorImage bit,
HtmlValues nvarchar(max)
)
here productid are
1=creditcard,2=prepaidcard,3 saving account,.........
each productid is having multiple rows of records ,
i want to select latest 2 records of each productid in one shot instead of going to database each time .
my procedure now is like..
create proc USP_GetArticle_ByProduct(#ProductID int) as
select top(2) * from ArticleTbl where ProductID=#ProductID
if i use this procedure each productid i have to go to database...
how to get one shot all product(latest 2 records ) using query????
SELECT
*
FROM
(
SELECT
/*Random order per product*/
ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY NEWID() ) AS Ranking,
*
FROM
ArticleTbl
) foo
WHERE
foo.Ranking <= 2
i figure this is on sql server yeah?
if so, you could do this...
select a1.*
from Articletbl a1
where a1.articleid in
(select top 2 a2.articleid
from ArticleTbl a2
where a2.productid = a1.productid
order by a2.articleid DESC)
order by a1.ProductID