i am trying to duplicate a column hotelid , and then i want to make a couples that begin with the same letter,
select cast (hotelid as int) as hotelid1 , cast (hotelid as int) as hotelid2
from hotel
where (postcode BETWEEN 1500 ANd 1999) or
( postcode BETWEEN 3000 and 3499)
and (hotelid2 > hotelid1 )
and ( LEFT(hotelid1, 1) like LEFT(hotelid2, 1))
how i can use the new lines in where conditions ?
MySQL uses HAVING for that
SELECT cast(hotelid as UNSIGNED ) as hotelid1 , cast(hotelid as UNSIGNED ) as hotelid2
from hotel
where (postcode BETWEEN 1500 ANd 1999) or
( postcode BETWEEN 3000 and 3499)
HAVING (hotelid2 > hotelid1 )
and ( LEFT(hotelid1, 1) like LEFT(hotelid2, 1))
Related
I have a query:
SELECT `Name`, `ID_dir`, 999 as `children`
FROM `dir` dir WHERE dir.`fid_parent` IS NULL
AND (
EXISTS (
SELECT 1
FROM `file` f
WHERE dir.ID_dir = f.fid_parent
)
OR (
SELECT 1
FROM `dir` d2
WHERE dir.ID_dir = d2.fid_parent
)
)
Where I check if the directory has any foreign key.
How can I move that information in place of 999 in "Select ... 999 as children"?
I want to return (0 or 1) xor Boolean in that place as children.
Put the EXISTS subquery in the SELECT list.
SELECT Name, ID_Dir, (
EXISTS (
SELECT 1
FROM `file` f
WHERE dir.ID_dir = f.fid_parent
)
OR (
SELECT 1
FROM `dir` d2
WHERE dir.ID_dir = d2.fid_parent
)
) AS children
FROM dir WHERE fid_parent IS NULL
I am writing an ETL in kettle pentaho to create a table from various sources including google analytics.
so Table 1 = All data from website joined to google analytics information
Table 2 = All Duplicate data from Table 1 joined to google analytics information
my problem is that some info on table 1 has the google analytics information missing but table 2 shows some data for Google Analytics on the same reference_number
So what I want to do is lookup [reference_number] from table 1 to table 2 and populate table 1 where some columns are null from info on table 2
Quick example EDIT*
Table 1 (Main Table) * *This table has an index built in on website_reference number (Unique)*
website_Reference_number GA_info_1 GA_info_2
A1 null null
A2 x y
Table 2 (Duplicates from Table 1)
eventlabel GA_info_1 GA_info_2
A1 z z
A2 x y
my output should be the following
Table 1 (Main Table)
Ref_number GA_info_1 GA_info_2
A1 z z
A2 x y
I am using a My_SQL database
UPDATE mytable
LEFT JOIN table2 ON mytable.Ref_number = table2.Ref_number
SET mytable.GA_info_1 = COALESCE (
mytable.GA_info_1,
table2.GA_info_1
),
mytable.GA_info_2 = COALESCE (
mytable.GA_info_2,
table2.GA_info_2
)
WHERE
mytable.GA_info_1 IS NULL
OR mytable.GA_info_2 IS NULL
Put all the fields which might be null into the where clause.
If the field is not null it will not be updated because it is the first argument in the coalesce function, if it is null it will be updated by the field of the other table.
edit: Also you can try it like this:
UPDATE mytable
INNER JOIN table2 ON mytable.Ref_number = table2.Ref_number
SET mytable.GA_info_1 = COALESCE (
mytable.GA_info_1,
table2.GA_info_1
),
mytable.GA_info_2 = COALESCE (
mytable.GA_info_2,
table2.GA_info_2
)
WHERE
CONCAT(mytable.GA_info_1, mytable.GA_info_2) IS NULL
For the performance issue: (as already mentioned in the comments)
Since you are not using the primary or foreign keys to join the tables, you would have to set an index on your Ref_number columns to speed up the join.
UPDATE DIM_ENQUIRIES_TEST
LEFT JOIN DIM_ENQUIRIES_TEST AS STAGING_GA ON DIM_ENQUIRIES_TEST.website_reference_number = STAGING_GA.eventlabel
SET DIM_ENQUIRIES_TEST.eventlabel = COALESCE (
DIM_ENQUIRIES_TEST.eventlabel,
STAGING_GA.eventlabel
),
DIM_ENQUIRIES_TEST.sourcemedium = COALESCE (
DIM_ENQUIRIES_TEST.sourcemedium,
STAGING_GA.sourcemedium
)
,
DIM_ENQUIRIES_TEST.deviceCategory = COALESCE (
DIM_ENQUIRIES_TEST.deviceCategory,
STAGING_GA.deviceCategory
)
,
DIM_ENQUIRIES_TEST.avgSessionDuration = COALESCE (
DIM_ENQUIRIES_TEST.avgSessionDuration,
STAGING_GA.avgSessionDuration
)
,
DIM_ENQUIRIES_TEST.timeonpage = COALESCE (
DIM_ENQUIRIES_TEST.timeonpage,
STAGING_GA.timeonpage
)
,
DIM_ENQUIRIES_TEST.avgtimeonpage = COALESCE (
DIM_ENQUIRIES_TEST.avgtimeonpage,
STAGING_GA.avgtimeonpage
)
,
DIM_ENQUIRIES_TEST.bouncerate = COALESCE (
DIM_ENQUIRIES_TEST.bouncerate,
STAGING_GA.bouncerate
)
,
DIM_ENQUIRIES_TEST.profileid = COALESCE (
DIM_ENQUIRIES_TEST.profileid,
STAGING_GA.profileid
)
,
DIM_ENQUIRIES_TEST.webpropertyid = COALESCE (
DIM_ENQUIRIES_TEST.webpropertyid,
STAGING_GA.webpropertyid
)
,
DIM_ENQUIRIES_TEST.accountname = COALESCE (
DIM_ENQUIRIES_TEST.accountname,
STAGING_GA.accountname
)
,
DIM_ENQUIRIES_TEST.tableid = COALESCE (
DIM_ENQUIRIES_TEST.tableid,
STAGING_GA.tableid
)
,
DIM_ENQUIRIES_TEST.tablename = COALESCE (
DIM_ENQUIRIES_TEST.tablename,
STAGING_GA.tablename
)
,
DIM_ENQUIRIES_TEST.keyword = COALESCE (
DIM_ENQUIRIES_TEST.keyword,
STAGING_GA.keyword
)
,
DIM_ENQUIRIES_TEST.country = COALESCE (
DIM_ENQUIRIES_TEST.country,
STAGING_GA.country
)
,
DIM_ENQUIRIES_TEST.campaign = COALESCE (
DIM_ENQUIRIES_TEST.campaign,
STAGING_GA.campaign
)
,
DIM_ENQUIRIES_TEST.sessions = COALESCE (
DIM_ENQUIRIES_TEST.sessions,
STAGING_GA.sessions
)
,
DIM_ENQUIRIES_TEST.sessionduration = COALESCE (
DIM_ENQUIRIES_TEST.sessionduration,
STAGING_GA.sessionduration
)
,
DIM_ENQUIRIES_TEST.bounces = COALESCE (
DIM_ENQUIRIES_TEST.bounces,
STAGING_GA.bounces
)
WHERE
DIM_ENQUIRIES_TEST.EventLabel IS NULL
OR DIM_ENQUIRIES_TEST.SourceMedium IS NULL
;
--I just check one because if one of them is null, it is likely that the rest of the columns that needs to be changed would also be null
I got a table called ATMONTH where i need to check the count of customers for each month
Table structure is like
CREATE TABLE [dbo].[ATMONTH](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Month] [varchar](50) NULL,
[COUNT OF CUSTOMER] [varchar] (50) NULL,
[RefMonthStart] [varchar](50) NULL,
[RefMonthEnd] [varchar](50) NULL)
i'm accepting a output like
1 1month 3000 0 30
2 2Month 4500 31 60
3 3month 4000 61 90
4 4Month 6000 91 120
.
.
.
24 24Month .. .. ..
25 >24Month .. .. ..
where count of customer i'm refering to other table.. here only month, refmonthstart and
refmonthend column must be manually inserted where refmonthstart and refmonthend columns
are number of days in in a month
how will i do it...??
Thanks in Advance
Try the following query
DML:
CREATE TABLE [dbo].[ATMONTH](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Month] [varchar](50) NULL,
[COUNT OF CUSTOMER] [varchar] (50) NULL,
[RefMonthStart] [varchar](50) NULL,
[RefMonthEnd] [varchar](50) NULL)
INSERT INTO [dbo].[ATMONTH]([Month])
VALUES ('Jan'),
('Feb'),
('Mar'),
('Apr'),
('May'),
('Jun'),
('Jul'),
('Aug'),
('Sep'),
('Oct'),
('Nov'),
('Dec');
CREATE TABLE dbo.MonthsAndDays
(
[Month] VARCHAR(3),
Days SMALLINT
)
INSERT INTO dbo.MonthsAndDays([Month],Days)
VALUES ('Jan',31),
('Feb',28),
('Mar',31),
('Apr',30),
('May',31),
('Jun',30),
('Jul',31),
('Aug',31),
('Sep',31),
('Oct',30),
('Nov',30),
('Dec',31);
Query:
DECLARE #refmonthstart INT = 0
;WITH CTE AS
(
SELECT a.ID,
a.Month,
a.[COUNT OF CUSTOMER],
a.[RefMonthStart],
a.[RefMonthEnd],
b.Days Days,
ROW_NUMBER() OVER ( ORDER BY a.[ID] ASC) i
FROM [dbo].[ATMONTH] a
INNER JOIN dbo.MonthsAndDays b
ON a.[Month] = b.[Month]
)
,
ResultSet AS
(
SELECT ID,
Month,
[COUNT OF CUSTOMER],
#refmonthstart AS RefMonthStart,
Days + #refmonthstart AS RefMonthEnd,
i
FROM CTE
WHERE i = 1
UNION ALL
SELECT T.ID,
T.Month,
T.[COUNT OF CUSTOMER],
R.RefMonthEnd + 1,
T.Days + R.RefMonthEnd,
T.i
FROM ResultSet R
INNER JOIN CTE T
ON R.i + 1 = T.i
)
SELECT *
FROM ResultSet
OPTION (MAXRECURSION 1000)
Query will recursively calculate refmonthstart and refmonthend value. Set the MAXRECURSION value depending upon the number of rows you have in the table.
You can set the initial refmonthstart value where #refmonthstart is declared, I have set it to 0(zero)
I have two tables :
create table sales (
unitcode int ,
categorycode smallint ,
ddate varchar(10) ,
price float
)
and
create table timetable (
year varchar(4) ,
month varchar(11) ,
ddate varchar(10)
)
I want to write a subquery to find :
in each month in each year which 2 products(unitcode , categorycode) have a top of price ?
Try this
;WITH cte AS (
SELECT unitcode,categorycode,t.ddate,price,ROW_NUMBER() OVER (PARTITION BY t.[year],t.[month] ORDER BY price desc) AS price_order,t.[year],t.[month]
FROM sales s
INNER JOIN timetable t
ON t.ddate = s.ddate
)
SELECT *
FROM cte
WHERE price_order <= 2
ORDER BY [year] ASC,[month] ASC,price DESC
Table looks like this:
create table #rankme (rankmeid int identity(1000, 1) primary key,
step int null, checkvalue int null)
insert into #rankme values ( 10 , 1 )
insert into #rankme values ( 15 , null )
insert into #rankme values ( 20 , null )
insert into #rankme values ( 40 , null )
select * from #rankme order by step,checkvalue
Taking step as a parameter, I am trying to find out if the requested checkvalue for the one before the step I asked for is null.
So I want to select where step=20 and get NULL.
And I want to select where step=15 and get a 1.
I was trying to come up with something based on "rank-1" but so far no cigar.
Help?
declare #step int = 15
select top(1) R.checkvalue
from #rankme as R
where R.step < #step
order by R.step desc