I have a ORACLE sql query that needs to add a header (Only one row) with the name of the columns in the outcome.
How could that be achieved?
1-1-2022 08:32:00 xxx1 166 1 04641127 8 1
1-1-2022 07:05:00 xxx1 167 1 10205792 8 1
1-1-2022 09:20:00 xxx1 176 1 10256841 8 1
1-1-2022 10:10:00 xxx1 177 1 10193856 8 1
Regards
Date dep room nr rec type count
6-4-2022 08:32:00 xxx1 166 1 04641127 8 1
6-4-2022 07:05:00 xxx1 167 1 10205792 8 1
5-4-2022 09:20:00 xxx2 176 1 10256841 8 1
5-4-2022 10:10:00 xxx2 177 1 10193856 8 1
UNION is one option; note that - in that case - both SELECT statements have to share the same number of columns and their datatypes.
Something like this:
select 'Date' col1, 'dep' col2, 'room' col3, 'nr' col4, 'rec' col5, 'type' col6, 'count' col7
from dual
union all
select to_char(date_column, 'dd-mm-yyyy hh24:mi:ss'),
dep,
to_char(room),
to_char(nr),
rec,
to_char(type),
to_char(count_column)
from some_table
Related
key1
key2
val
23
215
4489
23
216
4489
86
326
5245
86
325
4489
86
323
4489
04
369
1200
04
370
1673
04
368
4489
10
402
1673
10
400
5971
10
404
1200
10
401
9189
Output should be like this
key1
key2
val
86
326
5245
04
369
1200
04
370
1673
04
368
4489
10
402
1673
10
400
5971
10
404
1200
10
401
9189
How to compare two rows having same key1 but different key2 where val is same for key2's.
I tried having and group by query but as this outcome render from views and 3 different tables, which is hard to understand for me.
Any help will be appreciated
Added :
where key1 is same for key2 having same val need to exclude
but when
key1 is same for key2 having different val need to show as outcome
To me, it looks like
SQL> WITH
2 test (key1, key2, val)
3 AS
4 -- sample data
5 (SELECT '23', 215, 4489 FROM DUAL
6 UNION ALL
7 SELECT '23', 216, 4489 FROM DUAL
8 UNION ALL
9 SELECT '86', 326, 5245 FROM DUAL
10 UNION ALL
11 SELECT '86', 325, 4489 FROM DUAL
12 UNION ALL
13 SELECT '86', 323, 4489 FROM DUAL
14 UNION ALL
15 SELECT '04', 369, 1200 FROM DUAL
16 UNION ALL
17 SELECT '04', 370, 1673 FROM DUAL
18 UNION ALL
19 SELECT '04', 368, 4489 FROM DUAL
20 UNION ALL
21 SELECT '10', 402, 1673 FROM DUAL
22 UNION ALL
23 SELECT '10', 400, 5971 FROM DUAL
24 UNION ALL
25 SELECT '10', 404, 1200 FROM DUAL
26 UNION ALL
27 SELECT '10', 401, 9189 FROM DUAL),
28 temp
29 AS
30 -- count > 1 means that there are values that match
31 ( SELECT key1, val, COUNT (*) cnt
32 FROM test
33 GROUP BY key1, val)
34 SELECT a.key1, a.key2, a.val
35 FROM test a
36 WHERE (a.key1, a.val) IN (SELECT b.key1, b.val
37 FROM temp b
38 WHERE b.cnt = 1) --> no match
39 ORDER BY a.key1, a.key2;
KEY1 KEY2 VAL
-------- ---------- ----------
04 368 4489
04 369 1200
04 370 1673
10 400 5971
10 401 9189
10 402 1673
10 404 1200
86 326 5245
8 rows selected.
SQL>
If sample data come from a query you already wrote, then you'd use it as a CTE:
with your_query as
(select ... --> put your query in here
from ...
where ...
),
-- code I wrote goes here, starting from line #28
temp as
...
After you posted your query, it would be something like this; note that I don't know which columns you want to compare because there are no longer KEY1, KEY2 and VAL columns; you'll have to fix it yourself.
WITH
your_query
AS
( SELECT COUNT (*) cnt,
t2.ORIGINATOR_ID,
t3.name,
t4.MULTI_TRANCHE_FLAG,
v1.SOURCE_OF_DEAL,
ta1.PRICING_DATE
FROM vw_origination_deal_advice ta1
LEFT JOIN vw_orig_deal_advice_summary v1
ON v1.deal_id = ta1.orig_deal_advice_id
LEFT JOIN tbl_issue_tranche t4 ON t4.id = ta1.ISSUE_TRANCHE_ID
LEFT JOIN tbl_issue_originator t2
ON ta1.issue_tranche_id = t2.ISSUE_TRANCHE_ID
LEFT JOIN tbl_staff t3 ON t2.originator_id = t3.staff_id
WHERE ta1.ISSUE_TRANCHE_ID IN
( SELECT ta2.ISSUE_TRANCHE_ID
FROM vw_origination_deal_advice ta2
GROUP BY ta2.ISSUE_TRANCHE_ID)
AND ta1.PRICING_DATE LIKE '%-21%'
AND t4.MULTI_TRANCHE_FLAG = 1
GROUP BY ta1.orig_deal_advice_id,
ta1.ISSUE_TRANCHE_ID,
t4.ACTIVE,
t2.ORIGINATOR_ID,
t3.name,
t4.MULTI_TRANCHE_FLAG,
v1.SOURCE_OF_DEAL,
ta1.PRICING_DATE),
temp
AS
-- count > 1 means that there are values that match.
-- As KEY1, KEY2, VAL don't exist in your query, you'll have to use appropriate
-- names from in the rest of the query
( SELECT key1, val, COUNT (*) cnt
FROM test
GROUP BY key1, val)
SELECT a.key1, a.key2, a.val
FROM test a
WHERE (a.key1, a.val) IN (SELECT b.key1, b.val
FROM temp b
WHERE b.cnt = 1) --> no match
ORDER BY a.key1, a.key2;
I have a table with this structure and data, with start and stop positions of an audio/video. I have to count the original seconds and discard the not original ones.
E.g.
CUSTOMER_ID ITEM_ID CHAPTER_ID START_POSITION END_POSITION
A 123456 1 6 0 97
B 123456 1 6 97 498
C 123456 1 6 498 678
D 123456 1 6 678 1332
E 123456 1 6 1180 1190
F 123456 1 6 1190 1206
G 123456 1 6 1364 1529
H 123456 1 6 1530 1531
Original Data
Lines "E" and "F" does not represent original seconds because "D" line starts at 678 and finishes with 1332 so I need to create a new set of lines like this:
CUSTOMER_ID ITEM_ID CHAPTER_ID START_POSITION END_POSITION
A 123456 1 6 0 97
B 123456 1 6 97 498
C 123456 1 6 498 678
D 123456 1 6 678 1332
E 123456 1 6 1364 1529
F 123456 1 6 1530 1531
New Result Set
Can you help mw with this?
If I am following you correctly, you can use not exists to filter out rows whose range is contained in the range of another row:
select t.*
from mytable t
where not exists (
select 1
from mytable t1
where
t1.customer_id = t.customer_id
and t1.start_position < t.start_position
and t1.end_position > t.end_position
)
You can use the self join as follows:
Select distinct t.*
from your_table t
Left Join your_table tt
On t.customer_id = tt.customer_id
And t.item_id = tt.item_id
And t.chapter_id = tt.chapter_id
And t.rowid <> tt.rowid
And t.start_position between tt.start_position and tt.end_position - 1
Where tt.rowid is null
I wish to use some sort of SQL array to subtract values from a certain row (QTYOnHand) that decreases that row value every time and throws it into a rolling calculation for the other rows. I've been thinking of some sort of Self Join/Temp Table solution, but not sure how to formulate. Also, All the results will be partitioned by the ItemID below. Help would be appreciated.
Here's some data, If I do a simple row by row subtraction I will get this: 17-3 = 14, 17-5 = 12 and so on.
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 12
123 4 17 13
456 7 12 5
456 8 12 4
456 2 12 10
456 3 12 9
789 2 6 4
789 2 6 4
789 2 6 4
These are the results that I want, where I subtract every next value from the new QTYOnHand-ItemQty column value. Looks like 17-3 then 14 -5 then 9 -4 for Item_ID (123):
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 9
123 4 17 5
456 7 12 5
456 8 12 -3
456 2 12 -5
456 3 12 -8
789 2 6 4
789 2 6 2
789 2 6 0
try the following:
;with cte as
(
select *, ROW_NUMBER() over (partition by Item_ID order by Item_ID) rn
from YourTable
)
, cte2 as
(
select Item_ID, ItemQty, QTYOnHand, Case when rn = 1 then QTYOnHand else 0 end - ItemQty as calc, rn
from cte
)
select Item_ID, ItemQty, QTYOnHand, sum(calc) over (partition by Item_ID order by rn) as [QtyOnHand - ItemQty]
from cte2 t1
Please find the db<>fiddle here.
I need a script that will give me a total sum of amounts per sort for the past 3 days and upcoming week.
I have made a script (link to dbfiddle.uk) that will sum the amount of the grouped date and sort types. However I am unclear on how to move forward at this point.
I want the past 3 days and upcoming 7 days of the today's date (dynamically), not just the dates found in my table.
I also want to show all the sort types.
So if there are no records for the date and sort type, show 0 as result.
plannings table
id date cell_id farmer_id
1 2020-04-21 1 1
2 2020-04-22 1 1
3 2020-04-24 1 1
4 2020-04-21 2 1
5 2020-04-22 2 1
6 2020-04-23 1 1
7 2020-04-25 1 1
8 2020-04-26 1 1
9 2020-04-22 4 1
10 2020-04-21 4 1
11 2020-04-23 4 1
planning_amounts table
id planning_id sort_type_id amount
2 1 1 43
3 1 3 34
4 2 1 54
5 3 1 45
6 4 1 90
7 5 3 45
8 5 1 99
9 6 1 66
10 7 1 999
11 8 3 90
12 9 1 23
13 10 1 43
14 11 1 55
sort_types table
id name description
1 Fijn Fijn
2 Middel Middel
3 Reuze Reuze
4 Industrie Industrie
The expected result would look like this. (this obviously for the past 3 + upcomming 7 days)
amount description date
176 Fijn 2020-04-21
34 Reuze 2020-04-21
0 Middel 2020-04-21
0 Industrie 2020-04-21
176 Fijn 2020-04-22
45 Reuze 2020-04-22
0 Middel 2020-04-22
0 Industrie 2020-04-22
121 Fijn 2020-04-23
0 Reuze 2020-04-23
0 Middel 2020-04-23
0 Industrie 2020-04-23
Query
SELECT SUM(amount) as amount, a.date, c.description
FROM planning_amounts b
join plannings a ON b.planning_id = a.id
join (SELECT * from sort_types) c ON b.sort_type_id = c.id
group by date, c.description
order by date
Hope I understood your question a bit better now:
WITH DesiredDates AS
(SELECT CAST(DATEADD(dd,-3,GETDATE()) as DATE) AS DesiredDate UNION ALL
SELECT CAST(DATEADD(dd,-2,GETDATE()) as DATE) UNION ALL
SELECT CAST(DATEADD(dd,-1,GETDATE()) as DATE) UNION ALL
SELECT CAST(GETDATE() as DATE) UNION ALL
SELECT CAST(DATEADD(dd,1,GETDATE()) as DATE) UNION ALL
SELECT CAST(DATEADD(dd,2,GETDATE()) as DATE) UNION ALL
SELECT CAST(DATEADD(dd,3,GETDATE()) as DATE) UNION ALL
SELECT CAST(DATEADD(dd,4,GETDATE()) as DATE) UNION ALL
SELECT CAST(DATEADD(dd,5,GETDATE()) as DATE) UNION ALL
SELECT CAST(DATEADD(dd,6,GETDATE()) as DATE) UNION ALL
SELECT CAST(DATEADD(dd,7,GETDATE()) as DATE)
), DesiredDatesAndSortTypes AS (
SELECT * FROM DesiredDates CROSS JOIN (select id from sort_types) t
)
SELECT SUM(ISNULL(Amount,0)) as Amount, DesiredDate, c.Description
FROM planning_amounts b
join plannings a ON b.planning_id = a.id
right join DesiredDatesAndSortTypes ddst ON CAST(a.date as DATE)=ddst.DesiredDate and b.sort_type_id=ddst.id
join sort_types c ON ddst.id = c.id
GROUP BY ddst.DesiredDate,c.Description
ORDER BY DesiredDate,Description
Here's the fiddle:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c508639ba4ec5bd49b49c9afe0692c9f
If I missunderstood yet again, please clarify where :)
This question already has answers here:
Need to query distinct combination of two fields, along with a count that distinct combination occurs
(2 answers)
Closed 3 years ago.
I have a table
table_user
col1 col2
123 456
124 457
125 458
126 459
127 460
128 461
123 456
123 456
123 457
I need to find out the combination of col1 and col2 with counts.
In above example:
col1 col2 count_combination
123 456 3
123 457 1
124 457 1
125 458 1
126 459 1
127 460 1
128 461 1
How can I find it?
With group by col1, col2:
select col1, col2, count(*) count_combination
from table_user
group by col1, col2
Simple aggregation would work :
select co1l, col2, count(*) as count_combination
from table_user
group by co1l, col2;