I can't get the following query to work:
UPDATE ISSUE
SET DUE_DATE = DATE(ISSUE.DATE_ADDED,'+90 day')
JOIN CVE on CVE.CVE_ID = ISSUE.CVE_ID
WHERE CVE.CVSS >= 4 AND CVE.CVSS < 9
This is because in sqlite3 you can't join in an UPDATE. What I'm trying to do is set the due date of an issue to be 90 days after the date added if the CVE associated with the issue is: greater than or equal to four and less than 9. Is there an alternative option that I'm missing here?
You can join the rows in a sub-query.
UPDATE ISSUE
SET DUE_DATE = DATE(ISSUE.DATE_ADDED,'+90 day')
WHERE ISSUE.CVE_ID IN (
SELECT ISSUE.CVE_ID
FROM ISSUE JOIN CVE on CVE.CVE_ID = ISSUE.CVE_ID
WHERE CVE.CVSS >= 4 AND CVE.CVSS < 9
)
Related
What I wanted was to use CROSS APPLY, but I guess that doesn't exist in mysql. The alternative I've read is LATERAL. Well, I'm using mariadb 10.3 and I guess that doesn't exist either. The ticket table contains an id that's referenced by the ticket_id column in the note table. A ticket can have many notes, I'm trying to list all tickets with their most recent note date (post_date). How could I write the query below for mariadb?
SELECT t.*, n.post_date
FROM ticket t,
LATERAL (
SELECT note.post_date FROM note WHERE t.id = note.ticket_id ORDER BY note.post_date DESC LIMIT 1
) n;
Example table structure:
Ticket
id
subject
1
stuff
2
more
note
id
post_date
ticket_id
1
1
2
1
3
2
4
1
5
2
I did find an open jira ticket from people asking for mariadb to support lateral.
From what I read, LATERAL will not be supported in MariaDB until version 11. But we can just as easily use ROW_NUMBER here, which is supported:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY ticket_id ORDER BY post_date DESC) rn
FROM note
)
SELECT t.*, n.post_date
FROM ticket t
INNER JOIN cte n
ON n.ticket_id = t.id
WHERE n.rn = 1;
If you wanted a close translation of your current lateral join, then use:
SELECT t.*,
(SELECT n.post_date
FROM note n
WHERE t.id = note.ticket_id
ORDER BY n.post_date DESC
LIMIT 1)
FROM ticket t;
I need to simple current week list from sale_detail and I am using
Sqlite ,but problem is that condition not work but I check that similer sqlite macth date if 2015-1-9 not equal to 2015-01-09 so may program generate this that sqlite is not compare what is the solution second Condition operator not working Please Help me !
SELECT sale.sale_id, sale_date, sale_type, sale_detail.Quantity, sale_status p_cate_name, p_cate_size, p_cate_price
FROM sale
JOIN sale_detail ON sale.sale_id = sale_detail.sale_id
JOIN p_category ON p_category.p_cate_id = sale_detail.category_id
WHERE sale.sale_date >= date('now', 'weekday 0','-7 days')
AND sale.sale_date <= date('now', 'weekday 0')
Here is my example table:
column_example
10
20
25
50
Here is what I would like:
column_example2
10
5
25
I'm sure this is a simple question, but I haven't found the answer in the SQLite Syntax web page or via Google.
EDIT: To clarify, the code would likely return the outputs for:
20-10
25-20
50-25
This solution might be slow, but I had to consider the potential gaps between succeeding rowids:
http://sqlfiddle.com/#!5/daeed/1
SELECT
(SELECT x
FROM t AS t3
WHERE t3.rowid =
(SELECT MIN(tt.rowid)
FROM t AS tt
WHERE tt.rowid > t.rowid
)
)
- x
FROM t
WHERE diff IS NOT NULL
If it is guaranteed to not have any gaps between rowids, then you can use this simpler query:
http://sqlfiddle.com/#!5/1f906/3
SELECT t_next.x - t.x
FROM t
INNER JOIN t AS t_next
ON t_next.rowid = t.rowid + 1
I want to compare two results
one is stored in the first query, and the other is exactly the same as the first, but i want only to recieve data < today
"SELECT s.GSP_nom as nom, timestamp, COUNT(s.GSP_nom) as nb_votes, AVG(v.vote+v.prix+v.serviceClient+v.interface+v.interface+v.services)/6 as moy
FROM votes_serveur AS v
INNER JOIN serveur AS s ON v.idServ = s.idServ
WHERE s.valide = 1
AND v.date < CURDATE()
GROUP BY s.GSP_nom
HAVING nb_votes > 9
ORDER BY moy DESC LIMIT 0,15";
is that correct ?
thank you
GROUP BY, not ROUP BY
v.date < CURDATE() looks o.k.
I need some help to build SQL Query. I have table having data like:
ID Date Name
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
I need to get result something like...
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
4 1/4/2009 Null
5 1/5/2009 Null
6 1/6/2009 Null
7 1/7/2009 Null
8 1/8/2009 Null
............................
............................
............................
30 1/30/2009 Null
31 1/31/2009 Null
I want query something like..
Select * from tbl **where month(Date)=1 AND year(Date)=2010**
Above is not completed query.
I need to get all the record of particular month, even if some date missing..
I guess there must be equi Join in the query, I am trying to build this query using Equi join
Thanks
BIG EDIT
Now understand the OPs question.
Use a common table expression and a left join to get this effect.
DECLARE #FirstDay DATETIME;
-- Set start time
SELECT #FirstDay = '2009-01-01';
WITH Days AS
(
SELECT #FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, 1, #FirstDay)
)
SELECT DATEPART(d,d.CalendarDay), **t.date should be (d.CalendarDay)**, t.Name FROM Days d
LEFT JOIN tbl t
ON
d.CalendarDay = t.Date
ORDER BY
d.CalendarDay;
Left this original answer at bottom
You need DATEPART, sir.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1
If you want to choose month and year, then you can use DATEPART twice or go for a range.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1 AND DATEPART(yyyy,Date) = 2009
Range :-
SELECT * FROM tbl WHERE Date >= '2009-01-01' AND Date < '2009-02-01'
See this link for more info on DATEPART.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
You can use less or equal to.
Like so:
select * from tbl where date > '2009-01-01' and date < '2009-02-01'
However, it is unclear if you want month 1 from all years?
You can check more examples and functions on "Date and Time Functions" from MSDN
Create a temporary table containing all days of that certain month,
Do left outer join between that table and your data table on tempTable.month = #month.
now you have a big table with all days of the desired month and all the records matching the proper dates + empty records for those dates who have no data.
i hope that's what you want.