how to compare two sql query values - sql-server

I have two tables StockOutward and product outward..I have to fetch the sum(qty) that not equals to sum(qty) stockOutward..
StockOutward
Id ProductId Qty Location Orderid
1 7 2 2 38
2 8 1 2 38
3 7 1 2 38
ProductOutward
Id ProductId Qty Location Orderid
1 7 12 2 38
2 8 1 2 38
I need the output from stockoutward as ProductId 7
I have used the below query
Select
sum(qty) as Qty,ProductId
from
StockOutward
where
Orderid='38'
group by
ProductId
Union
Select
sum(qty) as Qty,
ProductId
from
ProductOutward
where
Orderid='38'
group by
ProductId

You could use JOIN and filter for inequality:
SELECT
s.ProductId
FROM (
SELECT
ProductId,
SumQty = SUM(Qty)
FROM StockOutward
GROUP BY ProductId
)s
INNER JOIN (
SELECT
ProductId,
SumQty = SUM(Qty)
FROM ProductOutward
GROUP BY ProductId
)p
ON p.ProductId = s.ProductId
AND p.SumQty <> s.SumQty

Select sum(qty) as Qty,ProductId from StockOutward where Orderid='38' group by ProductId
except
Select sum(qty) as Qty,ProductId from ProductOutward where Orderid='38' group by ProductId

Related

How to add and deduct quantity in inventory for SQL Server

I am developing simple inventory system but I am having hard time getting the accurate stock on hand after calculating the 3 quantities from 3 different tables. My goal is to add the sum of receiving_stock + sum of returning_stock - the outgoing_stock. But if outgoing_stock has no data all the records are null.
Here’s my data and query.
receiving_stock
Prod_ID, Qty
123 10
124 10
returning_stock
Prod_ID, Qty
124 10
125 10
outgoing_stock
No Data Yet
Actual Result:
Prod_id, qty
Null 10
Null 20
Null 10
Desired Result:
Prod_id, qty
123 10
124 20
125 10
Query:
Select prod_id, isnull(qty,0)-isnull(sold,0) on-hand
Select prod_id, sum(qty) qty
(
select prod_id,qty
From receiving_stock
Union all
select prod_id,qty
From returning_stock
) Za
Group by prod_id
) Zb
Left join
(
From
Select prod_id, sum(qty) sold
From outgoing_stock
Group by prod_id
) zc
) zd
On
Zb.prod_id=zd.prod_id
You can include the third table in UNION ALL with a negative quantity :
SELECT prod_id,qty
From receiving_stock
UNION ALL
SELECT prod_id,qty
From returning_stock
UNION ALL
SELECT prod_id, -qty
From outgoing_stock

SQL Server : getting the max and min of a sum aggregate

I have a list of orders for products with a ProductID and the quantity ordered.
For example:
ProductID Quantity
------------------
1 5
2 2
3 5
1 2
3 4
2 8
How do I get only the ProductID of the product which sold the most and least in SQL SERVER. I tried:
SELECT ProductID, SUM(Quantity) AS Total
FROM [Order Details]
GROUP BY ProductID
ORDER BY Total DESC
Now need only the max and min of Total.
Use a common table expression or a derived table to get the sum, and then query for min and max:
WITH CTE AS
(
SELECT ProductID, sum(Quantity) as Total
FROM [Order Details]
GROUP BY ProductID
)
SELECT MIN(Total) As Lowest, MAX(Total) AS highest
FROM CTE
We can try using ROW_NUMBER here, twice:
WITH cte AS (
SELECT ProductID, SUM(Quantity) AS Total,
ROW_NUMBER() OVER (ORDER BY SUM(Quantity)) rn_least,
ROW_NUMBER() OVER (ORDER BY SUM(Quantity) DESC) rn_greatest
FROM [Order Details]
GROUP BY ProductID
)
SELECT
ProductID,
CASE WHEN rn_least = 1
THEN 'least' ELSE 'GREATEST' END AS label,
Total
FROM cte
WHERE
rn_least = 1 OR rn_greatest = 1
ORDER BY
Total;

How can I find duplicate on one column

I have a SQL server database,and there are many duplicate in one(RanjePhoneNumber) column.
I am trying to select rows from a table that have duplicates in RanjePhoneNumber column and they have a same CityId.
My Table:
RanjePhoneNumber ContactId CityId
776323 280739 7
342261 186372 80
468284 75980 7
776323 101969 9
362875 170242 13
224519 164914 7
342261 203606 55
776323 280733 7
342261 203602 80
My expected results:
RanjePhoneNumber ContactId CityId
776323 280739 7
342261 186372 80
776323 280733 7
342261 203602 80
Group by those two columns:
SELECT RanjePhoneNumber, CityID
FROM dbo.TableName
GROUP BY RanjePhoneNumber, CityID
HAVING COUNT(*) > 1
If you want to select all columns you could use a ranking function:
WITH CTE AS
(
SELECT t.*, Cnt = COUNT(*) OVER (PARTITION BY RanjePhoneNumber, CityID)
FROM dbo.TableName
)
SELECT RanjePhoneNumber, ContactId, CityId
FROM CTE
WHERE Cnt > 1
If you don't want to find all rows which belong to this "duplicate-group" but only all but the first, use the ROW_NUMBER approach the other answer has shown.
;with cte
as
(select
Ranjephonenumber,
contactid,
cityid,
row_number() over (partition by Ranjephonenumber,cityid order by cityid) as rn
from table
)
select
Ranjephonenumber,contactid,city from cte where rn>1

SQL count number of groups

I have a table with pallets, items, item quantity:
pallet | item | qty
-------------------
1 1 2
1 2 4
2 3 2
2 5 3
3 4 4
I need to find count(pallet), count(item), sum(qty)
count(pallets) | count(items) | sum(qty)
----------------------------------------
3 5 15
I can get the sum(qty) and count(item) with
select count(0) as totalItems, sum(qty) as total from table
Is there a way to get the number of pallets without a sub-query?
Yes, use DISTINCT
select count(distinct pallet) as pallets,
sum(qty) as total,
count(*) as totalItems
from your_table
Simply use Distinct to avoid duplicate records to be count.
count(Distinct pallet)
Your query like this
select
count(distinct pallet) as pallets,
sum(qty) as Total,
count(item) AS [Total Items]
it will give output AS :

Problem in query in sql server 2008

I have a table called album with the following columns:
Album_Id, User_Id, Report_Id, PhotoName
There are different Photoname on a User_Id and Report_Id combination
Like:
Album_Id User_Id Report_Id PhotoName
1 1 16 A.jpg
2 1 16 B.jpg
3 2 17 C.jpg
4 2 17 D.jpg
I just want to retrieve data in format
User_Id Report_Id PhotoName1 PhotoName2
1 16 A.jpg B.jpg
2 17 C.jpg D.jpg
Max Photos are 4...
WITH cte
AS ( SELECT * ,
ROW_NUMBER() OVER ( PARTITION BY User_Id, Report_Id ORDER BY Album_Id ) AS RN
FROM T
)
SELECT User_Id ,
Report_Id ,
MAX(CASE WHEN RN = 1 THEN PhotoName
END) AS PhotoName1 ,
MAX(CASE WHEN RN = 2 THEN PhotoName
END) AS PhotoName2 ,
MAX(CASE WHEN RN = 3 THEN PhotoName
END) AS PhotoName3 ,
MAX(CASE WHEN RN = 4 THEN PhotoName
END) AS PhotoName4
FROM cte
GROUP BY User_Id ,
Report_Id
SELECT a.User_Id, a.Report_Id, a.PhotoName as 'PhotoName1', a2.PhotoName as 'PhotoName2', a3.PhotoName as 'PhotoName3', a4.PhotoName as 'PhotoName4'
FROM album a
LEFT JOIN album a2 on a2.User_Id = a.User_Id and a2.Report_Id = a.Report_Id AND a2.Album_Id != a.Album_Id
LEFT JOIN album a3 on a3.User_Id = a.User_Id and a3.Report_Id = a.Report_Id AND a3.Album_id NOT IN (a.Album_Id, a2.Album_Id)
LEFT JOIN album a4 on a4.User_Id = a.User_Id and a4.Report_Id = a.Report_Id AND a4.Album_id NOT IN (a.Album_Id, a2.Album_Id, a3.Album_Id)
ORDER BY a.User_Id, a.Report_Id ASC;

Resources