I checked the Search, but none seem to answer this Question.. I expect it to be farely simple though:
I have a query that results in two columns, but I need it to result in two rows.. Anyone know how?
this is the query:
SELECT (SELECT COUNT(Id) AS Expr1
FROM Table
WHERE (Description LIKE 'door%')) AS Door,
(SELECT COUNT(Id) AS Expr1
FROM Table AS Table_1
WHERE (Description LIKE 'window%')) AS Window
The result I GET is (of course):
[Door] [Window]
56 34
The result I'd LIKE to have is the following:
[OPTION] [NROfRecords]
Door 56
Window 34
Any Ideas? Thanks in advance!
You can use UNPIVOT, I would advise rewriting the query though to below:
select *
from
(
SELECT
sum(case when Description LIKE 'door%' then 1 else 0 end) Door,
sum(case when Description LIKE 'window%' then 1 else 0 end) Window
from Table1
) x
unpivot
(
NrOfRecords
for [Option] in (Door, Window)
) u
See SQL Fiddle with Demo
SELECT 'Door' AS Option, COUNT(id) FROM table WHERE description LIKE 'door%'
UNION ALL
SELECT 'Window' AS Option, COUNT(id) FROM table WHERE description LIKE 'window%'
OR...
WITH
filtered AS
(
SELECT
CASE WHEN description LIKE 'door%' THEN 'Door'
WHEN description LIKE 'window%' THEN 'Window'
ELSE 'Other' END AS option,
*
FROM
yourTable
WHERE
description LIKE 'door%'
OR description LIKE 'window%'
)
SELECT
option,
COUNT(id)
FROM
filtered
GROUP BY
option
Or...
SELECT
lookup.option,
COUNT(id)
FROM
(
SELECT 'door' AS option
UNION ALL
SELECT 'window' AS option
)
AS lookup
INNER JOIN
yourTable
ON yourTable.description LIKE lookup.option + '%'
GROUP BY
lookup.option
select
case when description like 'door%' then 'door'
when description like 'window%' then 'window'
else ''
end as [desc],
count(id)
from table
where description like 'door%' or description like 'window%'
group by
(
case when description like 'door%' then 'door'
when description like 'window%' then 'window' else '' end
)
Solution like this is following DRY principle - you do not repeat Door or Window anywhere.
It also easy to add another entities here, so you do not repeat a logic.
select
C.description,
count(*)
from Table1 as t
inner join (
select 'door%', 'Door' union all
select 'window%', 'Window'
) as C(pattern, description) on t.description like c.pattern
group by C.description
sql fiddle demo
Related
I am an Oracle SQL beginner and I have an issue with the code below:
SELECT unique_id,
CASE
WHEN type LIKE 'E-%' THEN
'electric'
ELSE
null
END electric_flag,
CASE
WHEN type LIKE 'G-%' THEN
'gas'
ELSE
null
END gas_flag,
CASE
WHEN type LIKE 'W-%' THEN
'water'
ELSE
null
END water_flag,
CASE
WHEN type LIKE 'S-%' THEN
'wastewater'
ELSE
null
END wastewater_flag
FROM (SELECT unique_id, type, end_dt
FROM table
WHERE end_dt IS NULL)
Which gives me the following results:
My goal is to have the results show like this:
It's almost like I want to group the results by the id, ignore rows that are all null, but combine the rows that return with the flag into a single row.
Any help would be greatly appreciated!
Here's a pseudo-code:
SELECT
*
FROM
(
SELECT
unique_id,
CASE type WHEN LIKE 'E-%' THEN 'electric'
WHEN LIKE 'G-%' THEN 'gas'
WHEN LIKE 'W-%' THEN 'water'
WHEN LIKE 'S-%' THEN 'wastewater'
ELSE NULL
END flag
FROM {table}
WHERE end_dt IS NULL
)
PIVOT
(
MAX(flag)
FOR flag IN ('electric' electric_flag, 'gas' gas_flag, 'water' water_flag, 'wastewater' wastewater_flag)
)
Here's an Oracle SQL Fiddle
You're almost there;
Conditionals are needed but they should be aggregated
a Grouping By unique_id clause should be added during the aggregation
a subquery is not needed
using ELSE null cases are redundant
it's suitable to add a ROW_NUMBER() analytic function in order to generate a column with ordinal values
So, use the following SQL Select Statement of Conditional Aggregation :
SELECT ROW_NUMBER() OVER (ORDER BY unique_id) AS id,
MAX(CASE
WHEN type LIKE 'E-%' THEN
'electric'
END) AS electric_flag,
MAX(CASE
WHEN type LIKE 'G-%' THEN
'gas'
END) AS gas_flag,
MAX(CASE
WHEN type LIKE 'W-%' THEN
'water'
END) AS water_flag,
MAX(CASE
WHEN type LIKE 'S-%' THEN
'wastewater'
END) AS wastewater_flag
FROM t
WHERE end_dt IS NULL
GROUP BY unique_id
Demo
I have table 'workadress' and it contain 6 columns:
work_ref,work_street ,work_zip,workTN,...
I want to find duplicate rows in the same table depending on:
If (work_street, work_zip) are duplicate together, then you should look at workTN. If it is the same then put value ' ok ', but if workTN is not the same, put 'not ok'. How can I do it with SQL?
Result like:
You can use window functions:
select t.*,
(case when min(workTn) over (partition by work_street, work_zip) =
max(workTn) over (partition by work_street, work_zip)
then 'ok' else 'not ok'
end) as result
from t;
I think just a simple group by and count should be enough to do the job like so:
select
t.*,
case when dups.dups = 1 then 'OK' else 'not OK' end
from my_table t
join (
select work_street, work_zip, count(distinct workTN) dups
from my_table
group by work_street, work_zip
) dups on dups.work_street = t.work_street amd dups.work_zip = t.work_zip
QUERY:
select ws_path from workpaths where
(
(ws_path like '%R_%') or
(ws_path like '%PB_%' ) or
(ws_path like '%ST_%')
)
OUTPUT:
/x/eng/users/ST_3609843_ijti4689_3609843_1601272247
/x/eng/users/ST_3610020_zozt5229_3610020_1601282033
/x/eng/users/ST_3611181_zozt5229_3611181_1601282032
/x/eng/users/ST_3611226_zozt5229_3611226_1601282033
/x/eng/users-random/john/N_3582168_3551186_1601040805
/x/eng/users-random/james/N_3582619_3551186_1601041405
/x/eng/users-random/jimmy/N_3582791_3551186_1601042005
/x/eng/users/R_3606462_3606462_1601251334
/x/eng/users/R_3611775_3612090_1601290909
/x/eng/users/R_3612813_3613016_1601292252
Is there way to group partially by ST_, N_ and R_?
i.e. group by ws_path wont work at the moment for the obvious reason
I need to only look at the last item in the path (split by '/') and then the front part of splitting with '_'
You can use regexp_substr to get the patterns being searched for and then group by the number of such occurrences.
select regexp_substr(ws_path,'\/R_|\/PB_|\/ST_'), count(*)
from workpaths
group by regexp_substr(ws_path,'\/R_|\/PB_|\/ST_')
Regexp is a good solution but can be expensive. A simpler substring might be cheaper and faster:
CREATE TABLE tbl (field1 VARCHAR(100));
INSERT INTO dbo.tbl
( field1 )
VALUES
('/x/eng/users/ST_3609843_ijti4689_3609843_1601272247'),
('/x/eng/users/ST_3610020_zozt5229_3610020_1601282033'),
('/x/eng/users/ST_3611181_zozt5229_3611181_1601282032'),
('/x/eng/users/ST_3611226_zozt5229_3611226_1601282033'),
('/x/eng/users-random/john/N_3582168_3551186_1601040805'),
('/x/eng/users-random/james/N_3582619_3551186_1601041405'),
('/x/eng/users-random/jimmy/N_3582791_3551186_1601042005'),
('/x/eng/users/R_3606462_3606462_1601251334'),
('/x/eng/users/R_3611775_3612090_1601290909'),
('/x/eng/users/R_3612813_3613016_1601292252');
SELECT
COUNT(CASE WHEN field1 LIKE '%/ST_%' THEN 1 ELSE NULL END) AS 'st_count',
COUNT(CASE WHEN field1 LIKE '%/N_%' THEN 1 ELSE NULL END) AS 'n_count',
COUNT(CASE WHEN field1 LIKE '%/R_%' THEN 1 ELSE NULL END) AS 'r_count'
FROM dbo.tbl
I have the following problem:
I have a table that looks something like this:
ArticleID|Group|Price
1|a|10
2|b|2
3|a|3
4|b|5
5|c|5
6|f|7
7|c|8
8|x|3
Now im trying to get a result like this:
PriceA|PriceRest
13|30
Meaning I want to sum all prices from group a in one column and the sum of everything else in another column.
Something like this doesnt work.
select
sum(Price) as PriceGroupA
sum(Price) as PriceRest
from
Table
where
Group='a'
Group<>'a'
Is there a way to achieve this functionality?
SELECT
sum(case when [Group] = 'a' then Price else 0 end) as PriceA,
sum(case when [Group] <> 'a' then Price else 0 end) as PriceRest
from
Table
Please try:
select
sum(case when [Group]='A' then Price end) PriceA,
sum(case when [Group]<>'A' then Price end) PriceRest
from
Table
SQL Fiddle Demo
You just need two sub-queries:
SELECT (SELECT SUM(PRICE)
FROM Table1
WHERE [Group] ='a') AS PriceGroupA,
(SELECT SUM(PRICE)
FROM Table1
WHERE [Group]<>'a') AS PriceRest
Demo-Fiddle
I've been trying to select the status of doing a LIKE comparison:
SELECT (`my_column` LIKE '%thepattern'%) AS `did_match`
Thanks!
Edit: Just to clarify this was used so I could get this:
SELECT
(CASE WHEN `title` LIKE '%keyword%' THEN 20 ELSE 0 END) +
(CASE WHEN `desc` LIKE '%keyword%' THEN 10 ELSE 0 END)
AS `match`
FROM myTable
ORDER BY `match`
You need to use:
SELECT t.my_column AS did_match
FROM YOUR_TABLE t
WHERE t.my_column LIKE '%thepattern%'
Replace YOUR_TABLE with the name of your table...
Without a FROM clause, MySQL (and SQL Server) will allow the query, but not against any data.
Not sure I understand the question, and it looks like OMG Ponies has figured it out better than I have, but in case this helps:
SELECT CASE WHEN `my_column` LIKE '%thepattern%'
THEN 'did_match' ELSE 'no_match' END CASE AS MYTEST
FROM ...
Link to CASE statement documentation.