Oracle - SQL Query to return a value from a column in another table where value equals a different column - sql

From within the results of a query I need to take the first 3 digits (got this part - substr) from a column in one table and pull the value from a second table where the 3 digit code is in a different column of the same row. While I'm not looking for assistance in this part I will be grouping by the code column for reference
Example
Table 1 Table 2
3_Digit Code_Column Description
123456 123 Blue
103456 103 Green
so if my substr query return 123 5 times I'm looking for
Blue 3

select t2.Code_Column, count(*)
from table1 t1
join table2 t2
on substr(t1.digit, 0, 3) = t2.Code_Column
group by t2.Code_Column
SQL Fiddle sample

Related

How to do the sum for each row of the values of a column?

I have a SQL table containing a column of list of int, like this
Index Column to sum
1 1:5:13:3:6:7:11:2:4:1:2:5
2 1:7:2:1:1
3 19:05:05
4 2:1:1:5:1:4:64:177:86:75:2:83:2:57:1
5 43
6 1:1:1:3:10:6:1
7 2:11:4:3:1
8 1:5:2:3:34:2:2
9 4:3:1:2
10 4:1:1:4
I would like to calcul in SQL the sum for each row of the Column to sum value.
It would give something like this:
1 60
2 12
...
10 10
Any idea?
Thanks
Your first effort should go into fixing your data model, as commented by Gordon Linoff. The numbers should be stored in a separate table, with each value on a separate row.
In Postgres, you can split the values to rows using regexp_split_to_table(), then aggregate:
select t.id, sum(x.val::int) result
from mytable t
cross join lateral regexp_split_to_table(t.column_to_sum, ':') as x(val)
group by t.id

Need to Transform a Rows of Data into a single Row

I have a set of Data in MS Access
Number Owner
1 Heelo
1 Hi
1 There
2 What
2 Up
This needs to be transferrid into
Number Owner1 Owner2 Owner3 Owner4
1 Heelo Hi There -
2 What Up - -
Any idea on how to go on with this?
The crux in this case is we don't have a third column from where we can pivot the data.
You could add a third column with a sequence of numbers:
SELECT Number, (select count(*)
from YourTable as s
where s.number = t.number) as sequence, owner
from YourTable as t
then apply this solution to the results: SQL to transpose row pairs to columns in MS ACCESS database

SQL - Select the longest substrings

I have the data like that.
AB
ABC
ABCD
ABCDE
EF
EFG
IJ
IJK
IJKL
and I just want to get ABCDE,EFG,IJKL. how can i do that oracle sql?
the size of the char are min 2 but doesn't have a fixed length, can be from 2 to 100.
In the event that you mean "longest string for each sequence of strings", the answer is a little different -- you are not guaranteed that all have a length of 4. Instead, you want to find the strings where adding a letter isn't another string.
select t.str
from table t
where not exists (select 1
from table t2
where substr(t2.str, 1, length(t.str)) = t.str and
length(t2.str) = length(t.str) + 1
);
Do note that performance of this query will not be great if you have even a moderate number of rows.
Select all rows where the string is not a substring of any other row. It's not clear if this is what you want though.
select t.str
from table t
where not exists (
select 1
from table t2
where instr(t1.str, t2.str) > 0
);

SQL - How to returns values outside a date range query

Hoping someone can help out here, I have the following data
Field 1 Field 2 Date Data
1 1 12/09/14 1
2 2 12/09/14 1
3 1 11/09/14 1
4 3 11/09/14 1
I need to write an sql query that sums all "Data" based on a date range and then anything that matches in Field 2. So if a line is out of the date range but the value in Field 2 matches another line that is within the date range, it should be included
For example, if I was to query everything for the 12/09/14, I want to see the sum of line 1, 2 and 3.... as line 3 is outside of the date range but it matches line 1 in the "Field 2" column. Line 4 should not be included as it is outside the range and does not have a matching value in "Field 2"
Any ideas?
I've been playing around with variations of queries but it either selects only the date range values or everything :(
EDIT:
Ok I've given Rajesh answer a try and it doesn't seem to include the data outside the date range. I was expecting the final sum in this example to equal 3 but it's only showing 2
select sum(a) from (
select sum(batch_m2_nett) as a
from batch_inf
where batch_date = to_date('30/09/15','DD/MM/RR')
union
select sum(f2.batch_m2_nett) as a
from batch_inf f1
inner join batch_inf f2
on f1.batch_date = to_date('30/09/15','DD/MM/RR')
and f1.batch_opt_start_batch = f2.batch_opt_start_batch
and f2.batch_date != to_date('30/09/15','DD/MM/RR')
);
SUM(A)
------
2
SQL> select batch_no, batch_opt_start_batch, batch_date, batch_m2_nett from batch_inf where batch_no in (8811,8812,8814);
BATCH_NO BATCH_OPT_START_BATCH BATCH_DATE BATCH_M2_NETT
-------- --------------------- --------------- -------------
8811 8814 30-SEP-15 1
8812 8814 30-SEP-15 1
8814 8814 01-OCT-15 1
the first statement gets sum of data values where date matches
the second statement gets sum of data values where field2 of matched date row is matching with other rows using self join
select SUM(s)
from
(
select SUM(data) as s
from fields
where date ='12/09/14'
union
select sum(f2.data) as s
from fields f1
inner join fields f2
on f1.date ='12/09/14'
and f1.field2 = f2.field2
and f2.date != '12/09/14'
) T

The MIN() Function Ms Access

this is a sample sql query that i created ms access query. i am trying to get only one row the min(DATE). how ever when i run my query i get multiple lines. any hits? thanks
SELECT tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
Min(tblWarehouseItem.whiDateIn) AS MinOfwhiDateIn,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID
FROM tblWarehouseItem
GROUP BY tblWarehouseItem.whiDateIn,
tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID;
If i have my sql code like that is working as it should:
SELECT MIN(tblWarehouseItem.whiDateIn) FROM tblWarehouseItem;
In the first query, you group by a number of columns. That means the minimum value will be calculated for each group, which in turn means you may have multiple rows. On the other hand, the second query will only get the minimum value for the specified column from all rows, so that there is only one row in the result set.
A simple example is shown below to illustrate the above.
Table:
Key Value
1 1
1 2
2 3
2 4
On Group By Key:
GroupKey MinValue
1 = min(1,2) = 1 -> Row 1
2 = min(3,4) = 3 -> Row 2
On Min (Value)
MinValue
=min(1,2,3,4) = 1 -> Row 1
For a table like above, if you want to select all rows and also show the minimum value from whole table rather than per group, you can do something like this:
select key, (select min(value) from table)
from table
SELECT WI.*
FROM tblWarehouseItem AS WI INNER JOIN (SELECT whiimtID, MIN(tblWarehouseItem.whiDateIn) AS whiDateIn
FROM tblWarehouseItem
GROUP BY whiimtID) AS MinWI ON (WI.whiDateIn = MinWI.whiDateIn) AND (WI.whiimtID = MinWI.whiimtID);