transpose some SQL columns into rows [closed] - sql

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 9 years ago.
Improve this question
ID YEAR FORCE NEIGHBOURHOOD ALL_CRIME ANTI_SOCIAL_BEHAVIOUR BURGLARY CRIMINAL_DAMAGE_AND_ARSON DRUGS OTHER_THEFT PUBLIC_DISORDER_AND_WEAPONS ROBBERY SHOPLIFTING VEHICLE_CRIME VIOLENT_CRIME OTHER_CRIME
1 2013-03 Police AAN_HP 290 91 27 33 11 64 8 6 3 14 27 6
i need to change the order of theses into something like
ID 1
YEAR 2012
FORCE Police
NEIGHBOURHOOD Bradford
ALL_CRIME 12345
ANTI_SOCIAL 87
BURGLARY 10
CRIMINAL_DAMAGE 20
DRUGS 15
OTHER_THEFT 30

If you want to changed the order in which the columns are returning change the order in which you are selecting them
SELECT 1,2,3 FROM t
Would return differently from
SELECT 2,1,3 FROM t
I don't know if this is what you wanted but I found the question confusingly worded!

Quite easy to do with a query:
with lvls as (select /* + materialize */ level lv from dual connect by level <= 9)
select id,
decode(lv,
1,'year',
2,'force'),
...
decode(lv,
1,to_char(year),
2,to_char(force),
...
from yourtable, lvls
order by id,lv;
hint "materialize" will not make Oracle be unperformant (he likes to join here the tables first and then run hierarchy if you do not specify this hint)

try this
select * from your_tab unpivot(col2 for col1 in(ID,YEAR,FORCE,NEIGHBOURHOOD, ALL_CRIME,ANTI_SOCIAL_BEHAVIOUR);
note:All columns mentioned in col1 must be of same datatype

Related

nest two queries: sum & Join [closed]

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 months ago.
Improve this question
How do I nest the following two queries?
I am trying to sum Duration from table scrap_log and filter via Reason but I want to use the matching string from table scrap_reasons where it holds the same product code integers.
SELECT Reason, SUM(Duration) FROM scrap_log GROUP by Reason
SELECT scrap_reasons.description, scrap_reasons.code
FROM scrap_reasons
JOIN scrap_log ON scrap_log.Reason=scrap_reasons.code
I tried many different ways of doing this.
scrap_log
Reason
Duration
10
20
10
40
11
40
13
33
13
33
11
2
scrap_reasons
code
description
10
Bad Color
11
Bad Shape
13
Bad Size
14
Bad etc..
OUTPUT
Total
Description
60
Bad Color
42
Bad Shape
66
Bad Size
select sum(scrap_log.Duration) as total
,scrap_reasons.description
from scrap_log join scrap_reasons on scrap_reasons.code = scrap_log.Reason
group by scrap_reasons.description
total
description
60
Bad Color
42
Bad Shape
66
Bad Size
Fiddle

SQL QUERY SQL SSRS [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Hi can anyone help me with sum up first two rows in table and then rest would be same. example is
ID SUM
12 60
0 20
1 30
2 50
3 60
I am expecting
ID SUM
0 80
1 30
2 50
3 60
I am doing this from memory - so if this doesnt work let me know and we can do it another way looking at the row number;
Assuming you have a unique ID to sort it by as you suggested, you could do something like this;
you may want to change the order to be desc if that's how you classify your 'top 2'
SELECT TOP 2 ID,
SUM(VALUE)
FROM [Table]
GROUP BY ID
ORDER BY ID
UNION
SELECT ID,
VALUE
FROM [Table]
WHERE ID NOT IN (SELECT TOP 2 ID
FROM [Table] ORDER BY ID)

SQL select statement to Exclude a column from being sorted [closed]

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 looking for a way to exclude a single column from being sorted using SQL select statement.
Lets say a table has 10 columns and when an ORDER BY is done on a specific column using select statement, all the 10 columns are sorted. Is it possible to exclude any specific column (say column 6) from being sorted? If so how.
**
Before
ID name
-----------
1 papaya
2 apple
3 strawberry
4 banana
Required
ID name
-----------
1 apple
2 banana
3 papaya
4 strawberry
Appreciate your help
This will generate the ids and they will be always shown in order but that won't be the actual id, you might need that as well
SELECT #a:=#a+1 id,name from tableName order by name

OracleSQL, I am stuck with listagg query [closed]

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
I have a list like this:
cities firms
------ -------
["NEWYORK"] 1
["CHICAGO"] 1
["LA"] 1
["DENVER","VIENNA','LONDON'] 2
["TORONTO"] 2
["WASHINGTON",'VIENNA'] 2
I want to replace the list with oracle sql like this:
cities firms
------ -------
NEWYORK,CHICAGO,LA 1
NEWYORK,CHICAGO,LA 1
NEWYORK,CHICAGO,LA 1
DENVER,VIENNA,LONDON,TORONTO,WASHINGTON 2
DENVER,VIENNA,LONDON,TORONTO,WASHINGTON 2
DENVER,VIENNA,LONDON,TORONTO,WASHINGTON 2
Something like that, maybe:
SELECT listagg("city", ',') WITHIN GROUP (ORDER BY "city") "cities",
"firms"
FROM (
SELECT DISTINCT REGEXP_SUBSTR("cities", '[^,]+',1, LEVEL) "city",
"firms"
FROM T
CONNECT BY LEVEL < 5 AND REGEXP_SUBSTR("cities", '[^,]+',1, LEVEL) IS NOT NULL
-- ^
-- :/ Arbitrary maximum depth...
) V
GROUP BY "firms"
Producing:
| CITIES | FIRMS |
|-----------------------------------------|-------|
| CHICAGO,LA,NEWYORK | 1 |
| DENVER,LONDON,TORONTO,VIENNA,WASHINGTON | 2 |
This is rather crude and probably need a lot more improvements. But this should give you some ideas to start from...
I have hard time to understand why this could be helpfull, but if you really need as much as duplicate records as in the original table, a simple JOIN will produce the desired output:
WITH W AS (
the above query
) V
GROUP BY "firms"
)
SELECT W."cities", "firms" FROM W JOIN T USING("firms");
See http://sqlfiddle.com/#!4/403e9/37

Select simplest element SQL Server recursion changed complexity of table [closed]

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 9 years ago.
Improve this question
I have T1 table with Id and name columns.
Table T2 with Aggregate_ID and Element_ID.
They are crossed
x1:
ID и Name
1 Car
2 Hood
3 Engine
4 Cylinder
5 Wheel
6 tyre
7 rim (car)
8 Rim fixation (Car)
x2:
Aggregate_ID Element_ID
1 2
1 3
1 4
1 5
3 4
5 6
5 7
7 8
I need to select simplest element like 2, 4, 8
Complexity and number of elements can be varied.
How can I do it with recursion?
There is another task:
I need to output all the simplest elements of which consists Wheel.
Recursive solution in SQL can be very complex. In your case I see no need to use it, since it will only make your code more complex.
You can use CTE if you still insist:
Recursive query in SQL Server
Non- recursive solution:,
You want only the elements that appear in T2 in the Element_ID but not in the Aggregate_ID:
SELECT Element_ID
FROM T2
EXCEPT
SELECT Aggregate_ID
FROM T2
Or if you want to display all of the information for the elements:
SELECT *
FROM T1
WHERE T1.ID NOT IN (SELECT Aggregate_ID
FROM T2)