I want to get data of different ranges like "SUBSCRIBER_ZIP between '30000' and '31999'"
and "SUBSCRIBER_ZIP between '39813' and '39901'" and "SUBSCRIBER_ZIP between '32000' and '34999'".
I want to get all the Active records(means status=1) having this search criteria. I have used Union for this but it does not provide me unique records:
Select distinct(SUBSCRIBER_EMAIL), SUBSCRIBER_Id FROM
(select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '30000' and '31999'
Union
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '39813' and '39901'
Union
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '32000' and '34999'
) x
But it does not provide me unique results. Please help me how can i do it.
Why not use OR?
select distinct SUBSCRIBER_EMAIL, SUBSCRIBER_ID
from SUBSCRIBER
where SUBSCRIBER_ACTIVE = 1
and (
SUBSCRIBER_ZIP between '30000' and '31999' or
SUBSCRIBER_ZIP between '39813' and '39901' or
SUBSCRIBER_ZIP between '32000' and '34999'
)
SELECT DISTINCT [SUBSCRIBER_EMAIL],[SUBSCRIBER_ID] FROM SUBSCRIBER WHERE ([SUBSCRIBER_ACTIVE] = 1) AND
(
(CAST(SUBSCRIBER_ZIP AS INT) BETWEEN 30000 AND 31999)
OR
(CAST(SUBSCRIBER_ZIP AS INT) BETWEEN 39813 AND 39901)
OR
(CAST(SUBSCRIBER_ZIP AS INT) BETWEEN 32000 AND 34999)
)
Note : Don't use quotations for the numbers
Related
I'm trying to replace a placeholder string inside a selection of 10 random records with a random string (a name) taken from another table, using only sqlite statements.
i've done a subquery in order to replace() of the placeholder with the results of a subquery. I thought that each subquery loaded a random name from the names table, but i've found that it's not the case and each placeholder is replaced with the same string.
select id, (replace (snippet, "%NAME%", (select
name from names
where gender = "male"
) )
) as snippet
from imagedata
where timestamp is not NULL
order by random()
limit 10
I was expecting for each row of the SELECT to have different random replacement every time the subquery is invoked.
hello i'm %NAME% and this is my house
This is the car of %NAME%, let me know what you think
instead each row has the same kind of replacement:
hello i'm david and this is my house
This is the car of david, let me know what you think
and so on...
I'm not sure it can be done inside sqlite or if i have to do it in php over two different database queries.
Thanks in advance!
Seems that random() in the subquery is only evaluated once.
Try this:
select
i.id,
replace(i.snippet, '%NAME%', n.name) snippet
from (
select
id,
snippet,
abs(random()) % (select count(*) from names where gender = 'male') + 1 num
from imagedata
where timestamp is not NULL
order by random() limit 10
) i inner join (
select
n.name,
(select count(*) from names where name < n.name and gender = 'male') + 1 num
from names n
where gender = 'male'
) n on n.num = i.num
select
concat(fullvisitorid,cast(visitid as string)) as unique_session_id
,case
when h.item.productSku is not null then h.hitNumber
else max(h.hitnumber)
end
,h.item.transactionid
,h.item.itemrevenue/pow(10,6)
,h.item.productSku
from `myproject.mydataset.ga_sessions_20180101`, unnest(hits) as h
group by 1
Looking at case statement above (line 3)
How do I return that hitnumber where the productsku is populated
otherwise return the max hitnumber and then group this by the unique_session_id?
How to filter out transactionid's that contain '_ABC' at the same time?
I would suggest doing the grouping and finding the max hit number in a subquery. If you are going to use an aggregate function like MAX() in the select clause, then you need to group on or have aggregate functions for the other fields in the select. It can be useful to do aggregate sub-queries using common table expressions.
WITH data AS (
SELECT
CONCAT(fullvisitorid, CAST(visitid AS string)) AS unique_session_id,
h.hitNumber,
h.item.transactionid,
h.item.itemrevenue/POW(10,6) AS itemRevenue,
h.item.productSku
FROM
`bigquery-public-data.google_analytics_sample.ga_sessions_20170801`,
UNNEST(hits) AS h
),
max_hits AS (
SELECT
unique_session_id,
MAX(hitNumber) AS max_hit_number
FROM data
GROUP BY 1
)
SELECT
d.unique_session_id,
CASE
WHEN d.productSku IS NOT NULL THEN d.hitNumber
ELSE m.max_hit_number
END,
d.transactionid,
d.itemrevenue,
d.productSku
FROM
data AS d JOIN max_hits AS m
ON d.unique_session_id = m.unique_session_id
I have a query that Counts 2 columns from 2 separate tables using subqueries, which works. Now I have to implement into this query the ability to filter out these results based on the Date of a Call Record. I will post the query in which I am working with:
SELECT (m.FirstName || " " || m.LastName) AS Members,
(
SELECT count(CallToLineOfficers.MemberID)
FROM CallToLineOfficers
WHERE CallToLineOfficers.MemberID = m.MemberID
)
+ (
SELECT count(CallToMembers.MemberID)
FROM CallToMembers
WHERE CallToMembers.MemberID = m.MemberID
) AS Tally
FROM Members AS m, Call, CallToMembers, CallToLineOfficers
Join Call on CallToMembers.CallID = Call.CallID
and CallToLineOfficers.CallID = Call.CallI
WHERE m.FirstName <> 'None'
-- and Call.Date between '2017-03-21' and '2017-03-22'
GROUP BY m.MemberID
ORDER BY m.LastName ASC;
Ok, so table Call stores the Date and its PK is CallID. Both CallToLineOfficers and CallToMembers are Bridge Tables that also contain only CallID and MemberID. With the current query, where the Date is commented out, that Date range should only return all names, but a count of 1 should appear under 1 person's name.
I have tried joining Call.CallID with both Bridge Tables' CallIDs without any luck, though I think this is the right way to do it. Could someone help point me in the right direction? I am lost. (I tried explaining this the best I could, so if you need more info, let me know.)
UPDATED: Here is a screenshot of what I am getting:
Based on the provided date in the sample, the new results, with the Date, should be:
Bob Clark - 1
Rob Catalano - 1
Matt Butler - 1
Danielle Davidson - 1
Jerry Chuska - 1
Tom Cramer - 1
Everyone else should be 0.
At the moment, the subqueries filter only on the member ID. So for any member ID in the outer query, they return the full count.
To reduce the count, you have to filter in the subqueries:
SELECT (FirstName || " " || LastName) AS Members,
(
SELECT count(*)
FROM CallToLineOfficers
JOIN Call USING (CallID)
WHERE MemberID = m.MemberID
AND Date BETWEEN '2017-03-21' AND '2017-03-22'
)
+ (
SELECT count(*)
FROM CallToMembers
JOIN Call USING (CallID)
WHERE MemberID = m.MemberID
AND Date BETWEEN '2017-03-21' AND '2017-03-22'
) AS Tally
FROM Members AS m
WHERE FirstName <> 'None'
ORDER BY LastName ASC;
I have 2 tables in oracle . users and profile table.
i am running following query to get results
SELECT fu.user_name ,papf.SSN
FROM apps.fnd_user fu, apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND (TO_CHAR(papf.EFFECTIVE_END_DATE) IS NULL
OR papf.EFFECTIVE_END_DATE > SYSDATE )
Thing is that ,
Profile table has multiple records for SSN.
So this query is returning dupes for SSN.
I just want to get one distinct record for SSN.
for eg result is like
ABC 123
ABC 123
How do i just display unquie values for SSN
Like
ABC 123
U tried distinct but did not work
Any suggestions ?
Proper use of Distinct for your case...
SELECT Distinct fu.user_name , papf.SSN
FROM apps.fnd_user fu
, apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND (TO_CHAR(papf.EFFECTIVE_END_DATE) IS NULL
OR papf.EFFECTIVE_END_DATE > SYSDATE )
Query using group by can be like below to have a single record:
SELECT fu.user_name ,
papf.SSN
FROM apps.fnd_user fu,
apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND ( papf.EFFECTIVE_END_DATE IS NULL
OR papf.EFFECTIVE_END_DATE > trunc(SYSDATE) --assuming effective_end_date doesnot have timestamp with it.
)
group by fu.user_name,
papf.SSN ;
I have two tables Procedures and ProcedureTypes.
Procedures has a column Type which is a varchar with the values (1, 2), (3, 4), (4, 5) etc...
ProcedureType has a primary key 'ID' 1 to 9.
ID Description
1 Drug
2 Other-Drug
etc...
ID is an integer value and Type is varchar value.
Now I need to join these two tables to show the values
ID in the Procedures table
ProcedureType in the Procedures table
Description in the ProceduresType table with the value separated by a "-".
For example if he value in Type is (1,2) the new table after join should show values in the description like (Drug-Other Drug)
I have used this query bot to no avail
SELECT * FROM dbo.[Split]((select RequestType from GPsProcedures), ',')
Can anyone tell me how to do it and why the above query is not working
with Procedures as (
select 1 as ID, '1,2,3' as Typ
),
ProcedureTypes as (
select 1 as TypeID, 'Drug' as Name
union select 2 , 'Other-Drug'
union select 3 , 'Test 3'
)
/*Get one extra column of type xml*/
,Procedures_xml as (
select id,CONVERT(xml,' <root> <s>' + REPLACE(Typ,',','</s> <s>') + '</s> </root> ') as Typ_xml
from Procedures
)
/*Convert the field string to multiple rows then join to procedure types*/
, Procdure_With_Type as (
select ID,T.c.value('.','varchar(20)') as TypeID,
ProcedureTypes.Name
from Procedures_xml
CROSS APPLY Typ_xml.nodes('/root/s') T(c)
INNER JOIN ProcedureTypes ON T.c.value('.','varchar(20)') = ProcedureTypes.TypeID
)
/*Finally, group the procedures type names by procedure id*/
select id,
STUFF((
SELECT ', ' + [Name]
FROM Procdure_With_Type inn
WHERE (Procdure_With_Type.ID = inn.ID)
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') AS NameValues
from Procdure_With_Type
group by ID
You can't have a select statement as a parameter for a function, so instead of this:
SELECT * FROM dbo.[Split]((select RequestType from GPsProcedures), ',')
Use this:
select S.*
from GPsProcedures P
cross apply dbo.[Split](P.RequestType, ',') S