samedi 25 avril 2015

Customer and branch query in company database


I have following four tables in my database :

EMPLOYEE

PK: employeeID;     
FK: empBranch references BRANCH;
FK: empSupervisor references EMPLOYEE

enter image description here

BRANCH

PK: branchNumber
FK: branchManager references EMPLOYEE

enter image description here

CUSTOMER

PK: customerID

enter image description here

ORDERS

PK: orderNumber
FK: customerID references CUSTOMER; 
FK: salesPerson references EMPLOYEE

enter image description here

Now I need to list out these two things :

  1. The Employee_ID and name of salespersons who have sold only to customers located in the same city as the city in which the salesperson’s branch is located.

  2. The Employee_ID and name of salespersons who have sold to every customer located in the same city as the city in which the salesperson’s branch is located.

Please help me to answer both queries


Split into different columns SQL Server


I've got data in a column of my table just like this:

cpc > cpc > organic
cpc > organic >cpc
mail > cpc > organic

How can I split that data to different columns using '>' as a delimiter?


Transform and copy table data


Sorry if this questioned was asked before, but i am posting it, since was not able to find anything related in forum. So any help would be appreciated. Here is my scenario.

We have an SQL table view A (just read access) with set of data located on Server A on domain A. We have our SQL database with table B located on Server B on Domain B. We have got the port opened up between 2 domains.

We need to write a SSIS job (runs per day) which will get the data from View A and insert in Table B. So what could be the best way for achieving this.

Note:

  • table View on server doesn't have deleted or last modified columns So wont be able to figure out what are the changes. (if there is any other way please do let me know)
  • Database data is around 50k records

Approach 1:

  • Export data from View
  • Transform it
  • Drop table on Server B
  • Insert the transformed data

Approach 2:

  • Export data from view
  • Transform it
  • Insert into a temp table
  • Make a log entry into log table
  • When the log entry says successfully copied, trigger to delete all the rows and copy the data from temp table to Table B.
  • If something went wrong, rollback.

Grouping based on the order of data


I have a set of data in the following format.

A
A
B
A
C
B
B
C

I want to group the data in such a way that the result should be grouped by alphabet and is should be based on order. For example for the above data the output should be

A - 2
B - 1
A - 1
C - 1
B - 2
C - 1


mysql to search in multiple quantity


I have a problem. I'm building a Client Information System with PHP and MYSQL. One of the feature of the application is to allow users to search for the name by typing ic number in multiple input. For example, information stored in the database:

ID : 1
NAME : John
IC : 111

ID : 2
NAME : James
IC : 222

ID : 3
NAME : Sarah
IC : 333


So, to search the data, in search field (form), i would like to type 111222 , without comma. so is there any ways? Any sql syntax which should i use?

Any help is very much appreciated


EDITED

Here is my code for searching function.

$ics = $_POST["nokp"]; 
$each =explode(' ',$ics);

$q=mysql_query("SELECT id,nama,nokp from client where nokp in(". implode(',', $each).") group by nama,nokp");


c# change primary key


I need to change a column type in a SQL Server table.

I need do it using a script or C#.

The original datatype is integer and the new values is varchar(50).

I can do it using this SQL script:

ALTER TABLE confezionamento 
  ALTER COLUMN partnumber varchar(50)   

but the problem is that this column is into a set of primary key.

When I try do execute, an error occurred because PK_CONFEZIONAMENTO use the column.

How I can do it? I can't access by SQL Server Management Studio.


SQL query returning events with an odd number of users assigned to them


I have 5 tables--users, groups, events, users_groups_link, users_events_link. Like so:

users
user_id

groups
group_id

events
event_id

users_groups_link
user_id
group_id

users_events_link
user_id
event_id

Each event has 2 or 4 groups assigned to it and each group contains 8-11 users. user_id=1 is a group vacancy. I'm wanting to make sure that each event has an even number of users so I'm trying to create a query that returns all events containing an odd number of users. So far I have this:

SELECT user_id,event_id 
FROM users,groups,events,users_events_link,users_groups_link
WHERE users.user_id=users_events_link.user_id
AND events.event_id=users_events_link.event_id
AND users.user_id=users_groups_link.user_id
AND groups.group_id=users_groups_link.group_id
AND user_id <> 1

which returns all events and users that are assigned to them (via their host group) like so:

user_id         event_id
56              1001
34              1001
76              1001
45              1001
87              1001
88              1001
54              1001
4               1001
63              1002
69              1002
77              1002
etc.

Now I'm wanting to refine this query further by just returning events with an odd number of users assigned to them i.e. events with their id in the right hand column repeating an odd number of times. And then maybe refining it further by eliminating the users_id column entirely and returning one event id for each event containing an odd number of users.

I've tried using "count()" and "group by" but am not getting the desired results so i'm obviously doing something wrong but I don't know what. Thanks in advance.