Self-join a table in SQL Server 2008 - sql

I would like to use INNER JOIN on one table. But I get this error message:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'a'.
My query is :
select *
from
(select
*,
ROW_NUMBER() OVER (ORDER BY GoodMainCode) as row
from [SarMem].[dbo].[Book_Data1]
where GoodName like '%A%' and GroupCode = 115) a
inner join a b on b.GoodMainCode = a.GoodMainCode
where a.row > 0 and a.row <= 100
updated

Do it with cte:
;with a as(select *, ROW_NUMBER() OVER (ORDER BY GoodMainCode) as row
from [SarMem].[dbo].[Book_Data1]
where GoodName like '%A%' and GroupCode = 115)
select * from a
join a b on b.GoodMainCode = a.GoodMainCode
where a.row > 0 and a.row <= 100

Maybe something like that?
WITH List AS (
SELECT
ROW_NUMBER() OVER (ORDER BY GoodMainCode) AS Row, *
FROM
[SarMem].[dbo].[Book_Data1]
WHERE
( GoodName like '%A%' )
AND ( GroupCode = 115 )
)
SELECT * FROM List WHERE Row between 1 and 100

I think here a is an alias name not the table name. So SQL SERVER won't allow a to create one more alias b.
So if you wanna use same table as a, then you have to rewrite subquery for b also.
Like,
select * from (
select *,ROW_NUMBER() OVER (ORDER BY GoodMainCode) as row from [SarMem].[dbo].[Book_Data1] where GoodName like '%A%' and GroupCode = 115 ) a
INNER JOIN (
select *,ROW_NUMBER() OVER (ORDER BY GoodMainCode) as row from [SarMem].[dbo].[Book_Data1] where GoodName like '%A%' and GroupCode = 115 ) b on b.GoodMainCode = a.GoodMainCode where a.row > 0 and a.row <= 100

Related

SQL Case MAX WHEN LIKE

I would like to create a case statement or a rank over statement for a particular outlier case I have.
I am not sure how to write a case statement utilizing CASE (pseudo code)
WHEN MAX Total_Revenue COMPANY like 'ABC%'
else COMPANY
i have tried rank over, but it is not working:
,RANK() OVER(COMPANY LIKE 'DEF%' ORDER BY Total_Revenue DESC) AS grp
Current table:
Company Total_Revenue
ABC 10
DEF1 25 --This row will NOT be selected as its less than
DEF2 35 -- this row will be kept
GHI3 65
JKL9 100
Ouput table:
Company Total_Revenue
ABC 10
DEF2 35 --kept
GHI3 65
JKL9 100
There's quite a few ways to do what it seems like you are after:
Using a subquery to find max revenue for each comp:
SELECT Company, Total_Revenue
FROM myTable
INNER JOIN
(
SELECT Left(Company, 3) as leftcomp,
max(Total_Revenue) as maxTotalRevenue
FROM mytable
GROUP BY Left(Company, 3)
) mt
ON Left(myTable.Company, 3) = mt.leftcomp
AND myTable.Total_Revenue = mt.maxTotalRevenue;
Window function that is later filtered by Where:
SELECT *
FROM
(
SELECT Company,
Total_Revenue,
MAX(Total_Revenue) OVER (PARTITION BY Left(Company, 3)) as maxTotalRevenue
FROM myTable
) mt
WHERE Total_Revenue = maxTotalRevenue;
Correlated subquery in the WHERE clause:
SELECT *
FROM myTable mt1
Where Total_Revenue =
(
SELECT max(total_revenue)
FROM myTable
WHERE Left(myTable.Company, 3) = Left(mt1.Company, 3)
);
SQLFiddle here
You seems want row_number() function :
SELECT t.*
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY LEFT(Company, 3) ORDER BY Total_Revenue DESC) AS Seq
FROM table t
) t
WHERE Seq = 1;
This is what you want ?
select * from t
where
Company not in (
select Company from t where Company like '%DEF%' order by Company desc offset 1
)
use aggregate function max() and sub-query
select * from Yourtable t1
where t1.Total_Revenue in (
select max(T.Total_Revenue ) as Total_Revenue from
(
select case when Company like'%DEF%' then 'DEF'
else Company end as Company ,
Total_Revenue from Yourtable
) as T
group by T.Company
)
http://sqlfiddle.com/#!18/e896c/5
Company Total_Revenue
ABC 10
DEF2 35
GHI3 65
JKL9 100

Picking one row over another

I have a table that looks like this:
ID Type Value
A Z01 10
A Z09 20
B Z01 30
C Z01 40
D Z09 50
E Z10 60
For each ID I would like to retrieve a value. Ideally the value should come from the row with type Z01. However, if Z01 is not available I'll pick Z09 instead. If nothing is available I would like to select nothing.
The result would look like this:
Id Type Value
A Z01 10
B Z01 30
C Z01 40
D Z09 50
How can I accomplish this with T-SQL?
This should give you what you want:
select *
from table t
where 1 = case
when t.type = 'Z01'
then 1
when t.type = 'Z09'
and not exists (select 1 from table where id = t.id and type = 'Z01')
then 1
else 0
end
An alternative, with using a more common approach is (re-writing the CASE expression):
select *
from table
where type = 'Z01'
OR (type = 'Z09' and not exists (select 1 from table where id = t.id and type = 'Z01'))
An obvious sargable approach (which will make your query use the appropriate index on your table, if it exists) would be:
select *
from table
where type = `Z01`
union all
select *
from table
where type = `Z09`
and not exists (select 1 from table where id = t.id and type = 'Z01')
And when I'm saying index I'm talking about a non-clustered index on the type column.
You can use query like this
;WITH cte
AS (SELECT
*, ROW_NUMBER() OVER (PARTITION BY id ORDER BY type) AS rn
FROM yourtable
WHERE type IN ('Z01', 'Z09'))
SELECT
id, type, value
FROM cte
WHERE rn = 1
I would use window functions:
select t.*
from (select t.*,
row_number() over (partition by id
order by (case when type = 'Z01' then 1
when type = 'Z09' then 2
end)
) as seqnum
from t
) t
where seqnum = 1;
I don't fully understand teh statement "if nothing is available, then choose nothing". If, by this, you mean you want 'Z01', 'Z09' or nothing at all, then just do:
select t.*
from (select t.*,
row_number() over (partition by id order by type) as seqnum
from t
where type in ('Z01', 'Z09')
) t
where seqnum = 1;
The where clause does the filtering, and you can just order by the type alphabetically.
Something like this:
select distinct
base.Id,
Coalesce(z01.Type, any.Type) Type,
Coalesce(z01.Value, any.Value)
from (select distinct id from [table]) base
left outer join [table] z01
on z01.id = base.id
and z01.Type = 'Z01'
left outer join [table] any
on any.id = base.id
and any.type != 'Z01'
Start with a WHERE clause. Select only entries with Z01 and Z09. Then use ROW_NUMBER to rank your rows and keep the best.
select id, type, value
from
(
select
id, type, value,
row_number()
over (partition by id order by case when type = 'Z01' then 1 else 2 end) as rn
from mytable
where type in ('Z01','Z09')
) ranked
where rn = 1;
Then try this
Select distinct a.id,
coalesce(t1.type, t9.type) type,
case when t1.type is not null
then t1.value else t9.value end value
From table a
left join table t1
on t1.id = a.id
and t1.type = 'Z01'
left join table t9
on t9.id = a.id
and t9.type = 'Z09'
Where a.type in ('Z01', 'Z09') -- < -- this last to eliminate row E

How to get the middle most record(s) from a group of data in sql

create table #middle
(
A INT,
B INT,
C INT
)
INSERT INTO #middle (A,B,C) VALUES (7,6,2),(1,0,8),(9,12,16),(7, 16, 2),(1,12,8), (9,12,16),(9,12,16),(7, 16, 2),(1,12,8), (9,12,16)
;WITH MIDS
AS (SELECT *,
Row_number()
OVER (
ORDER BY a, b, c DESC )AS rn
FROM #middle)
SELECT *
FROM MIDS
WHERE rn <= (SELECT CASE ( Count(*)%2 )
WHEN 0 THEN ( Count(*) / 2 ) + 1
ELSE ( Count(*) / 2 )
END
FROM MIDS) except (SELECT *
FROM MIDS
WHERE rn < (SELECT ( Count(*) / 2 )
FROM MIDS))
The query i have tried works >4 records but not for '3'.Now my question is how should i modify my query so that for 3 records i should get the 2nd record which is the middle most record among them,try to insert only 3 records from above records and help. Thanks in advance.
You can use OFFSET and FETCH
select *
from #middle
order by a, b, c desc
offset (select count(*) / 2 - (case when count(*) % 2 = 0 then 1 else 0 end) from #middle) rows
fetch next (select 2 - (count(*) % 2) from #middle) rows only
There are many ways to get the median in SQL. Here is a simple way:
select h.*
from (select h.*, row_number() over (order by a, b, c desc) as seqnum,
count(*) over () as cnt
from #highest h
) h
where 2 * rn in (cnt, cnt - 1, cnt + 1);
For an even number of records, you will get two rows. You need to decide what you actually want in this case.
How about this:
**EDITED
;WITH MIDS
AS (SELECT *,
Row_number()
OVER (
ORDER BY a, b, c DESC )AS rn
FROM #middle),
Cnt
AS
(SELECT COUNT(*) c, COUNT(*)%2 as rem, COUNT(*)/2 as mid FROM Mids)
SELECT *
FROM MIDS
CROSS APPLY cnt
where (rn >= cnt.mid and rn <= cnt.mid + 1 AND cnt.rem = 0) OR
(cnt.rem <> 0 AND rn = cnt.mid+1)

How to select alternative rows from table in sql?

In table there is a column named status which can have either 1 or 2.
Now I want to select pattern like this
First Row 1
Second Row 2
Third Row 1
Fourth Row 2
......
For even rows
select * from emp where rowid%2 = 0;
For odd rows
select * from emp where rowid%2 != 0;
MS SQL:
select t1.*, (RN % 2)+1 as [STATUS] from
(
select t.*, ROW_NUMBER() OVER (ORDER BY <ORDER COLUMN NAME HERE>) as RN
) t1
Oracle:
SELECT * from
( SELECT a1.*,rank() over (partition BY status order by rownum) RNK FROM TABLE1 a1
)
ORDER BY rnk,status
Sqlfiddle:
try this
select * from (select table.* ,rownum k from table) where mod(k,2)<>0;
Try This:
SELECT e1.* FROM Employee e1
INNER JOIN
(
Select e2.*, ROW_NUMBER() OVER (ORDER BY <Column Names>) AS RN FROM Employee e2
)E2
ON e1.eid=e2.eid
where e2.RN%2=0
TO Get ODD records use following:
SELECT e1.* FROM Employee e1 INNER JOIN
( Select e2.*, ROW_NUMBER() OVER (ORDER BY ) AS RN FROM Employee e2 )E2 ON e1.eid=e2.eid where e2.RN%2=1

How do I get records before and after given one?

I have the following table structure:
Id, Message
1, John Doe
2, Jane Smith
3, Error
4, Jane Smith
Is there a way to get the error record and the surrounding records? i.e. find all Errors and the record before and after them.
;WITH numberedlogtable AS
(
SELECT Id,Message,
ROW_NUMBER() OVER (ORDER BY ID) AS RN
FROM logtable
)
SELECT Id,Message
FROM numberedlogtable
WHERE RN IN (SELECT RN+i
FROM numberedlogtable
CROSS JOIN (SELECT -1 AS i UNION ALL SELECT 0 UNION ALL SELECT 1) n
WHERE Message='Error')
WITH err AS
(
SELECT TOP 1 *
FROM log
WHERE message = 'Error'
ORDER BY
id
),
p AS
(
SELECT TOP 1 l.*
FROM log
WHERE id <
(
SELECT id
FROM err
)
ORDER BY
id DESC
)
SELECT TOP 3 *
FROM log
WHERE id >
(
SELECT id
FROM p
)
ORDER BY
id
Adapt this routine to pick out your target.
DECLARE #TargetId int
SET #TargetId = 3
select *
from LogTable
where Id in (-- "before"
select max(Id)
from LogTable
where Id < #TargetId
-- target
union all select #TargetId
-- "after"
union all select min(Id)
from LogTable
where Id > #TargetId)
select id,messag from
(Select (Row_Number() over (order by ID)) as RNO, * from #Temp) as A,
(select SubRNO-1 as A,
SubRNO as B,
SubRNO+1 as C
from (Select (Row_Number() over (order by ID)) as SubRNO, * from #Temp) as C
where messag = 'Error') as B
where A.RNO = B.A or A.RNO = B.B or A.RNO = B.C
;WITH Logs AS
(
SELECT ROW_NUMBER() OVER (ORDER BY id), id, message as rownum FROM LogTable lt
)
SELECT curr.id, prev.id, next.id
FROM Logs curr
LEFT OUTER JOIN Logs prev ON curr.rownum+1=prev.rownum
RIGHT OUTER JOIN Logs next ON curr.rownum-1=next.rownum
WHERE curr.message = 'Error'
select id, message from tbl where id in (
select id from tbl where message = "error"
union
select id-1 from tbl where message = "error"
union
select id+1 from tbl where message = "error"
)
Get fixed number of rows before & after target
Using UNION for a simple, high performance query (I found selected answer WITH query above to be extremely slow)
Here is a high performance alternative to the WITH top selected answer, when you know an ID or specific identifier for a given record, and you want to select a fixed number of records BEFORE and AFTER that record. Requires a number field for ID, or something like date that can be sorted ascending / descending.
Example: You want to select the 10 records before and after a specific error was recorded, you know the error ID, and can sort by date or ID.
The following query gets (inclusive) the 1 result above, the identified record itself, and the 1 record below. After the UNION, the results are sorted again in descending order.
SELECT q.*
FROM(
SELECT TOP 2
id, content
FROM
the_table
WHERE
id >= [ID]
ORDER BY id ASC
UNION
SELECT TOP 1
id, content
FROM
the_table
WHERE
id < [ID]
ORDER BY id DESC
) q
ORDER BY q.id DESC