I'm not even sure if this is possible using SQL, but I'm completely stuck on this problem. I have a table like this:
Total Code
212 XXX_09_JUN
315 XXX_7_JUN
68 XXX_09_APR
140 XXX_AT_APR
729 XXX_AT_MAY
I need to sum the "total" column grouped by the code. The issue is that "XXX_09_JUN" and "XXX_7_JUN" and "XXX_09_APR" need to be the same group.
I was able to accomplish this by creating a new column where I assigned values based on the row's code, but since this needs to be done on multiple tables with millions of entries, I can't use that method.
Is there some way that I could group the rows based on a condition such as:
WHERE Code LIKE '%_09_%' OR Code LIKE '%_7_%'
This is not the only condition - I need about 10 conditions like this. Sorry if that doesn't make sense, I'm not sure how to explain this...
Also, if this can be accomplished using Visual Studio 2008 and SSRS more easily, that would work as well because that is the final goal of this query.
Edit: To clarify, this would be the ideal result:
Total Code
595 had_a_number
869 had_at
One option is to use a CASE expression:
GROUP BY CASE
WHEN Code LIKE '%!_09!_%' ESCAPE '!'
THEN 'had_a_number'
WHEN Code LIKE '%!_7!_%' ESCAPE '!'
THEN 'had_a_number'
WHEN Code LIKE '%!_AT!_%' ESCAPE '!'
THEN 'had_at'
ELSE 'other'
END
Add however many WHEN conditions to assign whatever condition to a "group".
Note that the underscore is a wildcard character for the LIKE operator. An underscore will match any single character. To search for a literal underscore, you would need to "escape" the underscore within the string literal.
'A_12_E' LIKE '%_12_%' => TRUE
'AB12DE' LIKE '%_12_%' => TRUE
'A_12_E' LIKE '%!_12!_%' ESCAPE '!' => TRUE
'AB12DE' LIKE '%!_12!_%' ESCAPE '!' => FALSE
SQL Fiddle
MS SQL Server 2008 Schema Setup:
CREATE TABLE TEST_TABLE(Total INT, Code VARCHAR(20))
GO
INSERT INTO TEST_TABLE VALUES
(212, 'XXX_09_JUN'),
(315, 'XXX_7_JUN'),
(68, 'XXX_09_APR'),
(140, 'XXX_AT_APR'),
(729, 'XXX_AT_MAY')
GO
Query 1:
SELECT SUM(Total) Total
,CASE
WHEN Code LIKE '%_%[0-9]%_%'
THEN 'had a number'
WHEN Code NOT LIKE '%_%[0-9]%_%'
THEN 'had at'
END AS Code
FROM TEST_TABLE
GROUP BY CASE
WHEN Code LIKE '%_%[0-9]%_%'
THEN 'had a number'
WHEN Code NOT LIKE '%_%[0-9]%_%'
THEN 'had at'
END
Results:
| TOTAL | CODE |
|-------|--------------|
| 595 | had a number |
| 869 | had at |
Heres a ridiculous way to solve your problem:
SQLFiddle
Schema:
CREATE TABLE tblTotalCode(
Total INTEGER,
Code VARCHAR(15)
)
INSERT INTO tblTotalCode VALUES(212,'XXX_09_JUN')
,(315,'XXX_7_JUN')
,(68, 'XXX_09_APR')
,(140,'XXX_AT_APR')
,(729,'XXX_AT_MAY')
Code:
SELECT CASE WHEN(LEFT(RIGHT(Code, LEN(Code) - CHARINDEX('_', Code, 1)), CHARINDEX('_', RIGHT(Code, LEN(Code) - CHARINDEX('_', Code, 1)), 1) - 1)) IN ('09','7') THEN '09 or 7' ELSE 'Not 09 or 7' END AS '09 or 7 Group'
, SUM(Total) AS 'Total'
FROM tblTotalCode
Group By CASE WHEN(LEFT(RIGHT(Code, LEN(Code) - CHARINDEX('_', Code, 1)), CHARINDEX('_', RIGHT(Code, LEN(Code) - CHARINDEX('_', Code, 1)), 1) - 1)) IN ('09','7') THEN '09 or 7' ELSE 'Not 09 or 7' END
Related
How to write sql query which will show values skipping first character if it is 0 (only the first character). All values are 3 characters long.
Examples:
numbers
123
023
003
102
should display as follows (after executing the query)
numbers
123
23
03
102
I used the following solution, but it removes all 0's, not just the first. How to fix it so that it only removes the first character if it is 0.
SUBSTRING(numbers, PATINDEX('%[^0]%', numbers+'.'), LEN(numbers))
I will be grateful for your help.
You can use CASE expression:
SELECT CASE WHEN LEFT(numbers, 1) = '0' THEN RIGHT(numbers, 2) ELSE numbers END AS FormattedNumbers
why not using simple substr() ?
select case when substr(mycol,1,1)='0' then substr(mycol,2) else mycol end
from my table
you did not mention your DB so i assumed its oracle. This will work in any RDBMS.
You can use charindex and substring methods to do string manipulation :)
select
case when charindex('0', number) = 1
then substring(number, 2, len(number))
else number end
from (
select '123' number
union all
select '023'
union all
select '003'
union all
select '102'
) a
I have a table where numbers are stored as strings and some bigger numbers look like 1,634.5 so I have to replace the "," with blanks to get accurate data.
I have been using this which works, but I can't figure out how to do that with sum and case as I need to sum up the revenue together when it equals xyz
code that works
(replace(revenue, ',', '')
code that doesn't work
sum(replace(revenue, ',', '') case WHEN food in ('burgers', 'fries') then revenue else 0 end)) as foodsum
group by foodcategory
i also tried using a nested query but didn't work, does anyone have any recommendations?
thanks!
Here's the correct way on aggregating your revenue based on foodcategory.
select case when food in ('burgers', 'fries') then sum(replace(revenue, ',', '')) else 0 end as foodsum
from table1
group by foodcategory
I have a text field and want to pull out an ID number from the field - the ID always starts with an 8 and is 12 characters long (e.g 899900014658), the current code uses the substring method below:
substring(textfield,charindex('8',textfield),12) as extractedID
This pulls out anything starting with an 8 though so I'm getting results like '8 am', '8 February' etc in the extractedID field.
Is there a method of extracting anything starting with an 8 where the second and third characters are also a number?
edit - solved using PATINDEX
SUBSTRING(SubmissionDiaryEntry,(PATINDEX('%8[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',SubmissionDiaryEntry)),12)
If I understand your question:
Select *
From YourTable
Where textfield like '8[0-9][0-9]%'
Ckeck below, it may help:
declare #textfield nvarchar(20) = '845456798234'
select case when substring(#textfield, 2, 1) like '[0-9]'
and substring(#textfield, 3, 1) like '[0-9]'
then substring(#textfield,charindex('8',#textfield),12)
else 'not valid'
end
as extractedID
-- test
set #textfield = '8r5456798234'
select case when substring(#textfield, 2, 1) like '[0-9]'
and substring(#textfield, 3, 1) like '[0-9]'
then substring(#textfield,charindex('8',#textfield),12)
else 'not valid'
end
as extractedID
however John's answer is much simplier and it looks like exactly what you are looking for.
Below is just to clarify..., using John's script, answer to your issue would be:
select substring(textfield,charindex('8',textfield),12) as extractedID
from yourTable
where textfield like '8[0-9][0-9]%'
I need to return results where a space is NOT used in the 3rd to last position (so from the right). Attempting to identify records where a US state acronym was not used.
JOHN MN
MATT HI
TERRY ARKANSAS
I'm running this through DBVisualizer and I am stumped. I've tried to add some CHARINDEX, RIGHT...etc, but no combination is working.
Ideally, it would look something like:
SELECT INSTR(COLUMN, **<>** ' ', -3) FROM TABLE
try Something like this :
select * from yourtable
where
case when length(trim(yourcolumn))>2 and right(trim(yourcolumn), 3) like ' %' then 1 else 0 end =0
or this
select * from yourtable
where trim(yourcolumn) not like '% __'
Try this:
SELECT IIF(len(RIGHT(COLUMN,LEN(COLUMN)-3))=LEN(REPLACE(RIGHT(COLUMN,LEN(COLUMN)-3),' ','')),0,1)
If it returns 0, that means it does not have a blank value from 3rd to the last position.
If it returns 1, that means it does have a blank.
I am working with a database of products, trying to extract the product color from a combined ID/color code column where the color code is always the string following the last hyphen in the column. The issue is that the number of hyphens, product ID, and color code can all be different.
Here are four examples:
ABC123-001
BCD45678-0165
S-XYZ999-M2235
A-S-ABC123-001
The color codes in this case would be 001, 0165, M2235, and 001. What would be the best way to select these into their own column?
I think the following does what you want:
select right(col, charindex('-', reverse(col)) - 1)
In the event that you might have no hyphens in the value, then use a case:
select (case when col like '%-%'
then right(col, charindex('-', reverse(col)) - 1)
else col
end)
It is great to check whether the hyphen exists or not in the string, to avoid the following error:
Invalid length parameter passed to the right function.
SELECT CASE WHEN Col like '%\%' THEN RIGHT(Col,CHARINDEX('\',REVERSE(Col))-1) ELSE '' END AS ColName