SQL query for the number of cases when a value of column1 (non-unique) can't be found within any record where column2 meets a basic criteria - sql

I am doing a beginners' SQL tutorial and I started to wonder whether a simple SQL query on this table: http://www.sqlcourse2.com/items_ordered.html could tell the number of items (also 1) which have only been purchased more items at a time, so there is no record which contains the quantity column with a value of 1 AND the item. I am really beginner at this so please try to keep it simple.
Thank you in advance!

Welcome to the fascinating world of SQL.
Well - I'm not giving you the answer, but a hint (after all, it's a training and your own thinking and finding the solution would be the best way for you to learn something new).
The way you formulate your question is somewhat puzzling.
When I combine what you ask with what is possible with SQL, the question that would make sense to me would be that you need to list (or count, I did not understand that very well) the items (or the complete rows in the table with matching item, that was not clear either), that were never sold with a quantity of 1.
If that's what you need, you will need a subselect to get all distinct items that were sold with a quantity of 1, and select the rows from your base table whose item value is not in the list you get from the subselect.
Do you need more hints?
Marco

Related

Generate Sequential IDs for Line Items on a Per Customer Basis in SQL Server

Apologies if the title is difficult to understand... it proved challenging to summarize my question.
The business has created a self-reporting spreadsheet to give out to our stores to track customer leads. Each row represents one follow-up action, therefor each customer can have multiple rows. Employees were not properly filling out the follow-up counter column (e.g. they'd forget they already spoke to somebody or someone else followed up a previous lead so one customer would have two rows labeled as follow-up #1) so we eliminated it and I'm now using a concatenation of date and sequential rowID column to order their interactions.
I would like to be able to derive follow-up #s with this data. I am now able to easily order the data by Customer, Date, and RowID to get an accurate flow of follow-ups, but I would like to now number those rows 1 through X so I can perform analysis and aggregate on the follow-up counter.
Simplified example of customers ordered with their Date/ID:
Please click link for example 1 (not enough rep to embed yet)
How could I then, with the data above, create the following:
Please click link for example 2 (not enough rep to embed yet)
I'd love to be able to derive the follow-up counters after the fact via SQL or T-SQL. Any ideas? Thank you.
Take a look at Window Functions, particularly ROW_NUMBER.
I believe you're looking for something like this:
SELECT
[Customer Name],
[date_id_concat],
ROW_NUMBER() OVER (PARTITION BY [Customer Name] ORDER BY [date_id_concat]) AS InteractionSeq
FROM
TABLE;

Sum with two conditions in Excel Table

I'm new to this forum, so if I make mistakes, tell me so I can learn ;).
So the question is, I want to make a summation of a column of a table in Excel, but only if it complies with two conditions. Table1 has 3 columns: Col1 contains a Date, Col2 a price and Col3 a catagory in which the price is logged.
I want the sum of all prices, for which the date falls within a certain month, and the Category complies with a choosen Category.
The code for both individual requirements works, and looks like this:
{=SUM(IF(MONTH(Table1[Date])=MONTH(A3);Table1[Price];0))}
{=SUM(IF(Table1[Category]="Category1";Table1[Price];0))}
However, the combined sum, =SUM(IF(AND(MONTH(Table1[Date])=MONTH(A3);Table1[Category]="Category1");Table1[Price];0)) does not work.
Do you know what I do wrong?
Thanks in advance
I think not really "do wrong" though perhaps a poor choice of approach (in my opinion, I happen not to be a fan of structured references). It is just that AND here does not return an array, only either TRUE (when the result would be the sum of all prices) or, much more likely FALSE (any one condition does not match, when the result is 0).
Instead I would recommend a PivotTable.

MS Access & Queries

Looking for a bit of help for an Access/Query question pertaining to a homework assignment that has 6 separate questions. I have completed all but one. The assignment wants me to do the following query.
The name, unit price, and quantity ordered for all products purchased by a customer whose id is entered from the keyboard.
Is there a function I'm overlooking for the ID entity criteria? I've looked and searched but cannot find how to add that part into the query. Thanks in advance for any help.
You can accomplish this by making the ID criteria a parameter. Access will then pop up an input form that lets you enter the value of the parameter.
You can read more here and here.

SQL select certain number of rows

Hello I need a SQL query statement that gets me rows 'start' to 'finish'.
For example:
A website with many items where page 1 selects only items 1-10, page 2 has 11-20 and so on.
I know how to do this with Microsoft SQL Server and MySQL but I need an implementation that is platform independent. :/
I have an Increment line for IDs but deleting in-between will mess the result when I select via
WHERE ID > number AND ID < othernumber
of course
Is this possible without fetching the whole database to a ResultSet?
I think your safest bet would be to use the BETWEEN operator. I believe it works across Oracle/MySQL/MSSQL.
WHERE ID BETWEEN number AND othernumber
Concerning your comment " I was just think for the case when first 100 IDs are gone I'll have to check further until there is something to fetch", you might wanna consider NOT actually ever deleting stuff from your database but to add a flag like "active" or something like that to your tables so you can avoid situations like the one you're now trying to avoid. The alternative is where you are now, having to find the max and min rows in a filter

what is the best way to record users searches and count how many times it has been queried?

I am not sure what SQL is exactly, databases I think, would SQL be the thing to use?
I want it so that when a user types into a search box it displays a "did-you-mean" box, that also shows how many other people used that term but I will do later ;)
currently I just want the text to be saved into database (?) and also how many times it's been done.
(new to stackoverflow so sorry if format/whatever is bad)
You just need a three table named something like search_queries which has columns for: id, search_text, and count. Everytime somebody searches just increment the count column where search_text is equal to what they searched for. This is a really broad question though and you need to start by learning the basics and report back here with more specific questions.