How to insert into aonther SQL Server table - sql

I have a table tblOriginal which looks like this (and has no primary key):
employeeId code amount date
-----------------------------
emp01 sbk 10000 201501
emp02 sbk 10002 201501
emp02 cka 2000 201501
emp03 sbk 10003 201501
emp04 sbk 10004 201501
emp01 sbk 20000 201502
emp01 dgr 5000 201502
emp02 sbk 20002 201501
emp02 qaw 2000 201502
emp02 dng 4000 201502
emp03 sbk 20003 201502
emp04 sbk 20004 201502
What I need to do get all data with the code sbk into a table named tblEmp (employeeId is the primary key) to look as follows:
employeeId code amount date amount2 date2
----------------------------------------------
emp01 sbk 10000 201501 20000 201502
emp02 sbk 10002 201501 20002 201501
emp03 sbk 10003 201501 20003 201502
emp04 sbk 10004 201501 20004 201502
And then get the remaining data into another table named tblOthers to look as follows. Primary key is also employeeId
employeeId code amount date code2 amount2 date2
--------------------------------------------------
emp02 cka 2000 201501
emp01 dgr 5000 201502
emp02 qaw 2000 201502 dng 4000 201502
My problem is I don't know how to take data which has the same name into a employeeId into the tables and store the data into the columns
Any help will be appreciate.
All tables have been created

if there is absolutely 2 amount you can use some thing like this for the other table
;with temp as (
select empemployeeId,
lag(amount,1,null) over (partition by empemployeeId ORDER BY date ASC) as amount,
lag(date,1,null) over (partition by empemployeeId ORDER BY date ASC) as date,
lag(amount,1,null) over (partition by empemployeeId ORDER BY date DESC) as amount2,
lag(date,1,null) over (partition by empemployeeId ORDER BY date DESC) as date2,
from tblOriginal
)
select empemployeeId,'sdk',amount,date,amount2,date2 into tblEmp from temp to
where to.code='sdk'

Please refer this link :
http://www.w3schools.com/sql/sql_and_or.asp
SQL AND & OR operators are used to filter records based on more than one condition.
AND Operator Example -
SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';
OR Operator Example -
SELECT * FROM Customers WHERE City='Berlin' OR City='München';
Combining AND & OR -
SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR City='München');

Related

Create a view on multiple row conditions

I have a Cycles table like this:
Value Name Month
10 P00006 201412
20 P00006 201501
30 P00006 201502
100 P00007 201509
200 P00007 201510
300 P00007 201511
I would like to create a view based on this table where for each Name I sum the values of the previous months.
This the expected result :
AccruedValue Name Month
10 P00006 201412
30 P00006 201501
60 P00006 201502
100 P00007 201509
300 P00007 201510
600 P00007 201511
I am not sure this can be achieved without a using a cursor so any help will be appreciated.
Test data:
create table cycles
(value NUMBER, name VARCHAR2(250), month VARCHAR2(250));
/
INSERT INTO cycles VALUES(10 , 'P00006','201412');
INSERT INTO cycles VALUES(20 , 'P00006','201501');
INSERT INTO cycles VALUES(30 , 'P00006','201502');
INSERT INTO cycles VALUES(100 , 'P00007','201509');
INSERT INTO cycles VALUES(200 , 'P00007','201510');
INSERT INTO cycles VALUES(300 , 'P00007','201511');
View my_view:
create or replace view my_view as
select
sum(value) over (partition by name order by month ) as AccruedValue ,
name ,
month
from cycles;
Result:
select * from v;
10 P00006 201412
30 P00006 201501
60 P00006 201502
100 P00007 201509
300 P00007 201510
600 P00007 201511

Finding a minimum date before another date

Let's say I have two tables. One is a table with information about customer service inquiries, which contains information about the customer and the time the inquiry was placed. The customer's information (in this case, the ID) is saved for all future inquiries.
CUST_ID INQUIRY_ID INQUIRY_DATE
001 34 2015-05-03 08:15
001 36 2015-05-05 13:12
002 39 2015-05-10 18:43
003 42 2015-05-12 14:58
003 46 2015-05-14 07:27
001 50 2015-05-18 19:06
003 55 2015-05-20 11:40
The other table contains information about the resolution dates for all customer inquiries.
CUST_ID RESOLVED_DATE
001 2015-05-06 12:54
002 2015-05-11 08:09
003 2015-05-14 19:37
001 2015-05-19 16:12
003 2015-05-22 08:40
The resolution table doesn't have a key to link to the inquiry table other than the CUST_ID, so in order to calculate the time to resolution, I want to determine the minimum inquiry date before the resolution for EACH resolution date. The resulting table would look like this:
CUST_ID FIRST_INQUIRY RESOLVED_DT
001 2015-05-03 08:15 2015-05-06 12:54
001 2015-05-18 19:06 2015-05-19 16:12
002 2015-05-10 18:43 2015-05-11 08:09
003 2015-05-12 14:58 2015-05-14 19:37
003 2015-05-20 11:40 2015-05-22 08:40
At first I just went with min(case when INQUIRY_DATE < RESOLVED_DT), but for people like customers 001 and 003 who have multiple inquiries across different dates, the query would just return the first ever inquiry date, not the first since the last inquiry. Does anyone know how to do this? I'm using Netezza.
One option is to create a subquery for each table (inquries and resolutions) which numbers the transaction for each CUST_ID using the date. Then, the two subqueries can be joined together using this ordered index column along with the CUST_ID.
I also used the INQUIRY_ID in the inquiries table to break a tie, should it occur. There is not way to break a tie in the resolutions table for a given customer and date based on the data you showed us.
SELECT t1.CUST_ID, t1.INQUIRY_ID AS FIRST_INQUIRY, t2.RESOLVED_DATE AS RESOLVED_DT
FROM
(
SELECT CUST_ID, INQUIRY_ID, INQUIRY_DATE,
(SELECT COUNT(*) + 1
FROM inquiries
WHERE CUST_ID = t.CUST_ID AND INQUIRY_DATE <= t.INQUIRY_DATE
AND INQUIRY_ID < t.INQUIRY_ID) AS index
FROM inquiries AS t
) AS t1
INNER JOIN
(
SELECT CUST_ID, RESOLVED_DATE,
(SELECT COUNT(*) + 1
FROM resolutions
WHERE CUST_ID = t.CUST_ID AND RESOLVED_DATE < t.RESOLVED_DATE) AS index
FROM resolutions t
) AS t2
ON t1.CUST_ID = t2.CUST_ID AND t1.index = t2.index
Here are what the subquery tables look like:
inquiries:
CUST_ID INQUIRY_ID INQUIRY_DATE index
001 34 2015-05-03 08:15 1
001 36 2015-05-05 13:12 2
002 39 2015-05-10 18:43 1
003 42 2015-05-12 14:58 1
003 46 2015-05-14 07:27 2
001 50 2015-05-18 19:06 3
003 55 2015-05-20 11:40 3
resolutions:
CUST_ID RESOLVED_DATE index
001 2015-05-06 12:54 1
002 2015-05-11 08:09 1
003 2015-05-14 19:37 1
001 2015-05-19 16:12 2
003 2015-05-22 08:40 2
Note that this solution is not robust to missing data, e.g. there is an inquiry which was not closed, or the resolution was never recorded.

Select for take opening balance for last date of month and sum of other amount in whole month

I have table balance_detail
sales_period sales_date opening_amt sales_amt payment_amt closing_amt
-----------------------------------------------------------------------------
201501 01-01-2015 210 100 110
201501 02-01-2015 110 300 280 130
201501 03-01-2015 130 50 80
201501 05-01-2015 80 600 670 10
201502 02-02-2015 10 160 100 70
201502 15-02-2015 70 100 170 0
And Want result like this
sales_period opening_amt sales_amt payment_amt closing_amt
-----------------------------------------------------------------------------
201501 80 1110 1110 10
201502 70 260 270 0
One method is to use conditional aggregation along with row_number():
select sales_period,
max(case when seqnum = 1 then opening_amt end) as opening_amt,
sum(sales_amt) as sales_amt,
sum(payment_amt) as payment_amt,
max(case when seqnum = 1 then closing_amt end) as closing_amt
from (select bd.*,
row_number() over (partition by sales_period order sales_date desc) as seqnum
from balance_detail bd
) bd
group by sales_period;

Compute sum of hours/day with overlapping periods, SQL

I have a table with the start date(event.startdate), end date(event.enddate), and the hours/person (event.hrday) of an event. I have another table with weekdays listed which has another field for each person (calendar.name). I want to populate those columns with the total hours worked each day. I can't seem to figure out how to properly sum the hours if two events overlap in dates, I can only come up with a correct value for a single event in a time period.
I believe that in theory this question has the answer I need: compute sum of values associated with overlapping date ranges
But I am very new to SQL and I don't fundamentally understand the solution posted even after some additional research. I am using Access 2013. Apologies if this is a super elementary question, I was hoping what I wanted to do could be handled "visually" with Access...
What I Have: ("event" table)
Startdate | Enddate | Hrsday | Name
5/1/2015 5/12/2015 1.25 Joe
5/7/2015 5/8/2015 8 Joe
What I'm looking for:("calendar" table, days already filled in first column)
Weekdays | Joe | name2 | name3 | ....
5/1/2015 1.25
5/4/2015 1.25
5/5/2015 1.25
5/6/2015 1.25
5/7/2015 9.25
5/8/2015 9.25
5/11/2015 1.25
5/12/2015 1.25
I've tried using the query builder within access to build an UPDATE query, but my result either does not appear at all (no updates, all null) or will only fill in one event with no overlaps. (5/1-5/12 all have 1.25).
I think you will need to create a "date table" if you want to be able to achieve this sort of result in MS-Access (without using windowed functions).
Here is a quick example of how this might work in SQL Server, but only using syntax available to MS-Access (I hope).
--Load the test data into a table variable
DECLARE #event TABLE (
[start_date] DATE,
end_date DATE,
hrsperday NUMERIC(19,2),
name VARCHAR(20));
INSERT INTO #event SELECT '20150401', '20150412', 1.25, 'Joe';
INSERT INTO #event SELECT '20150407', '20150408', 8, 'Joe';
--Add some more test data, to make it more "interesting"
INSERT INTO #event SELECT '20150401', '20150405', 0.1, 'Bill';
INSERT INTO #event SELECT '20150401', '20150430', 7.5, 'Bill';
INSERT INTO #event SELECT '20150412', '20150415', 0.5, 'Bill';
--Make a date table, this creates one on the fly but wouldn't work in MS-Access
--I store a date for each day in 2015/Apr, obviously I would want more dates eventually
DECLARE #dates TABLE (
[date] DATE);
WITH cte AS (
SELECT CONVERT(DATE, '20150401') AS [date]
UNION ALL
SELECT DATEADD(DAY, 1, [date]) FROM cte WHERE [date] < '20150430')
INSERT INTO
#dates
SELECT
[date]
FROM
cte OPTION (MAXRECURSION 0);
--Now the answer is trivial
SELECT
e.name,
d.[date],
SUM(hrsperday) AS hrs
FROM
#dates d
LEFT JOIN #event e ON d.[date] BETWEEN e.[start_date] AND e.end_date
GROUP BY
e.name,
d.[date]
ORDER BY
e.name,
d.[date];
--Note the format you want, but a PIVOT would give you this
--(I don't think PIVOT is supported by MS-Access though)
Results for this are:
name date hrs
Bill 2015-04-01 7.60
Bill 2015-04-02 7.60
Bill 2015-04-03 7.60
Bill 2015-04-04 7.60
Bill 2015-04-05 7.60
Bill 2015-04-06 7.50
Bill 2015-04-07 7.50
Bill 2015-04-08 7.50
Bill 2015-04-09 7.50
Bill 2015-04-10 7.50
Bill 2015-04-11 7.50
Bill 2015-04-12 8.00
Bill 2015-04-13 8.00
Bill 2015-04-14 8.00
Bill 2015-04-15 8.00
Bill 2015-04-16 7.50
Bill 2015-04-17 7.50
Bill 2015-04-18 7.50
Bill 2015-04-19 7.50
Bill 2015-04-20 7.50
Bill 2015-04-21 7.50
Bill 2015-04-22 7.50
Bill 2015-04-23 7.50
Bill 2015-04-24 7.50
Bill 2015-04-25 7.50
Bill 2015-04-26 7.50
Bill 2015-04-27 7.50
Bill 2015-04-28 7.50
Bill 2015-04-29 7.50
Bill 2015-04-30 7.50
Joe 2015-04-01 1.25
Joe 2015-04-02 1.25
Joe 2015-04-03 1.25
Joe 2015-04-04 1.25
Joe 2015-04-05 1.25
Joe 2015-04-06 1.25
Joe 2015-04-07 9.25
Joe 2015-04-08 9.25
Joe 2015-04-09 1.25
Joe 2015-04-10 1.25
Joe 2015-04-11 1.25
Joe 2015-04-12 1.25

SQL Filter the output based on decimal format

I'm using SQL Server 2008, and I have an output like this:
OrderNo Amount1 Amount2 Amount3 Amount4
----------------------------------------------------------------
20001 473.050000 24.080000 528.050000 63.040000
20002 473.052854 24.081236 528.054536 63.044256
20003 563.960000 35.220000 679.050000 95.780000
20004 563.963566 35.223569 679.052122 95.784569
20005 897.050000 96.960000 346.120000 33.940000
20006 897.052365 96.964568 346.121897 33.944544
20007 268.550000 19.660000 986.330000 81.550000
20007 268.551778 19.663655 986.333566 81.553365
How can I filter the output and remove the number with decimal format like "123.456789" and the remaining will be "123.450000" as shown below:
OrderNo Amount1 Amount2 Amount3 Amount4
----------------------------------------------------------------
20001 473.050000 24.080000 528.050000 63.040000
20003 563.960000 35.220000 679.050000 95.780000
20005 897.050000 96.960000 346.120000 33.940000
20007 268.550000 19.660000 986.330000 81.550000
Thanks,
If you want to check if a number has more than two non-zero digits then:
WHERE ROUND(Amount1,2) = Amount1
Use this accordingly for other fields.