sorting mixed table datas - mysql

I have two tables in a MySQL database. First table is contacts(customer, id) that stores customers' information. Second table history(report, nextFollowingDate, customerid) store the history of a customer contact, along with next following date. A customer can have multiple records with different values for nextFollowingDate.
Sample data are as follows.
contacts table:
customer id
a 1
b 2
c 3
history table:
report nextFollowingDate customerid
report1 2018/04/23 1
report2 2018/04/25 1
report3 2018/04/22 2
report4 2018/04/26 3
report5 2018/05/30 2
I would like to sort customers in contacts table with on the values of nextFollowingDate in the ascending order. It would look like follows.
customer nextFollow
1 2018/04/25
2 2018/05/30
3 2018/04/26
But I have no way in my mind of doing that.

SELECT customerId, MAX(nextFollowingDate) FROM history GROUP BY customerId
is what you are looking for. However, what do you exactly mean by last following date ASC is not very clear. I have written this answer based on your example result set that you have given as your expectation. Find this SQL Fiddle to see this in action.

Related

Sql SELECT query just shows 1 row

So im trying to get a table that shows 3 activities which have the id's of 6,7,8 and they must be from a specific country. It should show their names and the number of times they appear in the person_activity_interval but the table i get is only 1 row with the name of 1 activity and the count of all the activities that are listed. If i remove other 2 activities it gets me the correct name and count but i need a table with all 3. Do i need to use something like join or union?
SELECT name,count(activity_id)
FROM activity,person_activity_interval
WHERE oib IN (SELECT oib
FROM person
WHERE living_id IN (SELECT id
FROM living
WHERE country= "Poland"))
AND ((activity_id=8 AND id =8) OR (activity_id=7 AND id =7) OR (activity_id=6 AND id =6));

Fetch rows either with one matching condition or all rows if no matching rows

I have a simple table
**targeted_url_redirect targeted_countries msg_type non_targeted_url**
http://new.botweet.com india,nepal,philippines NEW http://twisend.com
http://expapers.com United States,Canada OLD http://all.twisend.com
https://tweeasy.com india,england OLD http://all.twisend.com
I receive traffics on my website followerise.com and I want to redirect users to specific urls based on their countries as defined in the above table. I am able to write query to get rows for the users who coming from the countries that are target stored in my database. But I am looking for a solution to get all the rows if the targeted_countries condition not return any rows.
I written below queries.
SELECT * FROM tweeasy_website_redirection
WHERE message_type='OLD'
AND targeted_countries LIKE '%#country%'
This query gives me desired rows if visitor coming from the countries india,england,United States,Canada
But I want all rows (2nd and 3rd) should be fetched if a user coming from the countries not specified in targeted_countries column.
Also let me know if I need to restructure this table into 2 or more tables to get desired result.
One option uses not exists:
SELECT t.*
FROM tweeasy_website_redirection t
WHERE
t.message_type = 'OLD'
AND (
t.targeted_countries LIKE '%#country%'
OR NOT EXISTS (
SELECT 1
FROM tweeasy_website_redirection t1
WHERE t1.targeted_countries LIKE '%#country%'
)
)
When it comes to the structure of your table: one design flaw is to store list of values as CSV list. You should have two separate tables: one to store the urls, and the other to store the 1-N relationship between urls and countries. Something like:
table "url"
id targeted_url_redirect msg_type non_targeted_url
1 http://new.botweet.com NEW http://twisend.com
2 http://expapers.com OLD http://all.twisend.com
3 https://tweeasy.com OLD http://all.twisend.com
table "url_countries"
url_id country
1 india
1 nepal
1 philippines
2 united states
2 canada
3 india
3 england
select * from tweeasy_website_redirection
where targeted_countries not in (SELECT targeted_countries FROM stack WHERE targeted_countries LIKE '%#country%')

SSRS lookup with source not in current tablix

I have 3 tables that I want to show in a tablix SSRS report in 3.0.
Table 1 - policy ID, amt paid by company
Query - Select * from table1 where amt paid by company <> 0
Table 2 - policy ID, policy number, previous policy number
Query - Select * from table2 where previous policy number <> ' '
Table 3 - previous policy number, paid under prior company
Query - Select * from table3 where paid under prior company <> 0
I want to display the following columns on one tablix row per entry in table 1:
Policy number
amt paid by company
previous policy number
paid under prior company.
I created a tablix. I can display everything from table 1 and use lookup for table 2 items but when I do a lookup for item from table 3 it gives me an error.
From my research about this error I understand it to mean I cannot use the source in lookup from any table but table 1 in my case. And I can only do one level in lookup.
I have looked and can find no example for this anywhere and I have tried other methods and cannot figure out how to get to that piece of data in table 3.
Is my only choice to combine tables 2 and 3 together then use in this report with the lookup?
You can use INNER JOIN to get a dataset that contains all fields you require to show in the tablix.
SELECT
table2.PolicyNumber,
table1.AmntPaidByCompany,
table2.PrevPolicyNumber,
table3.PaidUnderPriorCompany
FROM table2
INNER JOIN table3
ON table2.PrevPolicyNumber = table3.PrevPolicyNumber
INNER JOIN table1
ON table1.PolicyID = table2.PolicyID
WHERE table1.AmntPaidByCompany <> 0
AND table2.PrevPolicyNumber <> ''
AND table3.PaidUnderPriorCompany <> 0
Live Demo
Let me know if this helps.

Query: COUNT in Access To Only Count Unique Values

I have a table like so:
Customer Purchase Date Product
Frank 7/28/2015 Hammer
Bob 7/29/2015 Shovel
Bob 7/29/2015 Pickaxe
Bill 7/30/2015 Pliers
The Purchase Date field records a new entry for every purchase. So, if in one visit a customer purchases four items, my database creates four entries each with the same date.
I'm trying to write a query that displays the numbers of visits for each customer. Output like so:
Frank 1
Bob 1
Bill 1
But when I use the COUNT function on the date in my query, it returns:
Frank 1
Bob 2
Bill 1
I want my query to only count unique dates, but the COUNT function doesn't work. Everywhere I read, it also says that the SQL COUNT (Distinct) doesn't work in Access. Access help says that if I set the Query Properties to Unique Values "Yes", it should only return unique values, but it doesn't work. I tried Unique Record "Yes" also, but that didn't work either.
Please help! Thanks!
Try this:
select Cust, count(cust) as CustomerCount
from (Select Distinct Table1.Customer as cust, Table1.PurchaseDate
from Table1)
group by cust

Query to get the output as shown in designation table

EmpId EmpDeptName
1 Account
2 Sales
3 IT
4 HR
i want output in the format
EmpId Account Sales IT HR
1
2
3
4
If anybody knows how to write query to get this output , please provide me that query.
This type od data transformation is a PIVOT, where you take row data and convert it to columns.
You did not provide many details, but if you wanted the count of employees in each department, you could use something like this:
select Account, Sales, IT, HR
from
(
select EmpDeptName
from employees
) d
pivot
(
count(EmpDeptName)
for EmpDeptName in (Account, Sales, IT, HR)
) p;
This will display the total number of employees in each department as column data instead of rows.