SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A,**sum(WO+WP+HL+A)**
from TRN_ATTN072013 WHERE CONVERT(varchar,Tdate,112)>'20130712' and CONVERT(varchar,Tdate,112)<'20130717'
group by emp_no
ORDER BY emp_no
i want to sum up this columns WO,WP,HL,A how do i sum these columns
If this is SQL server then you can use CTE
with tablesum as
(
SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A
from TRN_ATTN072013
WHERE CONVERT(varchar,Tdate,112) > '20130712' and CONVERT(varchar,Tdate,112) < '20130717'
group by emp_no
)
select t.*, t.WO + t.WP + t.HL + t.A
from tablesum t
order by t.emp_no
in mysql ;)
SELECT emp_no, wo+wp+hl+A FROM
(SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A,**sum(WO+WP+HL+A)**
from TRN_ATTN072013 WHERE CONVERT(varchar,Tdate,112)>'20130712' and CONVERT(varchar,Tdate,112)<'20130717'
group by emp_no
ORDER BY emp_no) AS SUB_Q;
Related
I'm triying to make a regex in .net to match nested expresion.
I wan't to extract a condition and value1 and value2 like this:
CASE WHEN condition THEN value1 ELSE value2 END
The string that i would like to match it's this:
CASE WHEN 5 + CASE WHEN 6 < 3 THEN 5 ELSE 2 END = 0 THEN 0 ELSE CASE WHEN 6 < 0 THEN 2 ELSE 1 END / (3 + CASE WHEN 5 < 0 THEN 2 ELSE 0 END) END
Formatted string:
CASE WHEN 5 +
CASE WHEN 6 < 3
THEN 5
ELSE 2
END = 0
THEN 0
ELSE
CASE WHEN 6 < 0
THEN 2
ELSE 1
END / (3 + CASE WHEN 5 < 0
THEN 2
ELSE 0
END)
END
I use a below Regex string, but only match without nested expresion:
(CASE WHEN )(?<cond>[\s\S]*?)( THEN )(?<val1>[\s\S]*?)( ELSE )(?<val2>[\s\S]*?)( END)
What regular expression need to extract the first "CASE WHEN" as below?
Condition: 5 + CASE WHEN 6 < 3 THEN 5 ELSE 2 END = 0
Value1: 0
Value2: CASE WHEN 6 < 0 THEN 2 ELSE 1 END / (3 + CASE WHEN 5 < 0 THEN 2 ELSE 0 END)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
This post was edited and submitted for review 21 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
This way actually work in C#. But it does not work in SQL Server when executed:
Select
t3.MNAME + cast(t3.N as nvarchar) as 'کد دسته بندي',
t3.NAME as 'شرح دسته',
sum(case when t1.DT = 1 then t1.N1 - t1.N2 else 0 end) as 'خريد',
sum(case when t1.DT = 8 then t1.N1 - t1.N2 else 0 end) as 'برگشت خريد',
'' as 'حواله شعب',
'' as 'برگشت شعب',
'' as 'فروش',
sum(case when t1.DT = 9 then t1.N1 - t1.N2 else 0 end) as 'برگشت فروش',
sum(case when t1.DT = 10 then t1.N1 - t1.N2 else 0 end) as 'ت کاردکس',
sum(N1 - N2) as 'موجودي',
sum(case when t1.DT = 4 then t1.N1 - t1.N2 else 0 end) as 'ح شعب',
sum(case when t1.DT = 5 then t1.N1 - t1.N2 else 0 end) as 'ح ت شعب',
sum(case when t1.DT = 6 then t1.N1 - t1.N2 else 0 end) as 'ب شعب',
sum(case when t1.DT = 7 then t1.N1 - t1.N2 else 0 end) as 'ب ت شعب',
sum(case when t1.DT = 2 then t1.N1 - t1.N2 else 0 end) as 'ح ت فروش',
sum(case when t1.DT = 3 then t1.N1 - t1.N2 else 0 end) as 'فروش جزئي',
sum(case when t1.DT = 12 then t1.N1 - t1.N2 else 0 end) as 'فروش عادي',
sum(case when t1.DT = 11 then t1.N1 - t1.N2 else 0 end) as 'برگشت فروش تعدادي'
From
BC as t1, _TBS_TYPE as t3
Where
substring(t1.BARCODE, 1, 1) = t3.MNAME
and substring(t1.BARCODE, 2, 1) = cast(t3.N as nvarchar)
and t3.MNAME + cast(t3.N as nvarchar) and t3.NAME
group by
t3.MNAME, t3.N, t3.NAME
order by
2, 3
Error :
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
Try the below - without assigning some value you define a condition like - and t3.MNAME + cast(t3.N as nvarchar) and t3.NAME
Also, it is best to use explicit JOIN
Select t3.MNAME+cast(t3.N as nvarchar) as 'کد دسته بندي',
t3.NAME as 'شرح دسته',
sum(case when t1.DT = 1 then t1.N1-t1.N2 else 0 end) as 'خريد',
sum(case when t1.DT = 8 then t1.N1-t1.N2 else 0 end) as 'برگشت خريد',
'' as 'حواله شعب',
'' as 'برگشت شعب',
'' as 'فروش',
sum(case when t1.DT = 9 then t1.N1-t1.N2 else 0 end) as 'برگشت فروش',
sum(case when t1.DT = 10 then t1.N1-t1.N2 else 0 end) as 'ت کاردکس',
sum(N1-N2) as 'موجودي' ,
sum(case when t1.DT = 4 then t1.N1-t1.N2 else 0 end) as 'ح شعب',
sum(case when t1.DT = 5 then t1.N1-t1.N2 else 0 end) as 'ح ت شعب',
sum(case when t1.DT = 6 then t1.N1-t1.N2 else 0 end) as 'ب شعب',
sum(case when t1.DT = 7 then t1.N1-t1.N2 else 0 end) as 'ب ت شعب',
sum(case when t1.DT = 2 then t1.N1-t1.N2 else 0 end) as 'ح ت فروش',
sum(case when t1.DT = 3 then t1.N1-t1.N2 else 0 end) as 'فروش جزئي',
sum(case when t1.DT = 12 then t1.N1-t1.N2 else 0 end) as 'فروش عادي',
sum(case when t1.DT = 11 then t1.N1-t1.N2 else 0 end) as 'برگشت فروش تعدادي'
From BC as t1 inner join _TBS_TYPE as t3
on substring(t1.BARCODE,1,1)=t3.MNAME and substring(t1.BARCODE,2,1)=cast(t3.N as nvarchar)
group by t3.MNAME,t3.N,t3.NAME
order by 2,3
In Where condition filter right side of the value is missing in your code.
Where
substring(t1.BARCODE, 1, 1) = t3.MNAME
and substring(t1.BARCODE, 2, 1) = cast(t3.N as nvarchar)
and t3.MNAME + cast(t3.N as nvarchar) and t3.NAME -------->Missing code here
I am trying to do a project which uses Visifire to display graphed data. It takes in a single DataTable. I need to create an SQL query that searches a date column for a particular month (format is: Friday, 03 November 2017) and then stores how many times that month is repeated. It has to do this for all 12 months. This is done in C# in visual studio. Currently, it displays no data on the line graph.
public DataTable ReadDataMonthlyBooked()
{
// Declare references (for table, reader and command)
DataTable monthlyBookedTable = new DataTable();
SqlDataReader reader;
SqlCommand command;
string selectString = "SELECT COUNT (BookingNum) AS Bookings "
+ "FROM Booking "
+ "WHERE Booking.EndDate LIKE 'January' "
+ "AND Booking.EndDate LIKE 'February' "
+ "AND Booking.EndDate LIKE 'March' "
+ "AND Booking.EndDate LIKE 'April' "
+ "AND Booking.EndDate LIKE 'May' "
+ "AND Booking.EndDate LIKE 'June' "
+ "AND Booking.EndDate LIKE 'July' "
+ "AND Booking.EndDate LIKE 'August' "
+ "AND Booking.EndDate LIKE 'September' "
+ "AND Booking.EndDate LIKE 'October' "
+ "AND Booking.EndDate LIKE 'November' "
+ "AND Booking.EndDate LIKE 'December'";
try
{
// Create a new command
command = new SqlCommand(selectString, cnMain);
// open the connection
cnMain.Open();
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
// read data from readerObject and load in table
monthlyBookedTable.Load(reader);
reader.Close(); //close the reader
cnMain.Close(); //close the connection
return monthlyBookedTable;
}
catch (Exception ex)
{
return (null);
}
}
This is the current SQL query I have and I hope its hopelessly wrong with logic errors but I do not know SQL well enough.
select 'Friday, 03 November 2017'
select cast('03 November 2017' as date)
select SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100)
select cast(SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100) as date)
select datepart(month, cast(SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100) as date))
select datepart(month, cast(Booking.EndDate, CHARINDEX(',', Booking.EndDate) + 2, 100) as date))
, COUNT (BookingNum) AS Bookings "
from booking
group by datepart(month, cast(Booking.EndDate, CHARINDEX(',', Booking.EndDate) + 2, 100) as date))
You'll need a group by, like this
SELECT COUNT(BookingNum) as Bookings, MONTH(EndDate) as Month
FROM Booking
GROUP BY MONTH(EndDate)
But as your EndDate is a varchar, you'll have to extract the month
SELECT COUNT(BookingNum) as Bookings
, SUBSTRING(EndDate,
CHARINDEX(',',EndDate) + 4,
CHARINDEX(' ',EndDate, CHARINDEX(',',EndDate) + 5) - CHARINDEX(',',EndDate) + 4)
FROM Booking
GROUP BY SUBSTRING(EndDate,
CHARINDEX(',',EndDate) + 4,
CHARINDEX(' ',EndDate, CHARINDEX(',',EndDate) + 5) - CHARINDEX(',',EndDate) + 4)
There may be a better way to do this (cursors maybe?) but basically you want to get the location of the first ',' and add 4 to get the month (assuming the date is always 2 digits like in your example) and then use that as a start index for a search of the first blank (that'll be the end of the month name) and substract the first index we found to get the length fo the month.
So, basically, just change the date to a DATE field
or cast it
SELECT COUNT(BookingNum) as Bookings, MONTH(CONVERT(DATE, EndDate)) as Month
FROM Booking
GROUP BY MONTH(CONVERT(DATE, EndDate))
But I don't know if the format you are using will be interpreted correctly by the engine.
The error is in your SQL statement.
SELECT
COUNT(BookingNum) AS Bookings
FROM
Booking
WHERE
Booking.EndDate LIKE '%January%',
AND Booking.EndDate LIKE '%February%',
AND Booking.EndDate LIKE '%March%',
AND Booking.EndDate LIKE '%April%',
AND Booking.EndDate LIKE '%May%',
AND Booking.EndDate LIKE '%June%',
AND Booking.EndDate LIKE '%July%',
AND Booking.EndDate LIKE '%August%',
AND Booking.EndDate LIKE '%September%',
AND Booking.EndDate LIKE '%October%',
AND Booking.EndDate LIKE '%November%',
AND Booking.EndDate LIKE '%December%',
You try to get a record where the month is every month in the year at the same time.
If I'm right, you try to get the number of bookings per month.
So it should look something much more like
SELECT
SUM(CASE WHEN Booking.EndDate LIKE '%January%' THEN 1 ELSE 0 END) AS JanuaryBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%February%' THEN 1 ELSE 0 END) AS FebruaryBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%March%' THEN 1 ELSE 0 END) AS MarchBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%April%' THEN 1 ELSE 0 END) AS AprilBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%May%' THEN 1 ELSE 0 END) AS MayBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%June%' THEN 1 ELSE 0 END) AS JuneBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%July%' THEN 1 ELSE 0 END) AS JulyBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%August%' THEN 1 ELSE 0 END) AS AugustBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%September%' THEN 1 ELSE 0 END) AS SeptemberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%October%' THEN 1 ELSE 0 END) AS OctoberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%November%' THEN 1 ELSE 0 END) AS NovemberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%December%' THEN 1 ELSE 0 END) AS DecemberBookings
FROM
Booking
I tried to convert Int to Varchar but I'm unable to get output. Can anyone suggest any other way to do so?
I have below query and need to cast StoreNo (int) to a varchar:
ALTER PROCEDURE [dbo].[getrevenue]
#date1 DATE,
#date2 DATE,
#StoreNo NVARCHAR(max)
AS
BEGIN
DECLARE #sql_test NVARCHAR(max)
--SET #StoreNo='68,78,104'
SET #sql_test = 'SELECT t1.transtoreno AS StoreNO
,t3.NAME AS NAME
,t1.Dealdate AS DATE
,t1.UKEI AS UKEI
,t2.SubTotal AS SubTotal
,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
,t2.ValueInquiries AS TotalDiscount
,t2.NetSale AS Netsale
,t2.TotalSale AS ToatlSale
,t2.Cash AS Cash
,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
,ISNULL(t2.Card, 0) AS Card
,ISNULl(t2.Suica, 0) AS Suica
,t2.WONPOINT AS WAONPOINT
,t1.TaxExemption AS TAXExemption
,t2.TaxTotal AS TaxTotal
,t2.Returngoods AS Returngoods
,t2.Regiminus AS RegiMinus
,t2.PrintRecipt AS printrecipt
,ISNULL(t1.Visitorcount, 0) AS VisitorCount
FROM (
SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
,(DealDate) AS Dealdate
,SUM(SalePrice) AS UKEI
,SUM(TansuNebikiPrice) AS twoeyesdicount
,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
,Sum(RegiMinusNo) AS Receiptissue
,SUM(VisitorCount) AS Visitorcount
FROM POS_TtlTran
GROUP BY StoreNo
,DealDate
) t1
LEFT OUTER JOIN (
SELECT DATE AS D
,cast(StoreNo AS NVARCHAR) AS s
,SUM(CASE
WHEN SerialNo LIKE 23
THEN DayTotalAmt
ELSE 0
END) AS Cash
,SUM(CASE
WHEN SerialNo LIKE 31
THEN DayTotalAmt
ELSE 0
END) AS Card
,SUM(CASE
WHEN SerialNo LIKE 30
THEN DayTotalAmt
ELSE 0
END) AS GiftVochuer
,SUM(CASE
WHEN SerialNo LIKE 138
THEN DayTotalAmt
ELSE 0
END) AS Returngoods
,SUM(CASE
WHEN SerialNo LIKE 160
THEN DayTotalAmt
ELSE 0
END) AS PrintRecipt
,SUM(CASE
WHEN SerialNo LIKE 304
THEN DayTotalAmt
ELSE 0
END) AS Suica
,SUM(CASE
WHEN SerialNo LIKE 26
THEN DayTotalAmt
ELSE 0
END) AS WONPOINT
,SUM(CASE
WHEN SerialNo LIKE 139
THEN DayTotalAmt
ELSE 0
END) AS Regiminus
,SUM(CASE
WHEN SerialNo LIKE 4
THEN DayTotalAmt
ELSE 0
END) AS SubToTal
,SUM(CASE
WHEN SerialNo LIKE 7
THEN DayTotalAmt
ELSE 0
END) AS SubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 8
THEN DayTotalAmt
ELSE 0
END) AS TwoeyesubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 18
THEN DayTotalAmt
ELSE 0
END) AS ValueInquiries
,SUM(CASE
WHEN SerialNo LIKE 22
THEN DayTotalAmt
ELSE 0
END) AS TotalSale
,SUM(CASE
WHEN SerialNo LIKE 114
THEN DayTotalAmt
ELSE 0
END) AS TaxTotal
,SUM(CASE
WHEN SerialNo LIKE 2
THEN DayTotalAmt
ELSE 0
END) AS NetSale
FROM POS_FinTtl
GROUP BY StoreNo
,DATE
) t2 ON t1.transtoreno = t2.s
AND t1.Dealdate = t2.D
LEFT OUTER JOIN (
SELECT StoreNo AS No
,StoreName AS NAME
FROM Store
) t3 ON t2.s = t3.No
WHERE (
t1.transtoreno IN ('''
+ CAST(#StoreNo AS NVARCHAR(max)) + ''')
AND (t1.Dealdate between ''' + CAST(#date1 AS VARCHAR(30)) + '''
AND ''' + CAST(#date2 AS VARCHAR(30)) + '''))'
END
Hope this one will work
ALTER PROCEDURE [dbo].[getrevenue] #date1 DATE
,#date2 DATE
,#StoreNo NVARCHAR(max) `
AS
BEGIN
DECLARE #sql_test NVARCHAR(max)
--SET #StoreNo='68,78,104'
SET #sql_test ='SELECT t1.transtoreno AS StoreNO
,t3.NAME AS NAME
,t1.Dealdate AS DATE
,t1.UKEI AS UKEI
,t2.SubTotal AS SubTotal
,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
,t2.ValueInquiries AS TotalDiscount
,t2.NetSale AS Netsale
,t2.TotalSale AS ToatlSale
,t2.Cash AS Cash
,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
,ISNULL(t2.Card, 0) AS Card
,ISNULl(t2.Suica, 0) AS Suica
,t2.WONPOINT AS WAONPOINT
,t1.TaxExemption AS TAXExemption
,t2.TaxTotal AS TaxTotal
,t2.Returngoods AS Returngoods
,t2.Regiminus AS RegiMinus
,t2.PrintRecipt AS printrecipt
,ISNULL(t1.Visitorcount, 0) AS VisitorCount
FROM (
SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
,(DealDate) AS Dealdate
,SUM(SalePrice) AS UKEI
,SUM(TansuNebikiPrice) AS twoeyesdicount
,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
,Sum(RegiMinusNo) AS Receiptissue
,SUM(VisitorCount) AS Visitorcount
FROM POS_TtlTran
GROUP BY StoreNo
,DealDate
) t1
LEFT OUTER JOIN (
SELECT DATE AS D
,cast(StoreNo AS NVARCHAR) AS s
,SUM(CASE
WHEN SerialNo LIKE 23
THEN DayTotalAmt
ELSE 0
END) AS Cash
,SUM(CASE
WHEN SerialNo LIKE 31
THEN DayTotalAmt
ELSE 0
END) AS Card
,SUM(CASE
WHEN SerialNo LIKE 30
THEN DayTotalAmt
ELSE 0
END) AS GiftVochuer
,SUM(CASE
WHEN SerialNo LIKE 138
THEN DayTotalAmt
ELSE 0
END) AS Returngoods
,SUM(CASE
WHEN SerialNo LIKE 160
THEN DayTotalAmt
ELSE 0
END) AS PrintRecipt
,SUM(CASE
WHEN SerialNo LIKE 304
THEN DayTotalAmt
ELSE 0
END) AS Suica
,SUM(CASE
WHEN SerialNo LIKE 26
THEN DayTotalAmt
ELSE 0
END) AS WONPOINT
,SUM(CASE
WHEN SerialNo LIKE 139
THEN DayTotalAmt
ELSE 0
END) AS Regiminus
,SUM(CASE
WHEN SerialNo LIKE 4
THEN DayTotalAmt
ELSE 0
END) AS SubToTal
,SUM(CASE
WHEN SerialNo LIKE 7
THEN DayTotalAmt
ELSE 0
END) AS SubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 8
THEN DayTotalAmt
ELSE 0
END) AS TwoeyesubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 18
THEN DayTotalAmt
ELSE 0
END) AS ValueInquiries
,SUM(CASE
WHEN SerialNo LIKE 22
THEN DayTotalAmt
ELSE 0
END) AS TotalSale
,SUM(CASE
WHEN SerialNo LIKE 114
THEN DayTotalAmt
ELSE 0
END) AS TaxTotal
,SUM(CASE
WHEN SerialNo LIKE 2
THEN DayTotalAmt
ELSE 0
END) AS NetSale
FROM POS_FinTtl
GROUP BY StoreNo
,DATE
) t2 ON t1.transtoreno = t2.s
AND t1.Dealdate = t2.D
LEFT OUTER JOIN (
SELECT StoreNo AS No
,StoreName AS NAME
FROM Store
) t3 ON t2.s = t3.No
WHERE (
t1.transtoreno IN ('''
+ CONVERT(varchar(MAX), #StoreNo) + ''')
AND (t1.Dealdate between ''' + CAST(#date1 AS VARCHAR(30)) + '''
AND ''' + CAST(#date2 AS VARCHAR(30)) + '''))'
END
where (t1.transtoreno IN ('''
+ CAST(#StoreNo AS nvarchar(max)) + ''')
Should be
where (t1.transtoreno IN ('''
+ replace(CAST(#StoreNo AS nvarchar(max)),',',''',''') + ''')
I have a RichTextBox that contains:
CAP-00128G 0402C
CAP-00128G 0402C
CAP-00129G TANT3715
CAP-00129G TANT3715
CAP-00129G TANT3715
CAP-00129G TANT3715
CAP-00130G 12BGA_1.5
CAP-00151G ABC
IND-00007G 0402C
RES-00455G 0402R
RES-00455G 0402R
RES-00455G 0402R
I would like to insert the 2nd column value into the first set of quotation marks in a file that looks like this (accounting for running into the same value in the second set of quotations, ie, CAP-22128G, CAP-00129G, etc.) :
( 1 0 1 1 0 0 0 0 "" "CAP-00128G" "" 2 0 0 0 0 0 )
( 2 0 1 1 0 0 0 0 "" "CAP-00129G" "" 4 0 0 0 0 0 )
( 3 0 1 1 0 0 0 0 "" "CAP-00130G" "" 1 0 0 0 0 0 )
( 4 0 1 1 0 0 0 0 "" "CAP-00151G" "" 1 0 0 0 0 0 )
( 5 0 1 1 0 0 0 0 "" "IND-00007G" "" 1 0 0 0 0 0 )
( 6 0 1 1 0 0 0 0 "" "RES-00455G" "" 3 0 0 0 0 0 )
SO the final text would look like this after matching the values from the strings in the first file to their proper corresponding strings and making sure that the 2nd set of quotation marks matches the 2nd column in the first file. Also, if the RTB contains the values "TANT3715" or "ABC" I would like to insert a blank line (before) and still increment the counter.:
( 1 0 1 1 0 0 0 0 "0402C" "CAP-00128G" "" 2 0 0 0 0 0 )
( 2 0 1 1 0 0 0 0 "" "" "" 0 0 0 0 0 0 )
( 3 0 1 1 0 0 0 0 "TANT3715" "CAP-00129G" "" 4 0 0 0 0 0 )
( 4 0 1 1 0 0 0 0 "12BGA_1.5" "CAP-00130G" "" 1 0 0 0 0 0 )
( 5 0 1 1 0 0 0 0 "" "" "" 0 0 0 0 0 0 )
( 6 0 1 1 0 0 0 0 "ABC" "CAP-00151G" "" 1 0 0 0 0 0 )
( 7 0 1 1 0 0 0 0 "0402C" "IND-00007G" "" 1 0 0 0 0 0 )
( 8 0 1 1 0 0 0 0 "0402R" "RES-00455G" "" 3 0 0 0 0 0 )
Here is the code that prints out the file:
var part1 = lookup.Select(
(x, i) =>
string.Format(#"( {0} 0 1 1 0 0 0 0 """" ""{1}"" """" {2} 0 0 0 0 0 )",
i + 1, x.Key, x.Count()));
So my question is:
Can someone help me find the value that matches the 1st column value in the RichTextBox to the line that contains that particular value, and then insert the 2nd column value in the RTB into the quotation marks before the match? I know this may be confusing so let me know if I need to clarify please. :)
Thanks.
You can convert your input to a dictionary:
var input = "CAP-00128G 0402C\r\nCAP-00128G 0402C\r\n...";
var dict = input.Split(new string[] { "\r\n" })
.Select(row => row.Split(new string[] { " " }))
.ToDictionary(cols => cols[0], cols => cols[1]);
and then get the value for each key from the dictionary:
var part1 = lookup.Select(
(x, i) =>
string.Format(#"( {0} 0 1 1 0 0 0 0 ""{3}"" ""{1}"" """" {2} 0 0 0 0 0 )",
i + 1, x.Key, x.Count(), dict[x.Key]));
Is lookup the result of grouping the input?
var input = "CAP-00128G 0402C\r\nCAP-00128G 0402C\r\n...";
var lookup = input.Split(new string[] { "\r\n" })
.Select(row => row.Split(new string[] { " " }))
.GroupBy(cols => cols[0], cols => cols[1]);
Then you can do simply this:
var part1 = lookup.Select(
(x, i) =>
string.Format(#"( {0} 0 1 1 0 0 0 0 ""{3}"" ""{1}"" """" {2} 0 0 0 0 0 )",
i + 1, x.Key, x.Count(), x.First()));