Originally posted by G8_Summit
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
SQL challenge
Collapse
X
-
-
Thinking definitely you could do it with a cursor.
Simple loop, counting events until change of name, all the while building your list of event names.
But you would have to use a program.
I could do it in Cobol for youComment
-
Originally posted by G8_SummitThinking definitely you could do it with a cursor.
Simple loop, counting events until change of name, all the while building your list of event names.
But you would have to use a program.
I could do it in Cobol for youHard Brexit now!
#prayfornodealComment
-
declare @mob_rows varchar(260)
select @rowindex = 1
select @mob_rows = ''
while @rowindex < 8
begin -- row loop
select @colindex = @lowercollimit
select @single_row = ''
while @colindex < @uppercollimit
begin -- column loop
select @t = count(*)
from #tempneeds
where
TypeID = 1 -- page
and QuestionID = @rowindex -- rows
and HelpTypeID = @colindex -- columns
and Need = 'y'
select @colindex = @colindex + 1
select @single_row = @single_row + ' ' + ltrim(str(@t)) -- build up the row
end -- column loop
select @rowindex = @rowindex + 1
select @mob_rows = @mob_rows + '/' + ltrim(@single_row) -- concatenate the rows
end -- row loopComment
-
Originally posted by sasguruThinking maybe I could do this with a cursor ...
OK, it doesn't matter whether it's a yellow cat or a black cat, as long as it catches mice. But it wasn't quite the question; and the difference does point out something crucial about SQL: it's non-procedural.Comment
-
Originally posted by scotspinedeclare @mob_rows varchar(260)
select @rowindex = 1
select @mob_rows = ''
while @rowindex < 8
begin -- row loop
select @colindex = @lowercollimit
select @single_row = ''
while @colindex < @uppercollimit
begin -- column loop
select @t = count(*)
from #tempneeds
where
TypeID = 1 -- page
and QuestionID = @rowindex -- rows
and HelpTypeID = @colindex -- columns
and Need = 'y'
select @colindex = @colindex + 1
select @single_row = @single_row + ' ' + ltrim(str(@t)) -- build up the row
end -- column loop
select @rowindex = @rowindex + 1
select @mob_rows = @mob_rows + '/' + ltrim(@single_row) -- concatenate the rows
end -- row loopHard Brexit now!
#prayfornodealComment
-
MS Access crosstab query? Or is that considered cheating.McCoy: "Medical men are trained in logic."
Spock: "Trained? Judging from you, I would have guessed it was trial and error."Comment
-
If your using Oracle 9i or above the following will work.
create table list_test
(person varchar(10),
event varchar(10));
INSERT INTO LIST_TEST ( PERSON, EVENT ) VALUES ( 'person1', 'event1');
INSERT INTO LIST_TEST ( PERSON, EVENT ) VALUES ( 'person1', 'event2');
INSERT INTO LIST_TEST ( PERSON, EVENT ) VALUES ( 'person1', 'event3');
INSERT INTO LIST_TEST ( PERSON, EVENT ) VALUES ( 'person2', 'event1');
INSERT INTO LIST_TEST ( PERSON, EVENT ) VALUES ( 'person2', 'event2');
commit;
select person,
lv count,
events_list
from (select b.*, max(lv) over (partition by person) max_lv
from (
select id,
person,
parent_id,
level lv,
substr(sys_connect_by_path(event, '/'), 2) events_list
from (select rownum id,
person,
event,
nvl(lag(rownum) over (partition by person order by event desc), -1) parent_id
from list_test) a
connect by prior parent_id = id
)b
)
where lv = max_lv;Comment
-
If it's Oracle then I believe they have various non-Ansi functions to make this easier.
If it's SQL Server then you can still do this sort of thing using a stored proc, a temp table or two and some dynamic SQL...did it at my last job to create a query which would 'pivot' an unknown number of row values into columns...unfortunately I don't have the SQL to hand.
Wouldn't recommend doing it with cursors though, they're a lot, lot slower even compared to dynamic SQL, since the query engine never gets a chance to optimise things.Comment
-
Advertise it on some indian dominated outsourcing website and offer the price of a can of pepsi... jobs a good un.Vieze Oude ManComment
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- HMRC warns IT consultants and others of 12 ‘payroll entities’ Today 09:15
- How you think you look on LinkedIn vs what recruiters see Yesterday 09:00
- Reports of umbrella companies’ death are greatly exaggerated Nov 28 10:11
- A new hiring fraud hinges on a limited company, a passport and ‘Ade’ Nov 27 09:21
- Is an unpaid umbrella company required to pay contractors? Nov 26 09:28
- The truth of umbrella company regulation is being misconstrued Nov 25 09:23
- Labour’s plan to regulate umbrella companies: a closer look Nov 21 09:24
- When HMRC misses an FTT deadline but still wins another CJRS case Nov 20 09:20
- How 15% employer NICs will sting the umbrella company market Nov 19 09:16
- Contracting Awards 2024 hails 19 firms as best of the best Nov 18 09:13
Comment