Related and non-related SQL queries putted together [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 2 years ago.
Improve this question
I have two questions, one is how I can put these two queries together (add just the right column). Example:
Query 1
Phase menCount
A 40
B 50
C 60
D 20
Query 2
Phase womenCount
A 60
B 50
C 40
Wanted query result
Phase menCount womenCount
A 40 60
B 50 50
C 60 40
D 20 NULL
The second question is, how I can put together columns from non-related queries. Is this possible at all? I have at least 10 different srcripts and if I can put them into one call, it would be easier. Example:
Query 1
Phase phaseCount
A 20
B 40
C 60
Query 2
Stage stageCount
W 90
X 120
Y 150
Z 190
Wanted query result
Phase phaseCount Stage stageCount
A 20 W 90
B 40 X 120
C 60 Y 150
Z 190
Thanks for answers.

To put two queries together you can treate them as "table expressions". For example
select
coalesce(a.phase, b.phase) as phase,
a.mencount,
b.womencount
from (
-- query #1 here
) a
full join (
-- query #2 here
) b on a.phase = b.phase
To put unrelated queries together you need to produce a joining condition somewhere. For example, you can modify each query to produce an artificial row number using the ROW_NUMBER() function; then you can use this value to join against each other, as in:
select
a.*, b.*
from (
-- modified query #1 now
select
...,
row_number() over(order by ...) as rn
from ...
) a
full join (
-- modified query #2 now
select
...,
row_number() over(order by ...) as rn
from ...
) b on a.rn = b.rn
order by coalesce(a.rn, b.rn)

Related

Nested SELECT vs JOIN performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I have the following two tables in PostgreSQL database (simplified for the sake of example):
article
id
summary
1
Article 1
2
Article 2
3
Article 3
...
...
event
id
article_id
eventtype_id
comment
108
1
4
Comment 1
109
2
8
Comment 2
110
3
4
Comment 3
...
...
I would like to select only 1 event with eventtype_id=4 for each article. The result should look like this:
article_id
article_summary
event_comment
1
Article 1
Comment 1
2
Article 2
3
Article 3
Comment 3
...
Which of these 2 queries (Query 1 or Query 2) runs faster? Do they return the same result?
Query1:
SELECT
a.id AS article_id,
a.summary AS article_summary,
evnt.comment AS event_comment
FROM
article a
LEFT JOIN
event evnt ON evnt.article_id = a.id AND evnt.eventtype_id = 4;
Query2:
SELECT
a.id AS article_id,
a.summary AS article_summary,
(
SELECT
evnt.comment
FROM
event evnt
WHERE
evnt.article_id = a.id AND
evnt.eventtype_id = 4
LIMIT 1
) AS event_comment
FROM
article a;
Apart from the fact that queries are not the same, as you said in the comments, generally speaking, correlated queries are not considered suitable from a performance point of view. That's because these queries are applied row by row. They are usually helpful in some particular situations: read this. However, even in those situations, it is a good practice to use them in an exists clause if possible, So that whenever it finds a row for the query it returns true.

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 Server count numbers [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 4 years ago.
Improve this question
I have a table and it looks like this:
Num1 num2 num3
----------------
1 2 2<----- grouped numbers
1 2 3<----- another group
1 2 3<----- same numbers so I have a value of 2
2 4 4
2 2 3
2 4 3
3
3
4
I would like to know how to give groups of numbers, a number value.
Example1
1, 2, 2 are grouped horizontally
Example 2
1,2,2 this combination is shown x amount of times
1,2,2 times = 1
Example 3
1,2,3 = 2 times
2,3,3 = 4 times
This works but only on single numbers
select num, count(*)Times
from Numbers cross apply
(values (F2), (F3), (F4),(F5),(F6),(F7),(F8)) v(num)
where num is not null
group by num
order by num;
This also works but same problem
select value, count(*)
from Numbers
unpivot
(
value
for col in (F2, F3, F4,F5,F6,F7,F8,F9)
) u
group by value
ORDER BY 1;
The idea is to expand this to 16 columns and search all rows to find the matching
sets of numbers in each row.
Give an output of example 3, "times" being the column name
The amount of rows=2000,columns=16
if anyone can help please post
Oh, I think you want the count of each number. You can do this by unpivoting and aggregating:
select num, count(*)
from t cross apply
(values (num1), (num2), (num3)) v(num)
where num is not null
group by num
order by num;

MS Access SQL - Average of 3 Previous Rows [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a column of values where I need to take the average of the two previous values and the current value and display as a new column. I'm using MS Access 2013 and am more familier using SQL code rather than the SQL Query Wizard. So, if you could provide the code I'd appreciate it.
I've read on other threads that talked about a Lag function, but I believe Access does not allow that. Also, I've seen similar questions answered with subqueries, but I'm not familiar enough with those yet.
Below is what I'm looking for. Given column A (1,2,3,4,5) how do I make B (0,0,2,3,4)?
A | B
----------
1 | 0
2 | 0
3 | 2 = (1+2+3)/3
4 | 3 = (2+3+4)/3
5 | 4 = (3+4+5)/3
Utilize a self join to the same table for when the value of A is BETWEEN a - 2 and A.
SELECT
t.A
,SUM(pre.A) / 3 as B
FROM
TableName t
LEFT JOIN TableName pre
ON pre.A >= (t.A - 2)
AND pre.A <= t.A
GROUP BY
t.A
Note that this will actually give you 0,1,2,3,4. If you want 0,0,2,3,4 you will have to count the preceding rows to determine if there are three and if not make it 0 such as:
SELECT
t.A
,IIF(COUNT(pre.A) < 3, 0, SUM(pre.A) / 3) as B
FROM
TableName t
LEFT JOIN TableName pre
ON pre.A >= (t.A - 2)
AND pre.A <= t.A
GROUP BY
t.A

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