SQL Server 2012 - 查找重复的月份(字符串)但不同年份

时间:2017-09-14 13:54:32

标签: sql sql-server date

难以理解这一点。

我被要求创建一份报告,显示在去年同一个月注册的客户。

发票表看起来有点像这样:(无法弄清楚如何创建更好的表格)

invoiceid  customerid  monthinvoice  yearinvoice      
1          50          July           2016*  
2          51          July           2016  
3          52          July           2016*
4          53          July           2016
5          54          August         2016
6          50          July           2017*
7          51          August         2017
8          52          July           2017*
9          53          August         2017
10         54          September      2017

使用的唯一正确日期列是生成发票的日期和收到的付款日期。

标有*的记录是我唯一感兴趣的记录,我只想看到当我通过一个月作为参数时返回的2条记录(我将被要求显示8月份有多少客户续订了例如。如果第一张发票是2016年7月,而下一张发票是2017年8月,他们将被视为新客户,而不是续订(必须正好是12个月)) 1)50 2)52

任何帮助都非常感激。

5 个答案:

答案 0 :(得分:0)

类似这样的事情

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="dagger2.com.coordinatorlayoutdemo3.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/cover"
                    android:tint="#11000000"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.9" />

                <FrameLayout
                    android:id="@+id/frameLayout"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:layout_gravity="bottom"
                    android:background="@color/colorPrimary"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.3">

                    <LinearLayout
                        android:id="@+id/linearLayout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Maulik Dodia"
                            android:textSize="30sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:text="Android App Developer" />
                    </LinearLayout>
                </FrameLayout>

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.v7.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                app:cardElevation="8dp"
                app:contentPadding="16dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:lineSpacingExtra="8dp"
                    android:text="@string/str_lorem_ipsum"
                    android:textSize="18sp" />
            </android.support.v7.widget.CardView>
        </android.support.v4.widget.NestedScrollView>

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_anchor="@id/frameLayout">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <Space
                    android:layout_width="32dp"
                    android:layout_height="32dp" />

                <TextView
                    android:id="@+id/tvTitleToolBar"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="10dp"
                    android:gravity="center_vertical"
                    android:text="Maulik Dodia"
                    android:textSize="18sp" />
            </LinearLayout>
        </android.support.v7.widget.Toolbar>

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/imgAvatar"
            android:layout_width="@dimen/image_width"
            android:layout_height="@dimen/image_width"
            android:layout_gravity="center"
            android:src="@drawable/profile_avatar"
            app:civ_border_color="@android:color/white"
            app:civ_border_width="2dp"app:layout_behavior="dagger2.com.coordinatorlayoutdemo3.CustomBehaviourForAvatarImg" />

    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

答案 1 :(得分:0)

这是一种方法。首先,我们获得本月,本年度的所有发票,然后是上一年同月的发票。然后,我们使用HAVING过滤那些拥有记录的客户。

;with cte as(
select * 
from yourtable
where 
    (monthinvoice = @monthinvoice
    and yearinvoice = datepart(year,getdate()))
union 
select * 
from yourtable
where 
    (monthinvoice = @monthinvoice
    and yearinvoice = datepart(year,dateadd(year,-1,getdate()))))

select *
from cte
where customerid in (select customerid from cte group by customerid having count(invoiceid) > 1)

答案 2 :(得分:0)

我认为这应该适合你 -

SELECT I1.invoiceid, I1.customerid, I1.monthinvoice, I1.yearinvoice, I2.yearinvoice
FROM Invoice_table I1
INNER JOIN Invoice table I2
ON I1.customerid = I2.customerid
AND I1.monthinvoice = I2.monthinvoice
AND I1.yearinvoice = I2.yearinvoice + 1

答案 3 :(得分:0)

以下内容应该为您提供有关如何构建报告的一些想法。

<option disabled selected>Tipo de imóvel</option>

答案 4 :(得分:-1)

如果我正确地关注你的问题......

SELECT customerid FROM InvTblName T
   INNER JOIN (SELECT customerID
           FROM InvTblName     
           HAVING Z.invyear=T.invyear+1) Z
       ON T.invmonth=Z.invmonth