Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I need a help to convert my table.
I have original table named "SizeTable"
name
size1
size2
size3
shoes
8
9
10
and I want to convert the table as below
name
sizetype
sizeCode
shoe
size1
8
shoe
size2
9
shoe
size3
10
How can I convert column into row? since PIVOT and UNPIVOT CROSS APPLY keyword is not support in Firebird:
SELECT
name, [sizetype ], [sizeCode ]
FROM
(SELECT
sizetype, size1 value, 'size1' name
FROM
SizeTable
UNION ALL
SELECT
sizetype, size2 value, 'size2 ' name
FROM
SizeTable
UNION ALL
SELECT
sizetype, size3 value, 'size3' name
FROM
SizeTable) SRC
GROUP BY
name
Perhaps you can give this a go:
Select A.name
,B.*
From SizeTable A
Cross Apply ( values ('size1',size1)
,('size2',size2)
,('size3',size3)
) B(sizetype,sizeCode)
If you must use UNION ALL
Select Name,sizetype='size1',sizecode=size1 from SizeTable
Union All
Select Name,sizetype='size2',sizecode=size2 from SizeTable
Union All
Select Name,sizetype='size3',sizecode=size3 from SizeTable
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
select * from (
select distinct nationality,gender,regplacetype,count(gender) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 67 and gender = 'Man'
group by nationality,gender,regplacetype
) as man,
(
select distinct nationality,gender,regplacetype,count(nationality) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 57 and gender = 'female'
group by xnationality,gender,regplacetype) as female
with man as (
select distinct nationality,gender,regplacetype,count(gender) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 67 and gender = 'Man'
group by nationality,gender,regplacetype
) select * from man,
union
select distinct nationality,gender,regplacetype,count(nationality) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 57 and gender = 'female'
group by xnationality,gender,regplacetype
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
For example, let's say there's a table like this:
ID Food
-- ----
1 Fruit
So whenever it says Fruit, I want to break it out into 3 rows:
ID Food
-- ----
1 Apple
1 Banana
1 Orange
Any tips?
Hmmm . . . You could do:
select t.id, coalesce(a.alt, t.food) as food
from t outer apply
(select alt
from (values ('Apple'), ('Banana'), ('Orange')) v(alt)
where t.food = 'Fruit'
) a;
select ID, Food FROM YourTable WHERE food <> 'Fruit'
UNION
select ID, f2 FROM YourTable JOIN
(SELECT'Apple' f2
union
SELECT 'Banana' f2
UNION
SELECT 'ORANGE' f2
) DT
ON food = 'Fruit'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to insert the XML string like
1,2,3,4,5,6,7,8,9,,1,1,1,1,2,23,4
into a table
I wanted to insert the data into following table in a such manner that after every 3 values it should go into next row is it possible do this?
and the xml string is generated in the program.
Your question is quite unclear, but you might be looking for something like this:
DECLARE #Numbers VARCHAR(MAX)='1,2,3,4,5,6,7,8,9,,1,1,1,1,2,23,4';
WITH Splitted AS
(
SELECT A.B.value('.','int') AS Number
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowInx
FROM (SELECT CAST('<x>' + REPLACE(#Numbers,',','</x><x>') + '</x>' AS XML)) AS Casted(AsXml)
CROSS APPLY Casted.AsXml.nodes('/x') AS A(B)
)
,Extended AS
(
SELECT (RowInx -1 ) / 3 AS Rank3
,Number
,REPLACE('Field_' + CAST((RowInx % 3) AS VARCHAR(1)),'_0','_3') AS ColumnName
FROM Splitted
)
SELECT p.*
FROM Extended
PIVOT
(
MAX(Number) FOR ColumnName IN(Field_1,Field_2,Field_3)
) AS p
The result (btw: the empty value in your example "...8,9,,1,1" is taken as "0"):
Rank3 Field_1 Field_2 Field_3
0 1 2 3
1 4 5 6
2 7 8 9
3 0 1 1
4 1 1 2
5 23 4 NULL
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say I have a table like this:
ID. Location. Value.
1. AGF. 10.00
2. VHJ. -20.00
3. AGF. -20.00
4. AGF. 5.00
5. KLZ. 50.00
6. AGF. 10.00
I want to select the rows that have same Location AND whose Values sum to zero.
In this case the result should be:
1
3
6
because those rows are all in Location AGF and they sum to 0 (10 + -20 + 10).
Try:
Select ID from YourTable where Location IN(
Select location from YouTable
Group By Location
Having sum(Value) = 0
)
You need to find all locations with zero sum using grouping and group filters (group by and having clauses respectively). This can be done in a subquery. Then select all IDs with the just selected locations.
select ID
from YOUR_TABLE
where Location in (
select Location
from YOUR_TABLE
group by Location
having sum(Value) = 0
)
You could use GROUP BY and HAVING, like this:
Select ID from tablelocation where Location IN(
Select location from tablelocation
Group By Location
Having sum(Value) = 0
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
TableA TableB
Id Name Id Role
1 abc 1 Test
2 xyz
3 zxc
I want output as all Ids from table A which are not present in table B
O/p
Id Name
2 xyz
3 zxc
You could use the not in operator:
SELECT *
FROM TableA
WHERE id NOT IN (SELECT id FROM TableB)
Try this:
SELECT * FROM TABLE_1 WHERE (Id,Name ) NOT IN (SELECT Id,Name FROM TABLE_2)
Edit (after question was formatted properly):
SELECT * FROM TABLE_1 WHERE Id NOT IN (SELECT Id FROM TABLE_2)