I have the following table data
ID Night_111 NightOT_112 NightLeave_113
----------------------------------------------------
1 8.00 0.00 0.00
2 16.00 4.00 0.00
3 8.00 0.00 2.00
I want that table to convert it into this structure:
ID wageType hours
---------------------------
1 111 8
2 111 16
2 112 4
3 111 8
3 113 2
Please help
Thanks
There are several ways, but I advocate using apply:
select v.*
from t cross apply
(values (id, 111, Night_111), (id, 112, NightOT_112), (id, 113, NightOT_112)
) v(ID, wageType, hours)
where v.hours > 0;
You can do something similar using union all or unpivot. However, apply implements something called a lateral join, which is a very powerful operator and useful many other situations.
Related
I have MS SQL server table which contains the data as follows
id val1 val2
1 100.00 50.00
2 25.00 30.00
3 30.00 25.00
4 100.00 50.00
5 40.00 80.00
6 25.00 30.00
7 80.00 21.00
8 25.00 30.00
In the above table, few val1 val2 values combination occurs more than ones i.e. 100.00 50.00 is occurred twice, 25.00 30.00 occurred thrice. Likewise, if any combinations occurred more than ones, I would need to get those Ids.
So my result would be id - 1,2,4,6,8.
please help how to query this in MS SQL. Thanks
The CTE obtains the val1, val2 pairs that occur more than once. The we join with the table to get the id values:
;with cte as (
select val1, val2
from t
group by val1, val2
having count(*) > 1
)
select id
from t
join cte on t.val1 = cte.val1 and t.val2 = cte.val2
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
I really don't know what queries to be combined using these table to achieve my coressponding output please assist me
Table 1
w_ref t_id q_id qty
110 111 500 1
111 112 500 2
112 113 500 3
Table 2
t_id q_id material-x material-y material_id
111 500 14 15 7
112 500 18 18 7
113 500 10 11 8
Table 3
id material_name
7 abc
8 def
I expect that joining table output like this
Output
w_ref qty material-x material-y material_name
110 1 14 15 abc
111 2 18 07 abc
112 3 10 11 def
Here is a simple example of a SQL of a INNER JOIN. This will give you a basic understanding from the results given.
SELECT a.w_ref, a.t_id, a.q_id, a.qty, b.material-x FROM table1 a INNER JOIN table2 b ON b.t_id = a.t_id;
However their is very little of what you're trying to achieve and should not just post a question asking someone to sort it. Maybe add what you have tried so far and what outcomes you have had.
Here is the dataset from the table
TABLE XYZ
ID Date col1 col2 col3
1 2012-09-21 100.00 0.00 0.00
1 2012-09-21 0.00 10.00 20.00
2 2012-09-21 0.00 20.00 0.00
2 2012-09-21 100.00 0.00 20.00
3 2012-09-21 10.00 25.00 5.00
I want Result like below:
ID Date COL
1 2012-09-21 130.00
2 2012-09-21 140.00
3 2012-09-21 40.00
I tried below Query, but not getting a exact value from table.
Select ID,
date,
sum(Col1+col2+col3) as COL
From table xyz
Group by ID,DATE
I got below result set
ID Date COL
1 2012-09-21 130.00
2 2012-09-21 270.00
3 2012-09-21 310.00
IS there any better way to get expected result set or to do a pivot table?
Thanks
You can use CTE and then use group by on the result.
WITH S AS ( SELECT ID,DATE,COL1+COL2+COL3 AS. COLS FROM TABLE1);
SELECT ID,DATE,SUM(COLS)
FROM S
GROUP BY ID,DATE
I have a problem with a query where i need to get all the common values in column #2 that are for every element of column #1.
For example:
Column #1 Column #2
-------------------
21 2.00
21 5.00
21 6.00
21 8.00
21 9.00
41 2.00
41 3.00
41 4.00
41 5.00
41 6.00
41 9.00
52 2.00
52 5.00
52 9.00
52 10.00
52 20.00
Result
-------------------
2.00
5.00
Any help will be greatly appreciated.
Juan Alvarez
SELECT column2
FROM YourTable
GROUP BY column2
HAVING COUNT(*) = (SELECT COUNT(DISTINCT column1) FROM YourTable)