I need to output 1 column that contains all possible combinations of strings from 4 columns, concatenated and with spaces removed using T-SQL query or Excel formula.
Details:
I have a table with 4 rows and 4 columns filled with various strings of different lengths. I need to output the all possible combinations of strings in each column. Because I have a 4x4 table, I am expecting 256 different combinations.
Luckily, I can do this with this SQL query:
select
t1.A,
t2.B,
t3.C,
t4.D
from
table t1 cross join
table t2 cross join
table t3 cross join
table t4
However, this gives me an out put with 4 columns. I need concatenate the results and remove any spaces.
For now, I use the SQL query above, then copy and paste the results into Excel. From Excel, I would use the TRIM and CONCATENATE functions to get a single column of what I need.
Is there a simple way to do this with a T-SQL query?
Better yet, is there an Excel formula I can use to do this?
I have an Excel formula I use, but it only works for 2 columns. Once I get the combinations from the 2 columns, I copy and paste to another sheet, enter the values of the 3rd column into the new sheet and use the same formula. Not sure how to extend this to 4 columns.
IF(ROW()-ROW($F$1)+1>COUNTA(A:A)*COUNTA(B:B),"",INDEX(A:A,INT((ROW()-ROW($F$1))/COUNTA(B:B)+1))&INDEX(B:B,MOD(ROW()-ROW($F$1),COUNTA(B:B))+1))
Edit:
Example Table:
A B C D
-----------
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
Desired Results:
Results
--------
01020304
01020308
01020312
01020316
........
13141512
13141516
SELECT CONCAT(RTRIM(LTRIM(A.col1)),RTRIM(LTRIM(B.col2)),RTRIM(LTRIM(C.col3)),RTRIM(LTRIM(D.col4)))
FROM yourTable A
CROSS JOIN yourTable B
CROSS JOIN yourTable C
CROSS JOIN yourTable D
Related
Assuming I have the right naming, what O am trying to write is a function or stored procedure to compare names and find out if they are the same value.
I think its called fuzzy matching
For example, a table has 2 columns and table b has 3 columns:
Name
Number
Hello
24
Evening
56
Name
Num
F
Heello
23
some value
GoodEvening
15
some value
I want table like
A
D
Hello
Heello
Morning
GoodMorning
Currently, I'm using
Select A.Name, B.Name
from table A
left table B
on A.Name like B.Name
or (LTRIM(RTRIM(REPLACE(REPLACE(REPLACE( A.Name,' ',''),'-',''),'''',''))) = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(B.Name,' ',''),'-',''),'''',''))))
OR (A.Name LIKE '%'+B.Name+'%')
OR (B.Name LIKE '%'+A.Name+'%')
It is giving me a result, but not too accurate and is very slow, any other way I could try to compare these values?
I am using 2017 MS SQL Express. The following code creates this erroneous result. Any clues why?
SELECT BNAME, LIC_STRING INTO dbo.SEPARATE2
FROM dbo.together
CROSS APPLY STRING_SPLIT(LIC_STRING, ',');
table dbo.together result, dbo.SEPARATE2
BNAME | LIC_STRING BNAME | LIC_STRING
A1 | AB,AC,AD A1 | AB,AC,AD
B2 | AX,AD A1 | AB,AC,AD
A1 | AB,AC,AD
B2 | AX,AD
B2 | AX,AD
This is because you are selecting the original values. Your query multiplies the number of values -- one for each element, but you are not selecting those values.
You probably want each element, so include that in the SELECT:
SELECT t.BNAME, s.value
INTO dbo.SEPARATE2
FROM dbo.together t CROSS APPLY
STRING_SPLIT(t.LIC_STRING, ',') s;
Here is a db<>fiddle.
You are using cross apply which is applying your join on 2 tables where result contain table on left with all the columns and table on right with matching values.
Since in this query dbo.together has 2 rows and for each value in string_split it generate same number of rows. 1:n relation in between these 2 tables.
SELECT TOGETHER.BNAME, STR.VALUE INTO dbo.SEPARATE2
FROM dbo.together AS TOGETHER
CROSS APPLY (SELECT VALUE FROM STRING_SPLIT(TOGETHER.LIC_STRING, ',')) AS STR;
You may find this link for more details on CROSS APPLY.
I am using influxDB and I would like to extract some values which is greater than certain threshold in other table.
For example, I have two tables as shown in below.
Table A
Time value
1 15
2 25
3 9
4 22
Table B
Time threshold
1 16
2 12
3 13
4 15
Give above two tables, I would like to extract three values which is greater than first row in Table B. Therefore what I want to have is as below.
Time value
2 25
4 22
I tried it using below sql query, but it didn't give any correct result.
select * from data1 where value > (select spec from spec1 limit1);
Look forward to your feedback.
Thanks.
Integrate the condition in an inner join:
select * from tableA as a
inner join tableB as b on a.id=b.id and a.value > b.threshold
When your time column doesn't only include integer values, you have to format the time and join on a time range. Here is an example:
SQL join on time range
I’m looking to exclude all rows when there is matching edit_codes to line_nbr.
Teradata SQL query:
Select d.*
From decision_table d
LEFT OUTER JOIN edit_codes e ON
d.unique_key=e.unique_key
WHERE e.source_edit like (‘hp%’)
AND d.line_nbr <>SUBSTR(e.edit_line_nbr,
3,2)
***OR I wrote it as “AND d.line_number =
SUBSTR(e.edit_line_nbr,3,2)
Q1: Even though, row 2 and 3 (EDIT_CODES TO LINE_NBR) don’t match, however, since row 1 and 4 does I want to exclude. How do I write it to capture if line_nbr matches any edit_codes on any row?
row# EDIT_CODES LINE_NBR
1 HP01 01
2 HP02 01
3 HP01 02
4 HP02 02
I have the following query that generates my pivot results:
SELECT * FROM
(
SELECT
#tmp1.Name,
DATEDIFF(D,#tmp1.AuthDate,#tmp1.AuthExpirationDate) AS AuthLenInDays,
#tmp1.NbrOfAuthorizations,
#tmp1.MODE
FROM #tmp1
LEFT JOIN #tmp2
ON #tmp2.AuthID = #tmp1.AuthID
GROUP BY #tmp1.Name, #tmp1.NbrOfAuthorizations, #tmp1.AuthDate, #tmp1.AuthExpirationDate, #tmp1.MODE
) AS InnerTbl
PIVOT
(AVG(AuthLenInDays) FOR [MODE] IN ([Preservation])
) PivotResults1
The results are as follows:
Name NbrOfAuthorizations Preservation
Centro 1 79
Dennis 1 92
Therapy Center 1 68
Florez 1 92
I have two problems that I have not been able to figure out, I've tried everything I can think of and even other suggestions from stackoverflow.
I can't figure out how to change the name of the right-most column (Preservation)
in my results. It's an average number so I'd like to label that
column 'Average'.
Also, the NbrOfAuthorizations needs to be summed for all the values
in the table. I have tried using a pivot and this gets me close but
not all the way there, I have also tried using a SUM in the InnerTbl
query but that isn't it either.
If I take my raw data and export that to excel and do a pivot there, I can see the numbers and what I should be getting. I am trying to take that process and do it purely in SQL. Based on the data in the table, the values for the SUM should be
Name NbrOfAuthorizations Preservation
Centro 5 79
Dennis 1 92
Therapy Center 57 68
Florez 1 92
Any masters of pivot out there?
Looks like you don't need pivot at all:
select
t1.Name,
sum(t1.NbrOfAuthorizations) as NbrOfAuthorizations,
avg(datediff(dd, t1.AuthDate, t1.AuthExpirationDate)) as AuthLenInDays
from #tmp1 as t1
-- looks like you don't need join also, or there're multiple rows
-- in #tmp2 for row in #tmp1
-- left outer join #tmp2 as t2 on t2.AuthID = t1.AuthID
where t1.mode = 'Preservation'
group by t1.Name