SQL 中的字符串排序
String Sorting in SQL
大家好我有这个 SQL 查询,我的查询越来越好,但我仍然不擅长解析数据以使其看起来像你想要的那样。
SELECT r.queueName, COUNT(*) as qty FROM LeaseComplete.dbo.QueueEntries q
INNER JOIN LeaseComplete.dbo.Queues r
ON r.queueID = q.queueentryQueueID
LEFT JOIN LeaseComplete.dbo.QueueActions a
ON a.queueactQueueEntryID = q.queueentryID
AND a.queueactCreationDate >(CONVERT(datetime, GETDATE()) -1)
WHERE q.queueentryAddDate > (CONVERT(datetime, GETDATE()) -1)
AND q.queueentryTaskTypeCode = 'Collection'
AND q.queueentryStatus != 'Closed'
AND r.queueTypeCode = 'Collection'
AND r.queueTypeCode = 'Mileage'
AND r.queueTypeCode = 'Insurance'
AND r.queueName NOT LIKE '%referral%'
OR r.queueName LIKE '%Awhite%'
OR r.queueName LIKE '%Dsmith%'
OR r.queueName LIKE '%Kmartin%'
OR r.queueName LIKE '%Mgonzalez%'
OR r.queueName LIKE '%Ckponton%'
OR r.queueName LIKE '%WTamayo%'
OR r.queueName LIKE '%mbare%'
OR r.queueName LIKE '%dlittle%'
GROUP BY r.queueName
所以我有上面的查询,它应该提取任何数据,其中包含 queueName
列的首字母、姓氏,如果它还包含里程、保险和收款。所以我得到了下面的数据集。
+--------------------------------+-----+
| queueName | qty |
+--------------------------------+-----+
| Collection AWhite Delinquent | 494 |
| Collection AWhite Followup | 10 |
| Collection AWhite Legal | 9 |
| Collection CKponton Delinquent | 790 |
| Collection CKponton Followup | 38 |
| Collection CKponton Mileage | 2 |
| Collection DLittle Delinquent | 114 |
| Collection DLittle Legal | 28 |
| Collection DSmith Delinquent | 486 |
| Collection DSmith Followup | 21 |
| Collection DSmith Legal | 56 |
| Collection KMartin Delinquent | 97 |
| Collection KMartin Followup | 3 |
| Collection MBare Delinquent | 773 |
| Collection MBare Followup | 22 |
| Collection MBare Legal | 1 |
| Collection MBare Mileage | 44 |
| Collection MGonzalez Delinquen | 739 |
| Collection MGonzalez Followup | 31 |
| Collection Referral DSmith | 107 |
| Collection WTamayo Delinquent | 682 |
| Collection WTamayo Followup | 21 |
| Collection WTamayo Mileage | 1 |
| Insurance Collection AWhite | 44 |
| Insurance Collection CKponton | 59 |
| Insurance Collection DLittle | 17 |
| Insurance Collection DSmith | 46 |
| Insurance Collection KMartin | 2 |
| Insurance Collection MBare | 40 |
| Insurance Collection MGonzalez | 35 |
| Insurance Collection WTamayo | 50 |
| Insurance Monitor AWhite | 2 |
| Insurance Monitor CKponton | 2 |
| Insurance Monitor MBare | 1 |
| Insurance Monitor MGonzalez | 3 |
| Insurance Monitor WTamayo | 2 |
+--------------------------------+-----+
如您所见,它仍然至少拉动列中的 'Referral' 一次,这不是我想要的。我也在尝试弄清楚如何制作它,这样它也不会显示 Insurance Monitor
列。如何以正确的方式让这些字符串名称优先于彼此。对不起,如果我的语法很糟糕,我今天还在醒来。
你的查询是敬酒:你混合了 AND
和 OR
条件,没有任何 ()
来强制执行 parse/execution 命令,并且你有不可能的条件:
AND r.queueTypeCode = 'Collection'
AND r.queueTypeCode = 'Mileage'
单个 field/value 不可能同时具有多个不同的值。你基本上是在说 "This field must be a banana and a submarine simultaneously".
还有:http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html 请注意 AND
的优先级高于 OR
。
我猜你想要这样的东西
SELECT r.queueName, COUNT(*) as qty FROM LeaseComplete.dbo.QueueEntries q
INNER JOIN LeaseComplete.dbo.Queues r
ON r.queueID = q.queueentryQueueID
LEFT JOIN LeaseComplete.dbo.QueueActions a
ON a.queueactQueueEntryID = q.queueentryID
AND a.queueactCreationDate >(CONVERT(datetime, GETDATE()) -1)
WHERE (q.queueentryAddDate > (CONVERT(datetime, GETDATE()) -1)
AND q.queueentryTaskTypeCode = 'Collection'
AND q.queueentryStatus != 'Closed'
AND r.queueTypeCode IN ('Collection', 'Mileage', 'Insurance')
AND r.queueName NOT LIKE '%referral%')
AND( r.queueName LIKE '%Awhite%'
OR r.queueName LIKE '%Dsmith%'
OR r.queueName LIKE '%Kmartin%'
OR r.queueName LIKE '%Mgonzalez%'
OR r.queueName LIKE '%Ckponton%'
OR r.queueName LIKE '%WTamayo%'
OR r.queueName LIKE '%mbare%'
OR r.queueName LIKE '%dlittle%')
GROUP BY r.queueName
ORDER BY r.queueName
大家好我有这个 SQL 查询,我的查询越来越好,但我仍然不擅长解析数据以使其看起来像你想要的那样。
SELECT r.queueName, COUNT(*) as qty FROM LeaseComplete.dbo.QueueEntries q
INNER JOIN LeaseComplete.dbo.Queues r
ON r.queueID = q.queueentryQueueID
LEFT JOIN LeaseComplete.dbo.QueueActions a
ON a.queueactQueueEntryID = q.queueentryID
AND a.queueactCreationDate >(CONVERT(datetime, GETDATE()) -1)
WHERE q.queueentryAddDate > (CONVERT(datetime, GETDATE()) -1)
AND q.queueentryTaskTypeCode = 'Collection'
AND q.queueentryStatus != 'Closed'
AND r.queueTypeCode = 'Collection'
AND r.queueTypeCode = 'Mileage'
AND r.queueTypeCode = 'Insurance'
AND r.queueName NOT LIKE '%referral%'
OR r.queueName LIKE '%Awhite%'
OR r.queueName LIKE '%Dsmith%'
OR r.queueName LIKE '%Kmartin%'
OR r.queueName LIKE '%Mgonzalez%'
OR r.queueName LIKE '%Ckponton%'
OR r.queueName LIKE '%WTamayo%'
OR r.queueName LIKE '%mbare%'
OR r.queueName LIKE '%dlittle%'
GROUP BY r.queueName
所以我有上面的查询,它应该提取任何数据,其中包含 queueName
列的首字母、姓氏,如果它还包含里程、保险和收款。所以我得到了下面的数据集。
+--------------------------------+-----+
| queueName | qty |
+--------------------------------+-----+
| Collection AWhite Delinquent | 494 |
| Collection AWhite Followup | 10 |
| Collection AWhite Legal | 9 |
| Collection CKponton Delinquent | 790 |
| Collection CKponton Followup | 38 |
| Collection CKponton Mileage | 2 |
| Collection DLittle Delinquent | 114 |
| Collection DLittle Legal | 28 |
| Collection DSmith Delinquent | 486 |
| Collection DSmith Followup | 21 |
| Collection DSmith Legal | 56 |
| Collection KMartin Delinquent | 97 |
| Collection KMartin Followup | 3 |
| Collection MBare Delinquent | 773 |
| Collection MBare Followup | 22 |
| Collection MBare Legal | 1 |
| Collection MBare Mileage | 44 |
| Collection MGonzalez Delinquen | 739 |
| Collection MGonzalez Followup | 31 |
| Collection Referral DSmith | 107 |
| Collection WTamayo Delinquent | 682 |
| Collection WTamayo Followup | 21 |
| Collection WTamayo Mileage | 1 |
| Insurance Collection AWhite | 44 |
| Insurance Collection CKponton | 59 |
| Insurance Collection DLittle | 17 |
| Insurance Collection DSmith | 46 |
| Insurance Collection KMartin | 2 |
| Insurance Collection MBare | 40 |
| Insurance Collection MGonzalez | 35 |
| Insurance Collection WTamayo | 50 |
| Insurance Monitor AWhite | 2 |
| Insurance Monitor CKponton | 2 |
| Insurance Monitor MBare | 1 |
| Insurance Monitor MGonzalez | 3 |
| Insurance Monitor WTamayo | 2 |
+--------------------------------+-----+
如您所见,它仍然至少拉动列中的 'Referral' 一次,这不是我想要的。我也在尝试弄清楚如何制作它,这样它也不会显示 Insurance Monitor
列。如何以正确的方式让这些字符串名称优先于彼此。对不起,如果我的语法很糟糕,我今天还在醒来。
你的查询是敬酒:你混合了 AND
和 OR
条件,没有任何 ()
来强制执行 parse/execution 命令,并且你有不可能的条件:
AND r.queueTypeCode = 'Collection'
AND r.queueTypeCode = 'Mileage'
单个 field/value 不可能同时具有多个不同的值。你基本上是在说 "This field must be a banana and a submarine simultaneously".
还有:http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html 请注意 AND
的优先级高于 OR
。
我猜你想要这样的东西
SELECT r.queueName, COUNT(*) as qty FROM LeaseComplete.dbo.QueueEntries q
INNER JOIN LeaseComplete.dbo.Queues r
ON r.queueID = q.queueentryQueueID
LEFT JOIN LeaseComplete.dbo.QueueActions a
ON a.queueactQueueEntryID = q.queueentryID
AND a.queueactCreationDate >(CONVERT(datetime, GETDATE()) -1)
WHERE (q.queueentryAddDate > (CONVERT(datetime, GETDATE()) -1)
AND q.queueentryTaskTypeCode = 'Collection'
AND q.queueentryStatus != 'Closed'
AND r.queueTypeCode IN ('Collection', 'Mileage', 'Insurance')
AND r.queueName NOT LIKE '%referral%')
AND( r.queueName LIKE '%Awhite%'
OR r.queueName LIKE '%Dsmith%'
OR r.queueName LIKE '%Kmartin%'
OR r.queueName LIKE '%Mgonzalez%'
OR r.queueName LIKE '%Ckponton%'
OR r.queueName LIKE '%WTamayo%'
OR r.queueName LIKE '%mbare%'
OR r.queueName LIKE '%dlittle%')
GROUP BY r.queueName
ORDER BY r.queueName