简体   繁体   English

SQL如果没有行,则插入行

[英]SQL if there is no row, insert row

Preface: I have a query that pulls sales data and measures against their target. 前言:我有一个查询,可根据他们的目标提取销售数据和度量。 The issue is that only people who have sold an item appears on the chart for a given month. 问题在于,只有给定月份销售商品的人才会出现在图表上。 Eventually everyone gets a sale and gets on the chart. 最终,每个人​​都获得了一笔交易并获得了成功。 To alleviate this issue, I have created a 2nd statement that assigns a dummy row of data (used as a placeholder) and made the datepart(M, SOLD_DATE) = to datepart(M, getdate()) so that as of the 1st everyone is on the board. 为了缓解此问题,我创建了第二条语句,该语句分配了一个虚拟数据行(用作占位符),并将datepart(M,SOLD_DATE)=设置为datepart(M,getdate()),以便从第一个开始在板上。

My question: How can I set it to remove the placeholder as people get added to the board? 我的问题:当人们被添加到董事会中时,如何设置它以删除占位符?

SELECT
    USER_ID,
    SOLD_DATE,
    DATEPART(M, SALE_ID)
    **Target Case Statement in a nutshell**:
    insert monthly target value to 1st instance of sold item
    else 0
    End as 'target_amt' -- this is summed and aggregated in excel chart.
FROM 
   Sales

UNION ALL

SELECT    
    USER_ID,
    DATEPART(M, getdate()) as SOLD_DATE,
    Sale_ID,
    TARGET_AMT,
FROM 
    Sales
WHERE
    Sale_ID = **this contains on made up sale_id per person as a place holder.

So you can see that as of the 1st of every month, The UNION statement adds an item for each person. 因此,您可以看到,从每个月1日起, UNION语句将为每个人添加一个项目。 Now how to remove it when someone makes a sale is the question. 现在的问题是当有人进行销售时如何将其删除。

I am thinking some sort of subquery with "not in" or "except". 我在想某种带有“不在”或“除外”的子查询。 Or perhaps something involving a case statement??? 或涉及案件陈述的事情???

I'd post the full query but it's a beast (8 pages) and uses a few CTE's to derive the top select statement. 我会发布完整的查询,但它是一个野兽(8页),并使用一些CTE来得出顶部的select语句。

You should select from your User table and left join against your sales table. 您应该从“用户”表中进行选择,并保留对您的销售表的联接。 By doing this, it solves the original problem that users aren't showing up when they haven't sold something. 通过这样做,它解决了最初的问题,即用户未售出商品时却没有出现。

I agree with SpectralGhost - the best way to solve this issue is to change the selection criteria for the original query so that it includes a join from both tables - people with no sales will show null entries. 我同意SpectralGhost的观点-解决此问题的最佳方法是更改​​原始查询的选择条件,以使其包括两个表的联接-没有销售的人将显示空条目。

However, if you want to do it the hard way, then a simple if exists should suffice for checking and removing dummy records when other sales records have been added. 但是,如果您想用困难的方式做到这一点,那么当添加了其他销售记录后,如果存在一个简单的if就足以检查并删除虚拟记录。 Or do you mean to say that within the statements for adding sales it should be checking, adding dummy records, or deleting dummy records if they already exist but a sale is being added? 或者您是说要在添加销售的语句中检查,添加虚拟记录或删除虚拟记录(如果它们已存在但正在添加销售)? Seems like trying to push on a string to me. 似乎想向我施加压力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM