SQL Version of CONCAT and LIMIT - mysql

Trying to follow Calendar Recurring/Repeating Events - Best Storage Method to design the database using SQL Server 2005.
When i run this below query
SELECT EV.*
FROM events EV
RIGHT JOIN events_meta EM1 ON EM1.event_id = EV.id
RIGHT JOIN events_meta EM2 ON EM2.meta_key = CONCAT( 'repeat_interval_', EM1.id )
WHERE EM1.meta_key = 'repeat_start'
AND (
( CASE ( 1299132000 - EM1.meta_value )
WHEN 0
THEN 1
ELSE ( 1299132000 - EM1.meta_value )
END
) / EM2.meta_value
) = 1
LIMIT 0 , 30
I am getting 'CONCAT' is not a recognized built-in function name.
I searched and seems like it is supported only in SQL Server 2012. How can i acheive this with SQL Server 2005/2008? Also what about LIMIT syntax in SQL Server?
I want the above sql version of query.

The SQL Server equivalent of limit is TOP. e.g
SELECT TOP 10 ID
FROM Table.
TOP is not really synomonous with LIMIT, because you can't pass 2 arguments to it, (e.g. LIMIT 10, 20) to specify the starting point. In SQL Server 2012 the equivalent is OFFSET FETCH, but in 2005 and 2008 versions you need to use ROW_NUMBER and filter on the value of this, e.g.
SELECT *
FROM ( SELECT *, RowNum = ROW_NUMBER() OVER(ORDER BY ID)
FROM T
) t
WHERE t.RowNumber BETWEEN 10 AND 30;
You can just use + to concatenate strings:
SELECT 'a' + 'b'
SQL Server 2012 introduces the CONCAT function

The equivalent function in SQL Server to CONCAT() is + and you can limit the resultset by using the TOP keyword.
Write your query as:
SELECT TOP 30 EV.*
FROM events EV
RIGHT JOIN events_meta EM1 ON EM1.event_id = EV.id
RIGHT JOIN events_meta EM2 ON EM2.meta_key = 'repeat_interval_' + CAST (EM1.id as varchar(10)) -- cast is required in case id column is int
WHERE EM1.meta_key = 'repeat_start'
AND (
( CASE ( 1299132000 - EM1.meta_value )
WHEN 0
THEN 1
ELSE ( 1299132000 - EM1.meta_value )
END
) / EM2.meta_value
) = 1

assumed that EM1.id is of type int you first have to convert it to a varchar.
To concatenate use the + operator. And use TOP instead of LIMIT in Sql-server.
This should work:
SELECT TOP 30 EV.*
FROM events EV
RIGHT JOIN events_meta EM1 ON EM1.event_id = EV.id
RIGHT JOIN events_meta EM2 ON EM2.meta_key = ('repeat_interval_' + CONVERT(nvarchar(max), EM1.id))
WHERE EM1.meta_key = 'repeat_start'
AND (
( CASE ( 1299132000 - EM1.meta_value )
WHEN 0
THEN 1
ELSE ( 1299132000 - EM1.meta_value )
END
) / EM2.meta_value
) = 1

Related

MYSQL - Subqueries problem - Cant reuse the table

WITH t as (
SELECT *
FROM scd p
WHERE p.modified_date > FROM_UNIXTIME(1593060230)
AND ( p.main_id = 1
OR FIND_IN_SET(1, p.mult_ids) <> 0 )
ORDER BY modified_date DESC
LIMIT 2 OFFSET 0
),
del as (
SELECT
*
FROM t WHERE (status <> 1 AND status <> 2)
),
w_del as (
SELECT
*
FROM t WHERE (status = 1 OR status = 2)
)
SELECT w_del.*, del.* FROM w_del,del;
How do I achieve this with normal sub queries. I am using MySQL 5.7 and can't use CTEs. Im getting can't reuse table error if I use UNION/sub-queries. Is there a way to achieve this without temporary tables?
Please help.
You can just plug in the code for each alias . . . and keep doing that until you are at the base tables:
SELECT w_del.*, del.*
FROM (SELECT t.*
FROM (SELECT *
FROM scd p
WHERE p.modified_date > FROM_UNIXTIME(1593060230) AND
( p.main_id = 1 OR FIND_IN_SET(1, p.mult_ids) <> 0 )
ORDER BY modified_date DESC
LIMIT 2 OFFSET 0
) t
WHERE (status <> 1 AND status <> 2)
) w_del CROSS JOIN
(SELECT t.*
FROM (SELECT *
FROM scd p
WHERE p.modified_date > FROM_UNIXTIME(1593060230) AND
( p.main_id = 1 OR FIND_IN_SET(1, p.mult_ids) <> 0 )
ORDER BY modified_date DESC
LIMIT 2 OFFSET 0
) t
WHERE (status = 1 OR status = 2)
) del;
One critical point, though: The definition of t is using ORDER BY and LIMIT. If there are ties in the modified_date column, then the two subqueries could return different result sets. You have two choices to avoid a problem here:
Add additional keys to the ORDER BY to ensure that the sorting is stable (i.e. returns the same results each time because the combination of keys is unique).
Materialize the subquery using a temporary table.

How To Get My Teradata Query to Prompt For Dates Between ?YYYYMMDD AND ?YYYYMMDD and

HERE IS THE QUERY:
SELECT
FACDTE.QTR
,FACDTE.WK
,FACDTE."DATE"
,FACDTE.DIV
,FACDTE.DST
,FACDTE.FAC
,FACDTE.DAYS
,COALESCE(OOS.SCN,0) SCN
,RANK() OVER(
PARTITION BY FACDTE.QTR,FACDTE.WK,FACDTE."DATE"
ORDER BY COALESCE (CAST(OOS."OOS COUNT" AS INTEGER),'ns')) AS DIVRANK
,CAST(OOS."OOS COUNT" AS INTEGER) "OOS COUNT"
FROM
(
SELECT
FAC.PARENT_OP_AREA_CD DIV
,FAC.DISTRICT_FINANCE_CD DST
,FAC.STORE_ID FAC
,DTE.QUARTER_ID QTR
,DTE.WEEK_ID WK
,DTE.D_DATE "DATE"
,COUNT(DISTINCT(DTE.D_DATE)) DAYS
FROM LU_STORE_FINANCE FAC
JOIN (
SELECT
DTE_L1.D_DATE
,DTE_L1.WEEK_ID
,DTE_L1.QUARTER_ID
FROM LU_DAY_MERGE DTE_L1
JOIN (SELECT D_DATE FROM LU_DAY_MERGE
WHERE WEEK_ID=(SELECT DISTINCT(WEEK_ID) FROM LU_DAY_MERGE WHERE D_DATE =CURRENT_DATE -2)) DTE_L0 ON DTE_L0.D_DATE =DTE_L1.D_DATE AND DTE_L1.D_DATE < CURRENT_DATE
) DTE
ON DTE.D_DATE BETWEEN FAC.OPENED_DT AND FAC.CLOSED_DT AND
THE CODE IS BELOW I NEED TO PROMPT FOR DATES BETWEEN YYYYMMDD YYYYMMDD INSTEAD of Using the current_DATE... Can I just put in ?YYYYMMDD AND YYYYMMDD in that please?
FAC.PARENT_OP_AREA_CD = '17' AND NOT FAC.STORE_ID IN (4904,3332, 1478,0412,2631,1223) GROUP BY 1,2,3,4,5,6) FACDTE
LEFT JOIN
(SELECT
OOS_L1.STORE_ID FAC
,DTE_L2.WEEK_ID WK
,DTE_L2.D_DATE "DATE"
,AVERAGE(TOT_CT.TOT_OOS) AS "OOS COUNT"
,COUNT(DISTINCT(OOS_L1.SCAN_DT)) SCN
,SUM(CASE WHEN OOS_L1.PRODUCT_SOURCE_CD = 'W' THEN CASE WHEN ITM.DEPARTMENT_ID = 314 THEN OOS_L1.OOS_STR_SCAN_CNT ELSE 0 END ELSE 0 END) AS "DAIRY"
FROM
OOS_STORE_ITEM_DAY OOS_L1
JOIN (SELECT STORE_ID,SCAN_DT ,COUNT(UPC_ID) AS TOT_OOS FROM OOS_ITEM_DETAIL WHERE SCAN_TYP_CD = 'O' GROUP BY 1,2)TOT_CT
ON TOT_CT.STORE_ID = OOS_L1.STORE_ID AND TOT_CT.SCAN_DT=OOS_L1.SCAN_DT AND TOT_CT.TOT_OOS>49
JOIN LU_DAY_MERGE DTE_L2 ON OOS_L1.SCAN_DT=DTE_L2.D_DATE
JOIN LU_UPC ITM ON OOS_L1.UPC_ID = ITM.UPC_ID AND ITM.CORPORATION_ID = 1
GROUP BY 1,2,3) OOS
ON OOS.WK = FACDTE.WK AND OOS.FAC=FACDTE.FAC AND OOS."DATE" = FACDTE."DATE"
ORDER BY 1,2,3,4,5,6
Not saure, what the question is. But if you're talking about the feature in Teradata SQL Assistant to place parameters (with the ?-Syntax):
You can define parameters in SQL Assistant with Keyword ?. Just place ? with the parameter name (no spaces) anywhere in your Editor. SQL Assistant will ask for each parameter and search&replace it before the query is send to the database. The parameters can occur multiple times in the editor, you just have to enter the value once and it will be replaced each occurrence.
SELECT *
FROM myWebOrders
where orderDate >= CURRENT_DATE - ?OrderHistory_Days
UNION ALL
SELECT *
FROM myRetailOrders
where orderDate >= CURRENT_DATE - ?OrderHistory_Days
or
SELECT TOP 50 *
FROM ?myTable

Sql query with three conditions

I have a database with a table having content as below :
message_number message_type message_chat
0 IN Hi
1 OB Hello
2 IN Help
3 IN Want to find this thing
4 OB Sure
5 OB Please let me know
I have written 5 rows since i want to incorporate all possible cases that i want in my query in the example table that i showed.
Now in my query output, i want something like :
message_in message_out
Hi Hello
Help NULL
Want to find this string Sure
NULL Please let me know
So the cases that i want to consider are :
suppose if message_number=0 and message_number=1 both have message_type value as IN then put message_chat_in as message_chat(at message_number=0) and message_chat out as NULL and the iterate over message_number=1
if message_number =0 have message_type=IN and message_number =1 have message_type=OB, then show message_chat(at message_number=0) as message_chat_in and message_chat(at message_number=1) as message_out and dont iterate over message_number=1;
hope i have clarified the condition though i have included all three condition in the expected output.How should my sqlquery look like?
Edit : I am using mysql version 5.5.8
Try the following query
SELECT
q1.message_number in_num,
q1.message_chat in_chat,
q2.message_number out_num,
q2.message_chat out_chat
FROM
(
SELECT *,#i1:=IFNULL(#i1,0)+1 num
FROM Chat
ORDER BY message_number
) q1
LEFT JOIN
(
SELECT *,#i2:=IFNULL(#i2,0)+1 num
FROM Chat
ORDER BY message_number
) q2
ON q2.num=q1.num+1 AND q2.message_type<>q1.message_type
WHERE q1.message_type='IN'
UNION ALL
SELECT
q1.message_number in_num,
q1.message_chat in_chat,
q2.message_number out_num,
q2.message_chat out_chat
FROM
(
SELECT *,#i3:=IFNULL(#i3,0)+1 num
FROM Chat
ORDER BY message_number
) q1
RIGHT JOIN
(
SELECT *,#i4:=IFNULL(#i4,0)+1 num
FROM Chat
ORDER BY message_number
) q2
ON q2.num=q1.num+1 AND q2.message_type<>q1.message_type
WHERE q2.message_type='OB'
AND q1.message_type IS NULL
ORDER BY IFNULL(in_num,out_num)
SQL Fiddle - http://sqlfiddle.com/#!9/95a515/1
The second variant
SET #i1 = 0;
SET #i2 = 0;
SET #i3 = 0;
SET #i4 = 0;
-- the same query
SQL Fiddle - http://sqlfiddle.com/#!9/95a515/2
Or
SELECT 0,0,0,0 INTO #i1,#i2,#i3,#i4;
-- the same query
SQL Fiddle - http://sqlfiddle.com/#!9/95a515/5
why not using a analytic function here? I would do it with Lead() like this:
with inc as (
--Do the incorporation in this block. could be subquery too
--but its easier to read this way.
select
case when message_type = 'IN'
then message_chat
end as message_in
,case when LEAD(message_type) OVER (Order by message_number) = 'OB' --get the next message by number if it is type OB
then LEAD(message_chat) OVER (order by message_number)
end as message_out
from input
)
select *
from inc
where coalesce(message_in, message_out) is not null --filter out rows where with in & out is null
Ok, since there is no analytical functions in MySQL less than 8 the code may not be easy to follow:
with data_rn as
(
-- this isolate consecutive rows with the same message_type
select d1.*, count(d2.message_number) rn
from data d1
left join data d2 on d1.message_number > d2.message_number and d1.message_type != d2.message_type
group by d1.message_number
),
data_rn2 as
(
-- this marks the rows where new rows has to be added (i.e. when rn2 != 0)
select d1.*, count(d2.message_number) rn2
from data_rn d1
left join data_rn d2 on d1.rn = d2.rn and d1.message_type = d2.message_type and d1.message_number > d2.message_number
group by d1.message_number
),
data_added as
(
-- this add new rows
select message_number, message_type, message_chat
from data_rn2
union all
select message_number - 0.5, 'OB', NULL from data_rn2 where message_type = 'IN' and rn2 != 0
union all
select message_number - 0.5, 'IN', NULL from data_rn2 where message_type = 'OB' and rn2 != 0
order by message_number
), data_added_rn as
(
-- this compute new row numbering
select d1.*, ceil((count(d2.message_number)+1)/2) rn
from data_added d1
left join data_added d2 on d1.message_number > d2.message_number
group by d1.message_number
)
-- this will do the final formating
select max(case when message_type = 'IN' then message_chat end) message_in,
max(case when message_type = 'OB' then message_chat end) message_out
from data_added_rn
group by rn
demo
I have tried to comment each section appropriately.

MySQL - query to return NULL

I have the following code:
SELECT q25, (
(
AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall
FROM t_results
WHERE brand = 'XYZ'
AND DATE = 'MAY2012'
GROUP BY q25
ORDER BY Overall
DESC LIMIT 1
If there is no data found by the query phpmyadmin returns the following message (which is quite correct):
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0178 sec )
However, what I'd like is to actually return a NULL value, is this possible? I appreciate this might not be best practise but I'm working with inherited code and this might be the simplist and quickest route to a solution.
Thanks as always,
H.
Create a table with exactly one row. Then you can use left join to achieve the desired NULL result.
CREATE TABLE dummy (d TINYINT NOT NULL);
INSERT INTO dummy SET d = 1;
SELECT q25,
( ( AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall
FROM dummy LEFT JOIN t_results
ON brand = 'XYZ'
AND DATE = 'MAY2012'
GROUP BY q25
ORDER BY Overall DESC
LIMIT 1
You can also replace the dummy table with a subquery:
SELECT q25,
( ( AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall
FROM (SELECT 1) AS dummy LEFT JOIN t_results
ON brand = 'XYZ'
AND DATE = 'MAY2012'
GROUP BY q25
ORDER BY Overall DESC
LIMIT 1
Tested this via sqlfiddle, where you can also experiment with alternatives.
The conditions selecting the result, which used to be in the WHERE clause, now have to go into the ON clause. Otherwise the left join would produce non-NULL rows which would be removed by the WHERE, instead of generating a single NULL row if no matching row could be found. If there were no WHERE conditions in the original query, ON 1 could be used to express any row matches.
You can use a UNION combined with a LIMIT to supply the NULL values:
(SELECT q25,
(AVG(q1) + AVG(q2) + AVG(q3))/3 AS Overall
FROM t_results
WHERE brand = 'XYZ'
AND DATE = 'MAY2012'
GROUP BY q25
ORDER BY Overall DESC
LIMIT 1
)
UNION ALL
(SELECT NULL, NULL)
LIMIT 1;
This only works when you know that the first query will never yield more than one result, though. Which is the case here, so this might be the best solution for you, but the approach given in my other answer is more general.
There is a fiddle for this to experiment with.
The coalesce() function can be used to return the first non-null value from a number of comma separated columns or strings. The values/columns are evaluated left to right, so if you want to pop a string into the arguments that isn't null, make sure you place it to the right of the columns that you are testing against.
select
coalesce(
(
SELECT
q25
FROM
t_results
WHERE
brand = 'XYZ'
AND DATE = 'MAY2012'
GROUP BY
q25
LIMIT 1
), 'null') as q25,
coalesce(
(
SELECT
((AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall
FROM t_results
WHERE
brand = 'XYZ'
AND DATE = 'MAY2012'
LIMIT 1
), 'null') as Overall
from
t_results
group by
1, 2;
If you don't have data that matches your where clause, this will return null, null as a row.

Doctrine subquery within subquery

I'm trying to convert a raw mysql query to use doctrine.
The table is full of rows of statistics, and my query is checking to see how far from the average the stat gain has deviated from the average increase each day.
The SQL version works exactly how I'd expect it to act. Converting to Doctrine gives me an error.
Here's the original:
SELECT
l.*,
DAY(l.created_at) as day,
MONTH(l.created_at) as month,
YEAR(l.created_at) as year,
(
MAX(l.infamyrenown) -
MIN(l.infamyrenown) -
(
SELECT AVG(infamydifference) as avginf FROM
(
SELECT (
MAX(inf.infamyrenown) -
MIN(inf.infamyrenown)
) as infamydifference
FROM lotro_record inf
GROUP BY DAY(inf.created_at)
) as p1
)
) as infamy_deviance
FROM
lotro_record l
GROUP BY
year,month,day
And here's the broken Doctrine query:
Doctrine_Core::getTable("LotroRecord")
->createQuery("l")
->select("l.*")
->addSelect("DAY(created_at)")
->addSelect("MONTH(created_at)")
->addSelect("YEAR(created_at)")
->addSelect("(
MAX(l.infamyrenown) -
MIN(l.infamyrenown) -
(
select AVG(infamydifference) as avginf FROM (
SELECT (
MAX(inf.infamyrenown) -
MIN(inf.infamyrenown)
) as infamydifference
FROM LotroRecord inf
GROUP BY DAY(inf.created_at)
) as p1
)
) as infamy_deviance")
->where("lotro_character_id = {$this->getId()}")
->groupBy("DAY(created_at)");
Which generates this SQL:
SELECT l.id AS l__id,
l.infamyrenown AS l__infamyrenown,
l.kills AS l__kills,
l.killing_blows AS l__killing_blows,
l.kills_above_rating AS l__kills_above_rating,
l.kills_below_rating AS l__kills_below_rating,
l.deaths AS l__deaths,
l.lotro_character_id AS l__lotro_character_id,
l.created_at AS l__created_at,
l.updated_at AS l__updated_at,
DAY(l.created_at) AS l__0,
MONTH(l.created_at) AS l__1,
YEAR(l.created_at) AS l__2,
( Max(l.infamyrenown) - Min(l.infamyrenown) - (SELECT
Avg(infamydifference) AS avginf
FROM
(SELECT ( Max(l2.infamyrenown) - Min(l2.infamyrenown) ) AS l__0
FROM lotro_record l2
GROUP BY DAY(l2.created_at)) AS p1) ) AS l__3
FROM lotro_record l
WHERE ( l.lotro_character_id = 1 )
GROUP BY DAY(l.created_at)
The error is:
Unknown column 'infamydifference' in 'field list'
Any ideas?
I think it wants you to call it inf.infamydifference instead of just infamydifference in the DQL query you're written:
select AVG(inf.infamydifference) as avginf