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 :
Related
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
I have table like this
Id | Name | Status
------+------------+--------------
1 example1 3
1 example2 2
2 example3 3
2 example4 1
3 example5 1
4 example6 3
How To Write SELECT That Get To ME Result Like This
Id | Name | Status | Count_All
------+------------+--------------+------------
1 example1 3 6
2 example2 2 6
3 example3 3 6
4 example4 1 6
5 example5 1 6
6 example6 3 6
The Value Of Column Count_All, Is Count All Rows
Please use this solution..
SELECT Id , [Name] , [Status], COUNT(*) OVER() Count_All
FROM yourTableName
If I am understanding you correctly, you want a count of all rows in the table as the column Count_All, so Add a Count on column id to get all rows as a new column named Count_All then Group by your other columns to allow for the aggregate Count method.
SELECT [Id], [Name], [Status], COUNT([Id]) AS [Count_All]
FROM [dbo].[YourTable]
GROUP BY [Id], [Name], [Status]
This will do it:
SELECT
y.ID,
y.Name,
y.Status,
Count_All = (SELECT COUNT(*) FROM yourtable)
FROM
yourtable AS y
SELECT *,COUNT(1) OVER() AS COUNT FROM TABLE
I would like to get sequence number in mssql. Please see below.
There is a table.
UserID Score TeamID
------------------------------
1 100 1
2 200 1
3 500 2
4 600 2
5 700 2
6 1000 3
I would like to sort by total score group by team. see below
RankID UserID Score TeamID TotalScore
-----------------------------------------------------------
1 3 500 2 1800
1 4 600 2 1800
1 5 700 2 1800
2 6 1000 3 1000
3 1 100 1 300
3 2 200 1 300
I want to code only one sql query. Help me someone how can I do this? Thanks.
This should work:
;WITH TotalScore AS (
SELECT UserID, Score, TeamID,
SUM(Score) OVER (PARTITION BY TeamID) AS TotalScore
FROM mytable
)
SELECT DENSE_RANK() OVER (ORDER BY TotalScore DESC) AS RankID,
UserID, Score, TeamID, TotalScore
FROM TotalScore
ORDER BY TotalScore DESC, Score
The Common Table Expression used calculates the total score per TeamID using windowed version of SUM. Using DENSE_RANK together with this calculated field we can easily generate the required RankID.
You can try this
SELECT
RANK() OVER(ORDER BY i.TotalScore DESC) AS RankID
,A.*
FROM
(
SELECT
UserID
,Score
,TeamID
,(SELECT SUM(Score) FROM yourTable tb2 WHERE tb2.TeamID = tb1.TeamID) AS TotalScore
FROM yourTable tb1
) A
ORDER BY A.TotalScore DESC
Have a Try with below,
Select DENSE_RANK() over(Order by (Select Sum(Score) from #tablename V where V.TeamID=T.TeamID ) desc )RankID
,*,
Sum(Score) over(Partition by TeamID) TotalScore from #tablename T
order by DENSE_RANK() over(Order by (Select Sum(Score) from #tablename V where V.TeamID=T.TeamID ) desc )
Dense Rank in the 'Order by' to sort the results by RANK Id and the same is used in Select statement, Subquery in the Dense Rank will rank based on the sum(Score) for a TeamID
consider a table
employee id report_year report_quarter sequencenumber quarter1_wage quarter2_wage
101 2015 1 1 1000 0
101 2015 1 2 2000 0
102 2016 2 1 3000 0
102 2016 2 2 0 4000
The Result of the query must be
Total wages
6000
As in 2015 Employee id 101 with Highest sequence number 2 has 2000 comes under quarter1 as report _quarter is indicating it is 1 need to add take this value and add to the
2016 Employee id 102 with highest sequence number 2 has 4000(quarter2_wage)
as report _quarter is indicating it is 2
I really don't understand why you want to sum this way, but ignoring that you are going to be aggregating across quarters and thus the name "Total Wages" is mis-leading, here is how you accomplish that.
--Assign a row number per employee based off the sequence number.
--This will assign 1 to the highest sequence number for each employee
with cte as(
select
*,
ROW_NUMBER() over (partition by [employee id] order by sequencenumber desc) as rn
from yourTable)
--Sum and add the two quarters for all employees where the row number = 1
--Which is the highest sequence
select
sum(quarter1_wage) + sum(quarter2_wage) as TotalWages
from cte
where rn = 1
Have you tried something like this:
WITH cte0 AS(
SELECT 101 AS employeeid,2015 AS report_year,1 AS report_quarter,1 AS sequencenumber,1000 AS quarter1_wage,0 AS quarter2_wage union all
SELECT 101 ,2015 ,1 ,2 ,2000 ,0 union all
SELECT 102 ,2016 ,2 ,1 ,3000 ,0 union all
SELECT 102 ,2016 ,2 ,2 ,0 ,4000
),
cte1 as(
SELECT employeeid,report_year,report_quarter,MAX(sequencenumber) AS maxseqnum
FROM cte0
GROUP BY employeeid,report_year,report_quarter)
SELECT SUM(quarter1_wage+quarter2_wage) AS [Total wages]
FROM cte0 c0
INNER JOIN cte1 c1
ON c0.report_year = c1.report_year AND c0.report_quarter = c1.report_quarter AND c0.employeeid = c1.employeeid AND c0.sequencenumber = c1.maxseqnum
In sure this should be simple, but I'm having a brain fart over it.
In SQL Server 2005, how do you add an "extra count" to a GROUP BY query?
Consider the following...
;WITH DATA AS (
SELECT 1 AS ID, 1 AS TYPEID, 0 AS ACTIONNEEDED
UNION
SELECT 2 AS ID, 1 AS TYPEID, 0 AS ACTIONNEEDED
UNION
SELECT 3 AS ID, 2 AS TYPEID, 0 AS ACTIONNEEDED
UNION
SELECT 4 AS ID, 2 AS TYPEID, 1 AS ACTIONNEEDED
UNION
SELECT 5 AS ID, 2 AS TYPEID, 0 AS ACTIONNEEDED
)
SELECT TYPEID, COUNT(*) AS TOTAL
FROM DATA
GROUP BY TYPEID
The result is...
TYPEID TOTAL
1 2
2 3
But what I need is an extra column that totals the number of rows where ACTIONNEEDED=1...
TYPEID TOTAL ACTIONNEEDED
1 2 0
2 3 1
Note: unfortunately due to project constraints, I'm restricted to SQL Server 2005 compatible answers
You can use SUM with an inner CASE statement:
SELECT
TYPEID,
COUNT(*) AS TOTAL,
SUM(CASE WHEN ActionNeeded = 1 THEN 1 ELSE 0 END) AS ACTIONNEEDED
FROM DATA
GROUP BY TYPEID