SQL Lite seems a mite restrictive.
- 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!
Calling all hardcore SQL experts! A challenge
Collapse
X
-
-
Maybe that's where they got the "Lite" moniker?Originally posted by Bob Dalek View PostSQL Lite seems a mite restrictive.
It's a bit hard to sell a product as "SQL Restrictive" but bean counters love "Lite" as they equate it to "Cheap"."See, you think I give a tulip. Wrong. In fact, while you talk, I'm thinking; How can I give less of a tulip? That's why I look interested."Comment
-
It's actually VERY good. It just sticks to the bare essentials and no bloated loops, cursors and other non-SQL constructs. Lets face it, what's a loop doing in a SQL engine?Originally posted by Bob Dalek View PostSQL Lite seems a mite restrictive.First Law of Contracting: Only the strong surviveComment
-
Looping?Originally posted by _V_ View PostIt's actually VERY good. It just sticks to the bare essentials and no bloated loops, cursors and other non-SQL constructs. Lets face it, what's a loop doing in a SQL engine?Comment
-
Exactly to your spec - but you're not going to like it!
select
dateadd (d,(((d0*10+d1)*10+d2)*10+d3)*10+d4,'1/1/2008') as date
from
(select 0 as d0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 as d1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 as d2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 as d3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 as d4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
where
dateadd (d,(((d0*10+d1)*10+d2)*10+d3)*10+d4,'1/1/2008') < '1/1/2009'
order by dateComment
-
This is is type of obstrufication I was referring to earlier - its a very elegant solution - but not exactly maintainable. Either it should be heavily commented (I wanted to use a for loop but sqllite is tulip) or you could just leave it and be indespensible.....Originally posted by PerlOfWisdom View Postselect
dateadd (d,(((d0*10+d1)*10+d2)*10+d3)*10+d4,'1/1/2008') as date
from
(select 0 as d0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 as d1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 as d2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 as d3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 as d4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
where
dateadd (d,(((d0*10+d1)*10+d2)*10+d3)*10+d4,'1/1/2008') < '1/1/2009'
order by dateComment
-
If you know what you're doing and with a bit of thought you can pretty much do any if not all* data manipulation without using cursors.Originally posted by _V_ View PostIt's actually VERY good. It just sticks to the bare essentials and no bloated loops, cursors and other non-SQL constructs. Lets face it, what's a loop doing in a SQL engine?
*I've still to find something I can't do in a single SQL statementCoffee's for closersComment
-
SQL Server allows you to manage that easier with a CTE (common table expression)Originally posted by PerlOfWisdom View Postselect
dateadd (d,(((d0*10+d1)*10+d2)*10+d3)*10+d4,'1/1/2008') as date
from
(select 0 as d0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 as d1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 as d2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 as d3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 as d4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
where
dateadd (d,(((d0*10+d1)*10+d2)*10+d3)*10+d4,'1/1/2008') < '1/1/2009'
order by dateCoffee's for closersComment
-
Any database person who couldn't maintain that without comments should sell his computer to a second hand shop (Ebay would be too much of a challenge).Originally posted by BrilloPad View PostThis is is type of obstrufication I was referring to earlier - its a very elegant solution - but not exactly maintainable. Either it should be heavily commented (I wanted to use a for loop but sqllite is tulip) or you could just leave it and be indespensible.....Comment
-
Sorted
Thanks for the advice folks. Using a sequence table to join on to expand the date range I managed to get the whole calculation done in a single SELECT (look no loops required!!!).
Lovely stuff this SQLite!Code:SELECT a.refNumber, propertyName, type, location, bedrooms, sleeps, bathrooms, poolType, carEssential, case when length(specialOffer) > 0 then 'Y' else 'N' end as hasSpecialOffer, specialOffer, externalAvailabilityCalendarUrl, price FROM accommodation a INNER JOIN (select refNumber, sum(price) as price from (select a.refNumber as refNumber, (select price / 7.00 from pricingPeriods p inner join pricing pr on p.name = pr.period where pr.refNumber = a.refNumber and date( @startDate ,'start of year','+' || (startDay - 1) || ' days', '+' || (startMonth - 1) || ' months') <= s.date and date( @endDate,'start of year','+' || (endDay - 1) || ' days', '+' || (endMonth - 1) || ' months') >= s.date order by pricingId desc) as price from (select date(@startDate,'+' || id || ' days') as date from sequence s where id < (julianday(@endDate) - julianday(@startDate))) as s join accommodation a) as b group by refNumber) as z ON a.refNumber = z.refNumber WHERE a.isGroupFlag = 'N' AND a.activeFlag = 'A' AND a.location = @location AND a.bedrooms >= @bedrooms AND a.sleeps >= @sleeps AND NOT EXISTS (SELECT 'X' FROM accommodationCalendarEntries c WHERE c.refNumber = a.refNumber AND c.entryType IN ('Booked','Pending') AND ((c.fromDate <= @startDate AND date(c.toDate,'-1 day') >= @startDate)OR (c.fromDate <= date(@endDate,'-1 day') AND date(c.toDate,'-1 day') >= date(@endDate,'-1 day'))OR (@startDate <= c.fromDate AND date(@endDate,'-1 day') >= c.fromDate) OR (@startDate <= date(c.toDate,'-1 day') AND date(@endDate,'-1 day') >= date(c.toDate,'-1 day'))) )

PS. The permies haven't got a clue how this works. Which is nice.First Law of Contracting: Only the strong surviveComment
- 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

Comment