I'm trying to achieve sort of solution to the below table, as I need to make one of the rows in column as column
sku title name product_qty ID warehouse_id
-----------------------------------------------------
S001 Title 1 warehouse1 5 23 1
S001 Title 1 warehouse2 95 23 2
S001 Title 1 warehouse3 3 23 3
S002 Title 2 warehouse1 1 24 1
S002 Title 2 warehouse2 91 24 2
S002 Title 2 warehouse3 0 25 3
and what I would like to achieve
sku title name
warehouse 1 warehouse 2 warehouse 3
S001 Title 1 5 95 3
S002 Title 2 1 91 0
Please try this ... solution...You need to pivot the data.
CREATE TABLE Pivots
(
sku VARCHAR(10)
,title VARCHAR(10)
,name VARCHAR(10)
,product_qty INT
,ID INT
,warehouse_id INT
)
GO
INSERT INTO Pivots VALUES
('S001','Title1','warehouse1',5 , 23 , 1),
('S001','Title1','warehouse2',95 , 23 , 2),
('S001','Title1','warehouse3',3 , 23 , 3),
('S002','Title2','warehouse1',1 , 24 , 1),
('S002','Title2','warehouse2',91 , 24 , 2),
('S002','Title2','warehouse3',0 , 25 , 3)
GO
SOLUTION
SELECT sku,title titlename,ISNULL(MAX(x.warehouse1),0) warehouse1
,ISNULL(MAX(x.warehouse2),0) warehouse2 ,ISNULL(MAX(x.warehouse3),0) warehouse3 FROM Pivots p
PIVOT
(
MAX(product_qty) FOR name IN ([warehouse1],[warehouse2],[warehouse3])
)x
GROUP BY sku,title
OUTPUT
sku titlename warehouse1 warehouse2 warehouse3
---------- ---------- ----------- ----------- -----------
S001 Title1 5 95 3
S002 Title2 1 91 0
(2 rows affected)
SOLUTION 2 - USING conditional aggregation
SELECT sku,title titlename
,ISNULL(MAX(CASE WHEN name = 'warehouse1' THEN product_qty END),0) warehouse1
,ISNULL(MAX(CASE WHEN name = 'warehouse2' THEN product_qty END),0) warehouse2
,ISNULL(MAX(CASE WHEN name = 'warehouse3' THEN product_qty END),0) warehouse3
FROM Pivots p
GROUP BY sku,title
OUTPUT
sku titlename warehouse1 warehouse2 warehouse3
---------- ---------- ----------- ----------- -----------
S001 Title1 5 95 3
S002 Title2 1 91 0
(2 rows affected)
Related
Below is my table:
id order_number order_date order_details
---------------------------------------------
1 222 01-01-2020 44
2 222 02-01-2020 66
3 222 03-01-2020 20
4 223 03-01-2020 33
5 224 04-01-2020 55
6 225 02-01-2020 77
I want to have sum(order_details) where order_number = 222
like this table
order_date sum_order_details
----------------------------------
01-01-2020 130
02-01-2020 130
03-01-2020 130
I tried the below but it doesn't work
select order_number , order_date , sum(order_details) sum_orders from ex
group by order_number
having order_number = 222
It seems like you're after this is a windowed SUM:
SELECT order_date,
SUM(order_details) OVER () AS sum_orders
FROM YourTable
WHERE order_number = 222;
Hey i have a table like this
Product_name Rate Cost GST_percentage Recipt_no Amount Final_Amount ID Description GST_price Quantity OrderID Discount Net_Unit_Price Stock_Pending Payment_Pending
SINGTEL DATA + EZ $10 1.5 GB 7 DAYS 10 120.00 5 1 120.00 126 1 A 6.00 12 ODR1 0.00 10.00 Received Paid
SINGTEL DATA + EZ $10 1.5 GB 7 DAYS 12 180.00 0 2 180.00 180.00 2 A 0.00 15 ODR2 0.00 12.00 NULL NULL
SINGTEL DATA + EZ $8 CHINA 888 10 120.00 0 2 120.00 120.00 3 B 0.00 12 ODR2 0.00 10.00 NULL NULL
and i want to show the final_Amount column value groupped by order Id.then i want to show the final_amount for those which is Payment_Pending status is not null but i can't get the correct result.
Note:
i got a result as
query:
SELECT [OrderID],
SUM(convert(float,[Final_Amount])) as Final_Amount,
(select sum(convert(float,Final_Amount)) as Final_Amount
from Purchase_Order
where Payment_Pending is not null) as paid
FROM [Purchase_Order]
group by [OrderID]
order by OrderID desc
OrderID Final_Amount paid
ODR2 300 126
ODR1 126 126
but i want like this
OrderID Final_Amount paid
ODR2 300 0
ODR1 126 126
(Because ODR2 Payment_Pending Column filled with null)
Probably your sub-query is wrong. It need to include a reference to OrderId of main query
SELECT [OrderID],
SUM(convert(float,[Final_Amount])) as Final_Amount,
(select sum(convert(float,Final_Amount)) as Final_Amount
from Purchase_Order x
where x.Payment_Pending is not null
and x.OrderId = p.OrderId) as paid
FROM [Purchase_Order] p
group by [OrderID]
order by OrderID desc
Thank you in advance for assistance!
What i am trying to accomplish is this: I have a table with different salary grades in the same department. How can I display the Salary Grades in the Columns, with the count below for each columns, and then show the GRAND to of salary grades for each department. This is what i have:
Dept_Num Dept_Name Salary_Grade TOTAL_SalaryGrade
2005 Mrktg 1 39
2005 Mrktg 2 26
2005 Mrktg 3 5
2010 Payroll 1 20
2010 Payroll 2 8
2015 Acct 1 6
2015 Acct 3 6
Dept_Num Dept_Name Salary_Grade 1 Salary_Grade 2 Salary_Grade 3
2005 Mrktg 39 26 5
2010 Payroll 20 8 0
2015 Acct 6 0 6
TOTAL_SalaryGrade 65 34 11
SELECT[Dept_Num]
,[Dept_Name]
[Salary_Grades],
[TOTAL_Salary_Grades],
FROM [Employees]
Thank you for any assistance given!
Try as follows:
CREATE TABLE EMPLOYEES (Dept_Num INT, Dept_Name CHAR(20), Salary_Grade INT, TOTAL_SalaryGrade INT )
INSERT INTO EMPLOYEES VALUES (2005,'Mrktg' ,1,39 )
INSERT INTO EMPLOYEES VALUES (2005,'Mrktg' ,2,26 )
INSERT INTO EMPLOYEES VALUES (2005,'Mrktg' ,3, 5 )
INSERT INTO EMPLOYEES VALUES (2010,'Payroll',1,20 )
INSERT INTO EMPLOYEES VALUES (2010,'Payroll',2, 8 )
INSERT INTO EMPLOYEES VALUES (2015,'Acct' ,1, 6 )
INSERT INTO EMPLOYEES VALUES (2015,'Acct' ,3, 6 )
(SELECT convert(varchar,Dept_Num) [Dept_Num],Dept_Name, isnull([1],0) [Salary_Grade 1],isnull([2],0) [Salary_Grade 2],isnull([3],0) [Salary_Grade 3] from
(
select Dept_Num
, Dept_Name
, TOTAL_SalaryGrade
, Salary_Grade
from EMPLOYEES
) x
pivot
(
SUM(TOTAL_SalaryGrade)
for Salary_Grade in ([1],[2],[3])
) p )
union all
select 'TOTAL_SalaryGrade','',[1],[2],[3] from
(SELECT SUM (TOTAL_SalaryGrade) as total,Salary_Grade FROM EMPLOYEES GROUP BY Salary_Grade ) b
pivot
(sum(total) for Salary_Grade in ([1],[2],[3])) q
The result is:
*-----------------*----------*------------------*---------------*---------------*
|Dept_Num | Dept_Name| Salary_Grade 1 |Salary_Grade 2 |Salary_Grade 3|
*-----------------*----------*------------------*---------------*---------------
|2015 | Acct | 6 | 0 | 6 |
*-----------------*----------*------------------*---------------*---------------
|2005 | Mrktg | 39 | 26 | 5 |
*-----------------*----------*------------------*---------------*---------------
|2010 | Payroll | 20 | 8 | 0 |
*-----------------*----------*------------------*---------------*---------------
|TOTAL_SalaryGrade| | 65 | 34 |11 |
*-----------------*----------*------------------*---------------*---------------
I have a Employee Wages table like this, with their EmpID and their wages.
EmpId | Wages
================
101 | 1280
102 | 1600
103 | 1400
104 | 1401
105 | 1430
106 | 1300
I need to write a Stored Procedure in SQL Server, to group the Employees according to their wages, such that similar salaried people are in groups together and the deviations within the group is as minimum as possible.
There are no other conditions or rules mentioned.
The output should look like this
EmpId | Wages | Group
=======================
101 | 1280 | 1
106 | 1300 | 1
103 | 1400 | 2
104 | 1401 | 2
105 | 1430 | 2
102 | 1600 | 3
You can use a query like the following:
SELECT EmpId, Wages,
DENSE_RANK() OVER (ORDER BY CAST(Wages - t.min_wage AS INT) / 100) AS grp
FROM mytable
CROSS JOIN (SELECT MIN(Wages) AS min_wage FROM mytable) AS t
The query calculates the distance of each wage from the minimum wage and then uses integer division by 100 in order to place records in slices. So all records that have a deviation that is between 0 - 99 off the minimum wage are placed in the first slice. The second slice contains records off by 100 - 199 from the minimum wage, etc.
You can for +-30 deviation as the below:
DECLARE #Tbl TABLE (EmpId INT, Wages INT)
INSERT INTO #Tbl
VALUES
(99, 99),
(100, 101),
(101, 1280),
(102, 1600),
(103, 1400),
(104, 1401),
(105, 1430),
(106, 1300)
;WITH CTE AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY Wages) AS RowId FROM #Tbl )
SELECT
A.EmpId ,
A.Wages ,
DENSE_RANK() OVER (ORDER BY MIN(B.RowId)) [Group]
FROM
CTE A CROSS JOIN CTE B
WHERE
ABS(B.Wages - A.Wages) BETWEEN 0 AND 30 -- Here +-30
GROUP BY A.EmpId, A.Wages
ORDER BY A.Wages
Result:
EmpId Wages Group
----------- ----------- --------------------
99 99 1
100 101 1
101 1280 2
106 1300 2
103 1400 3
104 1401 3
105 1430 3
102 1600 4
For example i have a Table1:
ID Specified TIN Value DateCreated
----------------------------------
1 0 tin1 45 2014-12-30
2 1 tin2 34 2013-01-05
3 0 tin3 23 2015-02-20
4 3 tin4 47 2013-06-04
5 3 tin5 12 2012-04-02
And a Table2:
ID Table1ID RegistrationDate
----------------------------------
1 1 2015-10-12
2 2 2015-07-21
3 1 2015-11-26
4 1 2015-12-04
5 2 2015-09-18
I need select all columns from Table1 with first and last RegistrationDate column in Table2. The answer should be
ID Specified TIN Value DateCreated FirstRegDate LastRegDate
---------------------------------------------------------------
1 0 tin1 45 2014-12-30 2015-10-12 2015-12-04
2 1 tin2 34 2013-01-05 2015-07-21 2015-09-18
3 0 tin3 23 2015-02-20 NULL NULL
4 3 tin4 47 2013-06-04 NULL NULL
5 3 tin5 12 2012-04-02 NULL NULL
Hi one possible solution can be something similar to pseudo query below(if you can prepare the tables I will modify to reflect actual query)
SELECT table1.*, inlineTable2.firstRegDate, inlineTable2.lastRegDate
FROM Table1
LEFT JOIN
(
SELECT
Table1ID AS id,
MIN(registrationDate) as firstRegDate,
MAX(regsitrationDate) as lastRegDate
FROM table2
GROUP BY table1ID
) AS inlineTable2
ON table1.id = inlineTable2.id
You can group by all columns in table1, and look up the minumum and maximum registration date for the group:
select ID
, Specified
, ... other columns from table1 ...
, min(RegistrationDate)
, max(RegistrationDate)
from Table1 t1
left join
Table2 t2
on t1.ID = t2.Table1ID
group by
ID
, Specified
, ... other columns from table1 ...