sql-server to mysql translation - mysql

I'm trying to recreate in mysql something created in MS SQL. I'm having a heck of time getting the syntax right. Does anyone know what the equivalent mysql query would be for the following:
create table #tmp
(id int, Ran varchar(10), Result int, ref_id int)
insert #tmp values (1, 'Object1', 4.0, 1)
insert #tmp values (2, 'Object2', 100, 1)
insert #tmp values (3, 'Object1', 6.0, 2)
insert #tmp values (4, 'Object3', 89.0, 2)
select * from #tmp
Select t.ref_id
,TK = max(case when t.Ran ='Object1' then t.[Result] end)
,CRP= max(case when t.Ran ='Object2' then t.[Result] end)
,HPT= max(case when t.Ran = 'Object3' then t.[Result] end)
From #tmp t
group by t.ref_id
Thank you for taking a look!

This doesn't seem difficult:
create temporary table tmp (
id int,
Ran varchar(10),
Result int,
ref_id int
);
insert into tmp(id, Ran, Result, ref_id) values (1, 'Object1', 4.0, 1);
insert into tmp(id, Ran, Result, ref_id) values (2, 'Object2', 100, 1);
insert into tmp(id, Ran, Result, ref_id) values (3, 'Object1', 6.0, 2);
insert into tmp(id, Ran, Result, ref_id) values (4, 'Object3', 89.0, 2);
select * from tmp;
Select t.ref_id,
max(case when t.Ran ='Object1' then t.Result end) as TK,
max(case when t.Ran ='Object2' then t.Result end) as CRP,
max(case when t.Ran = 'Object3' then t.Result end) as HPT
From tmp t
group by t.ref_id;
Here is a pretty close SQL Fiddle.

MySQL equivalent query for above sql query:
create table #tmp
(id int, Ran varchar(10), Result int, ref_id int);
insert into #tmp values (1, 'Object1', 4.0, 1);
insert into #tmp values (2, 'Object2', 100, 1);
insert into #tmp values (3, 'Object1', 6.0, 2);
insert into #tmp values (4, 'Object3', 89.0, 2);
select * from #tmp;
Select t.ref_id
,TK = max(case when t.Ran ='Object1' then t.Result end)
,CRP= max(case when t.Ran ='Object2' then t.Result end)
,HPT= max(case when t.Ran ='Object3' then t.Result end)
from #tmp t
group by t.ref_id;

Related

Need to show data as per column wise in MySQL as per Image

I have data as main table now how can I show data as per query table. Please help.
Hello Please test this:
CREATE TABLE MainTable
(
Code INT, ItemName VARCHAR(30), SalesDate DATETIME, SalesQty INT);
INSERT INTO MainTable VALUES
(1, 'Product-1','2022-01-01',593),
(2, 'Product-2','2022-01-02',1152),
(3, 'Product-3','2022-01-03',1629),
(4, 'Product-4','2022-01-04',4216),
(5, 'Product-5','2022-01-05',1760),
(1, 'Product-1','2022-02-01',1634),
(2, 'Product-2','2022-02-02',1748),
(3, 'Product-3','2022-02-03',2259),
(4, 'Product-4','2022-02-04',2076),
(5, 'Product-5','2022-02-05',1040);
SELECT Code, ItemName,
(CASE WHEN MONTH(SalesDate) = 1 THEN SUM(SalesQty) END) AS January,
(CASE WHEN MONTH(SalesDate) = 2 THEN SUM(SalesQty) END) AS February
FROM MainTable
GROUP BY Code, ItemName,SalesDate;
Result_Set:

How to insert to a table if there exist a value in another table

i have tried this
INSERT into examqst set qno=1, qst="aa" ,qan1="a" , qan2="B", qan3="C", qan4="d", qant="A", qtype=0, examid=1
WHERE EXISTS (SELECT * FROM examname where examid=1 and s_id=10);
I want to insert to examqst only if there is a row examid in examname table and s_id in examname have a perticilar value
You could an INSERT INTO...SELECT:
INSERT INTO examqst (qno, qst, qan1, qan2, qan3, qan4, qant, qtype, examid)
SELECT 1, 'aa', 'a', 'B', 'C', 'd', 'A', 0, 1
WHERE EXISTS (SELECT 1 FROM examname WHERE examid = 1 AND s_id = 10);

mySQL - SUM and COUNT and JOIN to display all records [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 4 years ago.
This is a following question to this one Join two tables with SUM and COUNT.
What I try to do is to have all values displayed as some are in history table and not in rota table or vice-versa (999 and 777)
So my tables are:
create table history (
code int(10) primary key,
PN varchar(10) not null,
Qty int(10) not null,
LOC_ID int(10));
insert into history values (1, 'T1', 1, 1);
insert into history values (2, 'A1', 2,2);
insert into history values (3, 'J1', 3,3);
insert into history values (4, 'A2', 1,4);
insert into history values (5, 'J2', 2,1);
insert into history values (6, 'A3', 3,2);
insert into history values (7, 'J3', 4,3);
insert into history values (8, 'T1', 5,4);
insert into history values (9, 'A1', 1,1);
insert into history values (10, '999', 3,2);
insert into history values (11, 'J2', 4,3);
insert into history values (12, 'A1', 3,4);
insert into history values (13, 'J2', 5,1);
create table rota (
code int(10) primary key,
PN varchar(10) not null,
SN varchar(10) not null,
LOC_ID int(10));
insert into rota values (1, 'T1', 't1a',1);
insert into rota values (2, 'A1', 'a1a',2);
insert into rota values (3, 'J1', 'j1a',3);
insert into rota values (4, 'A2', 'a2a',4);
insert into rota values (5, 'J2', 'j2a',1);
insert into rota values (6, 'A3', 'a3a',2);
insert into rota values (7, 'J3', 'j3a',3);
insert into rota values (8, '777', 't1b',4);
insert into rota values (9, 'A1', 'a1b',1);
insert into rota values (10, 'J2', 'j2b',2);
insert into rota values (11, 'J2', 'j2c',3);
insert into rota values (12, 'A1', 'a1c',4);
insert into rota values (13, 'J2', 'j2d',1);
insert into rota values (14, 'J2', 'j2e',2);
insert into rota values (15, 'J2', 'j2f',3);
create table loca (
code1 int(10) primary key,
LOC varchar(10) not null);
insert into loca values (1, 'AAA');
insert into loca values (2, 'BBB');
insert into loca values (3, 'CCC');
insert into loca values (4, 'DDD');
The code I have got is
select CASE WHEN a.pn IS NULL THEN b.pn ELSE a.pn END AS PN
, a.q
, b.c
, a.LOC_ID
, b.LOC_ID
from
(select
h.pn
, sum(qty) q
, h.LOC_ID
from
history h
group by h.pn, h.LOC_ID) a
RIGHT JOIN
(select
r.pn
, count(sn) c
, r.LOC_ID
from
rota r
group by r.pn, r.LOC_ID) b
on a.pn = b.pn WHERE a.LOC_ID = b.LOC_ID
order by a.pn;
The above code works great for all PN that are in both tables. The problem is for values that are specific to one of the tables. I can remove the WHERE clause from JOIN but it is not corect. The question is - how to get all PNs from history and rota where some of them are present i just one table. I had some luck with RIGHT JOIN but that did not cover unique values from the other table. Any one came across solution before?
Results shoud look like the following table
PN LOC_ID Count Qty
T1 1 1 1
A1 2 1 2
J1 3 1 3
A2 4 1 1
J2 1 2 2
A3 2 1 3
J3 3 1 4
777 4 1 NULL
A1 1 1 1
J2 2 2 NULL
J2 3 2 4
A1 4 1 3
J2 1 2 2
J2 2 2 NULL
J2 3 2 4
999 2 NULL 3
use another join and that is left and make them union
select t.PN,t.q,t.c,t.LOC_ID,t.LOC_ID_b from
(
select CASE WHEN a.pn IS NULL THEN b.pn ELSE a.pn END AS PN
, a.q
, b.c
, a.LOC_ID
, b.LOC_ID as LOC_ID_b
from
(select
h.pn
, sum(qty) q
, h.LOC_ID
from
history h
group by h.pn, h.LOC_ID) a
RIGHT JOIN
(select
r.pn
, count(sn) c
, r.LOC_ID
from
rota r
group by r.pn, r.LOC_ID) b
on a.pn = b.pn and a.LOC_ID = b.LOC_ID
) as t
union
select t2.PN,t2.q,t2.c,t2.LOC_ID,t2.LOC_ID_b from
(
select CASE WHEN a.pn IS NULL THEN b.pn ELSE a.pn END AS PN
, a.q
, b.c
, a.LOC_ID
, b.LOC_ID as LOC_ID_b
from
(select
h.pn
, sum(qty) q
, h.LOC_ID
from
history h
group by h.pn, h.LOC_ID) a
left JOIN
(select
r.pn
, count(sn) c
, r.LOC_ID
from
rota r
group by r.pn, r.LOC_ID
) b
on a.pn = b.pn and a.LOC_ID = b.LOC_ID
) t2
http://sqlfiddle.com/#!9/c20c81/20

Returning records which only have one specific many to many relation

Given this structure
CREATE TABLE locations
(`id` int, `Name` varchar(128))
;
INSERT INTO locations
(`id`, `Name`)
VALUES
(1, 'Location 1'),
(2, 'Location 2'),
(3, 'Location 3')
;
CREATE TABLE locations_publications
(`id` int, `publication_id` int, `location_id` int)
;
INSERT INTO locations_publications
(`id`, `publication_id`, `location_id`)
VALUES
(1, 1, 1),
(2, 2, 1),
(3, 2, 2),
(4, 1, 3)
;
I would like to find only Location 2 based on the fact that it has only one relation with a publication_id = 2.
It should not return location one due to the fact that it has two relation rows.
This is sort of what I'm looking for but of course dosnt work because it limits the relationship to where publication_id = 2.
select * from locations
join locations_publications on locations_publications.location_id = locations.id
where locations_publications.publication_id = 2
group by (locations.location_id)
having count(*) = 1
You can do this with aggregation:
select location_id
from locations_publications
group by location_id
having count(*) = 1
If a location might have multiple records with the same publication, change the having criteria to count(distinct publication_id) = 1
Given your edits, you can use conditional aggregation for that:
select location_id
from locations_publications
group by location_id
having count(*) = sum(case when publication_id = 2 then 1 else 0 end)

How to retain and increase the value of variable in SQL server 2008?

Initially, I have two variables x (group variable) and y, now I'd like to create a third variable z which within each group of x starts from 1 and increase by 1 whenever y changes. Please see the following example. Could someone help how I can realize it in sql server 2008?
Thanks a lot!
x: a a a a a a a a b b b b b b b b
y: 0 0 0 1 0 0 2 3 0 1 0 0 2 3 0 0
Finally, I'd like to create z like this:
x: a a a a a a a a b b b b b b b b
y: 0 0 0 1 0 0 2 3 0 1 0 0 2 3 0 0
z: 1 1 1 2 3 3 4 5 1 2 3 3 4 5 6 6
You can use a recursive CTE and row_number something like this.
with C1 as
(
select T1.id,
T1.x,
T1.y,
row_number() over (partition by T1.x order by T1.id) as rn
from table1 as T1
), C2 as
(
select C1.id,
C1.x,
C1.y,
C1.rn,
1 as z
from C1
where C1.rn = 1
union all
select C1.id,
C1.x,
C1.y,
C1.rn,
C2.z + case when C1.y <> C2.y then 1 else 0 end
from C1
inner join C2
on C1.x = C2.x and
C1.rn = C2.rn + 1
)
select x, y, z
from C2
order by id
option (maxrecursion 0);
Note that using a CTE as the source for a recursive query can have some issues with performance. If that s the case I would recommend you to put the result of the CTE C1 in a temporary table with a primary key on (x, rn).
SQL Fiddle
Here is the simple solution:
declare #temp table ([row] int identity,[x] char,[y] int)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',1)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',2)
insert into #temp([x],[y]) values ('a',3)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',1)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',2)
insert into #temp([x],[y]) values ('b',3)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',0)
--select * from #temp
DECLARE #int int=0
;WITH CTE as(
select *,1 as incr from #temp where row=1
union all
select t.*,
CASE WHEN t.x=c.x then CASE WHEN t.y=c.y then incr else incr+1 end else CASE WHEN t.y=c.y then #int else #int+1 end end as incr
from #temp t inner join CTE c
on t.row=c.row+1
)
select * from CTE
You can't quite do this with row_number() over (partition by ... order by ...) and a CTE query doesn't seem to work either. I like to avoid using cursors whenever possible, but a cursor may be the very thing for your situation:
declare #tbl table (id int, x char(1), y int, z int null)
declare
#id int, #x char(1), #y int,
#x2 char(1) = null, #y2 int = null, #z int
insert into #tbl (id, x, y) values
(1, 'a', 0), (2, 'a', 0), (3, 'a', 0),
(4, 'a', 1),
(5, 'a', 0), (6, 'a', 0),
(7, 'a', 2),
(8, 'a', 3),
(9, 'b', 0),
(10, 'b', 1),
(11, 'b', 0), (12, 'b', 0),
(13, 'b', 2),
(14, 'b', 3),
(15, 'b', 0), (16, 'b', 0)
declare cr cursor for
select id, x, y
from #tbl
order by id
open cr
fetch next from cr into #id, #x, #y
while ##fetch_status = 0
begin
set #z =
case when #x2 is null or #x <> #x2
then 1
else
case when #y = #y2
then #z
else #z + 1
end
end
update #tbl
set z = #z
where id = #id
set #x2 = #x
set #y2 = #y
fetch next from cr into #id, #x, #y
end
close cr
deallocate cr
select x, y, z
from #tbl
order by id
Thought I'd throw my solution into the mix:
declare #temp table ([row] int identity,[x] char,[y] int)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',1)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',0)
insert into #temp([x],[y]) values ('a',2)
insert into #temp([x],[y]) values ('a',3)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',1)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',2)
insert into #temp([x],[y]) values ('b',3)
insert into #temp([x],[y]) values ('b',0)
insert into #temp([x],[y]) values ('b',0)
declare #temp1 table
(
[row] int
,[x] char
,[y] int
,[change] int
)
insert into #temp1
(
[row]
,[x]
,[y]
,[change]
)
select
[t1].[row]
,[t1].[x]
,[t1].[y]
,iif([t1].[x] = [t2].[x] and [t1].[y] <> [t2].[y],1,0) [change]
from #temp [t1]
left join #temp [t2] on [t2].[row] = [t1].[row] - 1
select * from #temp1
select
[row]
,[x]
,[y]
,(select sum([change]) from #temp1 [t2] where [t2].[row] <= [t1].[row] and [t2].[x] = [t1].[x]) + 1 [z]
from #temp1 [t1]