Lets say I have a table with columns Customer and job.
I have rows:
Customer 1, Job 1
Customer 1, Job 2
Customer 1, Job 3
Customer 2, Job 1
Customer 2, Job 2
Customer 2, Job 3
Customer 3, Job 1
Customer 3, Job 2
Customer 3, Job 3
I have a query:
Select *
from Table
Where Customer <> 'Customer 1'
AND (Customer <> 'Customer 2' AND Job <> 'Job 1')
The rows I would want returned from this are:
Customer 2, Job 2
Customer 2, Job 3
Customer 3, Job 1
Customer 3, Job 2
Customer 3, Job 3
But instead the rows returned are only for Customer 3 jobs 2 and 3. I want to still include Customer 2 with jobs 2 and 3 and Customer 3 Job 1.
There are a lot more columns than this in the database but these where the ones in question so I only included these. The rows actually store ticket data in them. I want the customer to be able to filter out what tickets show up in reports and they may want to exclude a customer and also exclude a job from another customer but include everything else. Customer names will be unique but job names can be duplicated across customers.
You can set individual Customer <-> Job exclusion clauses in your where, and then just do a final not in excluding anyone you either don't want or have an existing clause for:
select *
from Table
where (Customer = 'Customer 2' and Job <> 'Job 1')
or Customer not in ('Customer 1', 'Customer 2')
For example of extending this, if you had a Customer 4 and wanted to exclude Job 2 for them:
select *
from tbl
where (Customer = 'Customer 2' and Job <> 'Job 1')
or (customer = 'Customer 4' and Job <> 'Job 2') --add specific customer and job exclusion
or Customer not in ('Customer 1', 'Customer 2', 'Customer 4') --add customer 4 here because we have a specific clause for them
Related
I have the following tables to create in a database.
Customer Registration
Customer
Scheme
Customer Registration has the following columns:
CustomerRegistrationId
CustomerId
SchemeId
Customer has the following columns:
CustomerId
CustomerName
CustomerAddress
Scheme has the following columns:
SchemeId
SchemeName
IsActive
A Customer Registration can have only one Customer.
A Customer Registration can have many Schemes.
So my question is to whether is it better to structure the database tables like I have above. So if a Customer Registration had multiple Schemes the data would look something like....
Customer:
12, John Smith, 1234 Park Road
Scheme:
1, Multiple Buy Savings, True
2, School Book Scheme, True
3, Student Discount, True
Customer Registration:
1, 12, 2
2, 12, 3
So Customer Registration has multiple entries for the same Customer for each Scheme.
Or would it be better to implement a table that only stored the Customer Registration and Scheme Id. E.g.
CustomerRegistraion_Scheme_Link
CustomerRegistrationId
SchemeId
And Change Customer Registration to:
CustomerRegistrationId
CustomerId
So the data above would now look like:
Customer:
12, John Smith, 1234 Park Road
Scheme:
1, Multiple Buy Savings, True
2, School Book Scheme, True
3, Student Discount, True
Customer Registration:
1, 12
CustomerRegistration_Scheme_Link:
1, 2
1, 3
I'd appreciate any advice, thanks :)
I will explain my scenario with an example.
I have multiple rows in my table. I am picking those one by one for processing. I need to lock the row for processing.
Sample code looks like,
select top 1 * into #open_order from orders with (xlock)
where status = 'open' order by order_time;
EDIT: Added order by clause in the query.
My requirement is to run this in parallel connections. My problem here is, I cannot run this code on multiple connection in parallel. The second one waits until the first one commit the transaction.
Is there any way to exclude already locked rows from this select query?
I have come across with(readpast). But I don't know whether it can be used together or not.
EDIT: Sample data and expectation.
Orders table data:
id, order_time, status, remark
1, 2019-01-01 00:00:01, 'open', 'Sample 1'
2, 2019-01-02 00:00:01, 'open', 'Sample 2'
3, 2019-01-03 00:00:01, 'open', 'Sample 1'
If first row is locked, I am expecting to get the second row as result of the query.
to make an event session, how'd you define a relationship.
Event has days, days has sessions. Days will hold the number of rows depending upon the number of days.
Should I be making 3 Tables or more?
1) Event Table (id, name)
2) Day Table (id, date, event_id)
3) Session Table (id, name, start_time, end_time, day_id)
or something different, please advise.
one event has multiple days, and each day has multiple sessions.
Edit: (is this okay)
event
id name
1 event 1
2 event 2
-----
day
id date event_id
1 '2015-06-01' 1
2 '2015-06-02' 1
3 '2015-07-01' 2
4 '2015-07-02' 2
------
session
id name day_id
1 'session a' 1
2 'session b' 1
3 'session c' 2
4 'session d' 2
------
I wonder if the following scenario is possible to be done through SSIS.
I would like to insert records to SQL Server based on the value in 2 data sources: 1 is CSV file, and 1 SQL Server. For example, 2 data sources, one with the column named Customer ID, another one is from SQL Server with the columns named Task ID and Task Name. I want to insert records to SQL Server with columns named Customer Task ID, Customer ID, Task ID, and Task Name.
I have tried in several ways without any luck by capturing the Customer ID such as C5000 from Source 1 (CSV file), and then mergeing with the columns of Task ID and Task Name from Source 2 (SQL Server), next inserting the merged records to the destination (SQL Server), and then repeatedly executing the same task to insert records until the last Customer record. I summarized the source and destination samples below. Any feedback would be greatly appreciated!
Source 1 (CSV File)--
Row 1 contains Customer ID column: C5000
Row 2 contains Customer ID column: C5001
Source 2 (A table named Task on SQL Server)--
Row 1 contains Task ID column: T2000 and Task Name column: Planning
Row 2 contains Task ID column: T2001 and Task Name column: Review
Destination (A table named Customer Task on SQL Server)--
Row 1 contains Customer Task ID column: CT0001, Customer ID column: C5000, Task ID column: T2000, Task Name column: Planning
Row 2 contains Customer Task ID column: CT0002, Customer ID column: C5000, Task ID column: T2001, Task Name column: Review
Row 3 contains Customer Task ID column: CT0003, Customer ID column: C5001, Task ID column: T2000, Task Name column: Planning
Row 4 contains Customer Task ID column: CT0004, Customer ID column: C5001, Task ID column: T2001, Task Name column: Review
Use Cross Join to do this
INSERT INTO Customer_Task
(Customer_Task_ID,
Customer_ID,
Task_ID,
Task_name)
SELECT 'CT' + RIGHT('000')
+ CONVERT(VARCHAR(10), Row_number() OVER(ORDER BY (SELECT NULL))),
Customer_ID,
Task_ID,
Task_name
FROM Customer
CROSS JOIN Task
In SQL Server, Is there any chance to display results from 3 tables? (Actually 4)
The 4th table contains 1 column similar to others. The values of similar columns of other 3 tables are different. In other words, if you select '1' in the 4th table column, the 1st table's results should displayed, if you select '2', the 2nd etc...
select fourth.pk,
case fourth.columnx
when '1' then one.columnx
when '2' then two.columnx
when '3' then three.columnx
else 'Exception to the rule?'
end as 'Column A'
from fourth
join one on fourth.joinid=one.joinid
join two on fourth.joinid=two.joinid
join three on fourth.joinid=three.joinid