Wednesday, July 10, 2013

[SQL Server] Count value once in 30 days

[SQL Server] Count value once in 30 days


Count value once in 30 days

Posted: 10 Jul 2013 06:05 AM PDT

Hello,If I have a customer respond to the same survey in 30 days more than once, I only want to count it once. Can someone show me code to do that please?[code="sql"]CustomerID SurveyId ResponseDatecust1 100 5/6/13Cust1 100 5/13/13Cust2 100 4/20/13Cust2 100 5/22/13[/code]Then output should be like this:[code="sql"]CustomerID SurveyId CountSurveyCust1 100 1Cust2 100 2[/code]

Dynamic WHERE statement if stored procedure parameter is null

Posted: 09 Jul 2013 11:05 PM PDT

I have the stored procedure with @StartDate and @EndDate parametersmy WHERE statement is using those parameters to filter out dataWHERE condition1 and SomeDate >= @StartDate and SomeDate <= @EndDate and condition2However when @StartDate or @EndDate is null or empty string then WHERE statement should look like belowWHERE condition 1 and condition2I was trying already to do it with CASE of IF statement but without success so far.

non identity auto increment field

Posted: 10 Jul 2013 02:19 AM PDT

Hi all, I"m very new to SQL and have been searching the forums but haven't found what I'm looking for. I need to create 2 additional auto increment fields in a table. We are using SQL 2008 R2. I think I need to use a trigger to do this, but I'm not sure exactly the best way to do this. Here is my table structure.Employee_Number is IDENTITY fieldFirst_NameLast_NameLibrary_Barcode - needs to be auto incrementFood_Barcode - - needs to be auto incrementThe 2 barcode fields need to be auto increment fields when a new record is added. I was thinking of having another table that would contain the last value used for these 2 fields and then create a tirgger when a new record was added that would read the values and add 1 and insert the new value into their respective barcode fields above and then update the table containing the last number used. I wasn't sure if this was the best method to perform what I'm looking for. The numbers for these fields are only added when a new record is created, they will never change.Any help would be appreciated.Kevin

Change row values to columns

Posted: 10 Jul 2013 05:24 AM PDT

Hello,I'm looking for some help with SQL code.I have a SQL query with many JOINS which returns three or more AccountIDs for a customer, so right now it's like this:[code="sql"]CustomerId CustomerName AcctId ManyOtherColumnsForCustomerCust1 Peter 123Cust1 Peter 345Cust1 Peter 567[/code]I want to change it to look like this:[code="sql"]CustomerId CustomerName AcctId1 AcctId2 AcctId3Cust1 Peter 123 345 567[/code]Can someone pelase help me with code to get data as in the 2nd output? Thanks

Distinct Keyword

Posted: 09 Jul 2013 11:40 PM PDT

I am wanting to add a distinct keyword to this statement and I am not sure how or where exactly to add it.[code="vb"] SELECT Location.[Stack ID], Location.Location, Location.[Move Date], Location.[Move Time]FROM LocationWHERE (((Location.[Move Date])=[Please enter the date])); [/code] I am needing to eliminate duplicates in the [Stack ID] fields within the Location table.

No comments:

Post a Comment

Search This Blog