nest two queries: sum & Join [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 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

Related

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)

transpose some SQL columns into rows [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
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

count by column name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Trans|Store|Date
1 | 10 |9/01/13
2 | 10 |9/01/13
3 | 20 |9/01/13
4 | 10 |9/02/13
What I am trying to query is
(For Date = 9/01/13)
Store|#Trans|Date
10 | 2 |9/01/13
20 | 1 |9/01/13
How do I achieve this any ideas ?
You may try this:-
select store, count(*) as Transt from table
where Date = '9/01/13'
group by store
If that doesn't do it, this might.
Select
count(Store) as #Trans,
Store
from
tablename
where
Date = '9/01/13'
group by
Store
This smells like homework something fierce though.

Assign values to sorted result in SQL [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 got table like below after sorting in SQL:
M_ID
-----
2013/01
2013/02
2013/03
2013/04
2013/05
2013/06
Now I want to assign each entries a particular value like below
M_ID Days
--------------
2013/01 20
2013/02 30
2013/03 40
2013/04 50
2013/05 60
2013/06 70
So, can you please let me know how to do that in SQL Query?
Do you mean something like this (presuming sql-server)?
SELECT M_ID,
Days = (ROW_NUMBER()OVER(ORDER BY M_ID) + 1) * 10
FROM dbo.TableName
Demo

Having 1 distinct column and a sum value from another column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to have a solution to this question:
I have a datatable here, as shown below
cid amount
1 5
1 10
2 2
3 5
3 7
3 11
Now I need to write a statement that returns a distinct cid, and the sum of the amount column for each cid. The table should look as below:
cid amount
1 15
2 2
3 23
What is the best way to do this? Thanks.
select cid, sum(amount) amount from table group by cid
Try this way:
select cid, sum(amount)
from tab
group by cid
select cid, sum(amount)
from table_name
group by cid;
select cid,sum(amount) from c1 group by cid