creating table with correct data - sql

Im having problems finding the correct data. I have a Table which contains customers(customerID). Each customer is connected to a certain phonenumber(PhoneNr). Every number starts with 2-9.
Every customer have a callcenter(CallCenterID) they can call iff needed.
I want to know how many customers call each callcenter, divided from 2-9(PhoneNumber).
So I want to know how many calls a callcenter gets from every customer with 5, as there starting number in phonenumber.
So far so good. My Code in sql:
Select CallCenter, Count(Customers) AS Number
from ******
Where PhoneNumber Like '45%' --Just need the numbers from Danish customers.
Group By Callcenter;
Im new to much of this, but i've tried the whole day to come up with the right result.
Right now Im getting every callcenter, and the number for every call to them.
Can anyone help me?
:)

If I'm understanding correctly, you want the counts for all CallCenter's broken down by the first digit in the PhoneNumber:
SELECT CallCenter, SUBSTR(PhoneNumber, 1, 1) as startsWith, COUNT(*) as number
FROM myTable
GROUP BY CallCenter, SUBSTR(PhoneNumber, 1, 1)
ORDER BY 2, 3
If that's not what you wanted, please explain your question a bit better.

Related

Rebuild query which looking for specific string

I have products numbers which looks like below:
One example product (contain couple articles)
14.12312.1
14.12312.2
14.12312.3
14.12312.4
14.12312.5
Other example product (contain couple articles)
003.23.1
003.23.66
003.23.44
003.23.77
003.23.5
Important thing here is dots are separators:
<producentNumber>.<specialnumber>.<articlevariation>
Now i got query which could take first part of specific product for instance i can pass: 003.23. (with last dot) and then it gives me all starts with it so:
003.23.1
003.23.66
003.23.44
003.23.77
003.23.5
This is the query which is doing that:
SELECT ID, Nummer FROM [Cenea].[dbo].[T_Artikel]
WHERE
SUBSTRING(Nummer, 0, LEN(Nummer) + 2 - CHARINDEX('.', REVERSE(Nummer))) IN
(
'003.23.'
)
However now is the change that within last part of the articles number could appears dots and not only the numbers so it means i can have like this:
003.23.1
003.23.66..dwqd
003.23.4422.323.3
003.23.77....f
003.23.5
So when i pass 003.23. i will get not all but just 003.23.1 in this case. The logic stays as it was first two dots are holding the base but after the second there could be anything. Question is how to rebuild my sql query to adapt the change to get alkl articles?
Hope everything is clear and please provide working code as i have to make change on production today.
You can use LIKE to get all rows where Number starts with <some value>:
SELECT ID, Nummer FROM [Cenea].[dbo].[T_Artikel]
WHERE Number LIKE '003.23%'

SQL counting number of rows

I am looking for a way to search for a certain number of rows as a quality check. For example, we have tables that have a certain set of results that are needed.
Here is a quick table for an example:
ID: Name: Result: Reportable:
ONE A 10 X
TWO B 12 X
THREE C 1
FOUR D 18 X
FOUR(redo) D 11 X
So we are looking to double check results as there are people who accidentally report results multiple times (as in the case with ID FOUR). We have used having counts but we need the numbers to be specific and need a query to verify that number is satisfied.
In the table above we only want IDs ONE, TWO, and FOUR, however we have 4 results (one extra). Currently we have our check showing the count needed (ie 3) and the current result count (4) to show the mismatch but want a query to easily only show the result needed. We would need the redo result most of the time so we have set it so we take the latest date, but it doesn't help filter how many rows or results. I apologize if anything is confusing and I am not able to share the SQL query that we have currently. It's my first time posting so if I need to clarify anything please let me know as this seems to be very complicated. Thank you for your time.
EDIT: The details
We have one table (Table A) letting us know which results are reportable. The ones that are reportable go into another table (Table B). We have had issues in which people have made too many results reportable which overpopulates the Table B. Our old query had a count in Table B, but due to mistakes in people placing multiple reportables, samples which had many redos seem to be finished as they were all placed and met the count in Table B.
So now by using the Table A that helps tell us how many are Reportable, we want this to double check that the samples are indeed ready.
As I understand the question, you want ids that have multiple reportables. Assuming you really mean name, then:
select name
from t
where reportable = 'X'
group by name
having count(*) >= 2;

SQL Selecting records where one date range doesn't intersect another

I'm trying to write a simple reservation program for a campground.
I have a table for campsites (one record for every site available at the campground).
I have a table for visitors which uses the campsite table's id as a foreign key, along with a check in date and check out date.
What I need to do is gather a potential check in and check out date from the user and then gather all the campsites that are NOT being used at any point in that range of dates.
I think I'm close to the solution but there's one piece I seem to be missing.
I'm using 2 queries.
1) Gather all the campsites that are occupied during that date range.
2) Gather all campsites that are not in query 1.
This is my first query:
SELECT Visitors.CampsiteID, Visitors.CheckInDate, Visitors.CheckOutDate
FROM Visitors
WHERE (((Visitors.CheckInDate)>=#CHECKINDATE#
And (Visitors.CheckInDate)<=#CHECKOUTDATE#)
Or ((Visitors.CheckOutDate)>=#CHECKINDATE#
And (Visitors.CheckOutDate)<=CHECKOUTDATE));
I think I'm missing something. If the #CHECKINDATE# and #CHECKOUTDATE# both occur between someone else's Check-in and Check-out dates, then this doesn't catch it.
I know I could split this between two queries, where one is dealing with just the #CHECKINDATE# and the second is dealing with the #CHECKOUTDATE#, but I figure there's a cleaner way to do this and I'm just not coming up with it.
This is my second one, which I think is fine the way it is:
SELECT DISTINCT Campsites.ID, qryCampS_NotAvailable.CampsiteID
FROM Campsites LEFT JOIN qryCampS_NotAvailable
ON Campsites.ID = qryCampS_NotAvailable.CampsiteID
WHERE (((qryCampS_NotAvailable.CampsiteID) Is Null));
Thanks,
Charles
To get records that overlap with the requested time period, use this simple logic. Two time periods overlap when one starts before the other ends and the other ends after the first starts:
SELECT v.CampsiteID, v.CheckInDate, v.CheckOutDate
FROM Visitors v
WHERE v.CheckInDate <= #CHECKOUTDATE# and
v.CheckOutDate >= #CHECKINDATE# ;

Summarizing records by date in Rails 3 ActiveRecord

I have a large table with many records that share a timestamp. I want to get a result set that has a column summed by timestamp. I see how you can simply use the 'sum' method to get a columns total. I need to, however, group by a date column. This is far less obvious. I know I can use 'find_by_sql' but it will be hideous to code as I have to do this for over 20 columns. I assume AR must have some magic to do this which escapes me?
Date set example:
table/model: games/Game
player_name, points_scored, game_date
john, 20, 08-20-2012
sue, 30, 08-20-2012
john, 12, 08-21-2012
sue, 10, 08-21-2012
What i want to see in my results is:
game_date, total_points
08-20-2012, 50
08-21-2012, 22
Here is a crude example of what the SQL query would look like:
SELECT game_date, SUM(points_scored)
FROM games
GROUP BY game_date
Mind you, I actually have 20 'score' columns to SUM by timestamp.
How can I simply use AR to do this? Thanks in advance.
Ok. It took some digging and playing around but I figured it out. I was hoping to find something better than 'find_by_sql' and I did, but it isn't a whole lot better. Again, knowing that I need to SUM 20+ columns by timestamp, here is the solution in the context of the example above.
results = Game.select( 'game_date, SUM(points_scored) as "points_scored"').group( 'game_date' )
Now, that doesn't look so bad, but I have to type in the 20+ SUM() within that 'select' method. Doesn't save a whole lot of work from 'find_by_sql' but it works.

Joining Tables in Oracle 8i to Limit Results

My background in SQL is limited, but my Googling isn't, I feel that I might just be missing the vocabulary to ask this question properly so hopefully beyond an answer to my question I can get the vocabulary I need to research this issue further.
I have a parts table - PARTS
I have a Purchase Order table - PO
and I have a PO Line Item table - PO_LINEITEM
The question I'm attempting to answer is given a particular part I want to get the latest purchase order and then look at the price we paid.
The PO table holds the date (PO_DATE) of when the Purchase Order was filled and the PO_LINEITEM table holds the information regarding the particular line item such as part primary key (PART_PRIMARYKEY) and price (PART_PRICE). The PARTS table isn't as important as the rest except that I need to return a PART primary key so that I can base the resulting view off the PARTS table
I've been through sub-queries and scalable sub-queries and the like, but I can't seem to find the right combination.
I started from a base of:
SELECT a.PO_DATE, b.PART_PRIMARYKEY, b.PART_PRICE
FROM PO a, PO_LINEITEM b
WHERE a.PO_PRIMARYKEY = b.PO_PRIMARYKEY
As you would expect this returns a list of every instance of an object in a PO with it's price and the date the Purchase Order was filled. The closest I have come to crack this is by using the MAX function on the date such as:
SELECT MAX(a.PO_DATE) AS DATE, b.PART_PRIMARYKEY, b.PART_PRICE
FROM PO a, PO_LINEITEM b
WHERE a.PO_PRIMARYKEY = b.PO_PRIMARYKEY
GROUP BY b.PART_PRIMARYKEY, b.PART_PRICE
This returns a Max date for each price a we paid for a particular part, so:
PART 1234, £12.95, 12/08/2012
PART 1234, £13.00, 14/08/2012
PART 1234. £11.15, 17/08/2012
PART 2345, £5.25, 12/08/2012
PART 2345, £5.65, 13/08/2012
etc.
What I need is:
PART 1234, £11.15, 17/08/2012
PART 2345. £5.65, 13/08/2012
If I could just group by the PART_PRIMARYKEY that would be excellent, but I get an ORA-00979 not a GROUP BY expression when I try.
Like I said I feel that my lack of vocabulary around this issue is impeding me finding an answer, so if anyone could point me in the right direction I'd be grateful
So hopefully I'm not asking a question that is asked every other day, but haven't found because I didn't use the magical combination of words to find.
Thank you for any help you can offer.
Look up Analytic Functions. They were introduced in 8i though I'm not sure how advanced they were at the time compared to how very good they can be in 11. A few links that I've used to understand them:
http://www.oracle-base.com/articles/misc/analytic-functions.php
http://www.orafaq.com/node/55
Though, a sub-query such as this might suffice (I may have your column naming mixed up):
select A.PART_PRIMARYKEY, B.PART_PRICE, A.PO_DATE
from PO A, PO_LINEITEM B
where A.PO_PRIMARYKEY = B.PO_PRIMARYKEY
and (A.PART_PRIMARYKEY, A.PO_DATE) in
( select A.PART_PRIMARYKEY, max(A.PO_DATE)
from PO A, PO_LINEITEM B
where A.PO_PRIMARYKEY = B.PO_PRIMARYKEY
group by A.PART_PRIMARYKEY);