How do I exclude when all lines matches - sql

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

Related

Extract only variables which is greater than other table in influxDB

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

Need help grouping a column that may or may not have the same value but have the same accounts

How do I output data stored in Table 1 so that each like account number has that also has the same CPT group's together but the ones that do not match fall to the bottom of the list?
I have one table: select * from CPTCounts and this is what is displays
Format (relevant fields only):
account OriginalCPT Count ModifiedCPT Count
11 0 71010 1
11 71010 1 0
2 0 71010 1
2 0 71020 9
2 0 73130 1
2 0 77800 1
2 71010 1 0
2 71020 8 0
2 73130 1 0
2 73610 1 0
2 G0202 4 0
31 99010 1 0
31 0 99010 4
31 0 99700 2
What I want the results to be grouped like is below... and display like this or similar.
Account OriginalCPT Count ModifiedCPT Count
11 71010 1 71010 1
2 71010 1 71010 1
2 71020 8 0
2 73130 1 0
2 73610 1 0
2 G0202 4 0
31 99010 1 99010 4
31 0 99700 2
I have one table with the values above;
Select * from #CPTCounts
The grouping I am looking for is the Original = Modified CPT and sometimes I will not have a value in one side or the other but most of the times I will have a match. I would like to place all of the unmatched ones at the bottom of the account.
any suggestions?
I was thinking of creating a second table and joining the two with the account but how do I return each value?
select cpt1.account, cpt1.originalCPT, cpt1.count, cpt2.modifiedcpt, cpt2.count
from #cptcounts cpt1
join #cptcounts cpt2 on cpt1.accont = cpt2.account
but am having trouble with that solution.
I'm not sure I have an exact solution, but perhaps some food for thought at least. The fact that you need either the "original" or the "modified" set of columns makes me think that you need a full outer join rather than a left join. You don't mention which database you are using. In MySql, for example, full joins can be emulated by means of a union of a left and a right join, as in the following:
select cpt1.account, cpt1.originalCPT, cpt1.countO, cpt2.modifiedcpt, cpt2.countM
from CPTCounts cpt1
left outer join CPTCounts cpt2
on cpt1.account = cpt2.account
and cpt1.originalCPT=cpt2.modifiedCPT
where cpt1.account is not null
and (cpt1.originalCPT is not null or cpt2.modifiedCPT is not null)
union
select cpt1.account, cpt1.originalCPT, cpt1.countO, cpt2.modifiedcpt, cpt2.countM
from CPTCounts cpt1
right outer join CPTCounts cpt2
on cpt1.account = cpt2.account
and cpt1.originalCPT=cpt2.modifiedCPT
where cpt2.account is not null
and (cpt1.originalCPT is not null or cpt2.modifiedCPT is not null)
order by originalCPT, modifiedCPT, account
The ordering brings the non-matching rows to the top, but that seemed a lesser problem than getting the matching to work.
(Your output data is a bit confusing, because the CPT 71020, for example, occurs in both original and modified columns, but you haven't shown it as one of the matching ones in your result set. I'm presuming this is because it is just an example... but if I'm wrong, then I am missing some part of your intention.)
You can play around in this SQL Fiddle.

Concatenate and Remove spaces from 4 columns

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

SQL complex grouping "in column"

I have a table with 3 columns (sorted by the first two):
letter
number (sorted for each letter)
difference between current number and previous number of the same letter
I'd like to calculate (with vanlla SQL) a fourth new column RESULT to group these data when the third column (difference of number between contiguos record; i.e #2 --> 4 = 5-1) is greater than 30 marking all the records of this interval with letter-number of the first record (i.e A1 for #1,#2,#3).
Since the difference between contiguos numbers makes sense just for records with the same letter, for the first record of a new letter, the value of differnce is 31 (meaning that it's a new group; i.e. #6).
Here is what I'd like to get as result:
# Letter Number Difference RESULT (new column)
1 A 1 1 A1
2 A 5 4 A1
3 A 7 2 A1
4 A 40 33 A40 (*)
5 A 43 3 A40
6 B 1 31 B1 (*)
7 B 25 24 B1
8 B 27 2 B1
9 B 70 43 B70 (*)
10 B 75 5 B70
Now I can only find the "breaking values" (*) with this query where they get a value of 1:
select letter
,number
,cast(difference/30 as int) break
from table
where cast(difference/30 as int) = 1
Even though I'm able to find these breaking values I can't finish my task.
Can anyone help me finding a way to obtain the column RESULT?
Thanks in advance
FF
As I understand you need to construct the last result column. You can use concat to do that:
SELECT letter
,number
,concat(letter, cast(difference/30 as int)) result
FROM table
HAVING result = 'A1'
after some exercise and a little help from a friend of mine, I've found a possible solution to my sql prolblem.
The only requirment for the solution is that my first record must have a value of 31 in Difference field (since I need "breaks" when Difference > 30 than the previous record).
Here is the query to get the column RESULT I needed:
select alls.letter
,alls.number
,ints.letter||ints.number as result
from competition.lag alls
,(select letter
,number
,difference
,result
from (select letter
,number
,difference
,case when difference>30 then 1 else 2 end as result
from competition.lag
) temp
where result = 1
) ints
where ints.letter=alls.letter
and alls.number>=ints.number
and alls.number-30<=ints.number

Grouping 'like' rows in SQL and summing?

I have a report that looks like this:
Notice the rows that I have circled (row 2 and 3 in the 1109 group). These rows have the same MemberSep, Location, and Consumer text. The only difference is they each have different values for the TODKWH001 and TODKWH002 fields.
What I'd like to do is group rows like this together and sum the TODKWH001 and TODKWH002 fields together.
So, instead of these two rows:
00002574027 00000003105401 YEAGER FMS PMP 50 13 00 0 1
00002574027 00000003105401 YEAGER FMS PMP 50 13 00 4998 81
I'd have just one row:
00002574027 00000003105401 YEAGER FMS PMP 50 13 00 4998 82
Can I do this in SQL? Or should I try to do the grouping in my report?
Also, here is my SQL that I use to populate the report now:
SELECT CAR1.CAV_MBRHISTDETL.MBRSEP,
CAR1.CAV_MBRHISTDETL.LOCATION,
CAR1.CAV_MBRHISTDETL.BILLTYPE,
CAR1.CAV_MBRHISTDETL.BILLMOYR,
CAR1.CAV_MBRHISTDETL.RATE,
CAR1.CAV_LOCINFODETL.DIST,
CAR1.CAV_DEMANDHISTDETL.TODKWH_001,
CAR1.CAV_DEMANDHISTDETL.TODKWH_002,
CAR1.CAV_LOCINFODETL.ADDR1, CAR1.CAV_DEMANDHISTDETL.READTYPE
FROM CAR1.CAV_LOCINFODETL, { oj CAR1.CAV_MBRHISTDETL LEFT OUTER JOIN
CAR1.CAV_DEMANDHISTDETL ON CAR1.CAV_MBRHISTDETL.MBRSEP =
CAR1.CAV_DEMANDHISTDETL.MBRSEP AND
CAR1.CAV_MBRHISTDETL.BILLMOYR = CAR1.CAV_DEMANDHISTDETL.BILLMOYR }
WHERE CAR1.CAV_MBRHISTDETL.LOCATION = CAR1.CAV_LOCINFODETL.LOCATION AND
(CAR1.CAV_MBRHISTDETL.BILLMOYR IN ('1104', '1105', '1106', '1107', '1108','1109')) AND
(CAR1.CAV_MBRHISTDETL.RATE = '0096') AND (CAR1.CAV_MBRHISTDETL.BILLTYPE IN ('00', '01'))
ORDER BY CAR1.CAV_LOCINFODETL.DIST,
CAR1.CAV_MBRHISTDETL.BILLMOYR,
CAR1.CAV_MBRHISTDETL.MBRSEP
Work this into your current query:
SELECT MemberSep, Location, Consumer, SUM(TODKWH001), SUM(TODKWH002)
FROM yourtable
GROUP BY MemberSep, Location, Consumer