SQL Server 2014 :: How To Repeat The Same Row Combination
Jan 23, 2015
I am working on a scenario where I would need to repeat the same row for previous MonthIDs, here's the example (snapshot attached):
CustomerIDMonthIDRSCIDFlag1Flag2Flag3
1232943456000
1233001234011
1233033542010
1233032345111
1233043542010
1233042345111
1233053542010
1233052345111
1233062345111
1233063542010
1233072345111
1233073542010
1233083542010
1233093542010
1.For MonthIDs 309 & 307, I would need to add same combinations to 4 prior months (for 309, I would need the 309 row as 308, 307, 306, 305 and for 307, I would need to add 306, 305, 304, 303).
2.From the snapshot, Green rows are added for 309 MonthID and Orange rows are added for 307.
3.While we are adding 309 row to prior to 4 months, if any prior month has different combination than 309, that same combination need to be repeated for prior months.
I was using the below code to achieve the same:
declare @SC_j as int = 0;
declare @SC_k as int = 4;
while @SC_j <= 4
begin
insert into #Test
select distinct dt.CustomerID
[code]....
But my code starts from 1st MonthID however I would want this to be starting from last MonthID which I am not able to achieve.
View 9 Replies
ADVERTISEMENT
Mar 10, 2015
I've the following two tables:
create table #temp1 (name varchar(5), id int)
insert into #temp1 (name, id)
(
select 'a', 5
union
select 'a', 8
[code]....
As a result I would need every name from #temp2, where both searchred id's (5 & 8) from #temp1 are included.In this example i would like to get 'e' as a result, because in #temp2 'e' has the id's 5, 8 and 25.I've tried using cross apply, but cross apply returns every Name that have one of the ids... in this case it would also return 'c'...
Selectdistinct b.name
from(
Selectdistinct name
, id
from#temp1
wherename = 'a'
) as a
cross join(
Selectdistinct name
, id
from#temp2
) as b
wherea.id = b.id
View 3 Replies
View Related
Jun 4, 2015
I need a query to produce permutation combination.
declare @t2 as table (tab varchar(100))
insert into @t2 values ('V')
insert into @t2 values ('VL')
insert into @t2 values ('1099')
insert into @t2 values ('VOI')
declare @t1 as table (tab varchar(100))
insert into @t1 values ('I')
insert into @t1 values ('U') from the above I need following output (attached output),
View 3 Replies
View Related
Sep 30, 2015
I have a scenario where I need to develop a stored proc to identify invalid input provided.
Following is a sample scenario
Create table product (ProductId varchar(10),SizeId int,ProductColor varchar(10));
insert into Product
select 'Prod1',10,'Black' union ALL
select 'Prod1',10,'BLue' union ALL
select 'Prod2',20,'Green' union ALL
select 'Prod2',10,'Black' ;
[Code] ....
In following TSql Code , Color and Size are optional. Both are provided as comma separated input. I have provided "bbc" as wrong color and "MM" as wrong size. I want to identify if color is invalid or size (MM is in valid for Black and Blue) and to set flag accordingly.
I had tried out join but it is not serving needs.
---===========================================
-- Sql
--============================================
DECLARE
@ProdId varchar(10),
@color varchar(max) = Null,
@size varchar(max) = Null
BEGIN
set @ProdId='Prod1';
[Code] .....
View 9 Replies
View Related
Dec 12, 2014
I have some data in the following format;
MYTABLE
DOC_NO // REV_NO // FILE_NAME
ABC123 // A // abc123.pdf
ABC123 // B // abc123_2.docx
ABC124 // A // abc124.xlsx
ABC124 // A // -
ABC125 // A // abc125.docx
ABC125 // C // abc125.jpg
ABC125 // C // abc125.docx
ABC125 // C // -
ABC126 // 0 // -
ABC127 // A1 // abc127.xlsx
ABC127 // A1 // abc127.pdf
I'm looking to select all rows where the DOC_NO and REV_NO appear only once.(i.e. the combination of the two values together, not any distinct value in a column)
I have written the sub query to filter the correct results;
SELECT DOC_NO, REV_NO FROM [MYTABLE]
GROUP BY DOC_NO, REV_NO
HAVING COUNT(*) =1
I now need to strip out the records which have no file (represented as "-" in the FILE_NAME field) and select the other fields (same table - for example, lets just say "ADD1", "ADD2" and "ADD3")
I was looking to put together a query like;
SELECT DOC_NO, REV_NO, FILE_NAME, ADD1, ADD2, ADD3 FROM [MYTABLE]
WHERE FILE_NAME NOT LIKE '-' AND DOC_NO IN
(SELECT DOC_NO, REV_NO FROM [MYTABLE]
GROUP BY DOC_NO, REV_NO
HAVING COUNT(*) =1)
But of course, DOC_NO alone being in the subquery select is not sufficient, as (ABC125 /A) is a unique combination, but (ABC125 /C) is not, but these results would be pulled in.
I also cannot simply add an additional "AND" clause on its own to make sure the REV_NO value appears in the subquery, because it is highly repetitive and would have to specifically match the DOC_NO)
What is the easiest way of ensuring that I only pull in the records where both the DOC_NO and REV_NO (combination) are unique, or is there a better way of putting this select together altogether?
View 9 Replies
View Related
Jun 19, 2007
Hi
I have recently upgraded from SQL 2000 to SQL 2005 and I'm getting the following problem, can you suggest me if this is a issue with SQL 2005 or suggest me an asnwer for this.
Below is the exception from my log file
The cursor type/concurrency combination is not supported.
com.microsoft.sqlserver.jdbc.SQLServerException: The cursor type/concurrency combination is not supported.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.<init>(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.<init>(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.prepareStatement(Unknown Source)
The following is the piece of code where the problem I'm assuming is happening, how can I correct it.
varStmt1 = varConnection.prepareStatement(varCitationSQL.toString(),ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Have tried using both JDC v1.1 and 1.2 but of no use.
View 5 Replies
View Related
Mar 5, 2015
I want to make a query with the result like this, where Seqno is the result query which has repeating sequence number for same Field1
Field1Field2SeqNo
BVFUSBVFUS011
BVFUSBVFUS021
BVFUSBVFUS031
BVFRPBVFRP012
BVFRPBVFRP022
[Code] ....
I want the result also no ordered by Field1. it just as the natural order of the table..
View 3 Replies
View Related
Jul 11, 2006
I would like to select data from two different fields within the same row and insert them into a new table in different rows.
For example:
Source table contains the fields: DateTime, FirstValue, SecondValue
Destination table would have the fields: DateTime, Value
Sample source data:
2006-01-01T00:00:00, 25.OO, 20.OO
2006-01-02T00:00:00, 28.00, 24.OO
Sample destination data (inserted):
2006-01-01T00:00:00, 25.00
2006-01-01T00:00:00, 20.00
2006-01-02T00:00:00, 28.00
2006-01-02T00:00:00, 24.00
Is this doable in T-SQL?
View 3 Replies
View Related
Jun 11, 2007
Are combination graphs possible in reporting services?
View 2 Replies
View Related
Jun 18, 2004
Hi, I have following table
BetId GameId
==== ======
500 108
500 109
501 108
501 109
501 110
502 108
502 109
My query would have the form: select BetId where GameId in(108,109)
from Bets then it has to get me BetId : 500 and 502.
Not 501,since this is different combination(108,109,110) ;)
View 3 Replies
View Related
Jun 18, 2004
Hi,
Following is my table:Bets
BetId GameID
500 108
500 109
501 108
501 109
501 110
502 108
502 109
I want BetId 500 and 502 to be returned as result if i give select
criteria where game id = 108,109.
Pls.Note: It should not return BetId 501 in the result, since it belongs to different combination(108,109,110).
Similarly if i give, select criteria where game id =(108,109,110) it should return
BetId 501.not the 500 and 502..which is different combination..
Hope i clarified my problem..pls help me in this regard.Thanks a lot...
View 2 Replies
View Related
May 12, 2014
I have one table and I am trying to accomplish two tasks, generate all possible combinations and the probability of these combinations occuring. My sample data is below:
Time of Incident
7AM
8AM
9AM
10AM
11AM
12PM
1PM
2PM
3PM
4PM
5PM
6PM
7PM
Location
Off-Site
OnSite PC
Off-Site Virtual
IssueStatic
CR
CR Unable to Hear caller
Caller Unable to Hear CR
Garbled Speech
Echo
Call disposition
Able to Handle Call
Caller Initiated Disconnect
Transfered to 5508
Transfered to CCV
Provider
AT&T U-verse
Cox Cable
Mercury
Time Warner
VA Network
View 9 Replies
View Related
May 13, 2014
I want a list of rows in one table where a combination of two columns aren't in another table. One column is character and the second column is date/time. This happens to be SQL 2005. My first attempt using only one column worked OK:
SELECT * FROM TABLEA
WHERE CUSTID NOT IN (SELECT CUSTNO FROM TABLEB)
But when I added the combination of customer number and sales date, it made the query "run away" - I had to cancel it and it didn't return anything:
SELECT * FROM TABLEA
WHERE CUSTID + CONVERT(VARCHAR, SLS_DATE, 101) NOT IN
(SELECT CUSTNO + CONVERT(VARCHAR, SALES_DATE, 101) FROM TABLEB)
I wonder what might be wrong, but also whether this is the correct approach to begin with.
View 3 Replies
View Related
Sep 1, 2002
Hi all,
I'm new to SQL Server 2000. There are certain record that always return a repeated data in a colomn.
I'm using a very simple sql command. Just select.
In quetry analyser it always return a repeated data.
Thanks for help
View 2 Replies
View Related
Jun 22, 2006
Hi all
I am new in the forum and in SQL
I have this code
Code:
SELECT
PersPlac.Name,
Zoom1.Descr,
Client.PersonID,
Client.Name AS CLIENT_NAME,ClientCu.Period,ClientCu.YearID,IVPeriod.PrdDescr,(ClientCu.DebitP/((InvVAT.VATVal+100)/100)),InvVAT.VATVal
FROM Client
RIGHT JOIN Salesman WITH (READUNCOMMITTED)
ON (Client.SalesmanAA = Salesman.AA)
LEFT JOIN PersPlac WITH (READUNCOMMITTED)
ON (Client.Place = PersPlac.Code)
LEFT JOIN Zoom1 WITH (READUNCOMMITTED)
ON (Client.Zoom1 = Zoom1.ID)
LEFT JOIN CLIENTCU WITH (READUNCOMMITTED)
ON (CLIENT.PERSONID=CLIENTCU.PERSONID)
RIGHT JOIN InvVAT WITH (READUNCOMMITTED)
ON (CLIENT.TAXCAT=InvVAT.TAXCAT)
RIGHT JOIN IVPeriod WITH (READUNCOMMITTED)
ON (ClientCu.Period = IVPeriod.PeriodID)
WHERE Salesman.Name='ΚΟΡΜΠΟΣ ΦΩΤΙΟΣ'
AND ClientCu.Period<>0
AND PersPlac.Name<>'Εικονική Γεωγρ. Περιοχή'
AND Client.Name='ΦΩΤΕΙΝΙΑ ΑΝΝΑ'
and InvVAT.VATVal in (13,19)
the code run fine but every line is turn back 4 times.
Why?
Thanks for the help
View 2 Replies
View Related
Jun 26, 2007
After finally overcoming a number of hurdles with LisaNicholls help, I've got one hurdle left that I can't seem to get around. A system I'm updating currently outputs attendance registers in HTML which is built using some complex ASP.NET code. To make this more manageable, I decided to try and handle this output in Reporting Services.
Whilst the output looks fairly inocious and simple, in theory it's been a nightmare to implement. Because it's inteded for print output, my intention was to output the report straight to PDF, however I'm encountering an issue trying to get some of the data to "repeat" on each of the PDF pages.
If you look at the following image; http://mparter.pwp.blueyonder.co.uk/images/register_page.png, you'll see 4 main areas.
The problem areas are the red, green and blue ones.
The unmarked bottom section is a matrix which pages fine on PDF.
The TEXBOXES in the red area can possibly be worked-around by using report parameters.
The TABLES in the blue and green areas are the main problem. I can't include them in a report header workaround because there could be multiple rows per table
So, back to my question, how do I get the three marked coloured areas to repeat on each "page" of the PDF?
View 3 Replies
View Related
Nov 14, 2007
Hi all,
I am designing a mailing label report. I put the client info. on a sub-report.
I want to use a parameter to input the label amount and sub-report will be repeated by that amount.
How can I implement this?
TIA
Micror
View 4 Replies
View Related
May 30, 2004
Hi there, my situation is
I have a table x with 3 filed
a nvarchar(100), b smalldatetime, c text(16)
. I want to create a view like this:
select a + ' ' + b + ' ' + c as all_field from x where all_field like %my_str%
So, I always get a message error said wrong datatype, how can i do, please help me.
View 2 Replies
View Related
May 10, 2015
I have this data as below. I need to find out the combination from the data and take a count of them
CREATE TABLE A
( nRef INT,
nOrd INT,
Token INT,
nML INT,
nNode INT,
sSymbol VARCHAR(50),
[code].....
if you can see, the rows with column nRefNo 1 and 3 are same i.e. with same combination of Symbol viz. Silver and Castorseed.
Hence the desired output will be
Symbol Count
Castorseed-Silver 8
How to get this combination together and then take count of them. Please note i will be dealing with more than 5 million rows.
View 1 Replies
View Related
Jun 19, 2015
I want to get "all combinations of the same count" for provided list of numbers.As an example, if I have a string 'NA,T1,T1a'
For this string, I want to generate following:
NA,T1,T1a
NA,T1a,T1
T1,NA,T1a
T1,T1a,NA
T1a,NA,T1
T1a,T1,NA
query to achieve the same or if the solution is already available, provide me with the links.
View 7 Replies
View Related
Oct 28, 2007
For a class, I need to write SQL statements for an assortment of problems and I'm stuck on the last few:
What is the most popular combination of 2 products that are ordered simultaneously? The most popular combination is defined as follows: Consider any combination of two products such as (LS200A, ATM50A). Looking at all orders, count the number of times these two products were present in an order. An order could of course have other products in it than just these two, so you are not limited to examining only orders with exactly two products. If you repeat this process for all possible combinations of two products, then that combination which was present the highest number of times in all orders is the most popular combination of two products that were ordered simultaneously. The quantities ordered are not relevant in this context.
The result of the query should look something like the following:
First Product ----- Second Product ------ Max Number of Times
--- LS200A --------- ATM50A ------------------- 7 -----------
I know little to no SQL, just the basics of it and my professor is pretty terrible at teaching it. I'm thinking I'll need to make a virtual table with a CREATE VIEW but I'm not sure where to go after that. I also need to write statements for the most popular combination of three products. If anyone could give me a hand, I would really appreciate it. Thanks a lot.
View 1 Replies
View Related
Jul 23, 2005
Having a brainfart....I need a query that returns a record count, based on two distinct fields.For example:Order Revision Customer001 1 Bob001 2 Bob002 1 John003 1 John004 1 John005 1 Bob006 1 Bob006 2 BobThe query on the above data should return a count of orders, regardless ofthe revision numbers (each order number should only be counted once).So WHERE Customer = 'Bob', it should return OrderCount = 3TIA!Calan
View 2 Replies
View Related
Aug 21, 2007
Hi,
I have a table with 3 columns and 20 million records.
first 2 columns have VARCHAR(4) data type and third column is VARCHAR(5000).
I put 3rd column under FULLTEXT and implement a normal INDEX on 1st column.
Now when i try to search
SELECT
TOP 20
col1,
col3
FROM
tbl
WHERE
col1 = '1234'
AND
CONTAINS(col3,'"market*"')
I am facing following problems
1- It hang for like 1 minute and give 2 records, whereas if i remove col1='1234' from where clause it take less than 1 second.
2- Some time it show criteria is too complex, although i am only requesting a single word in col3.
I am noob in FULL-TEXT but i have done all research in books, microsoft forum and Google and not getting any information.
Please assist.
View 4 Replies
View Related
May 9, 2008
Dear all,
I have the follwing procedure:
Code Snippet
ALTER PROCEDURE [dbo].[Read_Current_User]
-- Add the parameters for the stored procedure here
@LoginUserName varchar(50) OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- get current user name
SET @LoginUserName=(SELECT [NomosConfig].[dbo].[Users].[UserName]
,[NomosConfig].[dbo].[Users].[Id]
,[NomosConfig].[dbo].[Users].[FirstName]
,[NomosConfig].[dbo].[Users].[LastName]
,[NomosConfig].[dbo].[Users].[Email]
FROM [NomosConfig].[dbo].[Users]
WHERE [NomosConfig].[dbo].[Users].[LoggedIn]=1
END
How can I set my @LoginUserName output parameter to be a combination of fields [NomosConfig].[dbo].[Users].[FirstName] + [NomosConfig].[dbo].[Users].[LastName] ?
As a result I should get @LoginUserName = FirstName + ' ' + Lastname
Thanks
regards
serge
View 4 Replies
View Related
Mar 7, 2008
I am trying to calculate the work experiance of an employee. I am using the function DATEDIFF for that.
For eg:
Code SnippetDATEDIFF(yy,JoiningDate,GETDATE())
But this will display the date difference in years, if I use mm, instead of yy then I will get the result in months.
But I want to dislay the result as a combination of year and month, like if an employee has 13 months of experiance then i want to get the result as 1.1 years. Is that possible using SQL?
Please help. All your helps will be appriciated.
View 12 Replies
View Related
May 10, 2015
I have this data as below. I need to find out the combination from the data and take a count of them
CREATE TABLE A
( nRef INT,
nOrd INT,
Token INT,
nML INT,
nNode INT,
sSymbol VARCHAR(50),
nMessageCode INT
)
INSERT INTO A
( nReferenceNumber,nOrderNumber,nTokenNumber,nML,nNode,sSymbol,nMessageCode )
VALUES
(1, 101, 1001,0,2,'SILVER',13073),
[code]....
if you can see, the rows with column nRefNo 1 and 3 are same i.e. with same combination of Symbol viz. Silver and Castorseed. How to get this combination together and then take count of them. Please note i will be dealing with more than 5 million rows.
View 6 Replies
View Related
Sep 27, 2007
This may get brought up again and again, but I don't know how to deal with it by heart yet... in fact I mean to say, I don't know how to deal with it.
With the given data set how do you only return one of the 2 rows where a given user ate the same fruit?
apples gregpeach greglemon gregapples greg
peach brucepeach brucelemon bruceapples bruce
pear paulpeach paulplumb paulplumb paul
apples barbpeach barblemon barbpear barb
What T-SQL is needed to produce the following dataset?
apples gregpeach greglemon greg
peach brucelemon bruceapples bruce
pear paulpeach paulplumb paul
apples barbpeach barblemon barbpear barb
View 2 Replies
View Related
Oct 21, 2006
hi guys,
In reporting services i have a table and below which i have a chart. The table has lot of values so the report spans multiple pages. I have set page breaks such that the table displays only 10 records. After all the values have been displayed in the last page the chart is displayed. I want the chart to display in each and every page of the report. It is the same chart that i want to display. How do i do this?
plz can anyone give me ideas.
Thanks,
Sai Abhiram Bandhakavi
View 3 Replies
View Related
Jun 21, 2015
how this can be achieved by writing a procedure in SQL Server DB.Remember that if there is a non-null value in the benchmark that is not equal to the equivalent criteria in the Hosting input then there is no match. Therefore those with region = IN would not match any of the given benchmarks. It is best fit as long as the non-matching values are to null value benchmark criteria.
Hosting_Site (Source File)
YEAR MONTH HOSTING CLASS REGION HOSTING COST
2015 1 A UK 75
2015 1 A IN 80
2015 1 B IN 60
2015 1 C US 100
BENCHMARK (Lookup Table)
Row Year Month Hosting class Region BENCHMARK COST
1 2015 1 A US 100
2 2015 1 B US 200
3 2015 1 A UK 50
4 2015 1 A null 70
STANDARD CLASS COST (If the criteria between source file and the lookup file doesn't match then have to load this cost)
Row Year Hosting class BENCHMARK COST
1 2015 A 22
2 2015 B 33
Then result should be:
FACT LOAD:
YEAR MONTH HOSTING CLASS REGION HOSTING COST BENCHMARK COST
2015 1 A UK 75 50
2015 1 A IN 80 70
2015 1 B IN 60 33 (Since for B class there is no region = IN or = null the Standard Class Cost must be used)
2015 1 C US 100 ??? (Reject record since there is no default value in Standard Class Cost or in Benchmark)
View 13 Replies
View Related
Jul 23, 2005
I have a case where a table has two candidate primary keys,but either (but not both) may be NULL. I don't want to storea copy of the concatenated ISNULL'ed fields as an additionalcolumn, though that would work if necessary. Instead, I triedthe following (this is a related simplified example, not myreal one):CREATE FUNCTION ApplyActionPK(@IP int = NULL,@DNS varchar(64) = NULL)RETURNS varchar(74) -- NOT NULLASBEGINdeclare @val varchar(74)set @val = str(ISNULL(@IP, 0), 10)set @val = @val + ISNULL(@DNS, '')return @val-- Also tried "return str(ISNULL(@IP, 0), 10)+ISNULL(@DNS, '')"-- Also tried "return ISNULL(STR(@IP, 10), ISNULL(@DNS, ''))"-- ... and other things...ENDGOcreate table ApplyAction(-- An action applies to a computerAct varchar(16) NOT NULL,-- The action to applyIP int NULL,-- The computer IP address, orDNS varchar(64) NULL,-- The DNS name of the computerTarget as dbo.ApplyActionPK(ComputerID, DNS), -- PK value-- Also tried "Target as ISNULL(STR(@IP, 10), ISNULL(@DNS, ''))"CONSTRAINT PK_ApplyAction PRIMARY KEY(Act, Target))SQL Server always complains that the primary key constraint cannot becreated over a nullable field - even though in no case will the 'Target'field be NULL.Please don't explain that I should store an IP address as a string.Though that would suffice for this example, it doesn't solve myactual problem (where there are four nullable fields, two of whichare FKs into other tables).What's the reason for SQL Server deciding that the value is NULLable?What's the usual way of handling such alternate PKs?Clifford Heath.
View 7 Replies
View Related
Mar 9, 2007
I do not know where to post this strange problem.
I have a perfectly working application, accessing a SQL Express database, displaying data and images.
Now I am trying to add a Crystal Report. I add a new connection (OLE DB (ADO)) The connection points to my .MDF database. I drag the fields I want to print to the report, including an image. Now I click the Report Preview. I get a message saying "The database table "Table-Name" can not be found, Proceed to remove this table from the report?" Yes/No. I select NO, and the preview shows up correctly.
Happily I continue to execute the program in debug mode. At startup I get errors saying that login failed. To get around this error, I have to close down Visual Studio and then open it up again, running the program without touching the Crystal Report Design. Now the program starts up normaly without problems.
Now comes the real strange behavior. I click on my link to request the Crystal report and I get a request to login to the database. I try to login but nothing is accepted. I again close down Visual Studio and open up SQL Server Management Studio Express. I connect to SQL Express and expand "Databases" MY .MDF file is listed but the + sign in front of it is missing and I have to delete the database and attach it again to get it back.
Where is this problem occuring????? I have no idea. Please help.
View 6 Replies
View Related
Mar 8, 2012
I have table with some columns with no primary key..
I have to find the combination of columns which may the row unique...
How to do that like microsoft...
View 14 Replies
View Related
Apr 14, 2001
Instead of explaining my question here I've made a seperate
html page with a full explanation of my problem with pictures
and examples.
I've posted the question to this URL
http://miona.com/listmenus/
View 1 Replies
View Related