반응형

Question :

 

You are given a table, Projects, containing three columns: Task_ID, Start_Date and End_Date. It is guaranteed that the difference between the End_Date and the Start_Date is equal to 1 day for each row in the table.

If the End_Date of the tasks are consecutive, then they are part of the same project. Samantha is interested in finding the total number of different projects completed.

Write a query to output the start and end dates of projects listed by the number of days it took to complete the project in ascending order. If there is more than one project that have the same number of completion days, then order by the start date of the project.

Sample Input

Sample Output

2015-10-28 2015-10-29
2015-10-30 2015-10-31
2015-10-13 2015-10-15
2015-10-01 2015-10-04


Explanation

The example describes following four projects:

  • Project 1: Tasks 1, 2 and 3 are completed on consecutive days, so these are part of the project. Thus start date of project is 2015-10-01 and end date is 2015-10-04, so it took 3 days to complete the project.
  • Project 2: Tasks 4 and 5 are completed on consecutive days, so these are part of the project. Thus, the start date of project is 2015-10-13 and end date is 2015-10-15, so it took 2 days to complete the project.
  • Project 3: Only task 6 is part of the project. Thus, the start date of project is 2015-10-28 and end date is 2015-10-29, so it took 1 day to complete the project.
  • Project 4: Only task 7 is part of the project. Thus, the start date of project is 2015-10-30 and end date is 2015-10-31, so it took 1 day to complete the project.

 

Answer :

 

select *
from(
    select MIN(val) as startdate, MAX(val) as enddate
    from(
        (select ROW_NUMBER() over(order by (select End_Date)) as ID, End_Date as val
        from Projects
        where End_Date not in (select Start_Date
        from Projects)

        union all

        select ROW_NUMBER() over(order by (select Start_Date)) as ID, Start_Date as val
        from Projects
        where Start_Date not in (select End_Date
        from Projects))
         ) Y
    group by ID
) Z
order by datediff(dd,startdate,enddate), startdate
반응형
반응형

파이썬에서 사용하는 함수 매개변수 종류에 대해 작성

 

파이썬에서는 아래 5가지 종류의 함수 매개변수를 선언해서 사용할 수 있다.

1. default arguments

2. keyword arguments

3. positional arguments

4. arbitrary positional arguments

5. arbitrary keyword arguments

 

1. default arguments

  • 기본 매개변수는 함수가 정의되는 동안 제공된는 값이다.
  • "=" 기호를 사용해 매개변수의 기본값을 설정한다.
  • 기본 매개변수는 함수 호출 동안 선택적으로 사용된다.
  • 함수 호출 시 기본 매개변수에 값을 제공하면 기본 매개변수에 덮어 씌여진다.
  • 함수는 기본 매개변수를 원하는 만큼 가질 수 있다. (갯수 제한 없음)
  • 기본값 설정이 필수적이지 않다.

예시)

def add(a, b=5, c=10):
	return (a+b+c)

위 예시는 b,c가 기본 매개변수로 설정되어 있는 것을 보여준다.

아래 함수 호출 예시를 참고

print(add(3)) #return 18 a = 3, b = 5, c = 10
print(add(3,4)) #return 17 a = 3, b = 4, c = 10
print(add(2,3,4)) #return 9 a = 2, b = 3, c =4

 

2. Keyword Arguments

  • key = value 형식으로 사용하는 매개변수
  • 함수에 정의된 매개변수 순서를 따르지 않아도 값이 통과된다.
  • 키워드 매개변수는 함수에 정의 된 매개변수랑 일치해야한다.
  • 함수 호출시 사용되는 매개변수 타입 중 하나이다.

예시)

def add(a,b=5,c=10):
    return (a+b+c)

위와 같이 함수를 선언한다.

아래 예시와 같이 함수 호출시 매개변수를 key=value 로 사용하는 것을 keyword arguments 라 한다.

정의된 매개변수 순서를 지키지 않고 매개변수 키 값만 알맞게 설정하여 호출 가능하다.

print (add(b=10,c=15,a=20)) #return 45
print (add(a=10)) #return 25

 

3. Positional Arguments

  • 위치 매개변수 함수 호출 시 매개변수 위치에 값을 대입하여 호출하는 것을 의미한다.

예시)

def add(a,b,c):
    return (a+b+c)
    
print (add(10,20,30)) #return 60
print (add(10,c=30,b=20)) #return 60

위 첫번째 print 문과 같이 매개변수 위치에 값을 설정하여 호출 가능하다.

두번째 print 문처럼 keyword 매개변수와 섞어서 사용이 가능하다.

 

위 세가지 매개변수 사용에 대해 주의할 점!

 

#1. default 매개변수는 non-defualt 매개변수보다 앞에 선언할 수 없다.
def add(a=5,b,c):
    return (a+b+c)
#Output:SyntaxError: non-default argument follows default argument

#2. keyword 매개변수는 positional 매개변수보다 앞에 선언할 수 없다.
def add(a,b,c):
    return (a+b+c)
print (add(a=10,3,4))
#Output:SyntaxError: positional argument follows keyword argument

#3. 선언되지 않은 keyword 매개변수는 사용불가
def add(a,b,c):
    return (a+b+c)
print (add(a=10,b1=5,c=12))
#Output:TypeError: add() got an unexpected keyword argument 'b1'

#4. 중복된 keyword 매개변수 사용 불가
def add(a,b,c):
    return (a+b+c)
print (add(a=10,b=5,b=10,c=12))
#Output:SyntaxError: keyword argument repeated

 

4. arbitrary positional arguments

  • 매개변수 앞에 *를 사용하여 선언한 매개변수
  • 매개변수 선언을 가변적으로 설정할 때 사용한다.
  • 매개변수를 list 타입과 동일하게 사용

예시)

def add(*b):
    result=0
    for i in b:
         result=result+i
    return result

print (add(1,2,3,4,5)) #return 15
print (add(10,20)) #return 30

위 예시처럼 add 함수에 매개변수를 가변적으로 설정하여 호출이 가능하다

 

5. arbitrary keyword arguments

  • 매개변수 앞에 **를 사용하여 선언한 매개변수
  • 4번과 같이 매개변수를 가변적으로 설정할 수 있고 key:value 매개변수를 dictionary 타입과 동일하게 사용

예시)

def fn(**a):
    for i in a.items():
        print (i)
fn(numbers=5,colors="blue",fruits="apple")
'''
return
('numbers', 5)
('colors', 'blue')
('fruits', 'apple')
'''

 

참고 사이트 : https://levelup.gitconnected.com/5-types-of-arguments-in-python-function-definition-e0e2a2cafd29

반응형

'프로그래밍 > Python' 카테고리의 다른 글

[Python]sqlalchemy 사용법  (0) 2022.12.07
[Python] flask_restful fields  (0) 2021.10.08
Single Linked list 구현  (0) 2018.03.22
창원시 버스 API 사용하기  (1) 2016.02.22
반응형

SP명세서 쿼리.sql
0.00MB
테이블명세서쿼리.sql
0.04MB

 

쿼리 바리로기로 등록하면 편한 쿼리

--단축키 쿼리
/*
도구 > 옵션 > 환경 > 키보드 > 쿼리 바로 가기
*/
--ctrl+3 : stored procedure, function 내용 보기
sp_helptext test_procedure

--ctrl+4 : 해당 문자열을 포함하는 프로시저 찾기
SELECT DISTINCT A.NAME FROM dbo.SYSOBJECTS AS A INNER JOIN dbo.SYSCOMMENTS AS B ON A.ID = B.ID WHERE A.TYPE = 'P' AND B.TEXT LIKE 

--ctrl+5 : 해당 컬럼명을 사용하는 테이블 찾기
SELECT T.name AS table_name, C.name AS column_name FROM sys.tables AS T INNER JOIN sys.columns AS C ON T.object_id = C.object_id WHERE C.name =
반응형

'Database > SQL Server' 카테고리의 다른 글

SQL 매월 마지막일 출력  (0) 2019.06.20
트랜잭션 격리수준과 with(nolock) 힌트  (0) 2019.04.29
B-TREE 구조  (0) 2018.11.15
SQL 실행계획  (0) 2018.11.12
[MSSQL] Table Column 속성 변경  (0) 2018.06.25

+ Recent posts