Edit - restating my need since two guys with way more rank than me misunderstood my question, so I need to make this better...
I have a table like the below. I need to select all of the rows for the first group of 'sec1' rows where the 'ison' column is 1. So the query should first return the 'bbb' row, but if I set all rows to ison=0 and then make the 'ccc' rows ison=1, then I would get two rows of 'ccc' in the result set. Can anyone help me with my rank/top? Using MSSQL 2008.
create table #grp ( sec1 varchar(4) , sec2 varchar(4) , ison bit )
insert into #grp values ( 'aaa' , '001' , 0 )
insert into #grp values ( 'aaa' , '002' , 0 )
insert into #grp values ( 'bbb' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
Select * From
( Select
sec1 ,
sec2 ,
ison ,
RANK() Over ( partition by sec1 order by sec1,sec2 ) as rowrank
from #grp
where ison=1
) tmp
where rowrank=1
Thanks.
create table #grp ( sec1 varchar(4) , sec2 varchar(4) , ison bit )
insert into #grp values ( 'aaa' , '001' , 0 )
insert into #grp values ( 'aaa' , '002' , 1 )
insert into #grp values ( 'bbb' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
SELECT *
FROM #grp
WHERE sec1 = (
Select TOP(1) sec1
From
( Select
sec1 ,
sec2 ,
ison ,
RANK() Over ( partition by sec1 order by sec1,sec2 ) as rowrank
from #grp
where ison=1
) tmp
where rowrank=1
Order by sec1, Sec2
)
Related
I have a column TASKID "3.001.2.2.1.3"
I have split them based on . (dot) in 4 columns with each column value:
TaskLevel1=3
TaskLevel2=3.001
TaskLevel3=3.001.2
TaskLevel4=3.001.2.2
The sample code for creating table and insert scripts are below:
CREATE TABLE [dbo].[Task]
(
[TaskID] [varchar](35) NULL,
[TaskLevel1] [varchar](35) NULL,
[TaskLevel2] [varchar](35) NULL,
[TaskLevel3] [varchar](35) NULL,
[TaskLevel4] [varchar](35) NULL
)
INSERT [dbo].[Task] ([TaskID], [TaskLevel1], [TaskLevel2], [TaskLevel3], [TaskLevel4])
VALUES (N'3.001.2.2.1', N'3', N'3.001', N'3.001.2', N'3.001.2.2')
INSERT [dbo].[Task] ([TaskID], [TaskLevel1], [TaskLevel2], [TaskLevel3], [TaskLevel4])
VALUES (N'3.001.2.2.1.3', N'3', N'3.001', N'3.001.2', N'3.001.2.2')
DECLARE #T TABLE (
[TaskID] [varchar](35) NULL
)
INSERT INTO #T VALUES
('3'),
('3.001'),
('3.001.2.2.1'),
('3.001.2.2.1.3')
;with cte([TaskID], [start], [level]) as
(
SELECT [TaskID], 1, 1
FROM #t
UNION ALL
SELECT [TaskID], CHARINDEX('.', [TaskID], [start]) + 1, [level] + 1
FROM cte
WHERE CHARINDEX('.', [TaskID], [start]) > 0
),
cte2 as
(
SELECT [TaskID], LEFT([TaskID], CHARINDEX('.', [TaskID] + '.', [start]) - 1) AS [val], [level]
FROM cte
)
SELECT [TaskID]
, [1] AS [TaskLevel1]
, [2] AS [TaskLevel2]
, [3] AS [TaskLevel3]
, [4] AS [TaskLevel4]
FROM cte2
PIVOT (MAX([val])
FOR [level] IN ([1], [2], [3], [4])) p
ORDER BY [TaskID]
I am using SQL server 2008. I want to insert data from a temp table to database. I am using While loop to insert data from temp table to database table.
Now I am facing an issue:
object already exist in the database.
declare #rev as int ,
#sQuotationNo NVARCHAR(15),
#sQRevNo int
set #rev=(select top 1 QRevNo from PDBCompr Where QuotationNo='JCS_G1415_008' and QRevNo<>'3' order by QRevNo desc)
;with cte as
(
SELECT
ROW_NUMBER() OVER(ORDER BY QuotationNo) AS sSLNO,
[CompanyCode] ,
[ProjectCode] ,
[PRevNo],
[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from PDBCompr
where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='2'
and AddCmprId not in (select distinct AddCmprId from PDBCompr where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='3' )
)
select * into #temp from cte
declare #cnt int , #loopCnt int=1
select #cnt =( select COUNT(*) from #temp)
while (#loopCnt<=#cnt)
begin
;with cte2 as
(
SELECT
sSLNO,
[CompanyCode] ,
[ProjectCode] ,
(select Max(PRevNo)+1 from PDBCompr) [PRevNo],
(select Max(CSlNo) +1 from PDBCompr)[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from #temp where sSLNO=#loopCnt
)
select * into PDBCompr from cte2
set #loopCnt= #loopCnt+1
drop table #temp
end
Please help me to find a proper solution. Thanks in advance
select * into PDBCompr from cte2
in this line you are again making a table "PDBCompr", but in above code you are selecting records from this table it means its already exist.
so if you want to insert record in this table then you need to change your query like below:
insert into PDBCompr select * from cte2
The webguys wants unique urls based on the name of the products
If more products have the same name, add a number after the name.
our.dom/red-sock
our.dom/red-sock-1
They do not want the product id or another number on all products, i.e.
our.dom/red-sock-123481354
I store this in a field i call seourl.
I have it covered when I create new products, a trigger tries adding the seourl, if it is already there, increment the number, until an unique value is found.
But I now have to give the entire table new seourls.
If I just
update tab set seourl=dbo.createurl(title)
I am sure to have collissions, and the operation is rolled back.
Is there a way to have the statement to commit the updates that work, and leave the rest unchanged?
Or must I just do a RBAR, Row By Agonizing Row operation in a loop?
Adapt this to your needs:
select
*
from (values('aaa'), ('aaa-12'), ('aaa-'), ('bbb-3')) as src (x)
cross apply (
select isnull(nullif(patindex('%-[0-9]%', x) - 1, -1), LEN(x))
) as p(idx)
cross apply (
select
SUBSTRING(x, 1, idx)
, SUBSTRING(x, idx + 1, LEN(x) - idx)
) as t(t, xx)
Try this:
declare #tmp table (
id int not null identity
, name varchar(100) -- you need name to be indexed
, urlSuffix int -- store the number (ot you'll have to use PATINDEX, etc. as previously shown)!
, url as name + ISNULL('_' + cast(NULLIF(urlSuffix, 0) as varchar(100)), '')
, unique (name, id) -- (trick) index on name
)
insert #tmp (name, urlSuffix)
select
src.name
, ISNULL(T.urlSuffix, -1) + ROW_NUMBER() OVER (PARTITION BY src.name ORDER BY (select 1))
from (values
('x')
, ('y')
, ('y')
, ('y')
, ('z')
, ('z')
) as src (name)
left join (
select
name
, MAX(T.urlSuffix) as urlSuffix
from #tmp AS T
GROUP BY name
) as T on (
T.name = src.name
)
insert #tmp (name, urlSuffix)
select
src.name
, ISNULL(T.urlSuffix, -1) + ROW_NUMBER() OVER (PARTITION BY src.name ORDER BY (select 1))
from (values
('a')
, ('b')
, ('b')
, ('b')
, ('z')
, ('z')
) as src (name)
left join (
select
name
, MAX(T.urlSuffix) as urlSuffix
from #tmp AS T
GROUP BY name
) as T on (
T.name = src.name
)
select
name, url
from #tmp
order by url
The solution to yur problem should lies in the use of ROW_NUMBER()
SQL Server 2008.
declare #pardate table ( pardateid int, pardatewhen datetime2(3) )
insert into #pardate values ( 1 , '2011-09-17 12:43' )
insert into #pardate values ( 2 , '2011-09-17 12:44' )
insert into #pardate values ( 3 , '2011-10-11 12:45' )
insert into #pardate values ( 4 , '2011-10-12 12:46' )
insert into #pardate values ( 5 , '2011-10-13 12:47' )
insert into #pardate values ( 6 , '2011-11-20 12:48' )
insert into #pardate values ( 7 , '2011-11-21 12:49' )
insert into #pardate values ( 8 , '2011-11-22 12:50' )
declare #child table ( childid int , pardateid int , childvalue char(6) )
insert into #child values ( 1 , 1 , 'aaaaaa' )
insert into #child values ( 2 , 2 , 'bbbbbb' )
insert into #child values ( 3 , 3 , 'cccccc' )
insert into #child values ( 4 , 4 , 'dddddd' )
insert into #child values ( 5 , 5 , 'cccccc' )
insert into #child values ( 6 , 6 , 'cccccc' )
insert into #child values ( 7 , 7 , 'eeeeee' )
insert into #child values ( 8 , 8 , 'ffffff' )
select pardatewhen , childvalue , COUNT(childvalue)
from #child childtable join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by pardatewhen , childvalue
I am trying to get a count of #child.childvalue every day, every hour, so there would be 8760 rows in my result.
First pass had a loop and a CONVERT which takes ~5 minutes to run with the actual result set (this is just a sample for illustation). I did create a CTE to make a calendar temp table (using http://www.sqlpointers.com/2006/07/generating-temporary-calendar-tables.html), and thought it could be joined somehow to add "empty values" into the result set.
I need to get a result set that looks like this
date hour count
...
2011-09-17 0 0
....
2011-09-17 12 2
....
2011-10-11 12 1
How can that be done efficiently?
Thanks.
try this.
;WITH cal AS
(SELECT CAST('2011-01-01' AS DATETIME) AS cal_date
UNION ALL
SELECT DATEADD(hour,1,cal_date)
FROM cal
WHERE cal_date < '2011-12-31 23:00'
)
, par AS
(
select CAST(pardatewhen AS DATE) AS pardate, DATEPART(hh,pardatewhen) AS parhour , COUNT(childvalue) as num
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by CAST(pardatewhen AS DATE), DATEPART(hh,pardatewhen)
)
SELECT CAST(cal.cal_date AS DATE) AS [date],DATEPART(hh,cal.cal_date) AS [hour],ISNULL(par.num,0) AS [childvalue_count]
FROM cal
LEFT JOIN par
ON CAST(cal.cal_date AS DATE) = par.pardate
AND DATEPART(hh,cal.cal_date) = par.parhour
OPTION (MAXRECURSION 9999)
Something like (have childvalue in your query but not in your example result?)
select Cast(pardatewhen as Date) as [date], DatePart(hour,pardatewhen) as [hour] , childvalue , COUNT(childvalue)
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by Cast(pardatewhen as Date), DatePart(hour,pardatewhen), childvalue
Note Date type was introduced in SQL 2008
SQL Server 2008, I have the following parent/child table schemata and rows:
create table #par( parid int primary key identity(1,1) , partext varchar(6) )
create table #chi( chiid int primary key identity(1,1) , parid int null , chirefid int null , chiinfo varchar(6) )
create table #chiref ( chirefid int primary key identity(1,1) , chisubdesc varchar(9) )
insert into #par values ( 'par1' ) , ('par2')
insert into #chiref values ( 'chi1' )
insert into #chiref values ( 'chi2' )
insert into #chiref values ( 'chi3' )
insert into #chi values ( 1 , 1 , 'aaa' )
insert into #chi values ( 1 , 2 , 'bbb' )
insert into #chi values ( 2 , 1 , 'ccc' )
insert into #chi values ( 2 , 2 , 'ddd' )
insert into #chi values ( 2 , 3 , 'eee' )
The child #chi has just key/value pairs inside, and I need to convert the text (key) into a column and put the value inside, so the result set is shaped like the below. What is the best way to do that (I cannot change the key/value stuff, it is inherited from another system). And there is a join on the #chiref table for the actual column names (which is really killing me).
partext chi1 chi2 chi3
par1 aaa bbb
par2 ccc ddd eee
Thanks.
EDIT - The thing to remember is that the column names have to match the "key value" in the table. So if the EAV row key is "chi1" then the pivot has to have "chi1". I had simply "1" which broke. So I got it.
Thanks again!
select partext, [chi1], [chi2], [chi3]
from
(
select p.partext, c.chitext, c.chiinfo
from #chi c
join #par p
on c.parid = p.parid
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in ([chi1], [chi2], [chi3])
) as PivotTable
or this one is a little more efficient, although more code:
with c
as
(
select parid, [chi1], [chi2], [chi3]
from
(
select parid, chitext, chiinfo
from #chi
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in ([chi1], [chi2], [chi3])
) as PivotTable
)
select p.partext, c.*
from c
join #par p
on c.parid = p.parid
if you need a dynamic column number, you can do dynamic query:
declare #ColumnList varchar(max)
select #ColumnList = isnull(#columnList + ', ', '') + '[' + chitext + ']'
from
(
select distinct chitext
from #chi
) tt
order by chitext
declare #Command varchar(max) = '
with c
as
(
select parid, ' + #ColumnList + '
from
(
select parid, chitext, chiinfo
from #chi
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in (' + #ColumnList + ')
) as PivotTable
)
select p.partext, ' + #ColumnList + '
from c
join #par p
on c.parid = p.parid
'
execute(#Command)