I'm new here, and new to SQL. I have searched, but I can't seem to find an answer to my question. Maybe you gurus can help. I have a table that has customer ID numbers and their status (number) among other things. For instance, a few lines would be like this:
Status - ACCTnum - CustName - City - State
95 - A330 - Billy Burger - Cleveland - Oh
11 - A330/Q - Billy Burger Store#2 - Cleveland - Oh
15 - B250 - Spanky - Columbus - Oh
15 - B250/Z - Spanky#2 - Springfield - OH
15 - B250/Y - Spanky#3 - Miami - FL
We see here, there is a main account number, and a sub account number, but they occupy the same field. Account A330 is billy burger, and his second store is A330/Q. The status column is for their salesman number. If the number is 95, it is a dead account. The problem is, for our purposes, the status of a main account cannot be dead if a sub account is in good standing. So what I need is a query that can basically select any records that meet the criteria: "If ACCTnum is status 95, and has sub accounts that are not status 95"
Ideally if I ran the query on the table above it should return the first two records, since A330 is status 95 and A330/Q is not. It should ignore the other records.
I have tried the INTERSECT command with no success (I assume because it only works for two different tables?). I am a total SQL n00b, be gentle ;)
select distinct c1.*
from Customers c1
inner join Customers c2 ON c2.ACCTnum LIKE c1.ACCTnum + '%'
where c1.ACCTnum.status = 95 and c2.ACCTnum <> 95
Related
I am new to SQL, starting today at college.
I need to find the telephone number of owners who took their pet for a visit in May 2020.
I have the following tables with the columns and some example data:
Visit:
Pet_ID, Vet_Id, Date, Time, Reason, Treatment
P0089 - V04 - 03/05/2020 - 17:50 - Limping - Ointment
P0001 - V04 - 07/05/2020 - 21:00 - Cut on nose and leg - Ointment
Pet:
Pet_ID, Name, Type, Breed, Gender, Born, Owner_ID, Notes
P0001 - Tiddles - Cat - Persian - F - 2009 - 2 - Has a bad temper & Scratches
P0089 - Ginger - Cat - Siamese - F - 2016 - 4 - Bad tempered
Owner:
Owner_ID, Surname, Forename, Title, Tel_Number, Address
1 - Jackson - Janet - Miss - 0141-223321 - 1 Main Street, Glasgow
2 - Singh - Raj - Mr - 0141-326535 - 22, The Hill, Glasgow
I have tried the following command.
SELECT Owner.Tel_Number
FROM Owner, Pet, Visit
WHERE Visit. Date_ BETWEEN "01/05/2020" AND "31/05/2020"
AND Visit.Pet_id = Pet.Pet_id
AND Pet.Owner_id = Owner.Owner_id
GROUP BY Owner.Tel_Number
But the result shows the telephone number for all owners? What am I doing wrong? Thank you.
EDIT: Apologies, I am using DB Browser for SQLite, not Oracle or anything other system like that. This is for college. And I have added some example data.
Always use proper, explicit, standard*, readable JOIN syntax.
However, your problem is probably the format of the date strings. This should always be in standard format. I would suggest:
SELECT o.Tel_Number
FROM Owner o JOIN
Pet p
ON p.Owner_id = o.Owner_id JOIN
Visit v
ON v.Pet_id = p.Pet_id
WHERE v.Date_ >= '2020-05-01' AND
v.Date_ < '2020-06-01'
GROUP BY o.Tel_Number
I have been practicing data manipulation in SQL Server Express 2017, and I have been provided with a data source I can't seem to make sense of. I was hoping there might be someone more familiar here that might be able to point me in the right direction. I need to work on some SQL queries on the dataset, but I haven't the faintest idea on where to start.
The data looks like this for instance:
Company Code - Field - Value (3 fields)
1001 - Vendor Name - 7 Eleven
1001 - Vendor Name - Bob Jane
1001 - Vendor Name - Krispy Kreme
1001 - Vendor Address - 102 Reservoir Street
1001 - Vendor Address - 110 Pitt Road
1001 - Vendor Address - 23 Foxy Place
Usually, I would expect to see it in a somewhat relational type of table like
Company Code Vendor Name Vendor Address
1001 7 Eleven 102 Reservoir Street.
1001 Bob Jane 110 Pitt Road.
What you have appears to be a design called EAV - entity attribute value. That term you can google. Unfortunately either you left out important information or your design is fundamentally broken. Given what you posted, there is no way to know that "Bob Jane" goes with "110 Pitt Road". Rows in a table have no reliable or specific order. You need a column in your table to define "order" if you want to associate rows based on "order".
I have this scenario:
Table Territory
ID (int) - CODE (varchar) - NAME (varchar)
Data:
1 - GB - UNITED KINGDOM
2 - GB - ISLE OF MAN
3 - GB - NORTHERN IRELAND
4 - PT - PORTUGAL
5 - DE - GERMANY
6 - DE - HELGOLAND ISLAND
Table Rules:
ID (int) - TERRITORY_CODES (varchar) - TERRITORY_IDS (varchar)
1 - 'GB,PT' - NULL
2 - 'DE,PT' - NULL
I know the second table should not be like this, but I have no option to change it.
I want to fill the column TERRITORY_IDS with the IDs from the table TERRITORY separated by comma. For example:
Table Rules
ID (int) - TERRITORY_CODES (varchar) - TERRITORY_IDS (varchar)
1 - 'GB,PT' - '1,4'
2 - 'DE,PT' - '5,4'
There are several IDs for each territory code, but I want only one ID for each territory table, it could be the first one, doesn't matter.
What you are looking to do is a Bad Idea. It is a good thing that you recognize this is a bad Idea. But for those reading this question and do not understand why it is bad, this violates the First normal form (1NF) principle. Which is all columns should be atomic, meaning that they hold 1 and only 1 value.
Lets get to the nuts and bolts on how to do this Coalesce to the rescue.
Since I do not know why 'gb,pt' and 'de,pt' are grouped that way I didnt wrap this in a Cursor to go through the whole table. But you can easily wrap this in a cursor and do the entire table contents for you.
DECLARE #TERRITORY_Ids varchar(100)
SELECT #TERRITORY_Ids = COALESCE(#TERRITORY_Ids+ ', ', '') +
Id
FROM table_terrytory
WHERE code in ('gb','pt')
INSERT INTO table_rules
SELECT 'gb,pt',#TERRITORY_Ids
Table1:
id - name - address
-----------------------------
1 - Jim - Some Street
2 - Adam - Some Street
3 - ABC - Some Street
Table2:
id - job - finished_by
---------------------------
1 - ABC - 2
2 - EFD - 3
3 - XYZ - 2
4 - BVC - 1
In the above two tables Table1.id and Table2.finished_by are supposed to be linked.
For, eg in table 2, job ABC was finished by Adam.
My objective is to select DISTINCT records from Table 2.
and the result should output all the job completed by each of the persons.
I have this query so far:
SELECT *
FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
LIMIT 0 , 30
This joins the Tables side by side, but how do i edit the query to make it display only distinct records, so that the output is:
id - job - id - name
----------------------------
1 - ABC - 2 - Adam
2 - EFD - 3 - ABC
4 - BVC - 1 - Jim
Update:
So, i've did some googling and made some changes to my query:
SELECT DISTINCT finished_by FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
LIMIT 0 , 30
But, it seems that only first line of the query is executed since, i dont see the LEFT JOIN table.
May be this query needs a bit more finishing??
More Updates:
So, from some very distinguished members of StacKOverflow it has been brought to my notice that my logic is totally wrong.. So, i'll try to explain what i am trying to achieve in simple words and not program/code. May be that way i can be fetch a quick solution.
So, there's my Company: CompanyA
people like Jim, Adam etc work for CompanyA.. But, CompanyA sends Jim, Adam etc.. to work for another Company.. Say Company1
Jim, Adam etc can be sent to work for multiple such companies. Say Jim is sent to work for Company1 twice and Adam was sent to work for Company1 thrice.
Table 2 maintains records of how many time a person went to work for Company1 in the following format:
Table2: (Ref: Company1)
id - job - finished_by - Date
------------------------------------
1 - ABC - 2 - 10 Oct
2 - EFD - 3 - 11 Oct
3 - XYZ - 2 - 12 Oct
4 - BVC - 1 - 13 Oct
Now, my objective is simple, The reports need to be generated as follows for Company1:
List the persons we sent to Company1 (in Alphabetic Order)
This list should include No. of times the person went (and Dates)
Should also Include the job he did there while he was working for Company1
For, eg an Ideal Output/Report would be:
Name of Employee - Job Description - Dates
ABC - EFD - 11 Oct
Adam - ABC, XYZ - 10 Oct, 12 Oct
Jim - BVC - 13 Oct
I can do all the basic reporting, But i judt dont know how to Convert the numbers that are sitting into Table2 in finished_by coloumn into their respective names from table1
I hope i'm clear with my question now.
Thanks, Everyone!!
I really appreciate your time and effort
Based on your latest update, it sounds like you want a comma-separated list of the "job" names and dates. MySQL's GROUP_CONCAT function accomplishes that. So perhaps something like this:
SELECT table1.*, GROUP_CONCAT(table2.job), GROUP_CONCAT(table2.date)
FROM table1
INNER JOIN table2 ON (t1.id = t2.finished_by)
GROUP BY t1.id
This will give you a list of all employees who did work, along with comma-separated lists of where they did work and when.
Keep in mind that there's no order to the values in each GROUP_CONCAT list. So you can't be sure, for example, that the first job listed corresponds to the first date listed. But if you wanted to keep that connection intact you'd want each job in a separate row anyway.
SELECT DISTINCT *
FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
LIMIT 0 , 30
does this work?
It sounds as though you want all Table1 details, together with a count of all jobs they have finished from Table2. If so, try this:
select t1.id,
max(t1.name) name,
max(t1.address) address,
count(t2.id) finished_jobs
from Table1 t1 left outer join Table2 t2 on t1.id = t2.finished_by
group by t1.id;
i am trying to count the number of riders in my data. and i am having trouble figuring this out. sample of my output is noted below. The data comes from many different tables and I had to join the tables which is not the problem I am having. I am trying to get the count number of RIDERS by EVENT by DESCRIPTION. And still display the columns as noted below.
SQL> SELECT EVENTNAME, DESCRIPTION, RIDERS2 FROM ERP_REPORT;
EVENTNAME DESCRIPTION RIDERS
------------------------------ ------------------------------
Ace Rental Car - Fair Hill Inv Day 1 Race on Fair Hill's Easy Aaron Adams
itational level Course
Ace Rental Car - Fair Hill Inv Day 1 Race on Fair Hill's Easy Aaron Adams
itational level Course
Ace Rental Car - Fair Hill Inv Day 2 Race on Fair Hill's Inte Aaron Adams
itational rmediate level Course
Huffy's Mountain Trip Weekend 1 Race 1 on Huffy Moun Sam Adams
tain's Easy level Course
Valley Spring Water Mountain B Day 3 Race on Hoola Hut Gorge' Jay Gillgan
ike Extravaganza s Intermediate level Course
I have tried a bunch of different code but this is one Sample I have tried. I am lost on this so any help would huge!
SELECT COUNT(DISTINCT RIDERS) as "RIDERS"
2 FROM ERP_REPORT;
Are you looking for something like:
SELECT COUNT(riders) AS rider_count, eventname, description
FROM erp_report
GROUP BY eventname, description;