query group by with having and get results in two different columns - oracle

I have a table like these:
Name Source ended_status date Environment
House DC 1 2019/10/03 Pro
Cat DC2 1 2019/10/05 Pro
Pen DC 1 2019/10/03 Pro
Pen DC 0 2019/11/07 Pre
I would like to get:
Source Environment Ended_Status_with_1 Ended_Status_with_2
DC Pro 2 0
DC Pre 1 0
DC2 Pro 1 0
So, they must be grouped by Source,Environment and I must calculate a summation of all that they have Ended_Status with 1 and all with ended status 2 and put in the same line.
How could I do that?
I can make query grouped by every ended_status but I can't put the two summations of ended status in same line.
Many thanks and sorry for my poor English!

also, try below query
select source,environment,sum(decode(ended_status,1,1,0)) ended_status_with_1,
sum(decode(ended_status,2,1,0)) ended_status_with_2 from mytable
group by source,environment

You can use conditional aggregation:
select
source,
environment,
sum(case when ended_status = 1 then 1 else 0 end) ended_status_with_1,
sum(case when ended_status = 2 then 1 else 0 end) ended_status_with_2
from mytable
group by
source,
environment

Related

looking for Number of Members in each country whose preference game is set to NES

SQL Number of Members in each country whose preference game is set to NES
EXPECTED OUTPUT
COUNT(AUD) COUNT(EUR) COUNT(IND) COUNT(LAO) COUNT(USA) COUNT(ZWE)
3 2 0 0 2 1
Here is the SQL fiddle
http://sqlfiddle.com/#!9/a15ec9f/3
We pivot.
select *
from t
pivot(count(MEMBER_ID) for COUNTRY_ID in('AU' as "count(AU)", 'US' as "count(US)", 'LAO' as "count(LAO)", 'IND' as "count(IND)", 'DE' as "count(DE)", 'ZW' as "count(ZW)")) p
count(AU)
count(US)
count(LAO)
count(IND)
count(DE)
count(ZW)
3
7
0
0
4
2
Fiddle
SELECT Member.MemberID, COUNTRY, GAME_TYPE
FROM MEMBER_TABLE
INNER JOIN COUNTRY_TABLE ON Member.COUNTRY_ID= COUNTRY_TABLE.COUNTRY_ID
WHERE Member.GAME_TYPE= 'NES';

How to break a VARCHAR2(4000) string line in Multiple Rows Counting the Words Repetition

I have an Issues DataBase, with many solutions open/closed for each issue in a view.
My chalenge is take all the answers, break it in each words it as was writed, and count these words separeted by column for the entire number of issues stored in this Data Base (view).
Sample.:
SELECT * FROM VW_ISSUE_REPORT;
issueID
ProblemReported
Solution
IsClosed
1
Printer Offline
Turn On the Printer ABC
Yes
2
Printer Paper Jam
Remove Paper Jam from Printer ABC
No
Result expected: (This is a historical database, I can't create functions, procedures, etc. Just a smart, and well known SELECT statement.).
SELECT MAGIC_SOLUTION( Solution) AS SolutionKeyWord , COUNT('X') AS SolutionRepetitions FROM VW_ISSUE_REPORT GROUP BY MAGIC_SOLUTION( Solution);
SolutionKeyWord
SolutionRepetitions
ABC
2
Printer
2
from
1
Jam
1
On
1
Paper
1
Remove
1
the
1
Turn
1
Best Regards
As you can't (at least, I think you can't) distinguish "names" from "solutions" (i.e. how to remove "ABC" from result? It can be anything, from HP to Canon to its IP address), one option is to split the whole solution into rows and count how many times each of its words repeats.
Sample data:
SQL> with test (issue_id, problemreported, solution, isclosed) as
2 (select 1, 'Printer Offline', 'Turn On the Printer ABC', 'Yes' from dual union all
3 select 2, 'Printer Paper Jam', 'Remove Paper Jam from Printer ABC', 'No' from dual
4 )
Query begins here:
5 select regexp_substr(solution, '[^ ]+', 1, column_value) solution_keyword,
6 count(*) solution_repetitions
7 from test cross join
8 table(cast(multiset(select level from dual
9 connect by level <= regexp_count(solution, ' ') + 1
10 ) as sys.odcinumberlist))
11 group by regexp_substr(solution, '[^ ]+', 1, column_value);
SOLUTION_KEYWORD SOLUTION_REPETITIONS
--------------------------------- --------------------
Paper 1
Printer 2
Turn 1
the 1
Remove 1
Jam 1
On 1
from 1
ABC 2
9 rows selected.
SQL>

FIFO inventory aging report using a single query in T-SQL

I've got an inventory transactions table :
Product
Date
Direction
Quantity
A
Date 1
IN
3
B
Date 2
IN
55.7
A
Date 3
OUT
1
B
Date 3
OUT
8
B
Date 3
IN
2
I can easily get the stock for any date with the following query :
SELECT Product,
SUM(CASE Direction WHEN 'IN' THEN Quantity ELSE -1 * Quantity END)
FROM Transactions
WHERE Date <= '#DateValue#'
GROUP BY Product;
Now my purpose is to get stocks aged like this using the FIFO principle :
Product
Total stock
0-30 days
31-60 days
61-90 days
91+ days
A
3
3
0
0
0
B
34.2
10
14.2
7
3
C
25
20
3
1
1
D
10
2
8
0
0
E
1
0
0
1
0
I am using SQL Server 2016 & SSMS 18.
The solution should be fast as it will be working against a table with 3,000,000+ rows.
A single query is preferred since it will be integrated into an ERP system.
I have yet to find a solution based on a single query after weeks of research. Any help is appreciated. Thanks in advance.

Using oracle loop to concatanete strings

I have someting like this
id day descrition
1 1 hi
1 1 today
1 1 is a beautifull
1 1 day
1 2 exemplo
1 2 for
1 2 this case
I need to do a funtion that for each day concatenate the descrtiomn colunm and return the result like this
id day descrition
1 1 hi today is a beautifull thay
1 2 exemplo for this case
Anny ideia about how can i do this usisng a loop in a function in oracle
You need a way of determining which order the values should be aggregated. The snippet below will rely on the implicit order in which Oracle reads the rows from the datafiles - if you have row movement enabled then you may get inconsistent results as the rows can be read in different orders as they are relocated in the underlying datafiles.
SELECT LISTAGG( description, ' ' ) WITHIN GROUP ( ORDER BY ROWNUM ) AS description
FROM your_table
GROUP BY id, day
It would be better to have another column that stores the order within each day.

Passing dynamic values to order records in oracle

I want to sort a record in following way.
Arrange records in group (by ID column)
Sort the step 1 results by ascending order (by NAME column)
2.1. If NAME Column having same values, then order by FLAG column value (ascending order)
Order the Step 2 results by Order Assist column (I will be passing dynamic value to sort using order assist column)
My Query:
SELECT IDENTIFIER, CODE, INC_EXC_FLAG,ORDER_ASSIST FROM DUMMY_SORT
WHERE METHOD_ID = '1'
GROUP BY (IDENTIFIER, CODE, INC_EXC_FLAG,ORDER_ASSIST)
ORDER BY ORDER_ASSIST ASC, CODE ASC, INC_EXC_FLAG ASC
Result of above Query:
ID NAME FLAG ORDER_ASSIST
A_EC AEC 0 EC1
B_EC_DET BEC 1 EC2
A_NIT ANIT 0 NIT1
A_NIT ANIT 1 NIT1
A_NIT BNIT 0 NIT1
B_NIT_DET BNIT 0 NIT2
B_NIT_DET BNIT 1 NIT2
A_SC ASC 0 SC1
A_SC ASC 1 SC1
B_SC_DET BSC 0 SC2
B_SC_DET BSC 1 SC2
C_SC_FUN CSC 0 SC3
D_SC_GRP DSC 0 SC4
But I want to generate the result according to dynamic values of order_assist
For Example:
If I am passing dynamic value as "SC" i want to fisrt order the records SC1,SC2,SC3. Then NIT1,NIT2 . then EC1,EC2.
If I am passing dynamic value as "NITG" i want to fisrt order the records NIT1,NIT2 then SC1,SC2,SC3. then EC1,EC2.
Expected result added when dynamic value is "SC"
ID NAME FLAG ORDER_ ASSIST
A_SC ASC 0 SC1
A_SC ASC 1 SC1
B_SC_DET BSC 0 SC2
B_SC_DET BSC 1 SC2
C_SC_FUN CSC 0 SC3
D_SC_GRP DSC 0 SC4
A_NIT ANIT 0 NIT1
A_NIT ANIT 1 NIT1
A_NIT BNIT 0 NIT1
B_NIT_DET BNIT 0 NIT2
B_NIT_DET BNIT 1 NIT2
A_EC AEC 0 EC1
B_EC_DET BEC 1 EC2
Sounds like maybe you're after something like:
order by case when p_sort_param = 'SC' and order_assist like 'SC%' then 1
when p_sort_param = 'SC' and order_assist like 'NIT%' then 2
when p_sort_param = 'NITG' and order_assist like 'NIT%' then 1
when p_sort_param = 'NITG' and order_assist like 'SC%' then 2
else 3
end,
order_assist
where p_sort_param is the parameter that gets passed in to provide the "dynamic" value. This assumes you're running the query via a stored procedure. If it's a manually run query (eg. in Toad), then add a colon in front of the parameter name to make :p_sort_param.
I cannot understand your specific ordering rules, but you should be able to achieve what you want using CASE expressions:
order by
case order_assist
when 'SC' then <first thing to order by for SC>
when 'NITG' then <first thing to order by for NITG>
...
end,
case order_assist
when 'SC' then <second thing to order by for SC>
when 'NITG' then <second thing to order by for NITG>
...
end,
... etc.

Resources